Note that there are some explanatory texts on larger screens.

plurals
  1. POopencv, accessing element for downsampling but white window appear
    text
    copied!<p>i'm learning c++ api of opencv, and for a simple approach i've started with try to downsample image (ok i know that there is pyrDown with gaussian resampling but it's for learning how to access element in <code>Mat</code> class)</p> <p>this is my code:</p> <pre><code>#include &lt;opencv2/core/core.hpp&gt; #include &lt;opencv2/highgui/highgui.hpp&gt; #include &lt;iostream&gt; #define original_window "original" #define manual_window "manual" using namespace cv; using namespace std; Mat img, manual; void downsample(Mat src, Mat &amp;dst, const Size&amp; s) { float factor = src.rows/(float)s.width; Mat_&lt;Vec3f&gt; _dst = Mat(s, src.type()); Mat_&lt;Vec3f&gt; _src = src; for(int i=0; i&lt;src.cols; i+=factor) { int _i = i/factor; for(int j=0; j&lt;src.rows; j+=factor) { int _j = j/factor; _dst (_j, _i) = _src(j,i); } } cout &lt;&lt; "downsample image size: " &lt;&lt; _dst.rows &lt;&lt; " " &lt;&lt; _dst.cols &lt;&lt; endl; dst = Mat(_dst); } int main(int /*argc*/, char** /*argv*/) { img = imread("lena.jpg"); cout &lt;&lt; "original image size: " &lt;&lt; img.rows &lt;&lt; " " &lt;&lt; img.cols &lt;&lt; endl; downsample(img, manual, Size(img.cols/2, img.rows/2)); namedWindow(original_window, CV_WINDOW_AUTOSIZE); namedWindow(manual_window, CV_WINDOW_AUTOSIZE); while( true ) { char c = (char)waitKey(10); if( c == 27 ) { break; } imshow( original_window, img ); imshow( manual_window, manual ); } return 0; } </code></pre> <p>now, i'm doing a downsampling in a fool way: i'm just deleting elements. and i'm try to use c++ api with <code>Mat_</code>.</p> <p>in manual window i get a white window, and i don't understand why. event if i try to cout manual i'seeing different values.. what's wrong with this piece of code?</p> <h1>EDIT 1</h1> <p>i've found a solution:</p> <pre><code>dst.convertTo(dst, src.type()); // in this particular case: src.type() == CV_8UC3 </code></pre> <p>at the end of <code>downsample()</code></p> <p>now my question is: why that? i declare <code>Mat(s, src.type());</code> why it is modified?</p> <h1>EDIT 2</h1> <p>if i use @go4sri answer with this line</p> <pre><code>_dst (_j, _i) = src.at&lt;Vec3f&gt;(j, i); </code></pre> <p>i get this output: <img src="https://i.stack.imgur.com/EtbSn.png" alt="enter image description here"></p> <p>i really does not understand why..</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload