Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we create n channel image in opencv,n will be around 20

Presently Iam working in finding disparity of stereo pair. I have got a situation in creating 20 channel data set, When I declare array of 3 dimension it was giving error, Instead can I create image of 20 channels so that I can store data. If I can what are the additional conditions I have to include to get results without any error of memory allocation or sort of .... Creating an Image of 20 channels will be even comfortable for me...

like image 343
nbsrujan Avatar asked Dec 03 '22 03:12

nbsrujan


2 Answers

The C++ interface of OpenCV presents cv::Mat, which replaces and improves the IplImage type of the C interface. This new type provides several constructors, including the one below which can be used to specify the desired number of channels through the param type:

Mat::Mat(int rows, int cols, int type)

Sample code:

#include <cv.h>
#include <highgui.h>
#include <iostream>

void test_mat(cv::Mat mat)
{
    std::cout << "Channels: " << mat.channels() << std::endl;
}

int main(int argc, char* argv[])
{
    cv::Mat mat20(1024, 768, CV_8UC(20));
    test_mat(mat20);

    return 0;
}
like image 112
karlphillip Avatar answered May 16 '23 10:05

karlphillip


Opencv implements template class for small matrices whose type and size are known at compilation time:

template<typename _Tp, int m, int n> class Matx {...};

You can create a specified template of a partial case of Matx, which is cv::Vec like those already written in opencv for 1,2, or 3 "channels" like that:

typedef Vec<uchar, 3> Vec3b; // 3 channel -- written in opencv 
typedef Vec<uchar, 20> Vec20b; // the one you need

And then, declare a Matrix of your new (20 channel of uchar) object:

cv::Mat_<Vec20b> myMat;
myMat.at<Vec20b>(i,j)(10) = .. // access to the 10 channel of pixel (i,j) 
like image 21
Eric Avatar answered May 16 '23 08:05

Eric