Reputation: 39
I'm trying to copy a smaller image, contained in a Mat, inside another larger image, contained in another mat.
The next code is the original mat.
cv::Mat mat(image.height(),image.width()+750,
CV_8UC3,image.bits(), image.bytesPerLine());
and that is the matter what i want to copy in the previous mat:
QImage your_image;
your_image.load(":/Sombrero.png");
your_image = your_image.convertToFormat(
QImage::Format_RGBA8888, Qt::ThresholdDither|Qt::AutoColor);
cv::Mat mat_im(your_image.height(),your_image.width(),CV_8UC3,your_image.bits(),
your_image.bytesPerLine());
As you can see, change the format of the image so that it has the same as the image stored in the original, but it is not working.
This question is different because i dont want to put an image over a regular image, like in other questions,i want to put a mat of a image over another mat image....
Upvotes: 0
Views: 1731
Reputation: 126
You can use
Mat(Rect) to specify a ROI and copy them. Like
Mat big, small;
cv::Rect rect;
small.copyTo(big(Rect));
The big and small mat has to be initialised. The Rect has to be the width and height of the small mat, x and y are the origin in the big mat.
You have to check the size of the mat (if the smaller one fit in the big at the origin) and the mats should have the same bit depth.
Edit: Full example
QImage src;
src.load(":/Sombrero.png"); // load to QImage
src = src.convertToFormat(QImage::Format::Format_RGB888); //convert to get sure the format
cv::Mat small(src.height(), src.width(), CV_8UC3, src.scanLine(0)); // convert the QImage to Mat
cv::Mat big(small.rows + 10, small.cols + 10, small.type(), cv::Scalar(0, 0, 0)); // generate a (10,10)px bigger mat
small.copyTo(big(cv::Rect(5, 5, small.cols, small.rows))); // copy the small to the big, you get a 5px black boarder
QImage result = QImage(big.data, big.cols, big.rows, 3 * big.cols, QImage::Format_RGB888); // if you want it back as QImage (3 is the count of channels)
Upvotes: 3