我正在将神经网络算法构建到 C++ 中,并使用图像来训练数据。
我需要将数据放在由 x,y|rgba 值(二维数组)表示的像素数组中。
我有 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 ROWS : 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 = 像素[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#