首页 > 解决方案 > opencv 可以在 cv::Mat 中绘制浮点坐标矩形吗?

问题描述

我想使用 cv::rectangle() 在 cv::Mat 中绘制一个矩形,但我可以绘制一个四点坐标值与浮点精度的矩形吗?(就像 Qt 在 QPainter 中所做的那样)。

标签: opencv

解决方案


试用Blend2d库,它可以快速与 OpenCV 轻松集成:

简单的例子:

#include <blend2d.h>
#include "opencv2/opencv.hpp"

int main(int argc, char* argv[])
{
    BLImage img(480, 480, BL_FORMAT_PRGB32);
    BLContext ctx(img);

    // Read an image from file.
    cv::Mat I = cv::imread("F:/ImagesForTest/lena.jpg");
    cv::cvtColor(I, I, cv::COLOR_RGB2RGBA);
    BLImage texture;
    //BLResult err = texture.readFromFile("texture.jpeg");
    texture.create(512, 512, BL_FORMAT_XRGB32);

    memcpy((uchar*)texture.impl->pixelData, (uchar*)I.data, 512 * 512 * 4);
    // Create a pattern and use it to fill a rounded-rect.
    BLPattern pattern(texture);
    ctx.setFillStyle(pattern);

    ctx.setCompOp(BL_COMP_OP_SRC_COPY);
    ctx.fillAll();

    // Coordinates can be specified now or changed later.
    BLGradient linear(BLLinearGradientValues(0, 0, 0, 480));

    
    // Color stops can be added in any order.
    linear.addStop(0.0, BLRgba32(0xFFFFFFFF));
    linear.addStop(0.5, BLRgba32(0xFF5FAFDF));
    linear.addStop(1.0, BLRgba32(0xFF2F5FDF));

    // `setFillStyle()` can be used for both colors and styles.
    ctx.setFillStyle(linear);
    


    
    ctx.setCompOp(BL_COMP_OP_MODULATE);
    ctx.fillRoundRect(40.0, 40.0, 400.0, 400.0, 45.5);

    ctx.setStrokeStyle(BLRgba32(0xFFFF0000));
    ctx.setStrokeWidth(3);
    ctx.strokeLine(0,0,480,480);
    ctx.end();

    //BLImageCodec codec;
    //codec.findByName("BMP");
    //img.writeToFile("bl-getting-started-2.bmp", codec);
    
    cv::Mat cvImg(img.height(), img.width(), CV_8UC4, img.impl->pixelData);
    cv::imshow("res", cvImg);
    cv::waitKey(0);

    return 0;
}

推荐阅读