欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

OpenCV半小时掌握基本操作之直方图

时间:2021-11-20 16:23:05|栏目:Python代码|点击:

【OpenCV】⚠️高手勿入!⚠️ 半小时学会基本操作 ⚠️ 直方图

概述

OpenCV 是一个跨平台的计算机视觉库, 支持多语言, 功能强大. 今天小白就带大家一起携手走进 OpenCV 的世界. (第 20 课)

在这里插入图片描述

直方图

原图:

在这里插入图片描述

cv2.calcHist()可以帮助我们统计像素并得到直方图.

格式:

calcHist(images, channels, mask, histSize, ranges, hist=None, accumulate=None)

参数:

  • images: 输入图像
  • channels: 颜色通道
  • mask: 掩模
  • histSize: bin 的数目, 用中括号括起来
  • ranges: 像素范围 [0, 256]

例 1 (灰度图统计直方图):

import cv2
from matplotlib import pyplot as plt

plt.style.use("fivethirtyeight")

# 读取图片, 并转换成灰度图
img = cv2.imread("girl.jpg", 0)

# 获取直方图
hist = cv2.calcHist([img], [0], None, [256], [0, 256])
print(hist)

# 直方图展示
plt.figure(figsize=(12, 6))
plt.plot(hist)
plt.title("hist of image")
plt.show()

输出结果:

在这里插入图片描述

例 2 (RGB 三通道直方图):

import cv2
from matplotlib import pyplot as plt

plt.style.use("fivethirtyeight")
plt.figure(figsize=(12, 6))

# 读取图片
img = cv2.imread("girl.jpg")

# 颜色通道
color = ["b", "g", "r"]

# 获取直方图
for i, c in enumerate(color):
    hist = cv2.calcHist([img], [i], None, [256], [0, 256])
    plt.plot(hist, color=c)

# 直方图展示
plt.legend(["B Channel", "G Channel", "R Channel"])
plt.title("RGB hist of image")

plt.show()

输出结果:

在这里插入图片描述

直方图 + mask

例子:

import numpy as np
import cv2
from matplotlib import pyplot as plt

plt.style.use("fivethirtyeight")

# 读取图片, 并转换成灰度图
img = cv2.imread("girl.jpg", 0)

# 创建mask
mask = np.zeros(img.shape, np.uint8)
mask[280:1000, 420:1500] = 255

# 获取mask后的图像
masked_img = cv2.bitwise_and(img, img, mask=mask)

# 直方图
hist_full = cv2.calcHist([img], [0], None, [256], [0, 256])
hist_mask = cv2.calcHist([img], [0], mask, [256], [0, 256])

# 图片展示
f, ax = plt.subplots(2, 2, figsize=(12, 9))
ax[0, 0].imshow(img, 'gray')
ax[0, 0].set_title("original image")
ax[0, 1].imshow(mask, 'gray')
ax[0, 1].set_title("mask")
ax[1, 0].imshow(masked_img, 'gray')
ax[1, 0].set_title("masked image")
ax[1, 1].plot(hist_full)
ax[1, 1].plot(hist_mask)
ax[1, 1].set_title("original vs masked hist")

plt.show()

输出结果:

在这里插入图片描述

直方图均衡化

直方图均衡化 (Histogram Equalization) 是一种增强图片对比度的方法. 将一副图像的直方图分布变成近似均匀分布.

格式:

cv2.equalizeHist(src, dst=None)

例子:

import cv2
from matplotlib import pyplot as plt

plt.style.use("fivethirtyeight")

# 读取图片, 并转换成灰度图
img = cv2.imread("girl.jpg", 0)

# 均衡化
img_equ = cv2.equalizeHist(img)

# 直方图
f, ax = plt.subplots(2, 2, figsize=(16, 16))
ax[0, 0].imshow(img, "gray")
ax[0, 0].set_title("before")
ax[0, 1].imshow(img_equ, "gray")
ax[0, 1].set_title("after")
ax[1, 0].hist(img.ravel(), 256)
ax[1, 1].hist(img_equ.ravel(), 256)

plt.show()

输出结果:

在这里插入图片描述

上一篇:Python实现字典按照value进行排序的方法分析

栏    目:Python代码

下一篇:python django 访问静态文件出现404或500错误

本文标题:OpenCV半小时掌握基本操作之直方图

本文地址:http://www.codeinn.net/misctech/184200.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有