RGBA使用C ++将值转换为2D数组

时间:2015-01-10 14:18:27

标签: php c++ imagemagick png magick++

我正在构建一个神经网络算法到C ++并使用图像训练数据。

我需要将数据放在由x,y | rgba值(A 2d数组)表示的像素数组中。

我有ImageMagick和Magick ++。h标题以及编译器选项都已解决。

我知道标题库正在运行,因为我可以:

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

我的图片是32x32,编译程序的结果是: root @jarvis:〜/ Documents / Programming / C ++ / ImageMagick#。/ magick COLS:32行:32

我似乎无法访问像素值。我不像我想的那样精通C ++,但PHP中的一个例子就是这样的函数:

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");

我的cpp文件是这样的:

#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;
}

我目前的工作是使用php函数,用于在db中存储图像数据集(输入向量)的Web表单。然后我至少可以从C ++端访问该表。

我知道如何做到这一点。我只是希望在导入方面提供更优雅的解决方案。提前谢谢大家!

编辑:

要访问像素数据,我尝试过像

这样的事情
#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;
}

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

带有像素[0] [0]或像素[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#

2 个答案:

答案 0 :(得分: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;
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;
}

答案 1 :(得分: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

示例输出:

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

当然,我必须将这些数字舍入到一个整数但是谢谢你的帮助!