Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with images in C++ or C

The first thing is that I am a beginner. Okay?

I've read related answers and questions, but please help me with this problem:

How can I open an JPEG image file in C++, convert it to a grayscale image, get its histogram, resize it to a smaller image, crop a particular area of it, or show a particular area of it?

For these tasks, is C or C++ faster in general?

What libraries are simplest and fastest? The running time is very important.

Thanks.

like image 349
mohammad Avatar asked Feb 05 '11 12:02

mohammad


1 Answers

here is an example using magick library.

program which reads an image, crops it, and writes it to a new file (the exception handling is optional but strongly recommended):

#include <Magick++.h>
#include <iostream>
using namespace std;
using namespace Magick;
int main(int argc,char **argv)
{
  // Construct the image object. Seperating image construction from the
  // the read operation ensures that a failure to read the image file
  // doesn't render the image object useless.
  Image image;

  try {
    // Read a file into image object
    image.read( "girl.jpeg" );

    // Crop the image to specified size (width, height, xOffset, yOffset)
    image.crop( Geometry(100,100, 100, 100) );

    // Write the image to a file
    image.write( "x.jpeg" );
  }
  catch( Exception &error_ )
    {
      cout << "Caught exception: " << error_.what() << endl;
      return 1;
    }
  return 0;
}

check many more examples here

like image 84
ayush Avatar answered Sep 18 '22 11:09

ayush