opencv - opencv 可以在 cv::Mat 中绘制浮点坐标矩形吗?
问题描述
我想使用 cv::rectangle() 在 cv::Mat 中绘制一个矩形,但我可以绘制一个四点坐标值与浮点精度的矩形吗?(就像 Qt 在 QPainter 中所做的那样)。
解决方案
试用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;
}
推荐阅读
- .net-core - 如何将列表和字符串作为输入参数传递给 HttpDelete
- sql - 如何在 Tarantool 中更改 sql 表的格式
- java - Java:如何同时放置 Paintcomponet 和其他组件?
- java - 在 Java 流中的终端操作之后使用中间操作
- c - char * 的范围与 C 中的 char 数组比较
- javascript - 如何通过打印推断的变量类型来调试 TypeScript?
- javascript - 如何将 JSP 文件中导入的 Java 常量访问到 Javascript 函数中
- python - 通过多个 FK 属性进行查询集过滤
- sql-server - 是否可以升级 GCP Cloud Sql Server Express 实例的版本?
- javascript - 是否可以编写一个函数来使用 Angular 9 过滤嵌套数组?