Home Build grid cv::Mat out of a vector of cv::Mat of different channels
Reply: 0

Build grid cv::Mat out of a vector of cv::Mat of different channels

faviouz
1#
faviouz Published in 2017-12-06 22:05:44Z

I am beginning work on a computer vision project and I want to be able to visualize multiple transformations on the input at once to help me think. I found the tile function online which takes a vector of Mat and puts them all together in one big Mat that contains all the images in a grid.

It works great except if I try to mix grayscale images with color images. I reckon this is because the final Mat needs to be specified a color channel and it doesn't play well with color channels that don't match. I tried constants other than CV_8UC1 but couldn't really figure it out.

This is my code right now. It works but if you uncomment //grid.push_back(frame); it will blow up as I've explained.

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

void tile(const vector<Mat> &src, Mat &dst, int grid_x, int grid_y)
{
    // patch size
    int width  = dst.cols/grid_x;
    int height = dst.rows/grid_y;

    // iterate through grid
    int k = 0;
    for(int i = 0; i < grid_y; i++) {
        for(int j = 0; j < grid_x; j++) {
            Mat s = src[k++];
            resize(s,s,Size(width,height));
            s.copyTo(dst(Rect(j*width,i*height,width,height)));
        }
    }
}

int main(int argc, char* argv[])
{
    VideoCapture cap;
    if(argc == 2) {
        cap.open(argv[1]);
    } else {
        cap.open(0);
    }

    if(!cap.isOpened()) {
        return -1;
    }

    int gridx = 2;
    int gridy = 2;
    namedWindow("grid", WINDOW_NORMAL);
    while(true) {
        vector<Mat> grid;

        Mat frame;
        cap >> frame; // get a new frame from camera
        //grid.push_back(frame);

        Mat gray;
        cvtColor(frame, gray, CV_BGR2GRAY);
        grid.push_back(gray);

        Mat edges;
        GaussianBlur(gray, edges, Size(7, 7), 1.5, 1.5);
        Canny(edges, edges, 0, 30, 3);
        grid.push_back(edges);

        // Fill the grid arbitrarily with `gray`
        while(grid.size() != gridx*gridy) {
            grid.push_back(gray);
        }

        int height = 800;
        int width = height*4/3;
        Mat res = Mat(height, width, CV_8UC1);
        tile(grid, res, gridx, gridy);
        imshow("grid", res);
        if(waitKey(30) == 'q') break;
    }

    return 0;
}

How can I fix this?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.327566 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO