openCV 的 Houghline 方法只给出 1 行作为输出

如何解决openCV 的 Houghline 方法只给出 1 行作为输出

我使用 houghLine 方法进行线检测。但它只给出 1 行作为输出,我认为这是投票最多的那一行。我尝试了来自 In opencv using houghlines prints only one line解决方案,但这需要很多时间并且不起作用。

我的代码是:

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,255) denotes the colour of the line to be  
                #drawn. In this case,it is red. 
                cv2.line(img,(x1,y1),(x2,y2),(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 像素的印地语字符。有人对此有任何建议或解决方案。

我在我的数据集中附加一张图像。有好几张这样的图片Image

解决方法

您的代码通常是正确的,只是您没有为线检测 lines = cv2.HoughLines(edges,1,np.pi/180,5) 选择正确的参数。

将此指令替换为:lines = cv2.HoughLines(edges,np.pi/90,18)

会给你这个结果:

Result

注意:如果你想检测更多或更少的行,你必须相应地改变参数。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 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元字符(。)和普通点?