I have a superSpeed usb 3.0 Ximea camera and I'm trying to code an application with a Ximea camera that consists on computer vision and machine learning.
I've been able to alocate the frames captured by the camera in it's buffer but I can find the way to save those images or frames as an JPEG or BMP file. I don't know if it's just a command line in my script or I need some kind of libraries to do it.
The images are aquired using these commands:
#define EXPECTED_IMAGES 10
for (int images=0;images < EXPECTED_IMAGES;images++)
{
// getting image from camera
stat = xiGetImage(xiH, 5000, &image);
HandleResult(stat,"xiGetImage");
printf("Image %d (%dx%d) received from camera\n", images, (int)image.width, (int)image.height);
}
As I can extract the data from the images, I suppose that the frame is still in the buffer, but I can't figure out the way to save it as a JPEG or BMP file in the computer.
I would appreciate any help with the code.
Thank you!
Aha, saving the image. I think you might have gotten the answer by now.
But here is mine, and I hope this will be useful for anyone working with machine vision cameras.
I have been working with XIMEA for quite a while now. XIMEA API does not include any functions to save images from the buffer to hard drive. So, you need to write your own function or use some library to save out images. And I think, essentially it all comes down to whether it's RAW or compressed image and what kind of image format you want to save out. ie. BMP, JPEG, PNG, PGM, EXR ......
Let's make couple assumptions first.
Here I assume you want to save out 8bit per pixel RAW image having a resolution of 1024*1024. The size of the image will be 8bit * 1024 * 1024 = 8388608bit = 1048576btye ~= 1MB
By looking at your code, you are using XIMEA API in C++.
Okay...... Here are two ways I used most often to save out images from XIMEA.
Writing all the image pixels to a binary file with a proper header according to the format you want to save out. Here is an example saving a data to a PGM format image.
FILE *file;
char fileName = "example.pgm";
char *image;
int width = 1024;
int height = 1024;
int byte_per_pixel = 1;
int max_pixel_value = 255;
file = fopen (fileName , "w+bx");
if(file == NULL){
fprintf(stderr, "Cannot open %s\n", fileName);
return -1;
}
size_t n = 0;
n += fprintf(file, "P5\n# Comment goes here!\n%d %d\n%d\n", width, height, max_pixel_value);
n += fwrite(image, 1, width * height * byte_per_pixel, file);
fclose (fileToSave);
Saving image to PGM may seem easy but when you need to save an image having pixel depth higher than 8bit, you need to deal with endianness issue, since PGM big-endian format. Here is a link to Netpbm formats if you want to read more about it. https://en.wikipedia.org/wiki/Netpbm_format
And also, other formats may have way more complicated data structure then you cannot just simply put down a header. So, using an image library or OpenCV will be a lot less cumbersome.
The handy OpenCV imwrite. Since you are gonna deal with pixels, OpenCV is a good library to have. OpenCV is a powerful library helps you with manipulating matrixes easier than ever. And it comes with a lot of useful stuff like GPU accelerated OpenCV functions. Back to the topic, imwrite can save images to many formats. Here is an example I wrote to save RAW data to PNG format.
string fileName = "example.png";
char *image;
int width = 1024;
int height = 1024;
int byte_per_pixel = 1;
int max_pixel_value = 255;
cv::Mat img_raw = cv::Mat(height, width, CV_8UC1, image);
vector compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(0);
cv::imwrite(PNGFileName, img_raw, compression_params);
imwirte will determine what kind of format you want to save out based on the filename extension. And just a couple lines of code. OpenCV saves out the image for you effortlessly. Here is a link to OpenCV documentation of imwirte, http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imwrite
I hope my answer can help you and others are wondering how to save out images.
to store images from XIMEA cameras I would recommend to use the OpenCV library
as it provides tools for storing both JPEG and BMP image formats. Please download
a short example that demonstrates the storing of several different data formats
from the camera to JPEG and BMP images. Download the archive with MSVC 2013 project and OpenCV3.0 binaries from here and use password SHWJGRAIHFLG for
extraction.
If you should have any other questions concerning XIMEA products, please visit
and register directly on the XIMEA customer support. Thank you.
Best regards,
XIMEA team
Related
I want to iterate over each pixel color in a jpg format image,
which library should I refer to to do this so that the code can be as short as possible?
I can think of either ImageMagick or CImg. Here is a CImg tutorial for you. They abstract away a lot of the decompression details and just give you a grid to work with.
If you go with CImg, you only need to use the data call. You can probably do something like:
CImg<unsigned char> src("image.jpg");
int width = src.width();
int height = src.height();
unsigned char* ptr = src.data(10,10); // get pointer to pixel # 10,10
unsigned char pixel = *ptr;
Qt has a QImage class:
QImage i("input.jpg");
int x, y;
for (y = 0; < i.height(); ++y) {
for (x = 0; x < i.width(); ++x) {
doSomethingWith(i.pixel(x, y));
}
}
I would use Allegro Game Library http://liballeg.org/Allegro
it's simple / open source / free / multiplatform, and you can iterate pixel by pixel if you want
jpeglib is quite a handy library to play with jpegs.
For access to pixel level information. libjpeg & JAI Image I/O Tools should be sufficient. JAI provide advance image processing framework more than this. All options allow you to read/write pixels to an image file.
libjpeg
In C/C++. It is available for Linux and Window. I have used it in Linux before and it works well. However, if you would like to process other image type, you may need to get other package and learn another API interface.
Java Advanced Imaging(JAI) Image I/O Tools
Remember to download the platform specific java package because there is specific optimization for different system. It covers not only JPG but other image format too with exact same API interface.
Java Advanced Imaging(JAI)
If you plan to do more advance level of processing such as using image operator and filter, you can use this. This is a package provide higher level functionality than JAI Image I/O Tools.
EDIT: to remove the 1, 2 & 3 as suggested
Try FreeImage, it seems pretty versatile and many projects use it.
I have a program that grabs an image using BitBlt() and is stored in a variable unsigned char* image. Ideally before uploading it to my server I would like to compress it since it's somewhat large uncompressed. I've tried a couple different ways with zlib, writing my own run length encoding method, and more but no luck. I chalk it up to me not having a strong understanding compression.
I searched and found some examples using zlib but my image appears to stay the same size after attempting to compress it. Here is that code:
unsigned char* compressBitmap(size_t inImgSize, unsigned char* inImage)
{
uLongf srcLen = inImgSize;
uLongf destLen = compressBound(srcLen);
unsigned char* outImage = new unsigned char[destLen];
int res = compress((Bytef*)outImage, &destLen, (Bytef*)inImage, srcLen);
return outImage;
}
I checked and confirmed it is not returning an error. Everything is executed and the image makes it to the server fine, just not compressed at all. I am probably making a bunch of dumb mistakes so any help is appreciated.
Lossless compression on image data doesn't usually lend itself to reduced file sizes (especially photos, simple icons maybe). If you want to know whether the compression is actually working, try uploading an entirely black image and see if the size reduces. If you can see a reduction in file size on a black image, but not the images you are using, you may have to consider using a lossy format instead (e.g. jpeg, png, etc).
I am trying to read a video file using OpenCV (C++), apply a filter to each frame and write a new modified frame into an output file.
The crucial parts of the code are these:
int out_format = CV_FOURCC('M','P','4','2'); // can be another one
double fps = media.get(CV_CAP_PROP_FPS),
width = media.get(CV_CAP_PROP_FRAME_WIDTH),
height = media.get(CV_CAP_PROP_FRAME_HEIGHT);
// On Linux FFMPEG is used to write videos
VideoWriter writer("./" + outputname + ".mkv", out_format, fps, Size(width, height) );
.......
.......
writer.write(newFrame); // Mat newFrame
The fact is, I am not proficient with codecs and video output formats, thus I would like to know why a file which is around 280 MB produces an output of 2 GB.
Is that a codec problem? I have tried DIVX, MPEG and others. Moreover, some output formats must go together with specific codecs.
Try using the 'M','P', '4', 'V' codec with the .mp4 container, that is, set the extension to your filename as something.mp4
For the mp4 container, you could also try, MPEG, MJPG, FMP4
I want to iterate over each pixel color in a jpg format image,
which library should I refer to to do this so that the code can be as short as possible?
I can think of either ImageMagick or CImg. Here is a CImg tutorial for you. They abstract away a lot of the decompression details and just give you a grid to work with.
If you go with CImg, you only need to use the data call. You can probably do something like:
CImg<unsigned char> src("image.jpg");
int width = src.width();
int height = src.height();
unsigned char* ptr = src.data(10,10); // get pointer to pixel # 10,10
unsigned char pixel = *ptr;
Qt has a QImage class:
QImage i("input.jpg");
int x, y;
for (y = 0; < i.height(); ++y) {
for (x = 0; x < i.width(); ++x) {
doSomethingWith(i.pixel(x, y));
}
}
I would use Allegro Game Library http://liballeg.org/Allegro
it's simple / open source / free / multiplatform, and you can iterate pixel by pixel if you want
jpeglib is quite a handy library to play with jpegs.
For access to pixel level information. libjpeg & JAI Image I/O Tools should be sufficient. JAI provide advance image processing framework more than this. All options allow you to read/write pixels to an image file.
libjpeg
In C/C++. It is available for Linux and Window. I have used it in Linux before and it works well. However, if you would like to process other image type, you may need to get other package and learn another API interface.
Java Advanced Imaging(JAI) Image I/O Tools
Remember to download the platform specific java package because there is specific optimization for different system. It covers not only JPG but other image format too with exact same API interface.
Java Advanced Imaging(JAI)
If you plan to do more advance level of processing such as using image operator and filter, you can use this. This is a package provide higher level functionality than JAI Image I/O Tools.
EDIT: to remove the 1, 2 & 3 as suggested
Try FreeImage, it seems pretty versatile and many projects use it.
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"