颜色直方图实验

来源:互联网 发布:贺卡排版设计软件 编辑:程序博客网 时间:2024/06/03 07:34

学习颜色空间和颜色直方图,使用OpenCV + Python进行一些小实验。

实验介绍

  1. 对图片进行颜色空间的转换
  2. 画出图片的颜色直方图
  3. 对两张图片的颜色直方图进行比较

实验环境

  • 操作系统:Ubuntu 14.04.3 LTS
    (刚开始用Windows 10,然后发现用Python的PIL读取jpg文件时得到的RGB编码与Ubuntu下不同,而bmp文件却是一致的。经测试在Ubuntu下用Python得到jpg文件的RGB编码与mspaint一致,故改用Ubuntu。)
  • 开发环境:Python 2.7.6 + OpenCV 2.4.11
    (OpenCV 3.x与OpenCV 2.x略有不同。)
  • Python Library:
    • numpy 1.10.2
    • matplotlib 1.5.0
    • Pillow 3.1.1

实验过程

查看不同颜色空间的编码

输入:图片文件路径、颜色空间
输出:编码
参考:http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_colorspaces/py_colorspaces.html#converting-colorspaces

  1. 读入图片(OpenCV中默认颜色空间为BGR)
  2. 转换颜色空间
  3. 输出每个通道的编码
import cv2input_filepath = 'Lenna.png'output_filepath = ['R.txt', 'G.txt', 'B.txt']img = cv2.imread(input_filepath)imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)# imgLAB = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)for k in range(3):    fd = open(output_filepath[k], 'w')    for i in imgRGB[:, :, k]:        fd.write(' '.join(['{:3}'.format(j) for j in i]) + '\n')    fd.close()

画出颜色直方图

输入:图片文件路径、颜色空间
输出:颜色直方图
参考:http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_histograms/py_table_of_contents_histograms/py_table_of_contents_histograms.html#table-of-content-histograms
http://docs.opencv.org/3.0-beta/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html#histogram-calculation

  1. 读入图片
  2. 转换颜色空间
  3. 获得颜色直方图(可量化,注意HSV的H通道的大小是180)
  4. 绘制颜色直方图
import cv2import numpy as npimport matplotlib.pyplot as pltinput_filepath = 'Lenna.png'img = cv2.imread(input_filepath)# RGBimgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)hist = [cv2.calcHist([imgRGB], [k], None, [256], [0, 256]) for k in range(3)]x = np.arange(256) + 0.5plt.subplot(221), plt.imshow(imgRGB)plt.subplot(222), plt.bar(x, hist[0], color = 'r', edgecolor = 'r')plt.subplot(223), plt.bar(x, hist[1], color = 'g', edgecolor = 'g')plt.subplot(224), plt.bar(x, hist[2], color = 'b', edgecolor = 'b')plt.show()# HSVimgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)hist = [cv2.calcHist([imgHSV], [0], None, [50], [0, 180]), \    cv2.calcHist([imgHSV], [1], None, [50], [0, 256])]x = np.arange(50) + 0.5plt.subplot(211), plt.bar(x, hist[0])plt.subplot(212), plt.bar(x, hist[1])plt.show()

figure_RGB
figure_HSV

比较颜色直方图

输入:两张图片文件路径、颜色空间、比较方法
输出:比较结果(一个实数值)
参考:http://docs.opencv.org/3.0-beta/doc/tutorials/imgproc/histograms/histogram_comparison/histogram_comparison.html#histogram-comparison

  1. 读入图片
  2. 转换颜色空间
  3. 获得颜色直方图并归一化
  4. 比较颜色直方图
import cv2import numpy as npimport matplotlib.pyplot as pltinput_filepath = ['1.jpg', '2.jpg']comp_method = [cv2.cv.CV_COMP_CORREL, cv2.cv.CV_COMP_INTERSECT, \        cv2.cv.CV_COMP_CHISQR, cv2.cv.CV_COMP_BHATTACHARYYA]# BGRimg = [cv2.imread(i) for i in input_filepath]hist = [cv2.calcHist([i], [k], None, [256], [0, 256]) for k in range(3) for i in img]for i in hist    for j in i:        cv2.normalize(j, j)hist = [np.mean(i, 0) for i in hist]for method in comp_method:    d = cv2.compareHist(hist[0], hist[1], method)    print(d)# HSVimgHSV = [cv2.cvtColor(i, cv2.COLOR_BGR2HSV) for i in img]hist = [cv2.calcHist([i], [0, 1], None, [50, 50], [0, 180, 0, 256]) \    for i in imgHSV]for i in hist_set:    cv2.normalize(i, i)for method in comp_method:    d = cv2.compareHist(hist[0], hist[1], method)    print(d)

参考

  1. 直方图. https://zh.wikipedia.org/wiki/%E7%9B%B4%E6%96%B9%E5%9B%BE#.E9.A2.9C.E8.89.B2.E7.9B.B4.E6.96.B9.E5.9B.BE
  2. 颜色直方图. http://baike.baidu.com/view/2438797.htm
  3. OpenCV-Python Tutorials. http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_tutorials.html
  4. Lenna. https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png
0 0
原创粉丝点击