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

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.

Related

Having problems loading a jpg file using libjpeg

I need to load jpg files in my application. I used libjpeg to save JPGs (from processed raw files) and it works nicely.
Reading them though is a different issue. I am getting very weird results, the image is very distorted, in 12 columns, which are mostly gray scale.
I followed the example, and the only modification I made is how to put the data in my buffer (the put_scanline_someplace() function is missing from the example.
Here is my relevant code (I need the data in BGR format):
dest=0;
while(cinfo.output_scanline < cinfo.output_height)
{
jpeg_read_scanlines(&cinfo, buffer, 1);
src=0;
for(i=0;i<cinfo.output_width;i++)
{
image_buffer[dest*3+2]=buffer[src*3+0];
image_buffer[dest*3+1]=buffer[src*3+1];
image_buffer[dest*3+0]=buffer[src*3+2];
src++;
dest++;
}
}
Is there something wrong with this code?
I found the solution. buffer isa pointer to an array of ints, so the code that works is like so:
image_buffer[dest*3+2]=buffer[0][src*3+0];
image_buffer[dest*3+1]=buffer[0][src*3+1];
image_buffer[dest*3+0]=buffer[0][src*3+2];

OpenCV imwrite() not saving image

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

Using openCV to read an image from Amazon S3

I am aware that there are multiple C++ libraries to access data from Amazon S3, using cURL. Using them I can download an image to the local machine, read it into OpenCV, and delete the original file.
But, I was wondering if it would be possible to read an image directly from S3 into OpenCV directly as say, a Mat object.
Would anyone be able to help me accomplish this?
Use cv::imdecode, which will load a buffer with some image data (png, jpeg, etc.) from memory into a matrix.
// Suppose that a std::vector<char> contains the data loaded by Curl.
std::vector<char> data;
// The template<typename T> explicit cv::Mat::Mat(const std::vector<T>& vec)
// constructor gives this vector a cv::Mat header.
cv::Mat data_mat(data);
// Now use cv::imdecode to decode the image to a BGR matrix.
cv::Mat image(cv::imdecode(data_mat));
So, this is what I had to do, as far as the Poco side of it goes.
std::auto_ptr<std::istream> pStr(URIStreamOpener::defaultOpener().open(uri));
StreamCopier::copyToString(*pStr.get(), str);
This would get the image from the supplied URI into the string str without any intermediate storage. The rest is as Kristoff explained.

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);
}

converting a binary stream into a png format

I will try to be clear ....
My project idea is as follow :
I took several compression algorithms which I implemented using C++, after that I took a text file and applied to it the compression algorithms which I implemented, then applied several encryption algorithms on the compressed files, now I am left with final step which is converting these encrypted files to any format of image ( am thinking about png since its the clearest one ).
MY QUESTION IS :
How could I transform a binary stream into a png format ?
I know the image will look rubbish.
I want the binary stream to be converted to a an png format so I can view it as an image
I am using C++, hope some one out there can help me
( my previous thread which was closed )
https://stackoverflow.com/questions/5773638/converting-a-text-file-to-any-format-of-images-png-etc-c
thanx in advance
Help19
If you really really must store your data inside a PNG, it's better to use a 3rd party library like OpenCV to do the work for you. OpenCV will let you store your data and save it on the disk as PNG or any other format that it supports.
The code to do this would look something like this:
#include <cv.h>
#include <highgui.h>
IplImage* out_image = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, bits_pr_pixel);
char* buff = new char[width * height * bpp];
// then copy your data to this buff
out_image->imageData = buff;
if (!cvSaveImage("fake_picture.png", out_image))
{
std::cout << "ERROR: Failed cvSaveImage" << std::endl;
}
cvReleaseImage(&out_image);
The code above it's just to give you an idea on how to do what you need using OpenCV.
I think you're better served with a bi-dimensional bar code instead of converting your blob of data into a png image.
One of the codes that you could use is the QR code.
To do what you have in mind (storing data in an image), you'll need a lossless image format. PNG is a good choice for this. libpng is the official PNG encoding library. It's written in C, so you should be able to easily interface it with your C++ code. The homepage I linked you to contains links to both the source code so you can compile libpng into your project as well as a manual on how to use it. A few quick notes on using libpng:
It uses setjmp and longjmp for error handling. It's a little weird if you haven't worked with C's long jump functionality before, but the manual provides a few good examples.
It uses zlib for compression, so you'll also have to compile that into your project.