2012-08-11 27 views

Trả lời

3

Có thể sử dụng phông chữ khác nhưng bạn cần phải liên kết các thư viện Qt để OpenCV và sử dụng chức năng cvAddText với một cvFontQt

http://docs.opencv.org/modules/highgui/doc/qt_new_functions.html#addtext

http://docs.opencv.org/modules/highgui/doc/qt_new_functions.html#fontqt

Có nhiều giải pháp khác mà bạn có thể thử, với nhiều hoặc ít hiệu suất tương tự như OpenCV. Ví dụ: bạn có thể sử dụng CAIRO để viết phông chữ vào hình ảnh.

9

Nếu bạn không thể hoặc không muốn sử dụng các ràng buộc Qt, đây là một cách để làm điều đó với 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); 
} 

Ví dụ gọi:

putTextCairo(mat, "Hello World", cv::Point2d(50,50), "arial", 15, Scalar(0,0,255), false, false); 

Nó giả định rằng mục tiêu hình ảnh là BGR.

Nó đặt trung tâm của văn bản vào điểm đã cho. Nếu bạn muốn một số vị trí khác nhau, bạn phải sửa đổi cuộc gọi cairo_move_to.

+0

Đây là câu trả lời hay! –

+0

Cảm ơn mẹo. Nhưng 'centerAnchor' có đề cập đến 'centerPoint' không? Và làm thế nào chúng ta có thể cài đặt Cairo trong Ubuntu 16.04? – user1098761

+0

@ user1098761 'sudo apt install libcairo2-dev'. Nếu bạn đang sử dụng CMake, bạn có thể làm theo câu trả lời này https://stackoverflow.com/questions/41636886/cairo-library-and-cmake. Tôi đã sử dụng FindCairo.cmake từ WebKit. –

Các vấn đề liên quan