Ask Your Question

Image data accessing issue(read/write)

asked 2013-12-10 01:41:00 -0600

updated 2020-11-01 14:53:29 -0600

I have a Mat Image single channel variable,when i store the same image variable into the disk using imwrite and then access it using imread as a single channel image itself, the data are totally diffrent(i.e, before stroing into disk and after reading it from disk).

What couble be the issue and how can i fix it.i am posting a small snippet below. Please note image variable is always single Channel.

After executing both the files contain diffrent result(i.e, image data).

The main problem is if i use the image variable i get wierd results but when i save the iamge to disk and again read it i get result as desired.

foo(Mat &image)
    FILE *fp = fopen("Before.txt","w+");
        fprintf(fp,"TYPE = %d\n",image.type());
        fprintf(fp,"CHANNELS = %d\n",image.channels());
            fprintf(fp,"DEPTH = %d\n",image.depth());
        fprintf(fp,"STEP = %d\n",image.step);

    for(int i = 0 ; i < image.rows;i++)
            for(int j = 0; j < image.cols;j++)



    image = cv::imread("image.bmp",CV_LOAD_IMAGE_GRAYSCALE);

    FILE *fp1 = fopen("After1.txt","w+");
    fprintf(fp1,"TYPE = %d\n",arrSegmentImg.type());
    fprintf(fp1,"CHANNELS = %d\n",arrSegmentImg.channels());
    fprintf(fp1,"DEPTH = %d\n",arrSegmentImg.depth());
    fprintf(fp1,"STEP = %d\n",arrSegmentImg.step);
    uchar* p1 =;
    for(int i = 0 ; i < arrSegmentImg.rows;i++)
        for(int j = 0; j < arrSegmentImg.cols;j++)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-12-10 03:39:54 -0600

Haris gravatar image

You may need to make your Mat continues before writing to the file like,

image = (image.reshape(0,1));

See OpenCV Mat Documentation for more details

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2013-12-10 01:41:00 -0600

Seen: 457 times

Last updated: Dec 10 '13