Ask Your Question
2

Some problem about displaying a raw image file

asked May 24 '13

hayden gravatar image

updated May 24 '13

berak gravatar image

hello every body, last time I posted this question. I have a raw image of 256 by 256 with 8 bits per pixels. The image is a gray scale image. As per answer, I wrote the following code:

/*******************************************/



#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <math.h>

using namespace cv;

int main(int argc, char** argv){

    Mat img = Mat(256, 256, CV_8UC1);
    img = imread("1.IMG", 1); 
    namedWindow( "RawImage", 1 );
    imshow( "RawImage", img );
    cvWaitKey();

    return 0;
}


/**********************************************/

But I can't display the image. abort is called. Could anyone help me figure out the problem?

Thanks in advance.

Preview: (hide)

2 answers

Sort by » oldest newest most voted
7

answered May 24 '13

berak gravatar image

looking at the docs for imread, it seems, that raw files are not supported.

you'll have to load the pixels manually, and then build a Mat from that:

#include <stdio.h>
char * imgpath = "1.IMG";
FILE * f = fopen(imgpath,"rb");
if ( ! f )
{
    printf("bad path : %s\n",imgpath);
    return -1;
}
char pixels[256*256];
fread(pixels,256*256,1,f);
fclose(f);

Mat img(256,256,CV_U8C1,pixels);

if there's any chance, that your original pixel ptr will go out of scope, you'll have to additionally clone the Mat, to have its own copy of the pixels:

Mat img = Mat(256,256,CV_U8C1,pixels).clone();
Preview: (hide)

Comments

Dear berak: I have a question about read image file.If the image too large to read, for example I use bmp file. How to open file? Thanks

wuling gravatar imagewuling (May 24 '13)edit

how is this done in Java?

celsius gravatar imagecelsius (Oct 1 '14)edit

Since i can't edit:

It is CV_8UC1 instead of CV_U8C1

http://docs.opencv.org/master/d1/d1b/...

danieltak gravatar imagedanieltak (Jun 27 '17)edit
0

answered May 24 '13

wuling gravatar image

I think you should check your file path,for example "c:\image\imag.img" or maybe make sure opencv imread support file file format:)

Preview: (hide)

Comments

Thank you very much. It works perfectly alright.

hayden gravatar imagehayden (May 24 '13)edit

Question Tools

Stats

Asked: May 24 '13

Seen: 5,504 times

Last updated: May 24 '13