Saving and retrieving Mat data
I want to save Mat data to a float array, process them, and then save them back to Mat. I don't want to use .at function, cause processing code is already written and uses some temp arrays.
I have this piece of code:
cv::Mat test(cv::Mat original) {
float * image_data = new float[original.rows * original.cols];
//Save to array
init_data_image_32(original, image_data);
//process
//Restore from array
cv::Mat res(original.rows, original.cols, CV_32F, image_data, original.cols);
cv::namedWindow("b", CV_WINDOW_NORMAL);
cv::imshow("b", res);
return res;
}
bool doublesEqual(double a, double b) {
double margin_of_error = 0.0001;
return abs(a - b) < margin_of_error;
}
void init_data_image_32(cv::Mat image, float * image_data) {
for (int i = 0; i < image.rows; i++)
for (int j = 0; j < image.cols; j++) {
image_data[i*image.cols + j] = image.at<float>(i, j);
if (!doublesEqual(image_data[i*image.cols + j], image.at<float>(i, j))) std::cout << image_data[i*image.cols + j] << " " << image.at<float>(i, j) << std::endl;
}
}
The imshow of the resulting image in the imshow(b) has nothing to do with original, its this one:
Furthermore, the init_data_image_32 has some lines of output from the if clause:
nan nan
nan nan
nan nan
..
What am i doing wrong?
what are you trying to achieve, that couldn't be done with a simple:
? please do not use temp arrays, new, or iterate over pixel values, those are all clear antipatterns here (you also have a memleak there)
@berak Under the //process clause there is code for processing the image_data, that is written with c-like arrays. I could change that code so that every element access is done with .at but this code uses temp, old and new arrays so it would be complicated. So i want to initialize the initial c-like array from the original's data. I know about the memory leak.