Reputation: 639
I am looking for a great C++ library for doing basic image manipulation. What I need done is to be able to convert an image to grayscale and have access to read the pixels of the image.
I have looked at OpenCV, GIL, CImg and Magick++, but either they were not too great, or I couldn't figure out how to convert an image to grayscale with the library.
Upvotes: 7
Views: 3120
Reputation: 74940
ITK is a great library for image processing. You can convert RGB to grayscale using itk::RGBToLuminanceImageFilter
.
Upvotes: 0
Reputation: 3562
To rough steps to do it using Cimg would be something like:
#include "CImg.h"
using namespace cimg_library;
...
{
...
CImg<unsigned char> image("source.jpg") //[load jpg][2] needs ImageMagick
cimg_for(img,ptr,float) { *ptr=0; } //[Loop][3] over every pixel, eg this method equivalent to 'img.fill(0);'
save_jpeg (const char *const filename, const unsigned int quality=100) //[save as a jpg][4]
...
}
Upvotes: 0
Reputation: 2103
You could also have a look at CxImage (C++ image processing and conversion library) .
Upvotes: 1
Reputation: 490108
If you're going to convert color to greyscale on your own, you don't normally want to give equal weight to the three channels. The usual conversion is something like:
grey = 0.3 * R + 0.6 * G + 0.1 * B;
Obviously those factors aren't written in stone, but they should be reasonably close.
You can simulate different colors of filters by changing the factors -- by far the most common filter in B&W photography is red, which would mean increasing the red and decreasing the other two to maintain a total factor of 1.0 (but keep the G:B ratio around 6:1 or so).
Upvotes: 6
Reputation: 96109
CImg is probably easiest to use, no install it's just a header file
Upvotes: 4