简体   繁体   English

无法在OpenCV中使用imwrite写入图像

[英]Unable to write images using imwrite in OpenCV

I am trying to write to an image using imwrite as follows: 我正在尝试使用imwrite写入图像,如下所示:

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. 但是没有成功,我读到它,一些解决方案是将jpg更改为bmp。 I tried it also but no result! 我也尝试过,但没有结果! Any help please 任何帮助请

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 . 积分图像的类型为CV_32UC1 ,因此大于255的值将饱和到255,并保存为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. 标准化值以适合范围[0,255] ,然后保存CV_8U图像。 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. 使用FileStorage保存原始数据。 You keep the original values. 您保留原始值。

  3. If you need more speed that FileStorage , you can save raw binary data. 如果您需要比FileStorage更高的速度,则可以保存原始二进制数据。 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. 这是显示解决方案1和2的示例代码。有关3的信息,请参阅给定的链接。

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM