开发者

Single channelled images in CImg

开发者 https://www.devze.com 2022-12-16 14:45 出处:网络
I use CImg for my image processing work. I work only on Gray Scale JPG, BMP, TIFF images presently. the problem i am facing with CImg function is as follows:

I use CImg for my image processing work. I work only on Gray Scale JPG, BMP, TIFF images presently. the problem i am facing with CImg function is as follows:

CImg stores the Pixel values in the following way. R1R2R3R4............G1G2G3G4.........B1B2B3B4.........

Even for grey scale images, 3 different channels are created separately. This makes my work very complicat开发者_开发技巧ed. Just for copying values from one image to another, i need to copy all the components of the pixels. I need to iterate across all the channels which make my algorithm slow.

Since i work only with grey scale images it does not matter to me whether it is single channelled or multichannelled.Is there a way to convert 3 channelled image to single channelled in CImg. Please let me know asap.

Thank you all in advance


Use the CImg::channel(int c) function:

CImg<float> img("input.jpg"); //3 channel
img.channel(0); //now single channel
img.save("output.jpg"); //will save as a 3 channel image again

http://cimg.sourceforge.net/reference/structcimg__library_1_1CImg.html#a83af84298188d07c59c49dd0ed4d2714


If you are only interested in single-channel images, you may as well save them as single-channel PGM images that CImg can read and write directly without needing any additional libraries:

#include "CImg.h"
using namespace cimg_library;
int main() {
   // Load colour image
   CImg<unsigned char> image("image.png");

   // Extract Red channel, which is same as Green and Blue in greyscale
   image.channel(0);

   // ALTERNATIVE TO PREVIOUS LINE IS TO USE LUMINANCE
   // image.RGBtoYCbCr().channel(0);

   // Save as single channel PGM file
   image.save_pnm("result.pgm");
}

You can later convert the PGM file (Wikipedia description of PGM) to a JPEG, or PNG, or TIFF with ImageMagick:

convert result.pgm image.jpg
convert result.pgm image.png

Keywords: image processing, image-processing, C++, library, CImg, NetPBM, PBMplus, PBM, PGM, PPM, PAM, greyscale, grayscale, single channel

0

精彩评论

暂无评论...
验证码 换一张
取 消