Ask Your Question

Confuse with the ROI rect parameter

asked 2014-10-07 02:28:00 -0500

zms gravatar image

Hi Everyone, Im confused here... I wanted to select the ROI half of the bottom or the image 1024x 576.

The parameter for rect (x,y,h,w) ... OK h and w and I understand. But what about x and y?? int main(int argc, char** argv) { Rect rectangle (50,30,100,512); image=imread("c:\a.jpg",1); // fill it however you want to Mat im_roi = image(rectangle); imshow("ROI",im_roi); }

If I had processed the ROI after the correct process, how can I draw back the result and put it back on the original image? Do I need to redo the rect roi again for both?

Thanks a lot


edit retag flag offensive close merge delete



ROI is the region of interest. It starts at the point(x, y) (top left corner of the Rect) and the w and h you know. Ex: image(1024x576), roi(50, 60, 70, 80); image(roi) will contain the part of the image from pixel at (50, 60) and goes until the pixel (50+70, 60+80). Just draw it on a paper, like a coordinate graph, only that (0, 0) is top-left.

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-10-07 03:39:15 -0500 )edit

Hey thanks!! I had try to get the half of the image with this code

Rect rectangle (0,image.rows/2,image.cols,image.rows/2); and it perfectly ROI the part. So if I had processed the ROI after the correct process, how can I draw back the result and put it back on the original image? Do I need to redo the rect roi again for both?

zms gravatar imagezms ( 2014-10-07 08:14:34 -0500 )edit

Due to the ROI the final result for processing had been shifted up when combining with the original image... I had draw the line by using the Line function on a Mat array and show put it on the original image. The lines now are shifted due to the ROI. I look back at the MAT function and not sure by constructing the MAT with the new dimension would help.. If you have any I would absolutely appreciate it..

zms gravatar imagezms ( 2014-10-07 08:48:28 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-10-07 09:54:39 -0500

thdrksdfthmn gravatar image

I am not really sure what you are saying, it is a little bit confusing, but if I had understand write, this should help: Normally, the Mat roiMat = image(roi); is the same as the roi of the image, so if you process it, then you actually process the image(roi). (If you do not want this, then you should do a copyTo or clone.)

As an example to better explanation, just do:

cv::Mat image = cv::imread("../img.jpg");
cv::Rect roi(15, 15, 40, 40);
cv::Rect rct(15, 15, 15, 15);
cv::Mat roiMat = image(roi); // you can do image(roi).clone() if you do not want this
cv::rectangle(roiMat, rct, CV_RGB(0, 255, 0), 2);
cv::imshow("image", image);

It will draw the rectangle in the read image, relative to the roi. I hope that this will answer your question.

edit flag offensive delete link more


Owh I'm sorry for confusing ... Ok i'm putting my code here.. the purpose of the code is to search the line in the half bottom of the pic aka the ROI. Once the lines had been found, it will be showed in the original image which had been clone. For eg the ori image is 1024X576. The ROI will only process the lower part which is the ROI (0,image.rows/2,image.cols,image.rows/2);

to get the lines the hough transform is used

cvtColor(dst, cdst, CV_GRAY2BGR);
cdst = draw.clone();

      vector<Vec4i> lines;

      HoughLinesP(dst, lines, 1, CV_PI/180, 10, 100, 20 );

      for( size_t i = 0; i < lines.size(); i++ )
        Vec4i l = lines[i];
        line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, CV_AA);

    imshow("detected lines", cdst);
zms gravatar imagezms ( 2014-10-07 10:56:39 -0500 )edit

where the cdst had been cloned from the original image. The line function, is drew on the cdst Mat but unfortunately, the line had been shifted above the half of the image, due to the ROI processed is halved from the image. What I had tried to debug, is trying to compensate the Point1 and Point2 from the line function to push it back to the original location in the original image but still do not succeed. If you have any thought of this, I do really appreciate about this. I have the pic but I don't know how to post it.

zms gravatar imagezms ( 2014-10-07 10:59:15 -0500 )edit

What I do not understand is cvtColor(dst, cdst, CV_GRAY2BGR); cdst = draw.clone();; that is you convert and then replace. You detect on dst and display on cdst. Maybe adding the rows/2 to each y coordinate of the 2 points will draw the line where you want.

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-10-07 11:12:04 -0500 )edit

Due to Canny need to be used, it will require grayscale level. dst is for canny output and converted to cdst in order for the line to be draw on the Mat due to the line will be in RGB. cdst=draw.clone - draw is the mat- an image from the original image.

OK :) I had tried to shift for compensation like what you had suggested and it had worked for the video.. here is the code!! THANKS!!!

line( cdst, Point(l[0], l[1]+frame.rows), Point(l[2], l[3]+frame.rows), Scalar(0,255,0), 3, CV_AA);

Debugging done for tonite.. continue tomorrow.. THANKS ALOT!!

zms gravatar imagezms ( 2014-10-07 11:36:26 -0500 )edit

what are you doing with cdst before cdst = draw.clone() ?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-10-08 03:14:44 -0500 )edit

the issue with the video frame I have to copy the mat in the beginning. I'm not sure why if I do not copy it, the correct mat does not display.. ok here is my code..


    bool bSuccess =; // read a new frame from video

    if (!bSuccess) //if not success, break loop
                    cout << "Cannot read the frame from video file" << endl;
    imshow("MyVideo", frame); //show the frame in "MyVideo" window

    draw = frame.clone();

-Canny detection - HT algorithm -Lines drawing

If I don't do that, the cdst will not appear as the original image in the frame. I'm pretty sure about that after I commented out the draw = frame.clone(). It will be other MAT which I do not know.


zms gravatar imagezms ( 2014-10-08 09:00:23 -0500 )edit

What I am saying that here cvtColor(dst, cdst, CV_GRAY2BGR); you convert the dst to BGR in cdst. Then you are doing nothing with that, but change it cdst = draw.clone();. So the converted dst to BGR is not used anywhere. Do you see? It is something like int i; int o; foo(i, o); o = 10; where foo(int i, int&o) { o = i + 7; }. Am I clear?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-10-08 09:21:17 -0500 )edit

ok sorry, I think I have to put the full code. Here you are.. :)

cvtColor(frame, frame_gray, CV_BGR2GRAY)

void CannyThreshold(int, void) { blur(frame_gray,dst,Size(3,3)); Canny(dst, dst, lowThreshold, lowThresholdratio, kernel_size ); cvtColor(dst, cdst, CV_GRAY2BGR); cdst = draw.clone(); Hough Transform here}

Actually what I had understand that canny will use the grayscale colour. If I didn't convert, the line would not be able to see in color if I don't convert.. that's what I understand from the code.. errr,, THat is actually I believe the case..

zms gravatar imagezms ( 2014-10-08 10:06:35 -0500 )edit

We still do not understand each other, leave it there. What I am saying is that you can remove cvtColor(dst, cdst, CV_GRAY2BGR); because it is not doing anything in the results. Except if you do something between that line and the cdst = draw.clone(); one (which in your post is the next one).

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-10-08 10:49:06 -0500 )edit

FInally I understand yr concern. You're correct as I redo the commented version, yes it does not need to to do it again since the cdst=draw.clone() already there for the new image shown.. Yes TQ VM!!

Appreciate it..

Rgds Zamani

zms gravatar imagezms ( 2014-10-09 03:01:14 -0500 )edit

Question Tools


Asked: 2014-10-07 02:28:00 -0500

Seen: 8,058 times

Last updated: Oct 07 '14