1. 程式人生 > >OpenCV之圖像像素&圖像色彩通道

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

red python版本 usr 有一個 imwrite 圖片路徑 class get gre

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

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