0

I am trying to convert color images to gray-scale using OpenCV 2.4.11 C++ for Visual Studio 2012. I have used the following code to convert the image to grayscale. However, I am unable to do so because I am not able to read the image. The message I get is "Error reading image" because img is empty. I have stored the required image in the Debug folder beside the exe file. I have also mentioned the image name as a command argument in the Debug section of the property pages. I am also trying to store the grayscale image in the disk. Thanks in advance. The code is as follows:

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

}

  • try using the full path of "Mountain_8-Bit_Grayscale.jpg" – Chris Maes Jul 17 '15 at 10:19
  • have a look [here](http://stackoverflow.com/a/31342428/5008845). You should put your image in the project dir. If you have it in Debug folder, use "./Debug/image_name" – Miki Jul 17 '15 at 10:57
  • if you start your project from development IDE you have to set the relative execution path. easiest way to test it is to use absolute path like "C:/blabla/Mountain_8-Bit_Grayscale.jpg" but care to use / instead of \ because of escape characters... – Micka Jul 17 '15 at 11:22

1 Answers1

0

You should try using the absolute path to the image not the relative path. The other steps are fine, the image is read properly and the image displaying and saving commands are given properly there is a path problem.

David Safrastyan
  • 725
  • 7
  • 18