简体   繁体   English

Python OpenCV绘图线宽度

[英]Python OpenCV Drawing Line Width

When specifying a line thickness greater than 1, the resulting line width drawn by cv2.line() is wider than specified. 当指定的线宽大于1时,cv2.line()绘制的结果线宽比指定的宽。 Specifying a thickness of 1,2,3,4,5,6 produces a line width of 1,3,5,5,7,7 respectively. 指定1,2,3,4,5,6的粗细会分别产生1,3,5,5,7,7的线宽。 I have tried using different lineType values (4,8,16) and sub-pixel point locations with the shift parameter with no effect on the line width. 我尝试使用具有shift参数的不同lineType值(4,8,16)和子像素点位置,但对线宽没有影响。 Am I doing something wrong? 难道我做错了什么?

For example: 例如:

import numpy as np
import cv2

a = np.zeros((10,10), dtype=np.uint8)
cv2.line(a, (0,4), (9,4), 1, 2)
print(a)

produces: 生产:

 [[0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]
 [1 1 1 1 1 1 1 1 1 1]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]]

Your question is a simple one, and the answer is a simple one, as well: No, you are not doing anything wrong. 您的问题很简单,答案也很简单:不,您没有做错任何事情。 The implementation of the line drawing in OpenCV seems to be quite basic. OpenCV中线条绘制的实现似乎是非常基本的。

I tried this with several line widths and also with fractional starting and ending positions. 我尝试了几种线宽,也使用起始和结束位置的分数。 The line width parameter does not really work. 线宽参数实际上不起作用。 The simple Brezenham lines (single-pixel lines from integer pixel positions) are fine, but the thicker lines are not good. 简单的Brezenham线(整数像素位置的单个像素线)很好,但较粗的线则不好。

See this test code: 请参阅以下测试代码:

import numpy as np
import cv2

a = np.zeros((200,200),dtype=np.uint8)
for u in np.exp(np.linspace(0,1j*2*np.pi,25)):
    cv2.line(a, (100, 100), (int(100+100*u.real+.5), int(100+100*u.imag+.5)), 255, 2)
cv2.imwrite('lines.png', a)

What you get is this: 您得到的是:

在此处输入图片说明

The line thickness depends heavily on the angle; 线的粗细主要取决于角度。 diagonal lines are much thicker than horizontal or vertical lines. 对角线比水平或垂直线粗得多。 Also, the thickness of the lines is anyway wrong (3 at thinnest) as you noticed. 另外,您发现线条的粗细还是错误的(最薄处为3)。 Using anti-alias does not make a significant change. 使用抗锯齿不会带来重大变化。

However, if this is done with fractional coordinates, the thickness variation is smaller (but the thickness is still wrong): 但是,如果使用分数坐标完成此操作,则厚度变化较小(但是厚度仍然错误):

import numpy as np
import cv2

a = np.zeros((200,200),dtype=np.uint8)
for u in np.exp(np.linspace(0,1j*2*np.pi,25)):
    cv2.line(a, (1600, 1600), (int(1600+1600*u.real+.5), int(1600+1600*u.imag+.5)), 255, 2, shift=4)
cv2.imwrite('lines_shift.png', a)

在此处输入图片说明

Also, the problem seems to vanish with thick lines (thickness set to 5, real thickness 7, no fractional coordinates). 此外,该问题似乎随着粗线而消失(厚度设置为5,实际厚度为7,无小数坐标)。 This suggest that the rotation-dependent error is additive: 这表明与旋转有关的误差是相加的:

在此处输入图片说明

Then let's try to draw overlapping files with widths 10, 11, and 12 with different shades of gray: 然后,让我们尝试以不同的灰色阴影绘制宽度分别为10、11和12的重叠文件:

import numpy as np
import cv2

a = np.zeros((200,200),dtype=np.uint8)
for u in np.exp(np.linspace(0,1j*2*np.pi,25)):
    cv2.line(a, (100, 100), (int(100+100*u.real+.5), int(100+100*u.imag+.5)), 255, 12)
    cv2.line(a, (100, 100), (int(100+100*u.real+.5), int(100+100*u.imag+.5)), 128, 11)
    cv2.line(a, (100, 100), (int(100+100*u.real+.5), int(100+100*u.imag+.5)), 64, 10)
cv2.imwrite('lines_variation.png', a)

在此处输入图片说明

In principle there should be three colours seen, but in practice only two are visible. 原则上应该看到三种颜色,但实际上只有两种可见。 This means that the lines with widths 11 and 12 are drawn with the same width (13). 这意味着宽度为11和12的线以相同的宽度(13)绘制。 Any tricks with the fractional coordinates produce the same result. 任何带有分数坐标的技巧都会产生相同的结果。

Summary: Only odd line widths are available. 摘要:仅奇数行宽可用。 Narrow lines have large relative width errors if fractional (shifted) coordinates are not used. 如果不使用分数(偏移)坐标,则窄线具有较大的相对宽度误差。 If you need to have more control, draw thicker line at a higher resolution and then downsample. 如果需要更多控制,请以较高的分辨率绘制粗线,然后下采样。 (Clumsy, I know.) (笨拙,我知道。)

The tests above have been carried ouy with OpenCV version 2.4.9. 以上测试已使用OpenCV 2.4.9版进行了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM