Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

    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: image description

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?

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 has nothing to do with original, its this one: image description

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?

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 has nothing to do with original, its this one: image description

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?

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 imshoe(b) has nothing to do with original, its this one: image description

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?

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 imshoe(b) imshow(b) has nothing to do with original, its this one: image description

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?