试用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;
}