且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我想用python将图像中的颜色从特定颜色范围更改为另一种颜色

更新时间:2023-01-28 21:32:26

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

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.