我想用python将图像中的颜色从特定颜色范围更改为另一种颜色 [英] I want to change the colors in image with python from specific color range to another color

查看:74
本文介绍了我想用python将图像中的颜色从特定颜色范围更改为另一种颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将棕色区域更改为红色(或其他颜色).只是我不知道如何获取棕色的范围并将它们放入 python 代码中.我知道如何更改单一颜色,但不知道如何更改一系列颜色.有任何想法吗?谢谢

I want to change the brown areas to RED (or another color). Just I don't know how to get the ranges for brown and put them in python code. I know how to change a single color, but not a range of colors. Any Ideas? Thanks

推荐答案

这应该会给你一个想法 - 评论很好:

This should give you an idea - it is pretty well commented:

#!/usr/local/bin/python3
import cv2 as cv
import numpy as np

# Load the aerial image and convert to HSV colourspace
image = cv.imread("aerial.png")
hsv=cv.cvtColor(image,cv.COLOR_BGR2HSV)

# Define lower and uppper limits of what we call "brown"
brown_lo=np.array([10,0,0])
brown_hi=np.array([20,255,255])

# Mask image to only select browns
mask=cv.inRange(hsv,brown_lo,brown_hi)

# Change image to red where we found brown
image[mask>0]=(0,0,255)

cv.imwrite("result.png",image)

我是如何确定棕色"的限制的?我在图像中找到了一个棕色区域,并将其裁剪以移除其他所有内容.然后我将它调整为 1x1 以平均该区域的所有棕色阴影并将其转换为 HSV 色彩空间,我打印它并取 Hue 的值为 15,然后 +/-5 给出范围为 10-20.将范围增加到 8-22 以选择更广泛的色调.

How did I determine the limits for "brown"? I located a brown area in the image, and cropped it out to remove everything else. Then I resized it to 1x1 to average all the shades of brown in that area and converted it to HSV colourspace, I printed that and took the value for Hue which was 15 and went +/-5 to give a range of 10-20. Increase the range to 8-22 to select a wider range of hues.

HSV/HSL 色彩空间在维基百科这里上有描述.

HSV/HSL colourspace is described on Wikipedia here.

关键字:图像处理、Python、OpenCV、inRange、颜色范围、素数.

Keywords: Image processing, Python, OpenCV, inRange, range of colours, prime.

这篇关于我想用python将图像中的颜色从特定颜色范围更改为另一种颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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