1

I am trying to write to an image using imwrite as follows:

Mat object = imread ("C:/Users/User/Desktop/book.jpg", CV_LOAD_IMAGE_GRAYSCALE);
//calculate integral image
        Mat iObject;
        integral(object, iObject);
        imshow("Good Matches", iObject);
        imwrite("C:/Users/User/Desktop/IntegralImage.jpg", iObject);
        cvWaitKey(0);

but it does not suceed, I read about it, some solutions was to change jpg to bmp. I tried it also but no result! Any help please

user3552658
  • 115
  • 1
  • 11

1 Answers1

1

Cannot save image

This is because you don't have the privileges to write in that location. Solutions:

  1. Start your program as administrator
  2. Change to a location where you have enough privileges

Integral image is saved blank

The integral image is of type CV_32UC1, so values higher than 255 will be saturated to 255, and saved as white. You can't recover original values from the saved image.

Solutions:

  1. Normalize the values to fit the range [0,255], and save the CV_8U image. You can't recover original values from the saved image, but at least is scaled and shown correctly.

  2. Use FileStorage to save the original data. You keep the original values.

  3. If you need more speed that FileStorage, you can save raw binary data. See here for an example. You keep the original values.

This is a sample code to show solutions 1 and 2. For 3 please refer to the given link.

#include <opencv2\opencv.hpp>
using namespace cv;

int main()
{
    // Load image
    Mat1b object = imread("path_to_image", IMREAD_GRAYSCALE);

    // Calculate integral image
    Mat1i iObject;
    integral(object, iObject);

    imwrite("save_1i.png", iObject);

    // the saved image has saturated values

    // /////////////////////////////////////////

    // Save using imwrite
    Mat1b save_1b;
    normalize(iObject, save_1b, 255, 0, NORM_MINMAX);
    imwrite("save_1b.png", save_1b);

    // the saved image has scaled values, but displayed correctly


    // /////////////////////////////////////////

    // Save using FileStorage
    {
        FileStorage fs("save_fs.yml", FileStorage::WRITE);
        fs << "integral" << iObject;

        // the saved file has original values
    }


    // Load using FileStorage
    Mat1i loadedIObject;
    {
        FileStorage fs("save_fs.yml", FileStorage::READ);
        fs["integral"] >> loadedIObject;
    }

    // loadedIObject has original values

    return 0;
}
Community
  • 1
  • 1
Miki
  • 40,887
  • 13
  • 123
  • 202