从带有Python的图像中删除特定颜色的所有内容(具有颜色变化的耐受性)
我在蓝色#00A2E8中有一些文本,在png图像(白色背景)上用黑色文字。
如何用python pil或openCV在图像上以蓝色(包括蓝色的文本)删除所有内容,并且对颜色的变化具有一定的容忍度?
的确,文本的每个像素都不是完美的相同的颜色,有变化,蓝色阴影。
这是我在想的是:
- 从RGB转换为HSV
- 查找蓝色的色调
H0
- 在间隔
[H0-10,H0+10]中为色相做一个numpy掩码。 >
- 在编码此之前,将这些像素设置为白色
,是否有一种更标准的方法可以使用PIL或OPENCV PYTHON来执行此操作?
示例 png file :foo
和bar
应删除块
I have some text in blue #00a2e8, and some text in black on a PNG image (white background).
How to remove everything in blue (including text in blue) on an image with Python PIL or OpenCV, with a certain tolerance for the variations of color?
Indeed, every pixel of the text is not perfectly of the same color, there are variations, shades of blue.
Here is what I was thinking:
- convert from RGB to HSV
- find the Hue
h0
for the blue - do a Numpy mask for Hue in the interval
[h0-10, h0+10]
- set these pixels to white
Before coding this, is there a more standard way to do this with PIL or OpenCV Python?
Example PNG file: foo
and bar
blocks should be removed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的图像有一些问题。首先,它具有完全多余的α通道,可以忽略。其次,蓝色周围的颜色与蓝色相当长!
我使用了您的计划方法,发现删除非常差:
这给出了:
如果您扩大了范围,要获得粗糙的边缘,您就开始影响绿色字母因此,在蓝色附近的空间上,像素 是白色的,像素 在蓝色附近:
<
a href =“ https://i.sstatic 。
具有其他图像的实际值,但原理是相同的。
Your image has some issues. Firstly, it has a completely superfluous alpha channel which can be ignored. Secondly, the colours around your blues are quite a long way from blue!
I used your planned approach and found the removal was pretty poor:
That gives this:
If you broaden the range, to get the rough edges, you start to affect the green letters, so instead I dilated the mask so that pixels spatially near the blues are made white as well as pixels chromatically near the blues:
That gets you this:
You may wish to diddle with the actual values for your other images, but the principle is the same.
我想用另一种方法来鸣叫。我的基本想法是将图像从bgr转换为实验室色彩空间如果我可以将区域隔离为蓝色。这可以通过关注实验室的B组件来完成,因为它代表了从黄色到蓝色的颜色。
代码
( 注意: 蓝色区域实际上更暗,因此可以很容易地隔离。)
i.sstatic.net/oxokj.jpg“ rel =“ nofollow noreferrer 应用阈值后,图像在包含数字文本的区域周围包含一些不需要的白色像素,我们不想考虑这些文本。
为了避免有多余的区域,我尝试了以下内容:
在下面的代码中:
如果您看到以下蒙版,它将所有像素的所有像素都包含在轮廓中。但是,“ bar”一词中的像素应不考虑 。
仅隔离具有蓝色像素的区域,我们执行”和“用阈值图像
th
我们实际上得到了我们真正想要的掩码。
蒙版中的白色区域
在原始图像res
的副本中制成白色:但是上面的图像不是不是完美。 Foo一词的边缘周围仍然可以看到一些区域。
在以下内容中,我们扩张
掩码
并重复。I would like to chime in with a different approach. My basic idea is convert the image from BGR to LAB color space and figure out if I can isolate the regions in blue. This can be done by focusing on the b-component of LAB, since it represents the color from yellow to blue.
Code
(Note: The blue regions are actually quite darker such that it can be isolated easily.)
But after applying threshold, the image contains some unwanted white pixels around the regions containing numeric text, which we do not want to consider.
To avoid the unwanted regions I tried out the following:
In code below:
If you see the following mask, it has enclosed all pixels within the contour in white. However, the pixels within the word "bar" should not be considered.
To isolate only the region with blue pixels, we perform "AND" operation with the threshold image
th
We got the mask we actually want. The regions that are white in
mask
are made white in the copy of the original imageres
:But the above image is not perfect. There are some regions still visible around the edges of the word foo.
In the following we dilate
mask
and repeat.我认为您正在寻找功能inrange:
I think you are looking for the function inRange: