python - openCV 的 Houghline 方法只给出 1 行作为输出
问题描述
我正在使用 houghLine 方法进行线检测。但它只给出 1 行作为输出,我认为这是获得最多票数的行。我尝试了In opencv using houghlines 的解决方案,只打印一行,但这需要很多时间并且无法正常工作。
我的代码是:
folderInPath = "DevanagariHandwrittenCharacterDataset/Test"
folderOutPath = "DevanagariHandwrittenCharacterDataset/Test_Lines"
for folderPath in os.listdir(folderInPath):
inPath = "DevanagariHandwrittenCharacterDataset/Test/" + folderPath
#os.mkdir(os.path.join(folderOutPath, folderPath+'_lines'))
outPath = os.path.join(folderOutPath, folderPath+'_lines')
dirs = "DevanagariHandwrittenCharacterDataset/Test/" + folderPath
for imagePath in os.listdir(dirs):
# imagePath contains name of the image for eg. 46214.png
inputPath = os.path.join(inPath, imagePath)
# inputPath contains the full directory name for eg. character_1_ka/46214.png|
# Reading the required image in which operations are to be done.
# Make sure that the image is in the same directory in which this python program is
img = cv2.imread(inputPath)
# Convert the img to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# Apply edge detection method on the image
edges = cv2.Canny(gray,50,150,apertureSize = 3)
# This returns an array of r and theta values
lines = cv2.HoughLines(edges,1,np.pi/180, 5)
for line in lines:
# The below for loop runs till r and theta values are in the range of the 2d array
for r,theta in line:
# Stores the value of cos(theta) in a
a = np.cos(theta)
# Stores the value of sin(theta) in b
b = np.sin(theta)
# x0 stores the value rcos(theta)
x0 = a*r
# y0 stores the value rsin(theta)
y0 = b*r
# x1 stores the rounded off value of (rcos(theta)-1000sin(theta))
x1 = int(x0 + 1000*(-b))
# y1 stores the rounded off value of (rsin(theta)+1000cos(theta))
y1 = int(y0 + 1000*(a))
# x2 stores the rounded off value of (rcos(theta)+1000sin(theta))
x2 = int(x0 - 1000*(-b))
# y2 stores the rounded off value of (rsin(theta)-1000cos(theta))
y2 = int(y0 - 1000*(a))
# cv2.line draws a line in img from the point(x1,y1) to (x2,y2). (0,0,255) denotes the colour of the line to be
#drawn. In this case, it is red.
cv2.line(img,(x1,y1), (x2,y2), (0,0,255),2)
# fullOutPath contains the path of the output
fullOutPath = os.path.join(outPath, 'lines_'+imagePath)
# All the changes made in the input image are finally written on a new image houghlines.jpg
cv2.imwrite(fullOutPath, img)
print("Done " + folderPath)
有关信息,我的图像输入是 32 x 32 像素的印地语字符。对此有任何建议或解决方案的人。
我在我的数据集中附加了一张图像。有几个这样的图像。图片
解决方案
推荐阅读
- javascript - 在 ng-repeat 内以编程方式更新 ng-model 时触发 ng-change
- html - 如何在 bootstrap 4 中使我的桌子高度更小?
- java - 怎么去掉这个字符从我在java中的字符串?
- batch-file - 如何在ffmpeg的批处理文件中编写注释?
- matlab - 在命令行中运行 MATLAB,允许显示绘图
- keras - 编写我自己的 FGSM 对抗性攻击总是零扰动
- python - 如何只运行 Celery schedule 多次,直到再次调用任务?
- docker - 码头工人:来自守护程序的错误响应:安装被拒绝:没有设置上下文
- javascript - 如何更改页面刷新消息弹出窗口上的文字?
- java - 共享首选项中的 MODE_APPEND 标志