OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Sun, 03 Feb 2013 05:59:44 -0600submat roi helphttp://answers.opencv.org/question/6755/submat-roi-help/I want to extract a submatrix from a martix at a particular instant and add it to the main matrix while the submatrix remains constant and the main matrix changes ...
I m confused using submat() function and ROI concept...
Pls explain me about submat,ROI,colRange,rowRange and also locateROI
Thu, 31 Jan 2013 11:33:15 -0600http://answers.opencv.org/question/6755/submat-roi-help/Answer by Alexander Smorkalov for <p>I want to extract a submatrix from a martix at a particular instant and add it to the main matrix while the submatrix remains constant and the main matrix changes ...</p>
<p>I m confused using submat() function and ROI concept...</p>
<p>Pls explain me about submat,ROI,colRange,rowRange and also locateROI</p>
http://answers.opencv.org/question/6755/submat-roi-help/?answer=6771#post-id-6771Mat object in OpenCV use reference counting approach. Mat has reference to buffer and buffer parameters. When you get submat, you get new Mat that shares image data buffer with original Mat. You need to copy ROI to new Mat with Mat::copyTo method and make all calculations with submat copy.Fri, 01 Feb 2013 01:53:59 -0600http://answers.opencv.org/question/6755/submat-roi-help/?answer=6771#post-id-6771Answer by Michael Burdinov for <p>I want to extract a submatrix from a martix at a particular instant and add it to the main matrix while the submatrix remains constant and the main matrix changes ...</p>
<p>I m confused using submat() function and ROI concept...</p>
<p>Pls explain me about submat,ROI,colRange,rowRange and also locateROI</p>
http://answers.opencv.org/question/6755/submat-roi-help/?answer=6772#post-id-6772First of all you can't add sub matrix to whole matrix because their size is different. You can only add it to another sub matrix of the same size.
// define location of sub matrices in image
Rect roi1( x1, y1, sub_width, sub_height );
Rect roi2( x2, y2, sub_width, sub_height );
// define sub matrices in main matrix
Mat sub1( main_mat, roi1 );
Mat sub2( main_mat, roi2 );
Note that no copy was performed. sub1 and sub2 both uses buffer of original image so any change in them will change original image.
// if there no intersection between roi1 and roi2 you simply do the following
sub2 += sub1;
But if there intersection between them this may lead to undefined results because sub1 will change during the addition. So you will need first to copy it to another place first, and only then perform addition.
// if roi1 and roi2 do intersect
sub2 += sub1.clone();
By the way, intersection of rectangles can be checked in following way:
if ( (roi1 & roi2).width == 0 )
// no intersection
Fri, 01 Feb 2013 02:01:45 -0600http://answers.opencv.org/question/6755/submat-roi-help/?answer=6772#post-id-6772Comment by muthu for <p>First of all you can't add sub matrix to whole matrix because their size is different. You can only add it to another sub matrix of the same size.</p>
<pre><code>// define location of sub matrices in image
Rect roi1( x1, y1, sub_width, sub_height );
Rect roi2( x2, y2, sub_width, sub_height );
// define sub matrices in main matrix
Mat sub1( main_mat, roi1 );
Mat sub2( main_mat, roi2 );
</code></pre>
<p>Note that no copy was performed. sub1 and sub2 both uses buffer of original image so any change in them will change original image.</p>
<pre><code>// if there no intersection between roi1 and roi2 you simply do the following
sub2 += sub1;
</code></pre>
<p>But if there intersection between them this may lead to undefined results because sub1 will change during the addition. So you will need first to copy it to another place first, and only then perform addition. </p>
<pre><code>// if roi1 and roi2 do intersect
sub2 += sub1.clone();
</code></pre>
<p>By the way, intersection of rectangles can be checked in following way:</p>
<pre><code>if ( (roi1 & roi2).width == 0 )
// no intersection
</code></pre>
http://answers.opencv.org/question/6755/submat-roi-help/?comment=6818#post-id-6818thank u so much...again i have an issue..
when the roi starting point is near the bounday and if the height,width exceeds the boundary level,then the prog stops...how can i fix it??Sat, 02 Feb 2013 03:35:20 -0600http://answers.opencv.org/question/6755/submat-roi-help/?comment=6818#post-id-6818Comment by Michael Burdinov for <p>First of all you can't add sub matrix to whole matrix because their size is different. You can only add it to another sub matrix of the same size.</p>
<pre><code>// define location of sub matrices in image
Rect roi1( x1, y1, sub_width, sub_height );
Rect roi2( x2, y2, sub_width, sub_height );
// define sub matrices in main matrix
Mat sub1( main_mat, roi1 );
Mat sub2( main_mat, roi2 );
</code></pre>
<p>Note that no copy was performed. sub1 and sub2 both uses buffer of original image so any change in them will change original image.</p>
<pre><code>// if there no intersection between roi1 and roi2 you simply do the following
sub2 += sub1;
</code></pre>
<p>But if there intersection between them this may lead to undefined results because sub1 will change during the addition. So you will need first to copy it to another place first, and only then perform addition. </p>
<pre><code>// if roi1 and roi2 do intersect
sub2 += sub1.clone();
</code></pre>
<p>By the way, intersection of rectangles can be checked in following way:</p>
<pre><code>if ( (roi1 & roi2).width == 0 )
// no intersection
</code></pre>
http://answers.opencv.org/question/6755/submat-roi-help/?comment=6824#post-id-6824It does not matter if it is Mat or just a simple array you allocated, accessing to memory outside of array boundaries is always causing crashes. So if this may happen then you must check that rectangle of sub-matrix is completely inside original image. It should be something like: if (roi1 & Rect(0,0,width,height) == roi1) than everything is OK.Sat, 02 Feb 2013 05:45:15 -0600http://answers.opencv.org/question/6755/submat-roi-help/?comment=6824#post-id-6824Comment by muthu for <p>First of all you can't add sub matrix to whole matrix because their size is different. You can only add it to another sub matrix of the same size.</p>
<pre><code>// define location of sub matrices in image
Rect roi1( x1, y1, sub_width, sub_height );
Rect roi2( x2, y2, sub_width, sub_height );
// define sub matrices in main matrix
Mat sub1( main_mat, roi1 );
Mat sub2( main_mat, roi2 );
</code></pre>
<p>Note that no copy was performed. sub1 and sub2 both uses buffer of original image so any change in them will change original image.</p>
<pre><code>// if there no intersection between roi1 and roi2 you simply do the following
sub2 += sub1;
</code></pre>
<p>But if there intersection between them this may lead to undefined results because sub1 will change during the addition. So you will need first to copy it to another place first, and only then perform addition. </p>
<pre><code>// if roi1 and roi2 do intersect
sub2 += sub1.clone();
</code></pre>
<p>By the way, intersection of rectangles can be checked in following way:</p>
<pre><code>if ( (roi1 & roi2).width == 0 )
// no intersection
</code></pre>
http://answers.opencv.org/question/6755/submat-roi-help/?comment=6806#post-id-6806So will the main matrix be altered after addition?? since we take only reference for the submatrix...right??Fri, 01 Feb 2013 22:28:26 -0600http://answers.opencv.org/question/6755/submat-roi-help/?comment=6806#post-id-6806Comment by muthu for <p>First of all you can't add sub matrix to whole matrix because their size is different. You can only add it to another sub matrix of the same size.</p>
<pre><code>// define location of sub matrices in image
Rect roi1( x1, y1, sub_width, sub_height );
Rect roi2( x2, y2, sub_width, sub_height );
// define sub matrices in main matrix
Mat sub1( main_mat, roi1 );
Mat sub2( main_mat, roi2 );
</code></pre>
<p>Note that no copy was performed. sub1 and sub2 both uses buffer of original image so any change in them will change original image.</p>
<pre><code>// if there no intersection between roi1 and roi2 you simply do the following
sub2 += sub1;
</code></pre>
<p>But if there intersection between them this may lead to undefined results because sub1 will change during the addition. So you will need first to copy it to another place first, and only then perform addition. </p>
<pre><code>// if roi1 and roi2 do intersect
sub2 += sub1.clone();
</code></pre>
<p>By the way, intersection of rectangles can be checked in following way:</p>
<pre><code>if ( (roi1 & roi2).width == 0 )
// no intersection
</code></pre>
http://answers.opencv.org/question/6755/submat-roi-help/?comment=6844#post-id-6844Thanks a lot mike :)Sun, 03 Feb 2013 05:59:44 -0600http://answers.opencv.org/question/6755/submat-roi-help/?comment=6844#post-id-6844Comment by Michael Burdinov for <p>First of all you can't add sub matrix to whole matrix because their size is different. You can only add it to another sub matrix of the same size.</p>
<pre><code>// define location of sub matrices in image
Rect roi1( x1, y1, sub_width, sub_height );
Rect roi2( x2, y2, sub_width, sub_height );
// define sub matrices in main matrix
Mat sub1( main_mat, roi1 );
Mat sub2( main_mat, roi2 );
</code></pre>
<p>Note that no copy was performed. sub1 and sub2 both uses buffer of original image so any change in them will change original image.</p>
<pre><code>// if there no intersection between roi1 and roi2 you simply do the following
sub2 += sub1;
</code></pre>
<p>But if there intersection between them this may lead to undefined results because sub1 will change during the addition. So you will need first to copy it to another place first, and only then perform addition. </p>
<pre><code>// if roi1 and roi2 do intersect
sub2 += sub1.clone();
</code></pre>
<p>By the way, intersection of rectangles can be checked in following way:</p>
<pre><code>if ( (roi1 & roi2).width == 0 )
// no intersection
</code></pre>
http://answers.opencv.org/question/6755/submat-roi-help/?comment=6817#post-id-6817Yes, it will be altered.Sat, 02 Feb 2013 02:53:26 -0600http://answers.opencv.org/question/6755/submat-roi-help/?comment=6817#post-id-6817