我正在使用opencv图像处理库。我只想用另一张小图片替换原始Mat图像中的矩形区域(小于原始图像)。我找不到如何实现它的方向。
请问是否有人能帮助我?提前谢谢!
请问是否有人能帮助我?提前谢谢!
这是一种方式,不自称是最佳方式。
Mat m = ... // your smaller mat
Mat submat= matOrig.submat(new Rect(x,y, m.cols(), m.rows()) );
m.copyTo(submat);
假设m没有超出matOrig的边界,那么这应该可以正常工作。当您获取子矩阵时,它仍然与其源Mat相关联。
val shifted = Mat.zeros(image.size(), image.type())
val copiedSize = Size(image.width() - abs(pixelOffset.x), image.height() - abs(pixelOffset.y))
val fromStartPoint = Point()
val toStartPoint = pixelOffset.clone()
val fromRect = Rect(fromStartPoint, copiedSize)
val toRect = Rect(toStartPoint, copiedSize)
image.submat(fromRect).copyTo(shifted.submat(toRect))
return shifted
这是我的代码:
Mat m = new Mat(originalImg,new Rect(x,y,w, h));
double[] size = {w, h};
Imgproc.resize(logo,logo,new Size(size));// make sure your logo is the same size with your roi
Imgproc.cvtColor(logo, logo, Imgproc.COLOR_RGBA2RGB);//make sure your logo has the same channel with your dst img
congrats.copyTo(m);
public void ChangeImage(Mat src, Mat small, Rect roi){ Mat subView =src.submat(roi); small.copyTo(subView); Highgui.imwrite("combined-image.png", src); }
- shadee