我该如何在BGR图像的像素上绘制文本字符串,以便在显示时可读c ++?

时间:2019-06-04 14:51:37

标签: c++ image draw

是否存在一种简单的方法来修改72x72像素BGR图像的像素,以使其包含显示图像时可读的文本字符串。

本质上,我需要在str上的下面创建的图像缓冲区img上绘制文本,以便可以在显示图像时读取它。

unsigned char img[72*72*3]; // 72*72*3 BGR image buffer
unsigned char B = 0x00; 
unsigned char G = 0x00;
unsigned char R = 0x00;
std::string str = "Test Text";

// Create BGR image
for (int i = 0; i < (72*72*3); i += 3)
{
    img[i + 0] = B;
    img[i + 1] = G;
    img[i + 2] = R;
}

// Draw str on BGR image buffer?

1 个答案:

答案 0 :(得分:1)

我建议CImg这样:

#include <iostream>
#include <cstdlib>
#define cimg_display 0
#include "CImg.h"

using namespace cimg_library;
using namespace std;

int main() {
   // Create 72x72 RGB image
   CImg<unsigned char> image(72,72,1,3);

   // Fill with magenta
   cimg_forXY(image,x,y) {
      image(x,y,0,0)=255;
      image(x,y,0,1)=0;
      image(x,y,0,2)=255;
   }

   // Make some colours
   unsigned char cyan[]    = {0,   255, 255 };
   unsigned char black[]   = {0,   0,   0   };

   // Draw black text on cyan
   image.draw_text(3,20,"Test text",black,cyan,1,16);

   // Save result image as NetPBM PNM - no libraries required
   image.save_pnm("result.pnm");
}

enter image description here

在功能,现代C ++和“仅标头” 方面,它体积小,速度快,功能全面,这意味着您也不需要链接任何对象。