日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Ubuntu >内容正文

Ubuntu

Ubuntu下使用OpenCV显示中文

發(fā)布時(shí)間:2024/1/8 Ubuntu 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Ubuntu下使用OpenCV显示中文 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

由于Opencv默認(rèn)不顯示中文,所以我們需要通過(guò)需要通過(guò)一些庫(kù)來(lái)設(shè)置OpenCV支持中文顯示

  • 源碼下載鏈接:Ubuntu下Opencv顯示中文

代碼說(shuō)明

項(xiàng)目需要ft2build.h,它是freetype庫(kù)中的一個(gè)頭文件。所以在shell中執(zhí)行下列語(yǔ)句安裝freetype:

sudo apt-get install libfreetype6-dev

然后使用下列語(yǔ)句查找ft2build.h并在cmake中鏈接即可:

sudo find / -name ft2build.h
  • simhei.ttf是字體文件,可以在windows系統(tǒng)直接進(jìn)行索引,更換自己喜愛(ài)的默認(rèn)字體即可。

結(jié)果

源碼解析

由于下載需要積分,所以將源碼粘貼出來(lái)


cmake文件

cmake_minimum_required(VERSION 3.12) project(chinese)set(CMAKE_CXX_STANDARD 14)find_package(OpenCV REQUIRED)message(STATUS ${OpenCV_LIBRARY_DIRS})include_directories(/usr/include/freetype2/${OpenCV_INCLUDE_DIRS})#link_directories(${OpenCV_LIBRARY_DIRS} )add_executable(chinese main.cpp CvxText.cpp CvxText.h)target_link_libraries(chinese ${OpenCV_LIBS} freetype)

CvxText.h

#ifndef OPENCV_CVX_TEXT_HPP_ #define OPENCV_CVX_TEXT_HPP_// source from: http://www.opencv.org.cn/forum.php?mod=viewthread&tid=2083&extra=&page=1 // 支持OpenCV中文漢字輸入#include <ft2build.h> #include FT_FREETYPE_H#include <opencv2/opencv.hpp>class CvxText { public:/*** 裝載字庫(kù)文件*/CvxText(const char* freeType);virtual ~CvxText();/*** 獲取字體.目前有些參數(shù)尚不支持.** \param font 字體類型, 目前不支持* \param size 字體大小/空白比例/間隔比例/旋轉(zhuǎn)角度* \param underline 下畫線* \param diaphaneity 透明度** \sa setFont, restoreFont*/void getFont(int* type, cv::Scalar* size=nullptr, bool* underline=nullptr, float* diaphaneity=nullptr);/*** 設(shè)置字體.目前有些參數(shù)尚不支持.** \param font 字體類型, 目前不支持* \param size 字體大小/空白比例/間隔比例/旋轉(zhuǎn)角度* \param underline 下畫線* \param diaphaneity 透明度** \sa getFont, restoreFont*/void setFont(int* type, cv::Scalar* size=nullptr, bool* underline=nullptr, float* diaphaneity=nullptr);/*** 恢復(fù)原始的字體設(shè)置.** \sa getFont, setFont*/void restoreFont();/*** 輸出漢字(顏色默認(rèn)為黑色).遇到不能輸出的字符將停止.** \param img 輸出的影象* \param text 文本內(nèi)容* \param pos 文本位置** \return 返回成功輸出的字符長(zhǎng)度,失敗返回-1.*/int putText(cv::Mat& img, char* text, cv::Point pos);/*** 輸出漢字(顏色默認(rèn)為黑色).遇到不能輸出的字符將停止.** \param img 輸出的影象* \param text 文本內(nèi)容* \param pos 文本位置** \return 返回成功輸出的字符長(zhǎng)度,失敗返回-1.*/int putText(cv::Mat& img, const wchar_t* text, cv::Point pos);/*** 輸出漢字.遇到不能輸出的字符將停止.** \param img 輸出的影象* \param text 文本內(nèi)容* \param pos 文本位置* \param color 文本顏色** \return 返回成功輸出的字符長(zhǎng)度,失敗返回-1.*/int putText(cv::Mat& img, const char* text, cv::Point pos, cv::Scalar color);/*** 輸出漢字.遇到不能輸出的字符將停止.** \param img 輸出的影象* \param text 文本內(nèi)容* \param pos 文本位置* \param color 文本顏色** \return 返回成功輸出的字符長(zhǎng)度,失敗返回-1.*/int putText(cv::Mat& img, const wchar_t* text, cv::Point pos, cv::Scalar color);private:// 禁止copyCvxText& operator=(const CvxText&);// 輸出當(dāng)前字符, 更新m_pos位置void putWChar(cv::Mat& img, wchar_t wc, cv::Point& pos, cv::Scalar color);FT_Library m_library; // 字庫(kù)FT_Face m_face; // 字體// 默認(rèn)的字體輸出參數(shù)int m_fontType;cv::Scalar m_fontSize;bool m_fontUnderline;float m_fontDiaphaneity; };#endif // OPENCV_CVX_TEXT_HPP_

