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

Posted 机智的小八

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于OpenCV之图像像素&图像色彩通道的主要内容,如果未能解决你的问题,请参考以下文章

Opencv图像色彩空间与通道

C/C++ vs2017 OpenCV简单入门

C/C++ vs2017 OpenCV简单入门

Opencv 图像处理:图像通道直方图与色彩空间

opencv学习-入门系列汇总

跟我一起学opencv 第四课之图像的基本操作