在OpenCV中使用CV_AA标志绘制线不会产生抗锯齿的线 [英] Drawing a line in OpenCV with CV_AA flags is not producing an anti-aliased line
问题描述
我无法获得cv :: line来绘制设置了CV_AA标志的抗锯齿线.这是示例代码来说明:
I am unable to get cv::line to draw an anti-aliased line with CV_AA flags set. Here is example code to illustrate:
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
int main(int argc, char* argv[])
{
cv::Mat base(100, 100, CV_32F);
cv::Point2i p1(20, 20);
cv::Point2i p2(70, 90);
cv::line(base, p1, p2, cv::Scalar(1.0), 1, CV_AA); // 1 pixel thick, CV_AA == Anti-aliased flag
cv::namedWindow("line test", CV_NORMAL);
cv::imshow("line test", base);
cv::waitKey(0);
return 0;
}
我尝试使用cv :: Point2d代替cv :: Point2i,但没有发现任何区别. (i ==整数,d ==两倍)我也尝试过使像素宽度大于1,但仍然没有AA.
I have tried using cv::Point2d instead of cv::Point2i and found no difference. (i == integer, d == double) I have also tried making the pixel width larger than 1, but there is still no AA.
但是,这确实适用于CV_8U(8位无符号)映像,而不是我在这里使用的CV_32F(32位浮点)映像. (对于CV_8U,必须将cv :: Scalar(255)而不是cv :: Scalar(1.0)传递到行函数中进行比较)
However, this does work with a CV_8U (8-bit unsigned) image, as opposed to CV_32F (32-bit float) image that I have here. (For CV_8U, you must pass cv::Scalar(255) instead of cv::Scalar(1.0) into the line function to compare)
我认为当前的解决方法是从CV_8U图像开始然后进行转换,但是有没有一种简单的方法可以仅通过CV_32F图像来实现该功能,而无需我编写自己的抗锯齿功能?
I suppose a current work-around would be to start with a CV_8U image then convert, but is there a straightforward way to do it with just an CV_32F image that doesn't require me to write my own anti-aliasing function?
我想念什么吗?
OpenCV文档: cv :: line reference
OpenCV docs: cv::line reference
推荐答案
./core/src/drawing.cpp:1569
void line( Mat& img, Point pt1, Point pt2, const Scalar& color,
int thickness, int line_type, int shift )
{
if( line_type == CV_AA && img.depth() != CV_8U )
line_type = 8;
//omitted
}
源代码指出,只要深度不是CV_8U,它将使用8位连接法绘制.
The source code states that as long as the depth is not CV_8U, it will draw using 8-connected method.
这篇关于在OpenCV中使用CV_AA标志绘制线不会产生抗锯齿的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!