Question

I am am trying to create a rectangular ROI on an image with the location of a certain pixel being the center of the rectangle . How should I go about doing it ?

image= imread("C:\\image.png",1);
watermark=imread("C:\\watermark.png",0);
split(image,yuv_channels);

ROI=yuv_channels[0](Rect(100,100,watermark.cols,watermark.rows)); 

How should i modify it such that location (100,100) is at the centre of the ROI ?

Thankyou in advance for any help rendered.

Était-ce utile?

La solution

Use the following code for creating a MxM rectangle with center at x,y. Points a,b can be used as the anchor points for the rectangle in the rect function of OpenCV

    Point a;
    Point b;
    //MxM rectangle
    a.x = x - M/2;
    a.y = y - M/2;
    b.x = x + M/2;
    b.y = y + M/2;

Autres conseils

i did it at c# before, there is a function in the class Image. the method called 'SetROI()' , i hope it is helpful for you

I'm not sure whether I understand, but if you just want to make point (rect.x, rect.y) to be in the middle of rectangle just use this code:

Rect rect = Rect(100, 100, 234, 456);
rect -= Point(rect.width/2, rect.height/2)

or if you want to do this in one line:

Rect rect = Rect(100 - watermatk.cols/2, 100 - watermark.rows/2, watermark.cols, watermark.rows);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top