如何将位图转换为可以用C++(ANN)处理的"矩阵"?

How to convert bitmaps to 'matrices' that can be processed in C++ ( ANN )?

本文关键字:ANN 处理 矩阵 C++ 位图 转换      更新时间:2023-10-16

我想将提取的字符位图(.bmp文件)输入到某种矩阵中,这些矩阵可以在C++中处理,然后输入到人工神经网络中,例如,该网络将接受72个输入-每个输入都是尺寸为6 x 12的二进制图像的一个像素。

例如:我有一个大小为40 x 80的二进制位图。我想把它做成一个尺寸为6 x 12的结构,它将由我的缩放位图组成。所以我需要一个位图库,它可以让我缩放bmp,然后将它们输入到ANN中

我在这里能用什么?

似乎任何图像处理库都可以满足您的需求。因此,我的建议是使用一个尽可能简单的库来集成到您的构建过程中。在这种情况下,CImg库对我们来说非常容易,因为它是由一个简单的.h文件组成的。

关于您的需求,一种可能的实施方式是

#include "CImg.h"
using namespace cimg_library;
int main(int argc,char **argv) 
{
  CImg<unsigned char> image("img/logo.bmp");
  //Simple resize with nearest neighbour interpolation
  //image = image.resize(64, 64); 
  //If you want to specify the interpolation type
  image = image.resize(64, 64, -100, -100, 4);//The last param specifies the interpolation type 
  //param interpolation_type Method of interpolation :
  //   -1 = no interpolation : raw memory resizing.
  //  - 0 = no interpolation : additional space is filled according to p border_condition.
  //  - 1 = nearest-neighbor interpolation.
  //  - 2 = moving average interpolation.
  //  - 3 = linear interpolation.
  //  - 4 = grid interpolation.
  //  - 5 = bicubic interpolation.
  //  - 6 = lanczos interpolation.
  CImgDisplay main_disp(image,"Image resized");
  //This last part of code is not usfeul for you, it is only used to display the resized image
  while (!main_disp.is_closed() ) 
    main_disp.wait();
  return 0;
}

位图文件格式(请参阅规范)已经将位图存储为矩阵,或者更准确地说是像素阵列,可以按行(或列)划分为2D阵列,但这无关紧要。

因此,您只需要读取标头并获得图像大小,然后读取压缩struct数组中的数据(如本文所述,不带填充)。

通过这种方式,您可以获得矩阵,然后将其封装在一个类中以存储宽度和高度属性,甚至可以将数组提供给个人风格矩阵的构造函数。

使用某种bmp库来访问数据(取决于平台)。这通常会使bmp成为一个平面阵列。取这个平面数组,将每个值插入到矩阵结构中,或者直接将其传递到NN代码中。如果没有更多信息,就无法为您提供更多信息。