OpenCV中的二维直方图实现与Numpy有何不同
Admin 2022-05-25 群英技术资讯 465 次浏览
只统计像素的灰度值这一特征,可将其成为一维直方图。二维直方图可以统计像素的色相和饱和度,用于查找图像的颜色直方图。
OpenCV仍然使用cv2.calcHist()函数来查找图像的颜色直方图,只是在指定参数时与之前有所区别。
cv2.calcHist()函数的基本格式如下:
hist =cv2.calcHist(image, channels, mask, histSize, ranges)
image
参数指定的原图像应从BGR色彩空间转换为HSV色彩空间, 实际参数需要用方括号括起来
channels
参数设置为[0,1]时, 表示同时处理色相和饱和度
histSize
参数设置BINS值为[180,256]时, 表示色相为180, 饱和度为256
ranges
参数设置为[0,180,0,256]时, 表示色相的取值范围为[0,180], 饱和度的取值范围为[0,2565]
cv2.calcHist()函数返回的颜色直方图可以直接使用cv2.show()函数显示。
import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread('XIAN.jpg') cv2.namedWindow('orininal', cv2.WINDOW_NORMAL) cv2.imshow('orininal', img) img2 = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) hist = cv2.calcHist([img2], [0, 1], None, [180, 256], [0,180,0,256]) cv2.namedWindow('2DHist', cv2.WINDOW_NORMAL) cv2.imshow('2DHist', hist) cv2.waitKey(0) cv2.destroyAllWindows() plt.imshow(hist, interpolation = 'nearest') # 绘制颜色直方图 plt.show() # 显示颜色直方图
cv2.calcHist()函数返回的颜色直方图是一个大小为180*256的二维数组,用cv2.imshow()函数显示时是一副灰度图像,不能直接显示出颜色的分布情况。
可以使用matplotlib.pyplot.imshow()函数绘制具有不同颜色的二维直方图。
Numpy的np.histogram2d()函数用于计算二维直方图,其基本格式如下:
hist, xedges, yedges = np.histogram2d(x, y, bins, range)
hist
为返回的直方图
xedges
为返回的x的直方图的BINS边界值
yedges
为返回的y的直方图的BINS边界值
x
和y
为原图对应通道转换成的一维数组
bins
为BINS的值, 如[180,256]
range
为像素范围, 格式为[[0, 180],[0, 256]]
img = cv2.imread('building.jpg') cv2.imshow('orininal', img) img2 = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) h, s, v = cv2.split(img2) hist, x, y = np.histogram2d(h.ravel(), s.ravel(), [180, 256], [[0, 180],[0, 256]]) cv2.imshow('2DHist', hist) cv2.waitKey(0) cv2.destroyAllWindows() plt.imshow(hist, interpolation = 'nearest') plt.show()
可以使用matplotlib.pyplot.imshow()函数绘制具有不同颜色的二维直方图。
import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread('home.jpg') plt.figure(figsize = (25,25)) imgrgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.subplot(2, 2, 1) plt.title('Original') plt.axis('off') plt.imshow(imgrgb) histb, e1 = np.histogram(img[0].ravel(), 256, [0, 255]) #计算B通道直方图 histg, e2 = np.histogram(img[1].ravel(), 256, [0, 255]) #计算G通道直方图 histr, e3 = np.histogram(img[2].ravel(), 256, [0, 255]) #计算R通道直方图 plt.subplot(2, 2, 2) plt.plot(histb, color = 'b') plt.plot(histg, color = 'g') plt.plot(histr, color = 'r') plt.title('Hist') img2 = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) #转换色彩空间为HSV h, s, v = cv2.split(img2) hist, x, y=np.histogram2d(h.ravel(), s.ravel(), [180, 256], [[0, 180], [0, 256]]) #计算颜色直方图 plt.subplot(2, 2, 3) plt.title('2Dhist') #设置子图窗口标题 plt.imshow(hist, interpolation = 'nearest',cmap = 'gray') #绘制颜色直方图 plt.show() #显示颜色直方图
# 2.使用OpenCV函数计算直方图 import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread('flower.jpg') plt.figure(figsize = (25,25)) imgrgb=cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.subplot(2, 2, 1) plt.imshow(imgrgb) plt.title('Original') plt.axis('off') histb = cv2.calcHist([img], [0], None, [256], [0,255]) #计算B通道直方图 histg = cv2.calcHist([img], [1], None, [256], [0,255]) #计算G通道直方图 histr = cv2.calcHist([img], [2], None, [256], [0,255]) #计算R通道直方图 plt.subplot(2, 2, 2) plt.plot(histb, color= 'b') plt.plot(histg, color= 'g') plt.plot(histr, color= 'r') plt.title('Hist') img2=cv2.cvtColor(img,cv2.COLOR_BGR2HSV) hist = cv2.calcHist([img2], [0, 1], None, [180, 256], [0, 180, 0, 256]) plt.subplot(2, 2, 3) plt.title('2Dhist') #设置子图窗口标题 plt.imshow(hist,interpolation = 'nearest',cmap = 'gray') #绘制颜色直方图 plt.show() #显示颜色直方图
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了Python连接MySQL数据库后的一些基本操作,并以银行管理系统项目为例,为大家具体介绍了一下部分功能的实现,文中的示例代码具有一定的学习价值,感兴趣的可以了解一下
这篇文章主要介绍了Pytorch中求模型准确率的两种方法小结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
Python内置函数-float()函数。float() 函数用于将整数和字符串转换成浮点数。
在本文中,我们用代码详细说明了Python中超实用的5个函数的重要作用,这些函数虽然简单,但却是Python中功能最强大的函数,下面一起来看看文章的详细介绍吧,希望对你的学习有所帮助
open()函数的作用是打开一个文件,并返回一个file对象(即文件对象),下面这篇文章主要给大家介绍了关于python中open函数对文件处理的相关资料,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008