Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 441
user198729 Avatar asked Jul 15 '10 11:07

user198729


People also ask

How is a JPG encoded?

JPEG uses a lossy form of compression based on the discrete cosine transform (DCT). This mathematical operation converts each frame/field of the video source from the spatial (2D) domain into the frequency domain (a.k.a. transform domain).

Can image processing be done in C++?

C or C++ C or C++ language have been used for image processing because it contains native libraries such as EmguCV, OpenGL and OpenCV have built-in intelligent feature, mainly used for image processing.

How do I read a JPEG?

It's the most widely accepted image format. You can open JPG files with your web browser, like Chrome or Firefox (drag local JPG files onto the browser window), and built-in Microsoft programs like the photo viewer and Paint application. If you're on a Mac, Apple Preview and Apple Photos can open the JPG file.


3 Answers

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;
like image 108
NG. Avatar answered Sep 18 '22 10:09

NG.


Qt has a QImage class:

QImage i("input.jpg");
int x, y;
for (y = 0; &lt; i.height(); ++y) {
   for (x = 0; x &lt; i.width(); ++x) {
      doSomethingWith(i.pixel(x, y));
   }
}
like image 32
akira Avatar answered Sep 21 '22 10:09

akira


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

like image 32
Hernán Eche Avatar answered Sep 19 '22 10:09

Hernán Eche