一次OpenCV相关作业,有一个助教小姐姐写的tutorial,很有用,链接如下:
链接:http://pan.baidu.com/s/1bZHsJk 密码:854s
1. 色彩空间:
将RGB图像转换成ycrcb和hsv图像并保存每种色彩空间每个通道的图像。
import cv2 import numpy as np img=cv2.imread('/Users/wangmengxi/Documents/mercy/ec601/openCV/ex2/rice_grains/rice_grains.jpg',cv2.IMREAD_COLOR) hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV) ycrcb=cv2.cvtColor(img,cv2.COLOR_BGR2YCrCb) R, G, B = cv2.split(img) cv2.imwrite("/Users/wangmengxi/Documents/mercy/ec601/openCV/ex2/rice_grains/Red.jpg", R) cv2.imwrite("/Users/wangmengxi/Documents/mercy/ec601/openCV/ex2/rice_grains/Green.jpg",G) cv2.imwrite("/Users/wangmengxi/Documents/mercy/ec601/openCV/ex2/rice_grains/Blue.jpg",B) H,S,V=cv2.split(hsv) cv2.imwrite("/Users/wangmengxi/Documents/mercy/ec601/openCV/ex2/rice_grains/Hue.jpg",H) cv2.imwrite("/Users/wangmengxi/Documents/mercy/ec601/openCV/ex2/rice_grains/Saturation.jpg",S) cv2.imwrite("/Users/wangmengxi/Documents/mercy/ec601/openCV/ex2/rice_grains/Value.jpg",V) Y,Cb,Cr=cv2.split(ycrcb) cv2.imwrite("/Users/wangmengxi/Documents/mercy/ec601/openCV/ex2/rice_grains/Y.jpg",Y) cv2.imwrite("/Users/wangmengxi/Documents/mercy/ec601/openCV/ex2/rice_grains/Cb.jpg",Cb) cv2.imwrite("/Users/wangmengxi/Documents/mercy/ec601/openCV/ex2/rice_grains/Cr.jpg",Cr)
输入:
输出:
Blue:
Red:
Green:
Cb:
Cr:
Y:
Hue:
Saturation:
Value:
分离通道:
R, G, B = cv2.split(img)
2. 计算图像像素:
下面代码是用来计算图像在(20,25)处的像素(分别是RGB, ycrcb和hsv通道的图)
#!/usr/bin/env python # encoding: utf-8 import numpy as np import cv2 baboon = cv2.imread("/Users/wangmengxi/Documents/mercy/ec601/openCV/OpenCV_homework/Test_images/baboon.jpg") hsv=cv2.cvtColor(baboon,cv2.COLOR_BGR2HSV) ycrcb=cv2.cvtColor(baboon,cv2.COLOR_BGR2YCrCb) (b,g,r) = baboon[20,25] (Cr,Cb,Y)=ycrcb[20,25] (v,s,h)=hsv[20,25] #print("baboon.jpg:") print("BGR:") print(b,g,r) print("ycrcb:") print(Cr,Cb,Y) print("hsv:") print(v,s,h)
注意:要把imread中的路径换成自己的图片路径
python版本:python2.7
输入:
输出:
问题:
What are the ranges of pixel values in each channel of each of the above mentioned colorspaces?
value range of R,G,B : 0~255
value range of Y,U,V : 16~235,16~240,16~240
value range of H,S,V : 0~180,0~255,0~255
value range of L,A,B : 0~100,-127~127,-127~127
value range of C,M,Y,K : 0~100