ROI(region of interest),也就是感兴趣区域,如果你设置了图像了ROI,那么在使用OpenCV的函数的时候,会只对ROI区域操作,其他区域忽略。举个例子:
原图:
现在要将这幅图的蓝色通道加150,如果没有设置ROI,则函数作用在整个图像上,整个图像的所有像素的蓝色通道都会被加上150;如果设置了ROI
Rect ROI(0,100,width/2,height/2);
则函数只会作用在我设置的ROI区域,其他区域保持不变。
通过JavaCV opencv包的相关接口,设置rect,可以实现对图像的裁剪,具体代码如下:
public class MatCut {
/**
* 按照指定的尺寸截取Mat,坐标原点在左上角
* @param src 源mat
* @param x
* @param y
* @param width
* @param height
* @return 截取后的mat
*/
public static Mat cut(Mat src, int x, int y, int width, int height){
if(x<0){
x=0;
}
if(y<0){
y=0;
}
if(x>src.width()){
x=src.width();
}
if(y>src.height()){
y=src.height();
}
Rect rect=new Rect(x,y,width,height);
return new Mat(src,rect);
}
public static Mat cut(Mat src,int x,int y)
{
int width=src.width()-2*x;
int height=src.height()-2*y;
return cut(src,x,y,width,height);
}
public static void main(String[] args) {
Mat ico= Imgcodecs.imread("G:\\Image\\20181214170231126.png");
Mat smallIco=cut(ico,200,200);
showImg(smallIco);
}
private static void showImg(Mat mat){
HighGui.imshow("结果",mat);
HighGui.waitKey();
}
}