就如先前說明的文章來看,copyto如果原影像與目標影像大小是一樣的,就會直接在原影像位置直接做修改,若希望有類似去背的功能,copyto也可以幫忙實現出來喔!
在copyto的函示中,有兩種用法,第一種就是直接把影像貼上去,第二種搭配Mask,可以對輸入影像去背,但只對Mask影像中黑色部分,其他則保留
copyTo (OutputArray m)
copyTo (OutputArray m, InputArray mask)
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(){
Mat DogImg = imread("dog.jpg", CV_LOAD_IMAGE_COLOR);
Mat OpenCVImg = imread("OpencvImg.png", CV_LOAD_IMAGE_COLOR);
Mat RoiImg, GrayImg, MaskImg, InvMaskImg;
resize(OpenCVImg, OpenCVImg, Size(200, 200));
imshow("OpenCVImg", OpenCVImg);
cvtColor(OpenCVImg, GrayImg, CV_BGR2GRAY);
threshold(GrayImg, MaskImg, 200, 255, CV_THRESH_BINARY);
bitwise_not(MaskImg, InvMaskImg);
imshow("InvMaskImg", InvMaskImg);
RoiImg = DogImg(Rect(750, 450, OpenCVImg.cols, OpenCVImg.rows));
OpenCVImg.copyTo(RoiImg, InvMaskImg);
imshow("DogImg", DogImg);
waitKey(0);
return 0;
}