RGB转换到SHV的方法
import cv2
img = cv2.imread(r'./cat/cat1.png')
cv2.imshow('original image', img)
HSV_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
cv2.imshow('HSV format image', HSV_img)
cv2.waitKey()
cv2.destroyAllWindows()
RGB转换到HSI的方法
import cv2
from skimage import data,color
from matplotlib import pyplot as plt
import numpy as np
L=255
def GetR(gray):
if gray<L/2:
return 0
elif gray>L/4*3:
return L
else:
return 4*gray-2*L
def GetG(gray):
if gray<L/4:
return 4*gray
elif gray>L/4*3:
return 4*L-4*gray
else:
return L
def GetB(gray):
if gray<L/4:
return L
elif gray>L/2:
return 0
else:
return 2*L-4*gray
img=cv2.imread('C:/Users/xpp/Desktop/Lena.png')#原始图像
cv2.imshow("original",img)
grayimg=color.rgb2gray(img) * 255#将彩色图片转换为灰度图片
colorimg=np.zeros(img.shape,dtype='uint8')
for ii in range(img.shape[0]):
for jj in range(img.shape[1]):
r,g,b=GetR(grayimg[ii,jj]),GetG(grayimg[ii,jj]),GetB(grayimg[ii,jj])#灰度值到彩色变换
colorimg[ii,jj,:]=(r,g,b)
cv2.imshow("result",colorimg)
cv2.waitKey()
cv2.destroyAllWindows()
plt.imshow(colorimg)
RGB转换到YUV的方法
import cv2 as cv
src = cv.imread(r"D:\p\mm\cat\cat1.png")
cv.namedWindow("rgb", cv.WINDOW_AUTOSIZE)
cv.imshow("rgb", src)
# RGB to YUV
yuv = cv.cvtColor(src, cv.COLOR_BGR2YUV)
cv.imshow("yuv format image", yuv)