Ask Your Question
1

Writing cv::Mat data is failed

asked 2017-09-09 03:24:25 -0600

ririgo gravatar image

updated 2017-09-09 06:17:16 -0600

Hi, I'm trying to read and write the data array of a cv::Mat only as a binary file. I saved the array like below.

FILE *file = fopen(file_name, "wb");
fwrite(mat.data, sizeof(char), height * width * 3, file); // RGB image
fclose(file);

Then I read it like below.

FILE *file2 = fopen(file_name, "rb");
fread(data, sizeof(char), height * width * 3, file2);
fclose(file2);
cv::Mat image(height, width, CV_8UC3, data);

It's fine when I check the result like this.

std::cout << data[height * width * 3 - 1] << image.at<cv::Vec3b>(height - 1, width - 1) << std::endl;

The last value of the "data" array and the red value of the last pixel of the cv::Mat image was the same. (ex, 37[41, 32, 37]) However, after I write the cv::Mat image again, the some pixel values are...destoyed like below. (ex, [127, 0, 0])

cv::imwrite(path, image);
cv::Mat test = cv::imread(path);
std::cout << test.at<cv::Vec3b>((height - 1, width - 1)) << std::endl;

The most of the pixels are OK but usually some of the last pixels are destroyed. Did I something wrong? Also, I read the binary file and save it as a image file using python cv2, that was OK. What should I do in C++?

edit retag flag offensive close merge delete

Comments

1

Wait, Your saving of the array is done using fread? I would say you need fwrite for that?

StevenPuttemans gravatar imageStevenPuttemans ( 2017-09-09 03:44:18 -0600 )edit
1

Oh, it was a typo, sorry. I did it using fwrite.

ririgo gravatar imageririgo ( 2017-09-09 03:53:41 -0600 )edit

I am wondering if you write a 8bit 3 channel image with a char iterator, and then read it back using char iterator, don't we drop precision? That might be what is happening here, because of the clear zeros, something like value overflow...

StevenPuttemans gravatar imageStevenPuttemans ( 2017-09-09 04:15:00 -0600 )edit

I called cvtcolor (rgba to rgb) first before writing the binary file. Will it be a problem?

ririgo gravatar imageririgo ( 2017-09-09 04:58:03 -0600 )edit
1

it is wrong :

std::cout << data[height * width - 1] << image.at<cv::Vec3b>(height - 1, width - 1) << std::endl

it should be

std::cout << data[height * width *3- 3] << image.at<cv::Vec3b>(height - 1, width - 1) << std::endl

see Creating a Mat object explicitly

LBerger gravatar imageLBerger ( 2017-09-09 05:39:31 -0600 )edit

Alright, another typo again, sorry.

ririgo gravatar imageririgo ( 2017-09-09 06:17:02 -0600 )edit

i used before matwrite and matread functions implemented by Miki the code is here

sturkmen gravatar imagesturkmen ( 2017-09-09 06:31:29 -0600 )edit

Is there any way to read the data written already? I think it is not the problem of the written data because it has no problem on the python cv2.

ririgo gravatar imageririgo ( 2017-09-09 06:45:15 -0600 )edit

what do you mean "data written already?". matread reads files written by matwrite

sturkmen gravatar imagesturkmen ( 2017-09-09 06:53:08 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-09-10 10:12:58 -0600

ririgo gravatar image

Oh, thank you all for your answering. I tested my code on another ubuntu server, it works well.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-09-09 03:24:25 -0600

Seen: 762 times

Last updated: Sep 09 '17