0
#include <opencv/cv.h>
#include <opencv/highgui.h>

using namespace cv;

int main( int argc, char** argv )
{
const char* imageName = "samp.png";

Mat image;
image = imread( imageName, 1 );


Mat gray_image;
cvtColor( image, gray_image, CV_BGR2GRAY );

imwrite( "/home/Downloads/Pictures/Gray_Image.jpg",gray_image );



namedWindow( imageName, CV_WINDOW_AUTOSIZE );
namedWindow( "Gray image", CV_WINDOW_AUTOSIZE );



imshow( imageName, image );
imshow( "Gray image", gray_image );

waitKey(0);

return 0;
}

'imwrite" in the above code does not create image.Everything else works fine,and I can see the image using imshow.But I dont know why it is not creating a image.I tried 'convertTo' and replacing '/' to '\' in the path, but it does not work.I will be pleased if I get any lead.Thanks!

2
  • 1
    your path: "/home/.../.../Gray_Image.jpg" looks murky. first, there's no such thing as /.../ did you mean /../ ? and then, /home is a toplevel folder already, you can't go up 2 times from there Commented Jul 8, 2014 at 6:35
  • I was just trying to say some directories are there in between, Ex: "/home/Downloads/Pictures/Gray-Image.jpg" Sorry for the ambiguity! Commented Jul 8, 2014 at 10:53

2 Answers 2

4

This is not documented clearly, but imwrite returns a boolean which is true if and only if it thinks that it could write the file successfully. You should check that value!

You'll probably find out that imwrite returns false. Most likely you do not have sufficient permissions or -- as berak pointed out -- your file path is invalid.

By the way, for proper error handling, you should also catch exceptions, in particular, if the user provides the output file URL. If OpenCV, for some reason, can't find a suitable encoder (i.e. it can't recognize which type of file you are going to write), it will throw an exception.

Sign up to request clarification or add additional context in comments.

Comments

1

Markus Mayr's response helped me find out that I was getting a false when I printed the result of imwrite. The reason turned out to be, the directory in which I was trying to write, didnt exist. I was assuming it will create the directory but imwrite silently failed.After I manually created the directory it worked

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.