Home > Enterprise >  concatenate the images to single image using opencv C
concatenate the images to single image using opencv C

Time:02-22

I am creating a single image from the video. But I read the video and create frames, then after that rotate the frames and crop the frames and these cropped frames are saved. These cropped frames should be combined to create a single image. So I have appended them in the vector of Mat and then want to concatenate them horizontally. Which I did but it doesn't show any image nor the image is saved.

I want to do something similar to python

list_images = []
for img in glob.glob(crop_dir   "*.jpg"):
 n = cv2.imread(img,0)
 list_images.append(n)
im_v = np.concatenate(list_images)
cv2.imwrite("finalimg.jpg",im_v)

My variable imglist is of type class std::vector<class cv::Mat,class std::allocator<class cv::Mat> >

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <filesystem>

using namespace std;
using namespace cv;
namespace fs = std::filesystem;

string crop_dir = "Task2_Resources/CropImages/";

void delete_dir_content(const fs::path& dir_path) {
    for (auto& path : fs::directory_iterator(dir_path)) {
        fs::remove_all(path);
    }
}

int main() {

    system("CLS");

    if (!fs::is_directory(crop_dir) || !fs::exists(crop_dir)) { // Check if  folder exists
        fs::create_directory(crop_dir); // create  folder
    }
    delete_dir_content(crop_dir);

    VideoCapture vid_capture("Task2_Resources/sample201.avi");

    int count = 0;
    Mat finalimg,mimg;
    vector<cv::Mat> imglist;
    
    Mat image;
    int x = 190, y = 59, h = 1, w = 962;

    if (!vid_capture.isOpened()){
        std::cout << "Cannot open the video file" << endl;
        return -1;
    }
    
    while (true) {

        Mat frame , rimg;
        vector<Mat> blocks;
        
        
        bool isSuccess = vid_capture.read(frame);
        if (!isSuccess){
            std::cout << "Cannot read the frame from video file" << endl;
            break;
        }

        //imshow("Frames", frame);
        count  ;
        float width = frame.cols;
        float height = frame.rows;
        Point2f center = Point2f((width / 2), (height / 2));
        double degree = -0.2;
        double scale = 1.0;
        Mat change = getRotationMatrix2D(center, degree, scale);  // Rotate & scale 
        warpAffine(frame, rimg, change, frame.size(), cv::INTER_CUBIC, cv::BORDER_CONSTANT, cv::Scalar(0, 0, 0));
        Mat cropped_image = rimg(Range(y,y h), Range(x, x w));

        string fname = to_string(count)   ".jpg";
        cv::imwrite(crop_dir   fname, cropped_image);    
        imglist.push_back(cropped_image);



    }
     
    hconcat(imglist,image);
   
   cout << typeid(imglist).name() << endl;
    cv::imwrite("final.jpg", image);
    std::cout << "Total frames created " << count << endl;

    return 0;
}

Visual Studio debug console enter image description here

CodePudding user response:

The only issue I could notice is h = 1 - that makes the height of cropped_image to be only 1 pixel.

Since you are not reporting any error message, we can't really tell why it's not working.
I recommend you to use the debugger, and iterate the code step by step (use an IDE for that).

For building a working example, you may follow the stages described next.


There is a chance that one of issues is related to sample201.avi video file.
For making the answer reproducible, we may use enter image description here

  • Related