am_karthick
am_karthick

Reputation: 110

imwrite does not create image

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

Upvotes: 0

Views: 1776

Answers (2)

SunitaKoppar
SunitaKoppar

Reputation: 157

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

Upvotes: 1

Markus Mayr
Markus Mayr

Reputation: 4118

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.

Upvotes: 4

Related Questions