且构网

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

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

更新时间:2023-01-28 21:54:31

这应该给您一个想法-它的注释非常好:

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色彩空间在Wikipedia 此处中进行了描述.

HSV/HSL colourspace is described on Wikipedia here.

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

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