Reputation: 49
I have 400x400 photo. I took the pieces as 4 separate photos of 100 * 400 and recorded them as 1, 2, 3, 4.jpg.
I have to combine 4 images croped from the one photo and get the original photo of 400 * 400. How can I do it ?
I don't want to use a ready(set) function. I need to do with for loop.
Upvotes: 1
Views: 890
Reputation: 4342
If you don't want to use available functions, you can create a 400x400 mask and assign the pixel values of each piece to the mask.
Here is the code:
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main() {
// 4 Pieces
Mat piece_1 = imread("/ur/image/directory/1.jpg",CV_LOAD_IMAGE_GRAYSCALE);
Mat piece_2 = imread("/ur/image/directory/2.jpg",CV_LOAD_IMAGE_GRAYSCALE);
Mat piece_3 = imread("/ur/image/directory/3.jpg",CV_LOAD_IMAGE_GRAYSCALE);
Mat piece_4 = imread("/ur/image/directory/4.jpg",CV_LOAD_IMAGE_GRAYSCALE);
// Mask
Mat source_image = Mat::zeros(Size(400,400),CV_8UC1);
for(int i=0;i<source_image.rows;i++)
{
for(int j=0;j<source_image.cols;j++)
{
if(i<=99)
source_image.at<uchar>(i,j)=piece_1.at<uchar>(i,j);
if(i>99 && i<=199)
source_image.at<uchar>(i,j)=piece_2.at<uchar>(i-100,j);
if(i>199 && i<=299)
source_image.at<uchar>(i,j)=piece_3.at<uchar>(i-200,j);
if(i>299 && i<=399)
source_image.at<uchar>(i,j)=piece_4.at<uchar>(i-300,j);
}
}
imshow("Result",source_image);
waitKey(0);
return 0;
}
Upvotes: 0
Reputation: 207345
This is not going to be easy. Just reading a JPG without using a library could take weeks or months for a professional.
You should use a library. I would recommend CImg from here as a good starting point.
Failing that, I would suggest using ImageMagick to convert your JPGs to NetPBM PPM format then you can read them much more easily.
magick 1.jpg -depth 8 1.ppm
When you have written the code to combine them, you can convert the combined PPM file back into a JPG with:
magick combined.ppm combined.jpg
Upvotes: 2