Jompa234
Jompa234

Reputation: 1248

Converting image from Triclops into opencv RGB image Mat.

I want to use OpenCV to do some processing on rectified images from the Bumblebee2 camera. I am using FlyCapture2 and Triclops to grab images from the sensor and rectify them. I want to convert the TriclopsColorImage into a cv::Mat to use with OpenCV.

From the TriclopsColorImage object I can get the following:

int nrows; // The number of rows in the image
int ncols; //The number of columns in the image
int rowinc; //The row increment of the image
unsigned char * blue; //pixel data for the blue band of the image
unsigned char * red;  //pixel data for the red band of the image
unsigned char * green; //pixel data for the green band of the image

I don't know how to convert this information into a cv::Mat image so that I can work on it. Can someone please point me in the right direction?

Upvotes: 1

Views: 1068

Answers (2)

Jompa234
Jompa234

Reputation: 1248

Alternate solution I came up with after a while.

   // Create a cv::Mat to hold the rectified color image. 
   cv::Mat cvColorImage(colorImage.nrows, colorImage.ncols,CV_8UC3);

   unsigned char* bp = colorImage.blue;
   unsigned char* rp = colorImage.red;
   unsigned char* gp = colorImage.green;

   for(int row = 0; row< colorImage.nrows; row++){

       for( int col =0; col< colorImage.rowinc; col++){

            //printf("%u %u %u ",*bp,*rp,*gp);
            bp++;
            rp++;
            gp++;

            cvColorImage.at<cv::Vec3b>(row,col)[0] = *bp;

            cvColorImage.at<cv::Vec3b>(row,col)[1] = *gp;

            cvColorImage.at<cv::Vec3b>(row,col)[2] = *rp;

       }
       cout << endl;
   }

   imshow("colorimage", cvColorImage);
   waitKey(300);

Upvotes: 0

Roger Rowland
Roger Rowland

Reputation: 26259

I haven't tested this and I don't know what version of OpenCV you're using, but something like the following should point you in the right direction. So, assuming your variable names from the question:

cv::Mat R(nrows, ncols, CV_8UC1, red, rowinc);
cv::Mat G(nrows, ncols, CV_8UC1, green, rowinc);
cv::Mat B(nrows, ncols, CV_8UC1, blue, rowinc);

std::vector<cv::Mat> array_to_merge;

array_to_merge.push_back(B);
array_to_merge.push_back(G);
array_to_merge.push_back(R);

cv::Mat colour;

cv::merge(array_to_merge, colour);

Upvotes: 2

Related Questions