使用C或C++将文本放置到图像上,而不使用OpenCV中的puttext函数

Ong*_*ong 0 c++ opencv image-processing cimg vivado-hls

有谁知道如何编写代码(C/C++)将文本放入图像而不使用opencv中的puttext函数?我已经用谷歌搜索这个功能很长一段时间了,但没有找到解决方案。

Mar*_*ell 5

我向您推荐CImg - 它是干净的、现代的 C++ 并且非常强大。集成到项目中非常简单,因为它“只有标头**”——只有一个包含文件,没有可链接的库。

它可以本机读取和写入NetPBM文件(PGM、PPM 等),无需任何特殊链接,并且可以使用libjpeg、或ImageMagick读取和写入其他所有内容。libtifflibpng

如果您愿意,它还可以通过 GDI 在 X11 或 Windows 上显示图像。

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

using namespace cimg_library;
using namespace std;

int main() {
   // Create 640x480 image
   CImg<unsigned char> image(640,480,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   };
   unsigned char yellow[]  = {255, 255, 0   };

   // Draw black text on cyan
   image.draw_text(30,60,"Black 64pt on cyan",black,cyan,1,64);

   // Draw yellow partially transparent text on black
   image.draw_text(80,200,"Yellow 32pt on black semi-transparent",yellow,black,0.5,32);

   // Save result image as NetPBM PNM - no libraries required
   image.save_pnm("result.pnm");
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述