Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenCV Mat image changes size after function call by reference
    primarykey
    data
    text
    <p>i am writing an application using OpenCV. Recently i've noticed strange things sometimes happen to cv::Mat object when passed to function by reference. Here's the code:</p> <pre><code>cv::Mat img1=cv::imread(argv[1]); cv::Mat img2=cv::imread(argv[2]); double scale=0.4; int thr=52; //resize image cv::Mat tmp; img1.copyTo(tmp); cv::resize(tmp,img1,cv::Size(),scale,scale); img2.copyTo(tmp); cv::resize(tmp,img2,cv::Size(),scale,scale); cv::Mat im1,im2; try { cv::Sobel(img1,im1,CV_8U , 0/*x order*/, 1/*y order*/,5/*kernel size*/,1/*scale*/); cv::Sobel(img2,im2,CV_8U , 0/*x order*/, 1/*y order*/,5/*kernel size*/,1/*scale*/); } catch(...) { std::cout &lt;&lt; "Something went wrong calculating Sobel!" &lt;&lt; std::endl; } cv::imshow("im1",im1); cv::imshow("im2",im2); //create empty mat with the same size as im1, im2 cv::Mat bin1(im1.size().height, im1.size().width, CV_8UC1); cv::Mat bin2(im2.size().height, im2.size().width, CV_8UC1); std::cout &lt;&lt; "_________OUT_________" &lt;&lt; std::endl; std::cout &lt;&lt; im1.size().width &lt;&lt; " " &lt;&lt; bin1.size().height &lt;&lt; std::endl; std::cout &lt;&lt; im2.size().width &lt;&lt; " " &lt;&lt; bin2.size().height &lt;&lt; std::endl; getExtreme(im1,bin1,thr); getExtreme(im2,bin2,thr); </code></pre> <p>The getExtreme function is defined like this:</p> <pre><code> void getExtreme(cv::Mat &amp;img, cv::Mat &amp;dst,int prag) { int x=img.size().width; int y=img.size().height; std::cout &lt;&lt; "_________________IN______________________" &lt;&lt; std::endl; std::cout &lt;&lt; x &lt;&lt; " " &lt;&lt; dst.size().width &lt;&lt; std::endl; std::cout &lt;&lt; y &lt;&lt; " " &lt;&lt; dst.size().height &lt;&lt; std::endl; int i,j; for (i=0;i&lt;x;i++) { for (j=0;j&lt;y;j++) { if(img.data[img.channels()*img.cols*j+i]&gt;prag) dst.data[dst.channels()*dst.cols*j+i]=255; } } } </code></pre> <p>while the images which size i am printing are the same, the output of the program is:</p> <p><em><strong></em>__</strong><em>OUT</em><strong><em>_</em>_</strong> 819 819 819 819 <strong><em>_</em>__<em>_</em>____</strong><em>IN</em><strong><em>_</em>__<em>_</em>__<em>_</em>___<em></strong> 819 819 435 435 <strong></em>__<em>_</em>__<em>_</em>_</strong><em>IN</em><strong><em>_</em>__<em>_</em>__<em>_</em>____</strong> 819 819 435 435</p> <p>so for some reason the bin1 and bin2 images (cv::Mat) change size. however, if i set the double scale=0.4; to double scale=1;, thus not resizing the image but retaining its original size, the size of the image matches outside and inside the function:</p> <p><em><strong></em>__</strong><em>OUT</em><strong><em>_</em>_</strong> 2048 1088 2048 1088 <strong><em>_</em>__<em>_</em>____</strong><em>IN</em><strong><em>_</em>__<em>_</em>__<em>_</em>___<em></strong> 2048 2048 1088 1088 <strong></em>__<em>_</em>__<em>_</em>_</strong><em>IN</em><strong><em>_</em>__<em>_</em>__<em>_</em>____</strong> 2048 2048 1088 1088</p> <p>What am i doing wrong? How does resizing influence the mat objects? why is it different at all, if it is passed by reference i should have the same object inside the function.</p> <p>thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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