2

我想根据图像的宽度和高度在opencv中绘制一个矩形(即我不想给静态值cvRectangle)我想绘制一个矩形,它覆盖任何大小图像的大部分区域,换句话说我想在每个图像中绘制最大的矩形,谢谢

4

2 回答 2

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))); 
于 2012-05-17T10:29:37.807 回答
1

可能是,您想使用百分比尺寸?

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));

因此,现在,如果您设置 ratio = 90,并且您的输入图像是 1000x1000 像素,那么您的 ROI 将是 900x900 像素,并且它将位于图像的中心。

于 2012-05-17T05:25:01.607 回答