zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    JavaScript 操作 DOM 元素
    字节数
    如何判断校准曲线是否合格
    [WPF]MVVM模式下如何在后台cs中调用绑定命令
    逆对数antilog0.03376如何计算
    未能解析此远程名称:'nuget.org' 的解决方法
    【WPF】WPF ScorllView触摸滚动实现
    【VS2017】清除NuGet下载旧版本缓存
    【UWP】截图
    test
  • 原文地址:https://www.cnblogs.com/x1mercy/p/7824687.html
Copyright © 2011-2022 走看看