使用OpenCV从边缘图像中删除长水平/垂直线 [英] Removing long horizontal/vertical lines from edge image using OpenCV

查看:486
本文介绍了使用OpenCV从边缘图像中删除长水平/垂直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用标准图像处理滤镜(来自OpenCV)从图像中删除长水平线和垂直线?

How can I use standard image processing filters (from OpenCV) to remove long horizontal and vertical lines from an image?

图像为B& W因此删除方式简单地画黑色。

The images are B&W so removing means simply painting black.

插图:

我目前正在使用Python进行迭代,迭代像素行和列并检测范围连续像素,删除长于N个像素的像素。但是,与OpenCV库相比,它确实很慢,如果有一种方法可以实现OpenCV功能,那么可能会快几个数量级。

I'm currently doing it in Python, iterating over pixel rows and cols and detecting ranges of consecutive pixels, removing those that are longer than N pixels. However, it's really slow in comparison to the OpenCV library, and if there's a way of accomplishing the same with OpenCV functions, that'll likely be orders of magnitude faster.

我想这可以通过使用内核的卷积来完成,该内核是一行像素(对于水平线),但是我很难确定能够完成这一操作的确切操作。

I imagine this can be done by convolution using a kernel that's a row of pixels (for horizontal lines), but I'm having a hard time figuring the exact operation that would do the trick.

推荐答案

如果你的线条是真正水平/垂直的,试试这个

if your lines are truly horizontal/vertical, try this

import cv2
import numpy as np
img = cv2.imread('c:/data/test.png')
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
linek = np.zeros((11,11),dtype=np.uint8)
linek[5,...]=1
x=cv2.morphologyEx(gray, cv2.MORPH_OPEN, linek ,iterations=1)
gray-=x
cv2.imshow('gray',gray)
cv2.waitKey(0)    

结果

这篇关于使用OpenCV从边缘图像中删除长水平/垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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