user2036989
user2036989

Reputation: 51

View greyscale RAW image file in C++ using openCV

I have a a raw image file which contains image data from the 5th byte onwards. Each byte in the file represents a 8 bit greyscale pixel intensity. I have been able to store the binary data of the image in a 2-D unsigned char array.

Can anyone tell me how to use the array or the file to display the image in openCV?

Right now I am using this code :

void openRaw() {
    cv::Mat img(numRows,numCols,CV_8U,&(image[0][0]);
    //img.t();
    cv::imshow("img",img);  
    cv::waitKey();
}

But its displaying a wrong image.

I also tried using the IplImage method, but I am not sure how to pass the pointer to the source image there.

Could anyone provide me with some code for this?

Thanks, Uday

Upvotes: 1

Views: 2446

Answers (1)

berak
berak

Reputation: 39796

"I have been able to store the binary data of the image in a 2-D unsigned char array."

that's your problem here.

opencv stores the pixel data in consecutive uchar*, so it expects the same from you.

your 2d array is most likely an array of pointers, it's not the same.

so: instead of the 2d array, make a 1d :

uchar *data = new uchar[rows * cols];

you can access single pixels there like :

uchar pixel = data[y*cols+x];    

and then pass the data pointer into your cv::Mat :

cv::Mat img( rows, cols, CV_8U, data );

oh, and please DON'T use IplImages ( aka the 1.0 api ) any more, avoid at all cost!

Upvotes: 1

Related Questions