OpenCV imwrite() not saving image - c++

I am trying to save an image from OpenCV on my mac and I am using the following code and so far it has not been working.
cv::imwrite("/Users/nickporter/Desktop/Gray_Image.jpg", cvImage);
Can anyone see why this might not be saving?

OpenCV does have problems in saving to JPG images sometimes, try to save to BMP instead:
cv::imwrite("/Users/nickporter/Desktop/Gray_Image.bmp", cvImage);
Also, before this, make sure you image cvImage is valid. You can check it by showing the image first:
namedWindow("image", WINDOW_AUTOSIZE);
imshow("image", cvImage);
waitKey(30);

I met the same problem and one possible reason is that the target folder to place your image. Suppose you want copy A.jpg to folder "C:\\folder1\\folder2\\", but in fact when folder2 doesn't exist, the copy cannot be successful(It is from my actual test, not from official announcement). And I solved this issue by checking whether the folder exists and create one folder if it doesn't exist. Here is some code may it help using c++ & boost::filesystem. May it help.
#include <boost/filesystem.hpp>
#include <iostream>
std::string str_target="C:\\folder1\\folder2\\img.jpg";
boost::filesystem::path path_target(str_target);
boost::filesystem::path path_folder=path_target.parent_path();//extract folder
if(!boost::filesystem::exists(path_folder)) //create folder if it doesn't exist
{
boost::filesystem::create_directory(path_folder);
}
cv::imwrite(str_target,input_img);

I also suggest to check folder permissions. Opencv quietly returns from imwrite without any exception even if output folder doesn't have write permissions.

I've just had a similar problem, loading in a jpg and trying to save it back as a jpg. Added this code and it seem to be fine now.
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
compression_params.push_back(100);
And you need to include the param in your writefile.
cv::imwrite("/Users/nickporter/Desktop/Gray_Image.jpg", cvImage, compression_params);

OpenCV 3.2 imwrite() seems to have a problem to write jpg file with Windows Debug mode. I use this way instead of imwrite().
cv::Mat cvImage;
#ifdef DEBUG
IplImage image = IplImage(cvImage);
cvSaveImage("filename.jpg", &image);
#else
cv::imwrite("filename.jpg", cvImage);
#endif

The following function can be dropped into your code to support writing out jpg images for debugging purposes.
You just need to pass in an image and a filename for it. In the function, specify a path you wish to write to & have permission to do so with.
void imageWrite(const cv::Mat &image, const std::string filename)
{
// Support for writing JPG
vector<int> compression_params;
compression_params.push_back( CV_IMWRITE_JPEG_QUALITY );
compression_params.push_back( 100 );
// This writes to the specified path
std::string path = "/path/you/provide/" + filename + ".jpg";
cv::imwrite(path, image, compression_params);
}

Although it is not true for your case. This problem may arise if the image path given as argument to the cv::imwrite function exceeds the allowed maximum path length (or possibly allowed file name length) for your system.
for linux see: https://unix.stackexchange.com/questions/32795/what-is-the-maximum-allowed-filename-and-folder-size-with-ecryptfs
for windows see: https://www.quora.com/What-is-the-maximum-character-limit-for-file-names-in-windows-10

Related

imread() still returns empty mat despite correct addressing in openCV 4.0.0

I am newbie to OPENCV.
Now I am trying to display an image using imread(), but imread() doesn't work(keep returning empty mat.) But when I try to draw lines, circles and so on by imread(), it works well.
So I thought there would be something wrong with addressing. And I tried everything I can but it still doesn't work.
And I also have googled a lot of things to solve it, but I can't get any answer to this problem.
What should I do? Is there something wrong I have missed?
I currently use Windows 10, Visual Studio 2017, and openCV 4.0.0 alpha.
Here is my code.
#include <iostream>
#include <opencv2/opencv.hpp>
#ifdef _DEBUG
#pragma comment(lib,"opencv_world400d.lib")
#else
#pragma comment(lib,"opencv_world400.lib")
#endif
using namespace std;
using namespace cv;
int main()
{
Mat image;
image =imread("C:/Users/ymin/source/repos/Project1/Project1/BENZ.bmp",IMREAD_ANYCOLOR);
if (image.empty())
{std::cerr << "Could not open file" << std::endl; return (1);}
imshow("image", image);
waitKey();
return 0;
}
From the OpenCV documentation for imread:
If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix.
This indicates that the file is not there (maybe typo?), you don't have permission to read it, it is in a format not recognized by OpenCV, or it is corrupted.
Make sure the file is where you think it is, and that it is readable, and that OpenCV is compiled to support whatever format the file is in.
(Note that the file extension does not determine its format, you can take a JPEG file and rename it to have a .bmp extension, but it is still a JPEG file.)

OpenCV imwrite not writing image

