0

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.

user1926691
  • 173
  • 1
  • 3
  • 17
  • I guess you know the column and row number. I may be wrong but you can estimate anchor point from your center area. – emreakyilmaz Dec 26 '12 at 08:04

3 Answers3

0

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

king
  • 304
  • 1
  • 8
  • Nope , I'm sorry . That didn't help . SetROI still uses (100,100) as the anchor point of the rectangle , i.e. the top left corner of the rectangle, – user1926691 Dec 26 '12 at 07:34
0

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;
Abhishek Thakur
  • 16,337
  • 15
  • 66
  • 97
0

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);
cyriel
  • 3,522
  • 17
  • 34