[英]Change a particular range of colors to anothe color in CV2 or PIL
I have an image as below我有一张如下图
In this image, I want to convert yellow numbers to BLACK
color & RED
regions to gray.在这张图片中,我想将黄色数字转换为
BLACK
,将RED
区域转换为灰色。 The already black & gray regions should not be changed.不应更改已经黑色和灰色的区域。 How to I do that?
我该怎么做? Can anyone help?
任何人都可以帮忙吗? I looked at several questions but i am not able to figure it out.
我看了几个问题,但我无法弄清楚。 Also, since the both yellow & red colors are not exactly the same but are in a range, i cannot to a direct replacement of the colors.
此外,由于黄色和红色 colors 并不完全相同,但在一个范围内,我不能直接更换 colors。 CV2 or PIL, both are OK.
CV2 或 PIL 都可以。
If HSV values have to be used, please let me know how to figure the range.如果必须使用 HSV 值,请告诉我如何计算范围。 This questions has been asked & some answers are already there but I am not able to grasp them, so any additional guidance will be helpful
已经提出了这个问题并且已经有一些答案,但我无法掌握它们,因此任何额外的指导都会有所帮助
The output I am expecting is something like the pic below (created by painting the pixels in Gimp).我期待的 output 类似于下图(通过在 Gimp 中绘制像素创建)。 Not exactly what you see, but you get the idea what I am looking for.
不完全是你看到的,但你明白我在找什么。
Update The solution that I ended up using is as below.更新我最终使用的解决方案如下。 This made all the yellow letters as black & that was sufficient for me (i think,with the similar approach all the red areas could be made grey too)
这使得所有黄色字母都变成黑色,这对我来说已经足够了(我认为,通过类似的方法,所有红色区域也可以变成灰色)
img = Image.open('Testpic.jpg').convert('RGB')
roi = img.crop((652,125,697,295))
orig_color1 = (210,140,0)
orig_color2 = (210,210,210)
replacement_color = (4,4,4)
roi = roi.resize((288, 1014), Image.ANTIALIAS)
data = np.array(roi)
data[((data >= orig_color2)).all(axis = -1)] = (255,0,0)
data[((data >= orig_color1)).all(axis = -1)] = replacement_color
data[((data == (255,0,0))).all(axis = -1)] = (255,255,255)
img2 = Image.fromarray(data, mode='RGB')
Your image is not the best quality and your results should improve if you can get a better input.您的图像质量不是最好的,如果您可以获得更好的输入,您的结果应该会有所改善。 I am just showing a technique with ImageMagick and may translate it into Python wand later if anyone likes it - it shouldn't be too hard.
我只是展示了一种使用ImageMagick的技术,如果有人喜欢它,可以稍后将其转换为 Python魔杖- 它应该不会太难。
So, rather than selecting ranges in HSL colourspace, in this instance it is easier and probably better to remap the image to a specific palette since you only want to differentiate 4 colours.因此,与其在 HSL 色彩空间中选择范围,在这种情况下,将图像重新映射到特定的调色板更容易并且可能更好,因为您只想区分 4 种颜色。 So, I made the following palette of 4 colours - a black, a grey, a yellow and a red like this:
因此,我制作了以下 4 种颜色的调色板——黑色、灰色、黄色和红色,如下所示:
magick xc:"gray(15)" xc:"gray(178)" xc:"rgb(230,50,30)" xc:"rgb(230,190,90)" +append palette.png
That makes this (much enlarged) 4x1 palette:这使得这个(放大很多)4x1 调色板:
Then I remapped your image to the colours of that palette:然后我将您的图像重新映射到该调色板的颜色:
magick column.png +dither -remap palette.png intermediate.png
Then I replace all colours vaguely close to yellow with black, and all colours vaguely close to red with white:然后我用黑色替换所有模糊接近黄色的颜色,用白色替换所有模糊接近红色的颜色:
magick intermediate.png -fill black -fuzz 30% -opaque yellow -fill white -opaque red result.png
Obviously change the -fill white
to -fill "gray(180)" if you want the background uniform, or add
-threshold 50%`:如果您想要背景均匀,显然将
-fill white
更改为-fill "gray(180)" if you want the background uniform, or add
-threshold 50%`:
If you don't want to use wand
for some reason, you can use my answer here to quantise to that specific 4-colour palette just the same.如果您出于某种原因不想使用
wand
,您可以在这里使用我的答案来量化到相同的特定 4 色调色板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.