Reputation: 60
I'm trying to paste a smaller image into a larger image, using masks in OpenCV 2.4 via C++.
Without a mask, I copy the small image to the larger image with the following code:
smallImage.copyTo(largeImage(cv::Rect(pt, smallImage.size()));
where pt
has the type of cv::Point2f
. It works perfectly. However, if I apply a mask:
smallImage.copyTo(largeImage(cv::Rect(pt, smallImage.size()), mask);
I get an error from Mat::create
(see documentation):
CV_Assert(!fixedType() || (CV_MAT_CN(type) ==
m.channels() && ((1 << CV_MAT_TYPE(flags)) & fixedDepthMask) != 0));
If I remove the cv::Rect
from my code, simplifying it to:
smallImage.copyTo(largeImage, mask);
it works, albeit it doesn't copy to the correct location. How do I solve this?
Upvotes: 1
Views: 12367
Reputation: 1456
The following code works without any error.
Mat large_img = imread("C:\\Koala.jpg");
Mat small_img;
resize(large_img,small_img,Size(100,100),1);
small_img.copyTo(large_img (Rect(100,100,100,100)));
imshow("Rsult",large_img);
waitKey(0);
The small image is re-sized version of large image and it is copied in b/w (100,100) location to (200,200) in the large image. You can adopt these lines according to your requirement.
Upvotes: 2
Reputation: 1422
To paste a image scaledImage to resultMat:
scaledImage.copyTo(resultMat);
But i don't think you can select a roi in Java to copy at a particular region.
Upvotes: 0