【问题标题】:OpenCV Convert Color image to GrayscaleOpenCV 将彩色图像转换为灰度
【发布时间】:2015-10-07 00:56:25
【问题描述】:

我正在尝试使用适用于 Visual Studio 2012 的 OpenCV 2.4.11 C++ 将彩色图像转换为灰度。我使用以下代码将图像转换为灰度。但是,我无法这样做,因为我无法读取图像。 我收到的消息是“读取图像时出错”,因为 img 是空的。我已将所需的图像存储在 exe 文件旁边的 Debug 文件夹中。我还在属性页的“调试”部分中提到了图像名称作为命令参数。我也在尝试将灰度图像存储在磁盘中。提前致谢。代码如下:

#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/features2d.hpp"

using namespace cv;



int main(int argc, const char**argv)
{
Mat img=imread("Mountain_8-Bit_Grayscale.jpg", CV_LOAD_IMAGE_GRAYSCALE);

    if(img.empty())
    {
        std::cout<< " --(!) Error reading image " << std::endl;
        system("pause");
        return -2;
    }

    namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
    imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window

    imwrite("image_bw.jpg", img);
    waitKey(0);
    destroyWindow("MyWindow");
    return 0;

}

【问题讨论】:

  • 尝试使用“Mountain_8-Bit_Grayscale.jpg”的完整路径
  • 看看here。您应该将图像放在项目目录中。如果您在 Debug 文件夹中有它,请使用“./Debug/image_name”
  • 如果您从开发 IDE 启动项目,则必须设置相对执行路径。测试它的最简单方法是使用像“C:/blabla/Mountain_8-Bit_Grayscale.jpg”这样的绝对路径,但由于转义字符,请注意使用 / 而不是 \...

标签: c++ image opencv image-processing computer-vision


【解决方案1】:

您应该尝试使用图像的绝对路径而不是相对路径。其他步骤正常,图像读取正确,图像显示和保存命令正确给出路径问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多