Mat类表示的图像进行ROI操作有两种方法
1. 使用拷贝构造函数Mat(constMat& m, const Rect& roi ),矩形roi指定了兴趣区
Mat src = imread(“xx.jpg”);
Mat srcROI( src, Rect(0,0,src.cols/2,src.rows/2));
2. 使用操作符”()”,即Mat operator () ( const Rect&roi ) const,矩形roi指定了兴趣区
Mat src = imread(“xx.jpg”);
Mat srcROI = src(Rect(0,0,src.cols/2,src.rows/2));
- 注意:以上两种操作,srcROI的数据与源图像src共享存储区,所以此后在srcROI上的操作也会作用在源图像src上。
- 所以,当需要提取出独立的ROI区域时,应该使用
Mat srcROI = src(Rect(0,0,src.cols/2,src.rows/2)).clone();
举例:
直接划定ROI
Mat originImg = imread("xx.jpg");
Mat img;
bool toUseROI = true;
const int x0 = 100;
const int y0 = 100;
const int roiW = 1024;
const int roiH = 528;
Rect roiRect(x0,y0, roiW, roiH);
if (toUseROI)
img = originImg(roiRect).clone();
else
img = originImg.clone();
以图像中心为原点,划定半径扩展ROI
const int originWidth = 1280;
const int originHeight = 720;
const int R = 650;
int x0 = MAX( (int)(originWidth * 0.5 - R*0.707), 0);
int y0 = MAX((int)(originHeight * 0.5 - R*0.707), 0);
int width = MIN((int)(R*1.414), originWidth-x0);
int height = MIN((int)(R*1.414), originHeight -y0);