0

I am tring to use cv::rectangle() to draw a rect in cv::Mat, but could I draw a rect whose four points coordinate value with a float percision ? (like what Qt do in QPainter).

  • Somewhat, but it's maybe not in the way you would expect. If you read the docs for the drawing functions (https://docs.opencv.org/master/d6/d6e/group__imgproc__draw.html) you'll see in the "detailed description" section talk about the "shift" parameter, which you can use with `cv::rectangle` – alkasm Nov 02 '20 at 03:32
  • Here's a related answer, using the "shift" parameter for drawing a circle: https://stackoverflow.com/a/44892317/5087436 – alkasm Nov 02 '20 at 03:34
  • @alkasm Unluckily it isn't what I want..What I really want is to do precise anti-analiasing... – Martin Chan Nov 02 '20 at 08:58
  • 1
    how do you get aliasing with a rectangle? – Eric Nov 02 '20 at 09:18
  • Yeah I'm not sure I understand. You can draw antialiased lines with OpenCV as well, or rotated rects, etc all with fixed point decimal values. – alkasm Nov 03 '20 at 03:49

1 Answers1

0

Try Blend2d library it fast and integates easyly with OpenCV:

Simple example:

#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;
}
Andrey Smorodov
  • 10,649
  • 2
  • 35
  • 42