I have an array of unsigned char array which is actually a RGBA raw image. How to export it into the image file? Any image file format is OK.
You can write it straight out as RGBA in binary. Then use ImageMagick, which is installed on most Linux distros, and available for OSX and Windows to convert it to PNG or something more common:
convert -size 300x400 -depth 8 binary.rgba result.png
You will need to tell ImageMagick the dimensions of the image as above because they are obviously not embedded within a raw file like they would be in a JPEG or PNG with a header.
If the filename you choose does not end in .rgba, you will need to prefix it with RGBA: like this
convert -size ... RGBA:something.bin result.png
If you are using OSX, I personally think homebrew is the easiest way to install ImageMagick...
brew install imagemagick
I think your best bet is just saving it to a .bmp file, because basically bitmap is a (upside down) array of rgba values. So you can just write a simple header and then write it line by line to the file.
For a header you'd have to write the file header first, and after that there is the DIB header. There are multiple options for this, but i'd suggest the simple one.
The bitmap format has support for a lot of fancy stuff, but most of it is optional and for your propose not necessary.
Related
I have an array of 16-bit values and I want to create an image (such as BMP) so that I can view it. Does anyone have suggestions of how to do this?
Thanks
Say your image is 200px by 100px. Write the 20,000 16-bit vales to a binary file.
At the commandline, run ImageMagick:
magick -depth 16 -size 200x100 gray:yourFile.bin image.png
Or, if you want a JPG
magick ... image.jpg
For a tiny bit more effort, you could write a 16-bit PGM file which would have the benefit that it contains its dimensions and bit depth in a small ASCII header so the conversion in ImageMagick is simpler, and other programs such as GIMP can read it:
magick yourFile.pgm image.jpg
The header would be:
P5
200 100
65535
... binary data as above ...
See Wikipedia NetPBM article.
I have an image stored in memory in the form of raw bytes i.e. I have a char* pointing to the memory location of the image data. Now, I need to somehow validate if the image data is legitimate.
What I have currently tried is to simply dump the bytes into a file. I tried dumping into 3 types of files, but no luck:
std::ofstream ofs;
ofs.open("Image.raw", std::ofstream::out);
ofs.write((char*)imgData, imageInfo.imageLen);
ofs.close();
// Have also tried "Image.tiff" and "Image.ppm"
Is there any way to view the contents? Just to mention, I am writing this code on Win platform. A few years back, I remember doing the similar thing on MAC OS X, which yielded successful results!.
You can write it straight out as RGB in binary like you already have - say to a file called image.rgb.
Then use ImageMagick, which is installed on most Linux distros, and available for OSX and Windows to convert it to PNG, JPEG or something more common:
convert -size 300x400 -depth 8 image.rgb result.png
or
convert -size 300x400 -depth 8 image.rgb result.jpg
You will need to tell ImageMagick the dimensions of the image as above because they are obviously not embedded within a raw file like they would be in a JPEG or PNG with a header.
If the filename you choose does not end in .rgb, you will need to prefix it with RGB: like this
convert -size ... RGB:something.bin result.png
to read bmp files we may use this http://msdn.microsoft.com/en-us/library/dd183376(VS.85).aspx
as the header file and then get rgb triplets. How to get the rgb triplets of jpeg file, is there any such header file available. Please share the link if any.
The JPEG file format does not store the rgb triplets directly but it uses some sort of image compression. The file actually contains blocks of 64 (if I remember correctly) pixels which are attributed with a cosine pattern defining the actual colors.
You really should use a library (libjpeg, imagemagick, gd, ... e.g., depending on your use case) to read and decode the files and generate the rgb triplets in memory.
According to the answer to this question on MSDN, you could use the GDI+ component, which can load not only BMP, but JPG and other image formats too. From it, you will get a memory bitmap.
Here is an example on how to do that.
Check this library: libjpeg. This library implements JPEG image encoding, decoding,
and transcoding.
I'm trying to use the imwrite() OpenCV function. I want to save the frames with the .TIFF extension. The problem that I have is that the saved images are compressed so I can't use them. Any idea how I can escape this compression?
thanks in advance
Do not mind what sietschie says. The TIFF flag is hardcoded in the opencv binaries with a LZW compression. You can just turn this off (comment it out) or change it.
In:
3rdparty/libtiff/tiff.h
Remove this line:
#define COMPRESSION_LZW 5 /* Lempel-Ziv & Welch */
Then compile. Presto.
Tiff options other than that are automatically set (8 bit, 16bit, color, rgb, rgba,etc) depending on your image
According to the documentation OpenCV only exposes a limited set of options for writing image files.
Non of which belongs to TIFF-Files.
So unless you want to use your own function or modify the OpenCV source, this is not possible.
I would suggest using another uncompressed format for saving the frames like PXM or BMP, unless you have some specific reasons to use TIFF-Files.
cv::imwrite("imagen.TIFF", bayer, {cv::IMWRITE_TIFF_COMPRESSION, 1,
cv::IMWRITE_TIFF_XDPI, 72,cv::IMWRITE_TIFF_YDPI,72});
The simplest way is recompiling OpenCV or direct using libtiff, but I consider as not very good idea changing 3rdparty/libtiff/tiff.h: after this modification you can't save compressed TIFFs at all with OpenCV, and under non-windows systems you usually have separate libtiff (not as a part of OpenCV).
I suggest simpler approach (still OpenCV recompilation, but you save possibility of writing compressed tiff and don't change libtiff directly):
saving uncompressed TIFFs with OpenCV
I am doing a CBIR system as an assignment.There are 100 .bmp files,but they have different size,how to re-size them to a same size?
Thanks.
Have a look at the CImg Library, it's quite easy to use. You can load your bitmap file then use one of the resize function.
Probably a overkill, but you can take a look on ImageMagick.
You should look at G'MIC, a command-line tool to batch image processing operations.
It is even more advanced than ImageMagick's convert tool.
Basically, you can call it like this :
gmic *.bmp -resize 128,128,1,3,3 -outputp resized_
to resize all your bmp images into 128x128 color images, and save them with filenames prefixed by 'resized_'.
G'MIC is available for Linux, Windows and Mac, at : http://gmic.sourceforge.net