CvxText.cpp

#include <wchar.h> #include <assert.h> #include <locale.h> #include <ctype.h> #include <cmath>#include "CvxText.h"// 打開(kāi)字庫(kù) CvxText::CvxText(const char* freeType) {assert(freeType != NULL);// 打開(kāi)字庫(kù)文件, 創(chuàng)建一個(gè)字體if(FT_Init_FreeType(&m_library)) throw;if(FT_New_Face(m_library, freeType, 0, &m_face)) throw;// 設(shè)置字體輸出參數(shù)restoreFont();// 設(shè)置C語(yǔ)言的字符集環(huán)境setlocale(LC_ALL, ""); }// 釋放FreeType資源 CvxText::~CvxText() {FT_Done_Face(m_face);FT_Done_FreeType(m_library); }// 設(shè)置字體參數(shù): // // font - 字體類型, 目前不支持 // size - 字體大小/空白比例/間隔比例/旋轉(zhuǎn)角度 // underline - 下畫線 // diaphaneity - 透明度 void CvxText::getFont(int* type, cv::Scalar* size, bool* underline, float* diaphaneity) {if (type) *type = m_fontType;if (size) *size = m_fontSize;if (underline) *underline = m_fontUnderline;if (diaphaneity) *diaphaneity = m_fontDiaphaneity; }void CvxText::setFont(int* type, cv::Scalar* size, bool* underline, float* diaphaneity) {// 參數(shù)合法性檢查if (type) {if(type >= 0) m_fontType = *type;}if (size) {m_fontSize.val[0] = std::fabs(size->val[0]);m_fontSize.val[1] = std::fabs(size->val[1]);m_fontSize.val[2] = std::fabs(size->val[2]);m_fontSize.val[3] = std::fabs(size->val[3]);}if (underline) {m_fontUnderline = *underline;}if (diaphaneity) {m_fontDiaphaneity = *diaphaneity;}FT_Set_Pixel_Sizes(m_face, (int)m_fontSize.val[0], 0); }// 恢復(fù)原始的字體設(shè)置 void CvxText::restoreFont() {m_fontType = 0; // 字體類型(不支持)m_fontSize.val[0] = 20; // 字體大小m_fontSize.val[1] = 0.5; // 空白字符大小比例m_fontSize.val[2] = 0.1; // 間隔大小比例m_fontSize.val[3] = 0; // 旋轉(zhuǎn)角度(不支持)m_fontUnderline = false; // 下畫線(不支持)m_fontDiaphaneity = 1.0; // 色彩比例(可產(chǎn)生透明效果)// 設(shè)置字符大小FT_Set_Pixel_Sizes(m_face, (int)m_fontSize.val[0], 0); }// 輸出函數(shù)(顏色默認(rèn)為白色) int CvxText::putText(cv::Mat& img, char* text, cv::Point pos) {return putText(img, text, pos, CV_RGB(255, 255, 255)); }int CvxText::putText(cv::Mat& img, const wchar_t* text, cv::Point pos) {return putText(img, text, pos, CV_RGB(255,255,255)); }int CvxText::putText(cv::Mat& img, const char* text, cv::Point pos, cv::Scalar color) {if (img.data == nullptr) return -1;if (text == nullptr) return -1;int i;for (i = 0; text[i] != '\0'; ++i) {wchar_t wc = text[i];// 解析雙字節(jié)符號(hào)if(!isascii(wc)) mbtowc(&wc, &text[i++], 2);// 輸出當(dāng)前的字符putWChar(img, wc, pos, color);}return i; }int CvxText::putText(cv::Mat& img, const wchar_t* text, cv::Point pos, cv::Scalar color) {if (img.data == nullptr) return -1;if (text == nullptr) return -1;int i;for(i = 0; text[i] != '\0'; ++i) {// 輸出當(dāng)前的字符putWChar(img, text[i], pos, color);}return i; }// 輸出當(dāng)前字符, 更新m_pos位置 void CvxText::putWChar(cv::Mat& img, wchar_t wc, cv::Point& pos, cv::Scalar color) {// 根據(jù)unicode生成字體的二值位圖FT_UInt glyph_index = FT_Get_Char_Index(m_face, wc);FT_Load_Glyph(m_face, glyph_index, FT_LOAD_DEFAULT);FT_Render_Glyph(m_face->glyph, FT_RENDER_MODE_MONO);FT_GlyphSlot slot = m_face->glyph;// 行列數(shù)int rows = slot->bitmap.rows;int cols = slot->bitmap.width;for (int i = 0; i < rows; ++i) {for(int j = 0; j < cols; ++j) {int off = i * slot->bitmap.pitch + j/8;if (slot->bitmap.buffer[off] & (0xC0 >> (j%8))) {int r = pos.y - (rows-1-i);int c = pos.x + j;if(r >= 0 && r < img.rows && c >= 0 && c < img.cols) {cv::Vec3b pixel = img.at<cv::Vec3b>(cv::Point(c, r));cv::Scalar scalar = cv::Scalar(pixel.val[0], pixel.val[1], pixel.val[2]);// 進(jìn)行色彩融合float p = m_fontDiaphaneity;for (int k = 0; k < 4; ++k) {scalar.val[k] = scalar.val[k]*(1-p) + color.val[k]*p;}img.at<cv::Vec3b>(cv::Point(c, r))[0] = (unsigned char)(scalar.val[0]);img.at<cv::Vec3b>(cv::Point(c, r))[1] = (unsigned char)(scalar.val[1]);img.at<cv::Vec3b>(cv::Point(c, r))[2] = (unsigned char)(scalar.val[2]);}}}}// 修改下一個(gè)字的輸出位置double space = m_fontSize.val[0]*m_fontSize.val[1];double sep = m_fontSize.val[0]*m_fontSize.val[2];pos.x += (int)((cols? cols: space) + sep); }

