How to process jpg images with c/c++ most easily? - c++

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.

Related

read jpg into a 2d array of pixels in CPP [duplicate]

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.

How to store images in c++ with Ximea

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

Simple bitmap graphics in c++?

I'm a Flash programmer and I'm currently exploring C++. In flash, you can create a bitmap and place it on the screen, and then use methods like getPixel(x, y), setPixel(x, y, c) ect. Press ctrl+enter and you can get started with what ever you want to do.
I use Visual C++ 2010. Since I've used Flash alot I'm used to simple and short commands. In C++ though, it's harder to figure out how to get a bitmap where you can manipulate pixels.
I don't know much about graphics enginges or 3D engines, it would be very useful information, but first I'd like to see what I can create with pixels, so do you know a simple way to create a manipulatable bitmap in C++? As optimized as possible, then I can write my own drawLine, drawCurve ect functions. :)
Because you mentioned Visual C++ 2010, I will assume that you are using Vista or higher and you want to first draw 2D graphics using the native Windows C++ approach. If this is the case, you want to use Direct2D. You may find old articles that use GDI, but this is the old way, so don't use it. Here is the link to MSDN introducing Direct2D.
If you really want to use C++ you should look at using GDI+ which is the standard windows/VS way of doing graphics. This is a slight highter level (friendlier) api than the older GDI api for doing graphics that dates back to (pre?) MFC days. You will need to get a basic grasp of device contexts etc... in order to understand how to load your bitmap from a file and get it on the screen.
CGI+ allows easy manipulation of a bitmap on a per pixel basis using the LockBits method. It can read most common image formats (bmp, jpg, png etc).
The example code below shows a typical load bitmap and read some pixels type code (it is taken verbatim from this msdn gdi+ article
#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;
INT main() {
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
Bitmap* bitmap = new Bitmap(L"LockBitsTest1.bmp");
BitmapData* bitmapData = new BitmapData;
Rect rect(20, 30, 5, 3);
// Lock a 5x3 rectangular portion of the bitmap for reading.
bitmap->LockBits(
&rect,
ImageLockModeRead,
PixelFormat32bppARGB,
bitmapData);
printf("The stride is %d.\n\n", bitmapData->Stride);
// Display the hexadecimal value of each pixel in the 5x3 rectangle.
UINT* pixels = (UINT*)bitmapData->Scan0;
for(UINT row = 0; row < 3; ++row) {
for(UINT col = 0; col < 5; ++col)
printf("%x\n", pixels[row * bitmapData->Stride / 4 + col]);
printf("- - - - - - - - - - \n");
}
bitmap->UnlockBits(bitmapData);
delete bitmapData;
delete bitmap;
GdiplusShutdown(gdiplusToken);
return 0;
}
As for draw line, draw curve etc routines - these are all found on the Graphics object in GDI+. Its the main object that sits between your code and the screen. A Graphics object would be used to render the above bitmap using Graphics.DrawImage.
Well the answer that is about to come is probably one you won't like, but I'll give it anyways :)
C++ has no notion of graphics output at all. Luckily, we have gotten a standardized way of printing text to the screen - but thats it. Really. No joking here.
However, most operating systems provide a means of graphics output (mostly via a C interface, as C is binary compatible to almost everything), and there are also C++ wrapper libraries that incorporate access to those. Dealing with OS issues is, however, a very different beast than C++ is (and thats kinda beast, I can tell ;).
Luckily, if you don't want to/have to understand the full story, I can recommend Qt (http://qt.nokia.com/products/) as a very decent library for GUI programming. And once you have a GUI window, you will also have the possibility to draw a bitmap into that GUI (I believe Qt has direct bitmap support and also the possibility to load images).
However, I will have to refer you to the tutorial docs of Qt, as even a simple introduction would go way over what would be a still understandable forum answer.
And this, I'm afraid, will be as easy as it can get drawing something onto the screen using C++. Nowhere near the simplicity of Flash for that purpose but in the end way more powerful.
Good luck.
You need external library to read images from file. Two libraries I like are SDL_image and stb_image. Both give you image in a format that lets you access and manipulate pixels.
Also to display the image you will need external library. SDL is popular and simple one.
Use PixelToaster if you just want easy access to a framebuffer.

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"

How to get into image manipulation programming?

How can I do simple thing like manipulate image programmatically ? ( with C++ I guess .. )
jpgs/png/gif .....
check out BOOST , it has a simple Image Processing Library called GIL. It also has extensions to import common formats.
http://www.boost.org/doc/libs/1_39_0/libs/gil/doc/index.html
Using .NET you have two options:
GDI+ from System.Drawing namespace (Bitmap class)
WPF engine wich can do a lot of things
If you want low level processing you can use unsafe code and pointers.
A Bitmap or Image is just a big array of bytes.
You need to learn:
what is a stride (extra padding bytes after each row of pixels)
how to compute the next row or a specific pixel location using width, height, stride
the image formats RGB, ARGB, white&black
basic image processing functions (luminosity, midtone, contrast, color detection, edge detection, matrix convulsion)
3D vectorial representation of a RGB color
Depending on how fancy you want to get, you may want to look at OpenCV. It's a computer vision library that has functions ranging from reading and writing images to image processing to advanced things like object detection.
Magick++ is a C++ API for the excellent ImageMagick library.
An advantage of ImageMagick is that it can be used from the command-line and a bunch of popular scripting and compiled languages too, and some of those might be more accessible to you than C++.