I was trying to write an image to a folder using OpenCV imwrite function.
The code compiles and runs successfully but the image is not saving in the folder/path.I am getting output from 'imshow' and my image is in CV_8UC1 format.
find the code below
Mat reflection = function which computes image
imshow("output image", reflection);
imwrite("E:/New folder/img.bmp", reflection);
so I checked current folder writing and modified code like this
bool check = imwrite("./img.bmp", reflection);
this 'bool check' status is 'false' and not writing the image.
I've also checked folder permission with guidelines from microsoft help my "E/New Folder/" is permitted to write. still, the image is not saving.
I am okay with any image format .jpg, .png and .bmp.
I am using windows 7, OpenCV 3.0, visual studio 2017.
Please help me and thanks for reading it
Opencv doesn't seem to support saving BMP files check the imwrite docs. Changing the filename to img.png should work. Also using ./ in windows is not valid, this is used in unix systems to represent the current working directory see Windows current directory.
Updating it to
bool check = imwrite(".\img.png", reflection);
or
bool check = imwrite("img.png", reflection);
Should work
OpenCV do support bmp, just use as follows.
bool check = imwrite("img.bmp", reflection);

How do I load an image (raw bytes) with OpenCV?

I am using Mat input = imread(filename); to read an image but I'd like to do it from memory instead. The source of the file is from an HTTP server. To make it faster, instead of writing the file to disk and then use imread() to read from it, i'd like to skip a step and directly load it from memory. How do I go about doing this?
Updated to add error
I tried the following but I'm getting segmentation fault
char * do_stuff(char img[])
{
vector<char> vec(img, img + strlen(img));
Mat input = imdecode(Mat(vec), 1);
}
See the man page for imdecode().
http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#imdecode
I had a similar problem. I needed to decode a jpeg image stream in memory and use the Mat image output for further analysis.
The documentation on OpenCV::imdecode did not provide me enough information to solve the problem.
However, the code here by OP worked for me. This is how I used it ( in C++ ):
//Here pImageData is [unsigned char *] that points to a jpeg compressed image buffer;
// ImageDataSize is the size of compressed content in buffer;
// The image here is grayscale;
cv::vector<unsigned char> ImVec(pImageData, pImageData + ImageDataSize);
cv:Mat ImMat;
ImMat = imdecode(ImVec, 1);
To check I saved the ImMat and was able to open the image file using a image viewer.
cv::imwrite("opencvDecodedImage.jpg", ImMat);
I used : OpenCV 2.4.10 binaries for VC10 on x86.
I hope this information can help others.

cvLoadImage for IplImage working, but cv::imread for cv::Mat not workign

I am facing a weird issue where I am unable to read a basic image file from the filesystem using cv::imread. Hence the below results into imageInput having null data, with rows and cols set to 0.
cv::Mat imageInput = cv::Mat();
imageInput = cv::imread("abc.jpg",cv::IMREAD_COLOR);
Interestingly, after commenting out the above code, the below code is able to read the iamge:
//cv::Mat imageInput = cv::Mat();
//imageInput = cv::imread("abc.jpg",cv::IMREAD_COLOR);
IplImage *rImg;
rImg = cvLoadImage("abc.jpg", CV_LOAD_IMAGE_COLOR);
rImg gets a width of 3296, and height of 2256 as expected.
I am clueless how this is possible. Please help.
The build seems to be fine in both cases. I tried using both opencv-2.4.5 and opencv-2.4.6
I have been able to figure out the issue now.
The issue was mention of release build static libraries instead of debug-build static libraries.
Earlier for the VC++ project, I was including the following lib dependencies in debug mode : opencv_core245.lib;opencv_highgui245.lib;opencv_imgproc245.lib;opencv_video245.lib
Upon changing it to opencv_core245d.lib;opencv_highgui245d.lib;opencv_imgproc245d.lib;opencv_video245d.lib, I am able to read images using imread without any issues.

imread not working in Opencv

