删除分段的行(OpenCV,Python) [英] Delete segmented lines (OpenCV, Python)

查看:223
本文介绍了删除分段的行(OpenCV,Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下代码:

import numpy as np
import cv2

gray = cv2.imread('image.png')
edges = cv2.Canny(gray,50,150,apertureSize = 3)
cv2.imwrite('edges-50-150.jpg',edges)
minLineLength=100
lines = cv2.HoughLinesP(image=edges,rho=1,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=80)

a,b,c = lines.shape
for i in range(a):
    cv2.line(gray, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 1, cv2.LINE_AA)
    cv2.imwrite('houghlines.jpg', gray)
    cv2.imshow('img', gray)
    cv2.waitKey(0)

我可以实现此(源)图像中的水平线:

I can achieve the horizontal lines that there are in this (source) image:

这是结果:

如何删除红色的行?我要实现的是删除这些行,以使图像更干净,并可供其他进程使用. 该代码取自

How can I delete the lines which are in red ? What I want to achieve is to delete these lines so the image is more clean and ready to use by another process. The code has been taken from here.

推荐答案

选中此链接

Check this link http://docs.opencv.org/3.2.0/d1/dee/tutorial_moprh_lines_detection.html and detect horizontal lines.Then, Set pixel values to zero containing horizontal lines. Given code is in C++ but you can easily convert it into Python.

您还可以使用线的斜率拒绝检测到的线中的水平线.排除所有斜率几乎等于零的线.

You can also reject horizontal lines from your detected lines using slope of lines. Exclude all the lines having slope almost equal to zero.

以下是您可以使用的代码段(基于斜率和形态学闭合,我对您的代码进行了很少的更改):

Following is the code snippet you can use (I made little changes in your code based on slope and morphological closing):

import numpy as np
import cv2

gray = cv2.imread('image.png')
edges = cv2.Canny(gray,50,150,apertureSize = 3)
cv2.imwrite('edges-50-150.jpg',edges)
minLineLength=100
lines = cv2.HoughLinesP(image=edges,rho=1,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=80)

a,b,c = lines.shape
for i in range(a):
    x = lines[i][0][0] - lines [i][0][2]
    y = lines[i][0][1] - lines [i][0][3]
    if x!= 0:
        if abs(y/x) <1:
            cv2.line(gray, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (255, 255, 255), 1, cv2.LINE_AA)

se = cv2.getStructuringElement(cv2.MORPH_ELLIPSE , (3,3))
gray = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, se)
cv2.imwrite('houghlines.jpg', gray)
cv2.imshow('img', gray)
cv2.waitKey(0)

输入:

输出:

这篇关于删除分段的行(OpenCV,Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