- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想将格式化文本写入图像。 OpenCV 仅提供一组有限的默认字体。是否可以使用其他人?例如从 *.ttf 文件(在 Ubuntu 中)读取它们?
最佳答案
如果您不能或不想使用 Qt 绑定(bind),这里有一种使用 CAIRO 的方法:
#include <opencv2/opencv.hpp>
#include <cairo/cairo.h>
#include <string>
void putTextCairo(
cv::Mat &targetImage,
std::string const& text,
cv::Point2d centerPoint,
std::string const& fontFace,
double fontSize,
cv::Scalar textColor,
bool fontItalic,
bool fontBold)
{
// Create Cairo
cairo_surface_t* surface =
cairo_image_surface_create(
CAIRO_FORMAT_ARGB32,
targetImage.cols,
targetImage.rows);
cairo_t* cairo = cairo_create(surface);
// Wrap Cairo with a Mat
cv::Mat cairoTarget(
cairo_image_surface_get_height(surface),
cairo_image_surface_get_width(surface),
CV_8UC4,
cairo_image_surface_get_data(surface),
cairo_image_surface_get_stride(surface));
// Put image onto Cairo
cv::cvtColor(targetImage, cairoTarget, cv::COLOR_BGR2BGRA);
// Set font and write text
cairo_select_font_face(
cairo,
fontFace.c_str(),
fontItalic ? CAIRO_FONT_SLANT_ITALIC : CAIRO_FONT_SLANT_NORMAL,
fontBold ? CAIRO_FONT_WEIGHT_BOLD : CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cairo, fontSize);
cairo_set_source_rgb(cairo, textColor[2], textColor[1], textColor[0]);
cairo_text_extents_t extents;
cairo_text_extents(cairo, text.c_str(), &extents);
cairo_move_to(
cairo,
centerPoint.x - extents.width/2 - extents.x_bearing,
centerPoint.y - extents.height/2- extents.y_bearing);
cairo_show_text(cairo, text.c_str());
// Copy the data to the output image
cv::cvtColor(cairoTarget, targetImage, cv::COLOR_BGRA2BGR);
cairo_destroy(cairo);
cairo_surface_destroy(surface);
}
调用示例:
putTextCairo(mat, "Hello World", cv::Point2d(50,50), "arial", 15, cv::Scalar(0,0,255), false, false);
假设目标图像是BGR。
它将文本的中心置于给定点。如果你想要一些不同的定位,你必须修改 cairo_move_to
调用。
关于image - OpenCV:如何将 HERSHEY 以外的其他字体与 cvPutText(如 Arial)一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11917124/
我正在尝试在屏幕上打印文本,但一直出现错误。 有人可以帮我处理代码吗? cvNamedWindow("Result", CV_NORMAL); cvMoveWindow("Result", 350,3
我是 OpenCV 的新手。我开始阅读 Learing OpenCV几天前,现在我想做一些测试。 我想编写一个 ASCII 艺术转换器。我已经完成图像迭代并按正方形对像素进行分组以计算每个区域的亮度以
我正在使用 openCV 函数 cvPutText,但它似乎不会执行或很快被覆盖。这是我的代码块: ... if(showResult==1){ cvNamedWi
我想将格式化文本写入图像。 OpenCV 仅提供一组有限的默认字体。是否可以使用其他人?例如从 *.ttf 文件(在 Ubuntu 中)读取它们? 最佳答案 如果您不能或不想使用 Qt 绑定(bind
这是简化的代码: try { Mat pic = imread("../files/words.bmp"); CvFont font; cvInitFont(&font, CV
我是一名优秀的程序员,十分优秀!