0

I am building a neural net algorithm into C++ and using images for training data.

I need the data to be in an array of pixels represented by x,y|rgba values (A 2d array).

I have ImageMagick and the Magick++.h header plus compiler options all worked out.

I know the header library is working because I can :

int col = image.columns();
int row = image.rows();
cout << "COLS: " << col << "ROWS : " << row << endl;

My images are 32x32 and the result of the compiled program is: root@jarvis:~/Documents/Programming/C++/ImageMagick# ./magick COLS: 32 ROWS : 32

I just cannot seem to access the pixel values. I am not so fluent in C++ as I'd like but an example in PHP would be a function like this:

Function ImageToVector($Filename){
    // open an image
    $im = imagecreatefrompng($Filename);
    $width = imagesx($im);
    $height = imagesy($im);

    $i = 0;
    // get a color value for each pixle in width/height matrix
    for ($x = 0; $x < $width; $x++){
        for($y =0; $y < $height; $y++){
            $color_index = imagecolorat($im,$x,$y);
            // make it human readable and store it in the inputVector array.
            //each pixel is read into the array one after the other making it a single inputVector
            //later, we should know the dimensions of our input images (which should all be the same size in pixels).
            //so we can lay it back down layer by layer if we wish to reconstruct the image from the rgba data in our input vectors later
            $inputVector[$i] =  imagecolorsforindex($im, $color_index);
            $i++;
        }
    }
    $color_tran = imagecolorsforindex($im, $color_index);
    //return the input vector for entire image as an array
    return ($inputVector);
}

$i=0;
$InputVector[$i] = ImageToVector("Example0.png");

My cpp file is this:

#include <iostream>
#include "/usr/include/ImageMagick/Magick++.h"

using namespace std;
using namespace Magick;

int main()
{
Image image("a.png");
int col = image.columns();
int row = image.rows();
PixelPacket *pixels = image.getPixels(0,0,col,row);


cout << "VALUE X: " << col << "  ROWS : " << row << endl;


return 0;
}

My work around currently is to use the php function as is with a web form used to store the set of image data (input vectors) in a db. Then I can at least access that table from the C++ side.

I know how to do that much already. I was just kind of hoping for a more elegant solution on the import side. Thanks in advance everyone!

EDIT:

To access the pixel data I have tried things like

#include <iostream>
#include "/usr/include/ImageMagick/Magick++.h"

using namespace std;
using namespace Magick;

int main()
{
Image image("a.png");
int w = image.columns();
int h = image.rows();
PixelPacket *pixels = image.getPixels(0, 0, w, h);

int row = 0;
int column = 0;
Color color = pixels[w * row + column];

int x = pixels[0];



cout << "COLS: " << x << endl;


return 0;
}

or int x = pixels[0][0];

with either pixels[0][0] or pixels[0]

root@jarvis:~/Documents/Programming/C++/ImageMagick# ./compile_main.sh
main.cpp: In function âint main()â:
main.cpp:20:17: error: cannot convert âMagickCore::PixelPacket {aka MagickCore::_PixelPacket}â to âintâ in initialization
root@jarvis:~/Documents/Programming/C++/ImageMagick# ./compile_main.sh
main.cpp: In function âint main()â:
main.cpp:20:20: error: no match for âoperator[]â in â* pixels[0]â
root@jarvis:~/Documents/Programming/C++/ImageMagick#
  • You haven't said anything about what the interface for `PixelPacket` looks like or what you've tried to do to get the pixels out of `PixelPacket` and into your array. Are you getting garbage in your array? Are you getting all zeros? What have you tried and what's wrong with your result? – mbgda Jan 10 '15 at 14:31
  • This should help you http://stackoverflow.com/questions/7678511/getting-pixel-color-with-magick – sn710 Jan 10 '15 at 15:37
  • Well I suppose then that is exactly where I am stuck raveesh. Thank you for your reply btw! I am kind of a noob with c++ so I don't know where to go from there to accomplish what I have in my php function. – Jay Slonaker Jan 10 '15 at 17:13
  • I actually got most of the code I have from that exact question. I just don't know how to access the rgb values and store them in an array like int a[x][y] = {{r,g,b},{100,200,125},{r,g,b},{r,g,b},{r,g,b}}; I am not really interested in outputting the image again. Just the rgb matrix. – Jay Slonaker Jan 10 '15 at 17:19
  • like for my images with 32x32 pixels, it would be like image[1023,3] – Jay Slonaker Jan 10 '15 at 17:22
  • or the rgba actually would be int image[1023,4]{{r,g,b,a},....} – Jay Slonaker Jan 10 '15 at 17:23
  • Magick::Color color = pixels[w * row + column]; is actually giving you the color at pos = w * row + column if w = column , then it is the last position. Beyond that i think you can do color.redQuantum or color.blueQuantum or color.greenQuantum to get the rgb values – sn710 Jan 10 '15 at 17:46

