#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!
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.
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
Related
When i specify the destination folder path of a image in my open cv project it works fine but when i keep that image in the project folder the image is not recognized.
i tried to run the following sample code
include "opencv\cv.h" // include it to used Main OpenCV functions.
#include "opencv\highgui.h" //include it to use GUI functions.
int main(int argc, char** argv)
{
IplImage* img = cvLoadImage( "Desert.jpg" ); //change the name (image.jpg) according to your Image filename.
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img);
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
return 0;
}
when i specify C:\Users\mia\Desktop\Desert.jpg problem is solved.
IplImage* img = cvLoadImage( "Desert.jpg" );
It's because you're using relative path here. If so, you should put the image in your TargetDir (the directory where your program are supposed to generated into).
P.S.: As commented by #berak, you should use the new OpenCV's API (e.g. use Mat instead of IplImage *).
I tried to convert RGB image into grayscale, conversion is successfull and i can display it with imshow. Yet, i cannot write it with imwrite, imwrite function returns NULL. My original image is 1920*1080 JPEG file with UINT8. Here is the code
#include <opencv\cv.h>
#include <opencv\highgui.h>
using namespace cv;
int main(int argc, char** argv)
{
char* imageName = argv[1];
Mat image;
image = imread(imageName, 1);
if (argc != 2 || !image.data)
{
printf(" No image data \n ");
return -1;
}
Mat gray_image;
cvtColor(image, gray_image, CV_BGR2GRAY);
if (imwrite("../../images/Gray_Image.jpg", gray_image) ==NULL) {
printf( "Writing image is not successfull!");
}
namedWindow(imageName, CV_WINDOW_AUTOSIZE);
namedWindow("Gray image", CV_WINDOW_AUTOSIZE);
imshow(imageName, image);
imshow("Gray image", gray_image);
waitKey(0);
return 0;
}
Why imwrite returns NULL?
The #include directive supports forward slashes. Use forward slashes there, for portability. And for sanity.
File handling functions generally don't support forward slashes in Windows. Use backward slashes there. Or, better, use some path handling class (such classes often support forward slashes, since they need to be portable, and automatically convert to backward slash in Windows).
In short,
/ → \ to fix the immediate problem.
That said, the OpenCV imwrite probably does not support automatic folder creation, so you'd better make sure that …
the specified folder exists, and
for a relative path (as you have), that the program execution's current folder is where you imagined the relative path starting.
I have tried to build the sample program from OpenCV documentation, but i have encountered a problem:
error: ‘CV_WINDOW_AUTOSIZE’ was not declared in this scope
Source of program:
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main( int argc, char** argv )
{
Mat image;
image = imread( argv[1], 1 );
if( argc != 2 || !image.data )
{
printf( "No image data \n" );
return -1;
}
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", image );
waitKey(0);
return 0;
}
I think that CV_WINDOW_AUTOSIZE constants have been contained in a certain header file, but I can't find the necessary header file.
CV_WINDOW_AUTOSIZE actually really is found in highgui.h, BUT, as #berak pointed out in the comments, that's part of the obsolete c-api. You should instead do one of two things:
Use WINDOW_AUTOSIZE instead, which is part of the C++ API. You don't need to change anything else to make this work, not even #include anything that isn't already #included in the example.
Use namedWindow( "Display Image" ) instead, since namedWindow uses WINDOW_AUTOSIZE by default and so you don't even have to include it as an argument.
Tested for OpenCV 3.0.0
It appears that in OpenCV 3.1 you need to use cv::WindowFlags::WINDOW_AUTOSIZE which is located in <opencv2/highgui.hpp>.
For opencv 4, it is defined in <opencv2/highgui/highgui_c.h>
since all the windowing stuff is in the highgui module, you'll need
#include <opencv2/highgui/highgui.hpp>
also, you'll need to link against the opencv_highgui library later
I have same issue and use
WINDOW_AUTOSIZE instead of
CV_WINDOW_AUTOSIZE
It has been change in version 4.
You can use directly WIDOW_AUTOSIZE if have defined using namespace cv as in your example.
Also, do not forget to add the correct dependencies for opencv
You will find it in highgui.h.
I try to use a different way for stitching images, but I got the following error...I try to change the format of the images or the size but nothing happens...any ideas?
error:
Error: Assertion failed (imgs.size() == imgs_.size())
in unknown function, file ......\src\opencv\modules\stitching\src\stitcher.cpp
, line 128
my code:
int main( int argc, char** argv )
{
Stitcher stitcher = Stitcher::createDefault();
Mat image11,image22;
Mat pano,output_frame;
vector<Mat> imgs,currentFrames;
// Load the images
Mat image1= imread( argv[1] );
Mat image2= imread( argv[2] );
printf("-- umwandlung works");
currentFrames.push_back(image1);
currentFrames.push_back(image2);
stitcher.estimateTransform( currentFrames );
stitcher.composePanorama(currentFrames, output_frame );
waitKey(0);
}
This is because estimateTransform() is not able to stitch all the images you have provided. You can check how many images were stitched using Stitcher::component(), it returns a vector of ints, which size is your goal.
Therefore, in your case its size should be 2 if the stitching was successful.
The question is similiar to this one:
using compose panorama without estimateTransform
I've just answered you there.
I am using CodeBlocks in my windows 7 64 bit and I use MinGw for my default c/c++ compiler.
Few days ago I need to use OpenCV, after I struggle a lot of error, I get unsolveable error like this :
The sample code:
#include "cv.h"
#include "highgui.h"
int main( int argc, char** argv ) {
IplImage* img = cvLoadImage( argv[1] );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", img );
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
}
I believe my linked & directory setting is correct. So please help me :) I am about to give up :(
Assuming that you are doing everything correct in the code and the image, this can be a problem due to incompatible opencv binaries.
Please have a look at a similar installation to compile and see if it works. I had a similar problem in my installation, which was fixed by compiling the binaries again.
The problem is most probably a failure when loading the image. But you will only be certain if you check the return of cvLoadImage():
IplImage* img = cvLoadImage( argv[1] );
if (!img)
{
printf("!!! cvLoadImage failed\n");
}
The function fails if the image format is not supported, or if the image is not found in the specified location.
You application expects to load the file passed from the command line, so you better execute your application with: Main.exe C:\some_img.png
You can also hardcode the filename in your code:
IplImage* img = cvLoadImage("C:\\some_img.png");
if (!img)
{
printf("!!! cvLoadImage failed\n");
}