颜色直方图实验
2016-03-16 14:05
423 查看
学习颜色空间和颜色直方图,使用OpenCV + Python进行一些小实验。
画出图片的颜色直方图
对两张图片的颜色直方图进行比较
(刚开始用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
读入图片(OpenCV中默认颜色空间为BGR)
转换颜色空间
输出每个通道的编码
输出:颜色直方图
参考: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
读入图片
转换颜色空间
获得颜色直方图(可量化,注意HSV的H通道的大小是180)
绘制颜色直方图
输出:比较结果(一个实数值)
参考:http://docs.opencv.org/3.0-beta/doc/tutorials/imgproc/histograms/histogram_comparison/histogram_comparison.html#histogram-comparison
读入图片
转换颜色空间
获得颜色直方图并归一化
比较颜色直方图
颜色直方图. http://baike.baidu.com/view/2438797.htm
OpenCV-Python Tutorials. http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_tutorials.html
Lenna. https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png
实验介绍
对图片进行颜色空间的转换画出图片的颜色直方图
对两张图片的颜色直方图进行比较
实验环境
操作系统: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
读入图片(OpenCV中默认颜色空间为BGR)
转换颜色空间
输出每个通道的编码
import cv2 input_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
读入图片
转换颜色空间
获得颜色直方图(可量化,注意HSV的H通道的大小是180)
绘制颜色直方图
import cv2 import numpy as np import matplotlib.pyplot as plt input_filepath = 'Lenna.png' img = cv2.imread(input_filepath) # RGB imgRGB = 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.5 plt.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() # HSV imgHSV = 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.5 plt.subplot(211), plt.bar(x, hist[0]) plt.subplot(212), plt.bar(x, hist[1]) plt.show()
比较颜色直方图
输入:两张图片文件路径、颜色空间、比较方法输出:比较结果(一个实数值)
参考:http://docs.opencv.org/3.0-beta/doc/tutorials/imgproc/histograms/histogram_comparison/histogram_comparison.html#histogram-comparison
读入图片
转换颜色空间
获得颜色直方图并归一化
比较颜色直方图
import cv2 import numpy as np import matplotlib.pyplot as plt input_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] # BGR img = [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) # HSV imgHSV = [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)
参考
直方图. 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颜色直方图. http://baike.baidu.com/view/2438797.htm
OpenCV-Python Tutorials. http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_tutorials.html
Lenna. https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png
相关文章推荐
- 百度空间的变迁
- EventBus使用详解(一)——初步使用EventBus
- 朱会灿:搜索引擎演变史【腾讯大讲堂第3期】
- EntityFramework之监听者判断SQL性能指标
- C# 函数
- SYN591型 多功能数字面板表
- codeforces 13E . Holes 分块
- 将Python脚本打包成可执行文件
- JavaScript 踩坑心得— 为了高速(下)
- 基于栈虚拟机和基于寄存器虚拟机的比较
- Android布局善用tools工具
- Linux---centos6编译安装nginx1.8.1(附:安装脚本)
- Swift - 实现发送短信的功能
- 协同过滤
- 会话技术session
- jmeter函数助手之time函数实操
- 困扰一天的rpm打包问题
- 规约模式Specification Pattern
- StringBuilder和String的区别
- SYN591-C型 时间间隔表