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);
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 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?