Ask Your Question
2

Some problem about displaying a raw image file

asked 2013-05-23 19:13:25 -0600

hayden gravatar image

updated 2013-05-24 01:40:55 -0600

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.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
7

answered 2013-05-24 01:38:25 -0600

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();
edit flag offensive delete link more

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 ( 2013-05-24 08:19:18 -0600 )edit

how is this done in Java?

celsius gravatar imagecelsius ( 2014-10-01 00:01:39 -0600 )edit

Since i can't edit:

It is CV_8UC1 instead of CV_U8C1

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

danieltak gravatar imagedanieltak ( 2017-06-27 13:08:30 -0600 )edit
0

answered 2013-05-23 20:25:00 -0600

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:)

edit flag offensive delete link more

Comments

Thank you very much. It works perfectly alright.

hayden gravatar imagehayden ( 2013-05-24 10:39:48 -0600 )edit

Question Tools

Stats

Asked: 2013-05-23 19:13:25 -0600

Seen: 5,376 times

Last updated: May 24 '13