i want to draw a rectangle in opencv according to the image width and height (i.e. i don't want to give a static values to cvRectangle
) i want to draw a rectangle which covers most of the region of any image big or small in other words i want to draw the biggest rectangle in each image,thanks
Asked
Active
Viewed 1.5k times
2

Eslam Hamdy
- 7,126
- 27
- 105
- 165
-
Do you mean just retrieve the image's width & height and feed them into cvRectangle for each image?? – mathematical.coffee May 17 '12 at 01:05
-
ok thanks @mathematical.coffee , i have updated my question with a relevant answer. – Eslam Hamdy May 17 '12 at 01:14
-
If you answered your own question, feel free to post the answer and accept it. – mathematical.coffee May 17 '12 at 01:27
2 Answers
1
May be, you'd like to use percentage dimensions?
IplImage *img=cvLoadImage(fileName,CV_LOAD_IMAGE_COLOR);
int imageWidth = img->width;
int imageHeight = img->height;
int imageSize = img->nSize;
int ratio = 90; // our ROI will be 90% of our input image
int roiWidth = (int)(imageWidth*ratio/100);
int roiHeight = (int)(imageHeight*ratio/100);
// offsets from image borders
int dw = (int) (imageWidth-roiWidth)/2;
int dh = (int) (imageHeight-roiHeight)/2;
cvRectangle(img,
cvPoint(dw,dh), // South-West point
cvPoint(roiWidth+dw, roiHeight+dh), // North-East point
cvScalar(0, 255, 0, 0),
1, 8, 0);
cvSetImageROI(img,cvRect(dw,dh,roiWidth,roiHeight));
So, now, If you set ratio = 90, and your input image is 1000x1000 pixels, than your ROI will be 900x900 pixels and it will be in the center of your image.

Larry Foobar
- 11,092
- 15
- 56
- 89
1
i have tried that and it works well
IplImage *img=cvLoadImage(fileName,CV_LOAD_IMAGE_COLOR);
int imageWidth=img->width-150;
int imageHeight=img->height-150;
int imageSize=img->nSize;
cvRectangle(img,cvPoint(imageWidth,imageHeight), cvPoint(50, 50),cvScalar(0, 255, 0, 0),1,8,0);
cvSetImageROI(img,cvRect(50,50,(imageWidth-50),(imageHeight-50)));

Eslam Hamdy
- 7,126
- 27
- 105
- 165