import cv2
import numpy as np
def color_threshold(image, lower, upper):
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_image, lower, upper)
result = cv2.bitwise_and(image, image, mask=mask)
return result
# 读取图片
image = cv2.imread('input.jpg')
# 设置颜色阈值范围,例如抠出绿色区域
lower = np.array([35, 100, 100])
upper = np.array([85, 255, 255])
# 执行抠图
masked_image = color_threshold(image, lower, upper)
# 保存抠图结果
cv2.imwrite('output.png', masked_image)