如何使用 HoughLines 找到这个白色斑点的 4 行?

如何解决如何使用 HoughLines 找到这个白色斑点的 4 行?

我正在尝试使用 houghlines 方法在白色矩形的每一边绘制 4 条线。
这是我正在处理的图像 Source image(2592 x 1246 图像)。

import cv2 as cv
import numpy as np
img = cv.imread("picture_compress.bmp")
img_gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
edges = cv.Canny(img_gray,100,200,5)
lines = cv.houghlines(edges,rho=2,theta=np.pi / 180,threshold=200)  # lines will be in polar coordinate system
colors = [(0,255),(0,255,0),(255,255)]

for i,line in enumerate(lines):
    rho,theta = line[0]    # rho --> distance from the coordinate origin (0,0) (Top-Left corner of the image)
                            # theta --> line rotation angle in radius
    # getting the cos(theta) and sin(theta) value
    a = np.cos(theta)
    b = np.sin(theta)
    # getting the origin value(top-left corner of the image)
    x0 = a * rho
    y0 = b * rho
    # 2 points
    pt1 = (int(x0 + 2592 * (-b)),int(y0 + 2592 * a))
    pt2 = (int(x0 - 2592 * (-b)),int(y0 - 2592 * a))
    # draw lines on the input image
    cv.line(img,pt1,pt2,colors[i % len(colors)],thickness=3,lineType=cv.LINE_AA)
print("Total number of lines:",str(len(lines)))
cv.namedWindow("Hough",cv.WINDOW_norMAL)
cv.imshow("Hough",img)
cv.waitKey(0)

我得到的结果是 Result

从最后一张图片中您可以看到在白色斑点的边缘绘制了多条线。

任务

  1. 在矩形的每一边只画 4 条线的最佳方法是什么?
  2. 如何仅使用 houghlines 而不是 houghlinesp 来获得 4 条有限线?

-------------------------------------------- ------ 已编辑 (17/03/2021) ----------------------------- ---------------------

让我发布我正在处理的 Original image

Fase 1:Eroded_Dilated_Image - 应用侵蚀膨胀并填充斑点后的结果。代码如下:

# This function is used for cleaning the image
def erode_dilate(img,num_iter):                    
    kernel = np.ones((3,3),np.uint8)
    erosion = cv.erode(src=img,kernel=kernel,iterations=num_iter)
    dilation = cv.dilate(src=erosion,iterations=num_iter) 
    return dilation

# This function will search for the blob with max area and will fill it
def find_draw_big_blob(img): 
    contours,_ = cv.findContours(image=img,mode=cv.RETR_TREE,method=cv.CHAIN_APPROX_SIMPLE)
    contour_area = [cv.contourArea(cnt) for cnt in contours]      # List of contours' area
    id_max_area = np.argmax(contour_area)     
    label_max_image = np.zeros(img.shape)
    draw_max_contour = cv.drawContours(image=label_max_image,contours=contours,contourIdx=id_max_area,color=255,thickness=-1)  

img = cv.imread("25559.bmp")
gray_image = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
ret1,thresh = cv.threshold(gray_image,thresh=127,maxval=255,type=cv.THRESH_OTSU)
image_erode_dilate = erode_dilate(img=thresh,num_iter=2)
big_blob = find_draw_big_blob(img=image_erode_dilate)
cv.imshow("Image",big_blob)

Fase 2:houghlines_Image - 应用霍夫变换后的最终结果。我用于此霍夫变换的代码已经在问题中。

但最终的结果并不是我想要的结果。
我想实现此 sample_image

显示的以下结果

解决方法

  • 在矩形的每一边只画 4 条线的最佳方法是什么?

在矩形的每一边只绘制 4 条线的最佳方法是使用 fast-line-detector

您可以在下面看到 fast-line-detector 的工作原理:

enter image description here

代码:


# Load the library
import cv2

# Load the image
img = cv2.imread("BWzd2.png")

# Convert to gray-scale
gry = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# Init. the fast-line-detector (fld)
fld = cv2.ximgproc.createFastLineDetector().detect(gry)

# Detect the lines
for line in fld:

    # Get current coordinates
    x1 = int(line[0][0])
    y1 = int(line[0][1])
    x2 = int(line[0][2])
    y2 = int(line[0][3])

    # Draw the line
    cv2.line(img,(x1,y1),(x2,y2),(255,255),5)

# Display
cv2.imshow("img",img)
cv2.waitKey(0)

结果:

enter image description here

,

在您的 Python/OpenCV 代码中,只需将阈值增加到 250 或将 theta 角加倍即可。

输入:

enter image description here

1) 提高门槛

lines = cv.HoughLines(edges,rho=2,theta=np.pi / 180,threshold=250)  # lines will be in polar coordinate system

import cv2 as cv
import numpy as np
img = cv.imread("picture_compress.png")
img_gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
edges = cv.Canny(img_gray,100,200,5)
lines = cv.HoughLines(edges,threshold=250)  # lines will be in polar coordinate system
colors = [(0,(0,255,0),255)]

result = img.copy()
for i,line in enumerate(lines):
    rho,theta = line[0]    # rho --> distance from the coordinate origin (0,0) (Top-Left corner of the image)
                            # theta --> line rotation angle in radius
    # getting the cos(theta) and sin(theta) value
    a = np.cos(theta)
    b = np.sin(theta)
    # getting the origin value(top-left corner of the image)
    x0 = a * rho
    y0 = b * rho
    # 2 points
    pt1 = (int(x0 + 2592 * (-b)),int(y0 + 2592 * a))
    pt2 = (int(x0 - 2592 * (-b)),int(y0 - 2592 * a))
    # draw lines on copy of the input image
    cv.line(result,pt1,pt2,colors[i % len(colors)],thickness=3,lineType=cv.LINE_AA)
print("Total number of lines:",str(len(lines)))
cv.namedWindow("Hough",cv.WINDOW_NORMAL)
cv.imshow("Hough",result)
cv.waitKey(0)

cv.imwrite("picture_compress_lines1.png",result)

enter image description here

2) 增加你的 theta,例如加倍:

lines = cv.HoughLines(edges,theta=2*np.pi / 180,threshold=200)  # lines will be in polar coordinate system

import cv2 as cv
import numpy as np
img = cv.imread("picture_compress.png")
img_gray = cv.cvtColor(img,threshold=200)  # lines will be in polar coordinate system
colors = [(0,result)
cv.waitKey(0)

cv.imwrite("picture_compress_lines2.png",result)

enter image description here

霍夫行的文本数据说:

Total number of lines: 5

但我只看到 4 行。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?