[c++] How to get Image.pixelColor() RGB channels?

Magick++ is an object-oriented C++ interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick++.
Post Reply
TheMohawkNinja
Posts: 3
Joined: 2019-07-05T17:47:00-07:00
Authentication code: 1152

[c++] How to get Image.pixelColor() RGB channels?

Post by TheMohawkNinja » 2019-07-05T18:00:23-07:00

Hello,

I am trying to write a program that will take a 30*30 sample of an image, but only use pixels that are deemed to be sufficiently saturated. I'm not sure how to check whether the red channel OR green channel OR blue channel are greater than 150.

Current program (the ".red()", ".green()", and ",blue()" bits are something I read online, but g++ throws "no member" errors. I've also tried it with ".quantumRed()" instead of ".red()", but it too throws a "no member" error):

Code: Select all

#include <cstdlib>
#include <Magick++.h>
#include <iostream>

using namespace std;
using namespace Magick;

int main(int argc, char **argv)
{
        InitializeMagick(*argv);
        Image background("/home/user/Downloads/space1.jpg");
        int bgW=background.columns();
        int bgH=background.rows();
        Image ColorSpace(Geometry(30,30),Color(255,255,255));
        Color ColorSample[30][30];

        //cout<<"Width="<<bgW<<endl;
        //cout<<"Height="<<bgH<<endl;

        //Make 30*30 sample space from image
        for(int y=0; y<30; y++)
        {
                for(int x=0; x<30; x++)
                {
                        ColorSample[x][y]=background.pixelColor(((bgW/30)*x),((bgH/30)*y));
                        //cout<<"Position=("<<((bgW/30)*x)<<","<<((bgH/30)*y)<<")"<<endl;

                        if(ColorSample[x][y].red()>150 || ColorSample[x][y].green()>150 || ColorSample[x][y].blue()>150)
                        {
                                ColorSpace.pixelColor(x,y,ColorSample[x][y]);
                        }
                }
        }

        ColorSpace.magick("jpg");
        ColorSpace.write("ColorSpace.jpg");

        return 0;
}

TheMohawkNinja
Posts: 3
Joined: 2019-07-05T17:47:00-07:00
Authentication code: 1152

Re: [c++] How to get Image.pixelColor() RGB channels?

Post by TheMohawkNinja » 2019-07-10T19:06:59-07:00

Figured it out. Had to make a "colorRGB" object, Set it's values through GetPixelColor() and then pull the channels by the .red(), .blue(), and .green() methods.

Post Reply