main.cpp

#include <iostream> #include <fstream> #include <stdlib.h> //#include <io.h> #include <string> #include <vector> #include "opencv2/opencv.hpp" #include "time.h" #include "CvxText.h"static int ToWchar(char* &src, wchar_t* &dest, const char *locale = "zh_CN.utf8") {if (src == NULL) {dest = NULL;return 0;}// 根據(jù)環(huán)境變量設(shè)置localesetlocale(LC_CTYPE, locale);// 得到轉(zhuǎn)化為需要的寬字符大小int w_size = mbstowcs(NULL, src, 0) + 1;// w_size = 0 說(shuō)明mbstowcs返回值為-1。即在運(yùn)行過(guò)程中遇到了非法字符(很有可能使locale// 沒(méi)有設(shè)置正確)if (w_size == 0) {dest = NULL;return -1;}//wcout << "w_size" << w_size << endl;dest = new wchar_t[w_size];if (!dest) {return -1;}int ret = mbstowcs(dest, src, strlen(src)+1);if (ret <= 0) {return -1;}return 0; }int main() {cv::Mat img = cv::imread("./demo.jpg");if (!img.data || img.channels() != 3) {fprintf(stderr, "read image fail\n");return -1;}CvxText text("./simhei.ttf"); //指定字體cv::Scalar size1{ 80, 0.5, 0.1, 0 }; // (字體大小, 無(wú)效的, 字符間距, 無(wú)效的 }text.setFont(nullptr, &size1, nullptr, 0);char* str = (char *)"你好,李家網(wǎng)";wchar_t *w_str;ToWchar(str,w_str);text.putText(img, w_str, cv::Point(50,100), cv::Scalar(0, 0, 255));cv::resize(img, img, cv::Size(300,300));cv::imshow("demo", img);cv::waitKey(0);cv::imwrite("./res.jpg", img);return 0; }

總結(jié)

以上是生活随笔為你收集整理的Ubuntu下使用OpenCV显示中文的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。