为什么我的 cv2.line 显示为矩形而不是直线?

Why does my cv2.line shows as rectangular shape instead of a line?

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = np.zeros([10,10,3],dtype=np.uint8)

t2 = (2, 0) 
t3 = (0, 0)
cv2.line(img,t2,t3,(0,0,255),2,8)
plt.imshow(img)
plt.show()

输出为

我玩弄粗细和线型。无论如何它都不提供一行!

编辑: 在对评论提出建议后,我尝试了不在边缘的不同值,(4,5) 和 (2,5)。

厚度=2

cv2.line(img,t2,t3,(0,0,255),2,8)

厚度 = 0

cv2.line(img,t2,t3,(0,0,255),0,8)

您的代码 img = np.zeros([10,10,3],dtype=np.uint8) 告诉我您的图片大小为 10x10 像素。这样你就不会像你在代码示例中请求的那样得到一条像样的线来显示。您被完全放大了大约 6400 倍或更多。 t3 位于 t2 的左侧(这不符合逻辑,除非……应用阿拉伯语阅读方法(从右到左))所以……让我们缩小至少 2-100 倍,然后将 t2 放在 t3 的左侧,看看会发生什么!

方法:

放大你的图像..假设你的阵列达到 100x100 像素......然后你建议的 cv2.line(img,t2,t3,(0,0,255),2,8) 行也被缩小并成比例......并且 t2 & t3 变成 t2 = (20, 10) & t3 = (40, 10) 换句话说......它变成了我们通常期望的看起来像矩形或直线的东西查看。如果您再次缩小并且您的 np 阵列变为 1000x1000...如果需要调整 t2 和 t3 坐标,然后让我们知道您的问题是否仍然存在或已解决。

根据您的示例,我对其进行了修改以表明我的意思:

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = np.zeros([100,100,3],dtype=np.uint8)  # imagesize

t2 = (20, 10)                               # left coordinate of line.
t3 = (40, 10)                               # right coordinate of line
cv2.line(img,t2,t3,(0,0,255),2,8)
plt.imshow(img)
plt.show()

做你自己:放松并阅读我的个人笔记......最重要的是......玩弄代码,看看 diagonal line 中的变化变成你喜欢看到的东西。享受吧!

个人说明:

Tahlil: breath in.. and out... my friend... nobody is toying with you! We don't do homework but instead help you to improve you your skills, method of questioning, giving insight in the question you ask and help finding solutions. In return we expect from you that you contribute in helping others as well. Toying gets flagged and removed (almost) by default!

As you stated by sgarizvi to answer in answers... I served you an answer and insight how to improve.. can I now get my cookie ... please. Enjoy life and your stay at SO ;-)