rv1126 opencv c++加中文水印
1背景
Ubuntu 18.04 PC上用opencv c++加中文水印可以成功,通过libopencv_freetype.so,但是rv1126上没有这个库,网上发现opencv本身不带freetype,需要opencv_contrib扩展,里面有modules带freetype,所以需要移植开源的opencv_contrib,并将配置项对接到menuconfig里面。
开源链接如下:
https://github.com/opencv/opencv_contrib
或者
https://github.com/Itseez/opencv_contrib
解决办法:在buildroot里面看到一段注释如下:opencv3.mk里面
所以解决方法是将freetype模块从opencv_contrib里面移到了opencv里面,然后加上编译选项,修改opencv3.mk来编译freetype,需要注意的是,opencv里面的freetype,需要依赖系统的两个工具:
BR2_PACKAGE_FREETYPE
BR2_PACKAGE_HARFBUZZ
2.步骤
1.将freetype模块从opencv_contrib里面拷贝到opencv的module里面,也就是解压buildroot/dl目录下opencv的压缩包,然后将freetype源码拷贝到解压的opencv源码目录中module文件夹下,拷贝之后重新打包成tar.gz,buildroot会在编译的时候自动解压的。
2.修改buildroot/package/opencv3里面的配置:
zl@zl-Lenovo:~/rv1126-workspace/rv1126_rv1109_linux_sdk/buildroot/package/opencv3$ git diff .
diff --git a/package/opencv3/Config.in b/package/opencv3/Config.in
index b9ea033f13..a54cc4249b 100644
--- a/package/opencv3/Config.in
+++ b/package/opencv3/Config.in
@@ -176,6 +176,14 @@ config BR2_PACKAGE_OPENCV3_LIB_SHAPE
Include opencv_shape (shape descriptors and matchers) module
into the OpenCV build.
+config BR2_PACKAGE_OPENCV3_LIB_FREETYPE
+ bool "freetype"
+ depends on BR2_PACKAGE_FREETYPE
+ depends on BR2_PACKAGE_HARFBUZZ
+ help
+ Include opencv_freetype module into the OpenCV build. This
+ FreeType2 wrapper module allows to draw strings with outlines and bitmaps.
+
config BR2_PACKAGE_OPENCV3_LIB_STITCHING
bool "stitching"
select BR2_PACKAGE_OPENCV3_LIB_CALIB3D
diff --git a/package/opencv3/opencv3.mk b/package/opencv3/opencv3.mk
index 3e3e5987a5..cc0769c894 100644
--- a/package/opencv3/opencv3.mk
+++ b/package/opencv3/opencv3.mk
@@ -104,6 +104,7 @@ OPENCV3_CONF_OPTS += \
-DBUILD_opencv_video=$(if $(BR2_PACKAGE_OPENCV3_LIB_VIDEO),ON,OFF) \
-DBUILD_opencv_videoio=$(if $(BR2_PACKAGE_OPENCV3_LIB_VIDEOIO),ON,OFF) \
-DBUILD_opencv_videostab=$(if $(BR2_PACKAGE_OPENCV3_LIB_VIDEOSTAB),ON,OFF) \
+ -DBUILD_opencv_freetype=$(if $(BR2_PACKAGE_OPENCV3_LIB_FREETYPE),ON,OFF) \
-DBUILD_opencv_viz=OFF \
-DBUILD_opencv_world=OFF
3.make menuconfig配置相应的项
从上面修改可以看到opencv的freetype模块,依赖系统的两个模块:
BR2_PACKAGE_FREETYPE和BR2_PACKAGE_HARFBUZZ
需要确保这两个是打开的,opencv3里面才会出现相应的freetype选项,勾选之后,保存配置修改,然后删掉opencv3,重新编译就可以生成libopencv_freetype.so了。注意:如果是pc上编译的话,需要更新下交叉编译链。里面的库变了。
结论:通过测试上面修改之后,通过下面的代码,可以给图片添加中文水印:
const char *filename = "../ttf-test/opencv-test.jpg";
cv::Mat mat = cv::imread(filename);
if (mat.empty())
{
throw("Faild open file.");
}
cv::Point p = cv::Point(mat.cols/2, mat.rows/2);
cv::Ptr<cv::freetype::FreeType2> ft2 = cv::freetype::createFreeType2();
// cv::String ttf_path = "FZHuangTJXSJF.TTF";
// cv::String ttf_path = "simfang.ttf";
cv::String ttf_path = "../ttf-test/simsun.ttf";
ft2->loadFontData(ttf_path,0);
ft2->putText(mat, "勿忘初心", p, 70, cv::Scalar(0, 255, 0), cv::FILLED,cv::LINE_AA,true);
cv::imwrite("../ttf-test/opencv-test-new.jpg", mat);
// 保存修改后的图片
注意:
实际使用中发现,freetype的这种方式,调用ft2->putText的时候,mat必须是3通道的,否则会报错,原因如下图所示:
- 分享
- 举报
-
浏览量:1206次2023-08-31 16:23:55
-
浏览量:1536次2023-12-29 17:51:55
-
浏览量:1180次2024-05-24 17:28:44
-
浏览量:883次2023-08-31 16:27:23
-
浏览量:908次2024-01-23 11:28:32
-
浏览量:691次2024-01-02 09:46:50
-
浏览量:921次2023-08-30 18:37:06
-
2024-01-05 17:19:54
-
浏览量:1471次2023-07-30 08:53:58
-
浏览量:3911次2021-06-18 16:05:42
-
浏览量:2410次2023-11-29 08:59:50
-
浏览量:1133次2023-12-07 00:53:19
-
浏览量:1143次2024-01-09 17:35:19
-
浏览量:1940次2023-08-11 17:24:43
-
浏览量:737次2023-12-25 14:23:01
-
浏览量:579次2023-11-28 14:16:24
-
浏览量:615次2023-12-21 16:28:56
-
浏览量:579次2024-02-29 16:42:40
-
浏览量:1322次2024-01-27 16:28:20
-
广告/SPAM
-
恶意灌水
-
违规内容
-
文不对题
-
重复发帖
林
感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明