I'm trying to use the imread function from OpenCV2.2.
My code is very simple.
cv::Mat host= imread("1.bmp", CV_LOAD_IMAGE_GRAYSCALE);
After that, the host matrix became filled by zeros pointers, i.e. an image has not loaded.
If I use cvLoadImage then it all works properly.
The file exists, and I am not mixing the release and debug libraries. Why imread doesn't work?
Reproduced with opencv 2.4.8.
If you are running in Debug, check that you are also using debug libraries, it fixed our problem. : OpenCV imread(filename) fails in debug mode when using release libraries.
i was facing the same problem with 2.4.6 . The reason was that while selecting the library , i had selected both the debug version and the release version. When i selected only the debug version for the library everything worked fine
I can confirm, that there are some problems with imread in OpenCV 2.2. However the problems only occurred on a Windows 32bit system. On a linux and on a mac it worked. I can't tell why it didn't work, but we had a small workaround for that.
We fixed this problem with the following macros, maybe you could try this out and use "ourImread" from then on.
#ifdef WIN32
#define ourImread(filename, isColor) cvLoadImage(filename.c_str(), isColor)
#else
#define ourImread(filename, isColor) imread(filename, isColor)
#endif
I've had the same problem
cv::Mat image= cv::imread("immagine12.jpg"); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
cv::waitKey(5000);
return -1;
}
cv::namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image );
//non so perchè ma senza il waitKey e lasciando solo il system pause non carica l'immagine...
cv::waitKey(5000);
system("pause");
but I fixed it when I inserted the cv::waitKey(5000);
I don't know why but with system pause it can't load the image and it goes on pause after it loads the image!
I have similar problem on Linux, when reading 32 bit tiff images only.
Mat mat= imread(filename, CV_LOAD_IMAGE_ANYDEPTH);
Problem was because OpenCV was not build with Tiff support for some reason.
If you think that it is an OpenCV bug then, please, post your image and instructions for reproducing to the OpenCV bugtracker.
I know it is late but someone might found this helpful. I was facing the same problem while using imread using OpenCV-3.0. I tried all solutions but actually I was not adding the library opencv2/imgcodecs.hpp. Although imshow was working without it, but after I add this I was able to read the image.
Opencv4.3 Windows10 x64 Visual Studio 2019 I finally solved that problem!!!
Ok, now, when you meet the problem, you can try:
Check your settings, make sure you don't mix include .lib file in "Project->Properties->Linker->Input" for Release/Debug, that opencv_xxx430d.lib(note the d) is for Debug, and opencv_xxx430.lib(no d) is for Release, and the platform is x64.
And in C/C++->General, you need to set the Additional include directory that X:/(opencv_build/install)/include and X:/(opencv_build/install)/include/opencv2 respectively for Release/Debug.
Maybe you should #include <opencv2/imgcodecs.hpp>.
Finally, the Unicode Problem, which is also my problem that, the unicode symbol in the image file path, fuck! I don't know why because my code is very normal string path = "D:/kk.jpg(note that, it is / not \) but you can try to debug your code, maybe you can find there is something like path = "?D:/kk.jpg", what the fuck! So don't directly copy/paste your image path, even you've already use the absolute path, you can still meet the unicode problem, so, what you need to do is delete the line of code(string path = "D:/kk.jpg), and then just retype each character one by one! If you're lucky, you will see that image with imshow. Good luck.
see related question here
Please make sure your path is correct ,
According to the Opencv's API , I'd try this call:
arrayMat[i]=imread("1.jpg" , 1 );
The parameters for imread :
Mat imread(const string& filename, int flags=1)
Loads an image from a file.
Parameters:
filename – Name of file to be loaded.
flags – Specifies color type of the loaded image:
>0 the loaded image is forced to be a 3-channel color image
=0 the loaded image is forced to be grayscale
<0 the loaded image will be loaded as-is (note that in the current implementation the alpha channel, if any, is stripped from the output image, e.g. 4-channel RGBA image will be loaded as RGB if ).
Good luck
S
I have the same problem. I solved it. The key is whether the filename has jpg.
If the filename is p1, you should use something like this imread("p1.jpg"). But we often set the filename as p1.jpg, here we should use something like this imread("p1.jpg.jpg").
This also happened to me, my simple solution was to use the C API and then convert to Mat:
IplImage* img = cvLoadImage("c://frame_201.bmp");
Mat mat = Mat(img);
The reverse is also true: if you are building Release and you have Debug libraries, then imread() quietly fails (errno is 0 after imread(), but the image object is not populated).
Another possibility:
If you're on OS X and statically link OpenCV be sure to use libjpeg which is bundled with OpenCV, not the system's one.
I had similar problem with OpenCV 3.0, except that cvLoadImage was not working as well. So, this might not really answer your question, but maybe it will help someone else.
I also had the very same problem that imread didn't work and cvLoadImage did work.
I decided to create a new Visual Studio from scratch and now it works.
There is no general problem with imread in OpenCV 2.4.3 under win32.
I know you want use "imread" & "CV_LOAD_IMAGE_GRAYSCALE" and convert automatically.
But this other way for load one picture and convert to gray scale:
define CV_NO_BACKWARD_COMPATIBILITY
#include <cv.h>
#include <highgui.h>
#include <math.h>
int main(){
/* load the image */
IplImage* img = cvLoadImage("yourPicture.bmp"); //jpg - bmp
/* retrieve properties */
int width = img->width;
int height = img->height;
int nchannels = img->nChannels;
int step = img->widthStep;
IplImage* img2 = cvCreateImage(cvSize(img->height, img->width),IPL_DEPTH_8U,1);
/* setup the pointer to access image data */
uchar *data = ( uchar* )img->imageData;
uchar *data2= ( uchar* )img2->imageData;
/* convert to grayscale manually */
int i, j, r, g, b, byte;
for( i = 0 ; i < height ; i++ ) {
for( j = 0 ; j < width ; j++ ) {
r = data[i*step + j*nchannels + 0];
g = data[i*step + j*nchannels + 1];
b = data[i*step + j*nchannels + 2];
byte = ( r + g + b ) / 3;
int v0=0, v1=0, v2=0;
data2[i*(img2->widthStep)+j*(img2->nChannels)+0] = byte;
data2[i*(img2->widthStep)+j*(img2->nChannels)+1] = byte;
data2[i*(img2->widthStep)+j*(img2->nChannels)+2] = byte;
}
}
cvNamedWindow("ImagenColor", 1);
cvShowImage("ImagenColor", img);
cvNamedWindow("Gray", 1);
cvShowImage("Gray", img2);
}