OpenCV之圖像像素&圖像色彩通道


一次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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM