Ask Your Question
0

Image data accessing issue(read/write)

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

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

            {
                fprintf(fp,"%d\t",image.data[i*image.cols+j]);
            }
            fprintf(fp,"\n");
        }
        fclose(fp);


    imwrite("image.bmp",image);

    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 = arrSegmentImg.data;
    for(int i = 0 ; i < arrSegmentImg.rows;i++)
    {
        for(int j = 0; j < arrSegmentImg.cols;j++)
        {
            fprintf(fp1,"%d\t",image.data[i*image.cols+j]);
        }
        fprintf(fp1,"\n");
    }
    fclose(fp1);



    }
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

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

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
Login/Signup to Answer

Question Tools

1 follower

Stats

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

Seen: 321 times

Last updated: Dec 10 '13