先来python的,

# -*- coding:utf-8 -*-
import cv2 as cv
import numpy as np

def rgb2hsv(r, g, b):
    r, g, b = r/255.0, g/255.0, b/255.0
    mx = max(r, g, b)
    mn = min(r, g, b)
    m = mx-mn
    if mx == mn:
        h = 0
    elif mx == r:
        if g >= b:
            h = ((g-b)/m)*60
        else:
            h = ((g-b)/m)*60 + 360
    elif mx == g:
        h = ((b-r)/m)*60 + 120
    elif mx == b:
        h = ((r-g)/m)*60 + 240
    if mx == 0:
        s = 0
    else:
        s = m/mx
    v = mx
    H = h / 2
    S = s * 255.0
    V = v * 255.0
    return H, S, V

def fixHSVRange(h, s, v):
    # Normal H,S,V: (0-360,0-100%,0-100%)
    # OpenCV H,S,V: (0-180,0-255 ,0-255)
    return (180 * h / 360, 255 * s / 100, 255 * v / 100)


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


rgbs=[(254,214,105),(255,238,187),(151,196,252),(208,239,196),(232,157,224)
    ,(219,95,209),(13,95,152)]
for rgb in rgbs:
    pixel =rgb2hsv(rgb[0], rgb[1], rgb[2])
    brown_hi =  np.array([pixel[0] + 20, pixel[1] + 20, pixel[2] + 40])
    brown_lo =  np.array([pixel[0] - 20, pixel[1] - 20, pixel[2] - 40])
    mask=cv.inRange(hsv,brown_lo,brown_hi)
    image[mask>0]=(255,255,255)

rgbs=[(175,174,173),(170,173,167),(175,175,173),(237,236,229)]
for rgb in rgbs:
    pixel =rgb2hsv(rgb[0], rgb[1], rgb[2])
    brown_hi =  np.array([pixel[0] + 10, pixel[1] + 10, pixel[2] + 20])
    brown_lo =  np.array([pixel[0] - 10, pixel[1] - 10, pixel[2] - 20])
    mask=cv.inRange(hsv,brown_lo,brown_hi)
    image[mask>0]=(255,255,255)



cv.imshow('res',image)
cv.imwrite("d:\Screenshot3.png", image)
# cv.imwrite("result.png",image)
cv.waitKey(0)
cv.destroyAllWindows()
View Code

相关文章:

  • 2022-12-23
  • 2021-07-10
  • 2021-12-19
  • 2021-04-30
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-15
  • 2022-12-23
  • 2022-12-23
  • 2021-11-15
  • 2021-06-07
相关资源
相似解决方案