2 Answers2

0

I edited your code a bit, just to make it clear

#include <iostream>
#include "/usr/include/ImageMagick/Magick++.h"

using namespace std;
using namespace Magick;

int main()
{
Image image("a.png");
int w = image.columns();
int h = image.rows();
PixelPacket *pixels = image.getPixels(0, 0, w, h);

int row = 0;
int column = 0;
Color color = pixels[0]; // get first pixel color as an example

unsigned int red = color.redQuantum;
unsigned int blue = color.blueQuantum;
unsigned int green = color.greenQuantum;
unsigned int alpha = color.alphaQuantum;


cout << "RED:" << red << "BLUE:" << blue << "GREEN:" << green << "ALPHA:" << alpha << endl;


return 0;
}
sn710
  • 581
  • 5
  • 20
  • compiler error: cannot resolve overloaded function âredQuantumâ based on conversion to type âfloatâ Feel like we are getting close though! lol and THANK YOU! – Jay Slonaker Jan 10 '15 at 18:18
  • So it returns a float maybe :) ...try with float red = color.Quantum – sn710 Jan 10 '15 at 18:19
  • I think I am seeing the "formula" I was missing though. You are accessing pixel[0] there and setting variables based on the array inside of pixel[0]. I was not able to figure out how to even access the elements in the array/object pixel[0] – Jay Slonaker Jan 10 '15 at 18:21
0
#include <iostream>
#include "/usr/include/ImageMagick/Magick++.h"

using namespace std;
using namespace Magick;

int main()
{
    Image image("a.png");
    int w = image.columns();
    int h = image.rows();
    PixelPacket *pixels = image.getPixels(0, 0, w, h);

    int row = 0;
    int column = 0;


    for(int i = 0; i < w*h; i++){
        Color color = pixels[i]; // get first pixel color as an example

        float red = color.redQuantum();
        float blue = color.blueQuantum();
        float green = color.greenQuantum();
        float alpha = color.alphaQuantum();

        //translate the bit value into standard rgba(255,255,255) values
        if (red != 0){ red = red/256;}  //if the (r)gba  vector is 0, don't divide by 256
        if (blue != 0){ blue = blue/256;} //if the r(g)ba  vector is 0, don't divide by 256
        if (green !=0) { green = green/256;}//if the rg(b)a  vector is 0, don't divide by 256
        if (alpha !=0) { alpha = alpha/256;}//if the rgb(a)  vector is 0, don't divide by 256

        //output red,green,blue values
        cout << "R: " << red << " G: " << green << " B :" << blue << " A:" << alpha << endl;


    }

    return 0;
}

**edited for clearer output

example output:

R: 110.43 G: 110.43 B :110.43 A:0
R: 114.445 G: 114.445 B :114.445 A:0
R: 117.457 G: 118.461 B :118.461 A:0
R: 121.473 G: 121.473 B :122.477 A:0
R: 124.484 G: 125.488 B :125.488 A:0
R: 127.496 G: 128.5 B :128.5 A:0
R: 130.508 G: 130.508 B :130.508 A:0

Of course I will have to round those numbers to an int but thank you for your help here!