I am working on a project which merges the torn pieces of a paper into one image. Till now I have done the pre-processing to find the contours and find the matched pieces.
I am getting 2 images which are matched but are in separate 2 cv::Mat
objects.
Now, I need to merge these 2 images into 1 image. One way of doing so is to copy pixel by pixel of both the images into new image, but this will be very time expensive and processor expensive.
I need a OpenCV library function or workaround with similar function to do the job.
You can use copyTo function of openCV. For example assume that piece1 and piece2 are images of two pieces of paper:
Mat twoPieces (piece1.rows, 2*piece1.cols, piece1.type());
piece1.copyTo (twoPieces(Rect(0, 0, piece1.cols, piece1.rows)));
piece2.copyTo (twoPieces(Rect(piece1.cols, 0, piece2.cols, piece2.rows)));
I assumed here that the images are of the same size but if they not you can update the code. Also the example above showed how to copy whole image. You can define mask and copy only the places with pieces of paper. To draw mask you can use polylines.
Mat mask1(piece1.size(), CV_8U, Scalar(0));
polylines(mask1, piece1Contours, true, 255, CV_FILLED);
piece1.copyTo (twoPieces(Rect(0, 0, piece1.cols, piece1.rows)), mask1);
And of course if you know the shift between the pieces of paper you can change the point of origin in Rect so that both pieces will be adjacent to each other.
Mat mask2(piece2.size(), CV_8U, Scalar(0));
polylines(mask2, piece2Contours, true, 255, CV_FILLED);
piece2.copyTo (twoPieces(Rect(dx, 0, piece2.cols, piece2.rows)), mask2);
Once again I simplified the example assuming that the shift is only in X direction. It can be done with shift in both X and Y direction but it will require more calculations, like estimation of bounding box of your polygons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With