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#