OpenCV jpeg format image in memory - c++

Is there a possibility of saving an image to a string instead of a file with OpenCV?
I have found just this function in the docs:
int cvSaveImage(const char* filename, const CvArr* image)
And it can only save to files.

How did you want to code the image in the string?
You could just store red,green,blue values as comma separated strings but coding the image binary as something like base64 would be more compact.
Edit: if you mean a jpeq format in memory see - OpenCV to use in memory buffers or file pointers

Maybe you can simply create a char* buffer and copy your IplImage data structure there ?
But be careful, you should carefully copy the pointer variables, such as imageData, imageDataOrigin and so on.
So the idea is to put everything in your buffer in the way you can decode it from the receiver's side.
And opencv is an opensource library and you can simply copy the opencv's savefile function body, replacing writing in a file to writing in your buffer

Related

How can I send an wxImage through wxSocket in c++?

How can I send a wxImage through TCP wxSocket in c++?
Should I convert the wxImage to a wxString or can it be directly send over?
In that case how do I convert a wxImage tobe a wxString?
You have direct access to the image data via its GetData() member, so you could just send it directly, at least if you have an image without alpha. However in practice, compressing it first would probably be a good idea as uncompressed images are huge. You could either use zlib compression or, perhaps simpler, write the image in PNG or JPEG format to a memory buffer and send this buffer instead.
Converting the image to a string would be possible too but highly suboptimal.
Hai wxImage::GetData() return unsigned char* so you can easily send image as a string.Don't forget to send image width&height, this two attributes must need for again converting data into image.

Read image and access bytes in boost::gil

Is it possible to:
read an image given by just a filename (not knowing the image format) to a 2d matrix rgb uncompressed form (e.g. read an JPG to a 2d array)
access the bytes of that image, copy them, change them... (e.g. inverse the colors, I need a pointer to the image bytes, setters/getters won't do )
save those bytes to any given image format (e.g. save the inversed image to PNG)
Is it possible with boost::gil ? Maybe there is a different library more appropriate for such a task?
Sample code would be highly appreciated.
Yes, you can do all that in boost::gil.
What you should know though, is that boost::gil is only a universal interface and doesn't handle reading/writing images all by itself. You still need to use a second library, e.g. libpng..
Yes, yes and yes.
There are functions that enable you to read and write JPEG, TIFF and PNG images: see here.
For the second bullet, it is what just GIL is meant to do. You can manipulate images using its facilities (click here).

How do I create a QIcon from binary data?

I have a custom file format which stores some images. I load these images into memory during the operation of my program. The images are loaded in binary format (i.e. the same way they would appear on disk). I know what format the images are in (pretty much all jpeg). The problem is that I dont know the width/height, but I want to display the images without writing them to some temp file on disk. Anyone know how to do this?
You can get the size of a qIcon using actual size
http://qt-project.org/doc/qt-5.0/qicon.html#actualSize
You shouldn't have to write anything to disk, you just need to read your binary into something like QIcon, so that you can use QTs methods to get the width and height of the image. You just need to get the data into one of QTs data structures.
Check out QImage and QPixmap, I think they have methods for reading in binary. Or you can check this SO post here: QImage from unsigned char buffer (jpg format)

Send Iplimage data as jpeg

I am trying to send Iplimage to another program as a array of bytes in c++/cli. The program needs to save the Iplimage as jpeg image. Let's say the Iplimage is img which I am obtaing by cvQyeryFrame from an avi video file, I am returning img->imagedata to the other program. Does img->imagedata contain the header for the jpeg image to be saved or does it only contain the data. If it only contains the data, how can I include the header? I can save the image using cvSaveImage and then read it but there should be a more direct way (maybe cvEncodeImage? )
thanks.
The data in the Iplimage is already decoded into own format of openCV (typically 8bit B,G,R) once it has been read from disk.
The newer versions of opencv can encode/decode an image in memory, see imencode.
If you need the entire JPEG data, you might want to consider reading the entire file from the disk with fread() instead of cvLoadImage().
If that's not going to work for you, consider Martin's answer.

Convert image into useable byte array in C?

Does anyone know how to open an image, specifically a jpg, to a byte array in C or C++? Any form of help is appreciated.
Thanks!
The ImageMagick library can do this too, although often it provides enough image manipulation functions that you can do many things without needing to convert the image to a byte array and handle it yourself.
You could try the DevIL Image Library I've only used it in relation to OpenGL related things, but it also functions as just a plain image loading library.
Check out the source code for wxImage in the wxWidgets GUI Framework. You will most likely be interested in the *nix distribution.
Another alternative is the GNU Jpeg library.
Here is how I would do it using GDIPlus Bitmap.LockBits method defined in the header GdiPlusBitmap.h:
Gdiplus::BitmapData bitmapData;
Gdiplus::Rect rect(0, 0, bitmap.GetWidth(), bitmap.GetHeight());
//get the bitmap data
if(Gdiplus::Ok == bitmap.LockBits(
&rect, //A rectangle structure that specifies the portion of the Bitmap to lock.
Gdiplus::ImageLockModeRead | Gdiplus::ImageLockModeWrite, //ImageLockMode values that specifies the access level (read/write) for the Bitmap.
bitmap.GetPixelFormat(),// PixelFormat values that specifies the data format of the Bitmap.
&bitmapData //BitmapData that will contain the information about the lock operation.
))
{
//get the lenght of the bitmap data in bytes
int len = bitmapData.Height * std::abs(bitmapData.Stride);
BYTE* buffer = new BYTE[len];
memcpy(bitmapData.Scan0, buffer, len);//copy it to an array of BYTEs
//...
//cleanup
pBitmapImageRot.UnlockBits(&bitmapData);
delete []buffer;
}
I have my students use netpbm to represent images because it comes with a handy C library, but you can also put images into text form, create them by hand, and so on. The nice thing here is that you can convert all sorts of images, not just JPEGs, into PBM format, using command-line tools the Unix way. The djpeg tool is available a number of places including the JPEG Club. Students with relatively little experience can write some fairly sophisticated programs using this format.
OpenCV can also do this.
http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/index.html
search for: "Accessing image elements"