Removing Horizontal Lines in image (OpenCV, Python, Matplotlib)(删除图像中的水平线(OpenCV、Python、Matplotlib))
问题描述
使用以下代码,我可以删除图像中的水平线.见下面的结果.
结果还不错,不是很完美,但是很好.我想要实现的是
我的一个问题是:如何在不应用灰色效果的情况下保存 Sobel X
?作为原始但经过处理..
另外,有没有更好的方法呢?
编辑
对源图像使用以下代码很好.效果很好.
但是如果我有这张图片呢?
我尝试执行上面的代码,结果真的很差……
我正在处理的其他图像是这些......
这是一种方法
将图像转换为
接下来我们创建一个特殊的水平内核来检测水平线.我们将这些线条绘制到蒙版上,然后在蒙版上找到轮廓.为了去除线条,我们用白色填充轮廓
检测到的线
面具
填充轮廓
图像当前有间隙.为了解决这个问题,我们构建了一个垂直内核来修复图像
<块引用>
注意根据映像,内核的大小会有所变化.例如,为了检测更长的行,我们可以使用
(50,1)
内核.如果我们想要更粗的线条,我们可以增加第二个参数为(50,2)
.这是其他图片的结果
检测到的线
原始(左),删除(右)
检测到的线
原始(左),删除(右)
完整代码
Using the following code I can remove horizontal lines in images. See result below.
The result is pretty good, not perfect but good. What I want to achieve is the one showed here. I am using this code.
Source image..
One of my questions is: how to save the
Sobel X
without that grey effect applied ? As original but processed..Also, is there a better way to do it ?
EDIT
Using the following code for the source image is good. Works pretty well.
But if I have this image ?
I tried to execute the code above and the result is really poor...
Other images which I am working on are these...
解决方案Here's an approach
Convert image to grayscale
Otsu's threshold to get binary image
Create special horizontal kernel and morph open to detect horizontal lines
Find contours on mask and "fill in" the detected horizontal lines with white to effectively remove horizontal lines
Create vertical kernel and repair image with morph close
After converting to grayscale, we Otsu's threshold to obtain a binary image
Next we create a special horizontal kernel to detect horizontal lines. We draw these lines onto a mask and then find contours on the mask. To remove the lines, we fill in the contours with white
Detected lines
Mask
Filled in contours
The image currently has gaps. To fix this, we construct a vertical kernel to repair the image
Note depending on the image, the size of the kernel will change. For instance, to detect longer lines, we could use a
(50,1)
kernel instead. If we wanted thicker lines, we could increase the 2nd parameter to say(50,2)
.Here's the results with the other images
Detected lines
Original (left), removed (right)
Detected lines
Original (left), removed (right)
Full code
这篇关于删除图像中的水平线(OpenCV、Python、Matplotlib)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!