簡體   English   中英

OpenCV將彩色圖像轉換為灰度

[英]OpenCV Convert Color image to Grayscale

我正在嘗試使用適用於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;

}

您應該嘗試使用圖像的絕對路徑而不是相對路徑。 其他步驟都很好,可以正確讀取圖像,並且可以正確給出圖像顯示和保存命令,這是路徑問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM