希望有能力的朋友还是拿C++做。
本节讨论金字塔(高斯金字塔和拉普拉斯金字塔)以及统计图像直方图
Gaussian pyramid:用来降采样,主要的图像金字塔
#向下采样
import cv2
import numpy as np
img = cv2.imread('cat.jpeg')
print(img.shape)
#分辨率减小的操作,下采样,只需一个参数,每次固定1/4
dst = cv2.pyrDown(img)
print(dst.shape)
cv2.imshow('img', img)
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
逆亚采样
# 向上采样
import cv2
import numpy as np
img = cv2.imread('cat.jpeg')
print(img.shape)
#分辨率增大的操作,下采样,只需一个参数,每次固定4倍
dst = cv2.pyrUp(img)
print(dst.shape)
cv2.imshow('img', img)
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
Laplacian pyramid:用于从金字塔底层图像重建上层未采样图像(实际上是原图和高斯金字塔之间的残差,丢到的东西就是拉普拉斯金字塔),在数字图像处理中即为预测残差,可以对图像进行最大程度的还原,配合高斯金字塔一起使用
import cv2
import numpy as np
img = cv2.imread('cat.jpeg')
print(img.shape)
# 这是第0层 的
#先缩小
dst = cv2.pyrDown(img)
#再放大
dst = cv2.pyrUp(dst)
# Laplace = 原图 - dst
lap0 = img - dst
#这是第一层的
dst1 = cv2.pyrDown(dst)
dst1 = cv2.pyrUp(dst1)
lap1 = dst - dst1
cv2.imshow('img', img)
cv2.imshow('dst0', lap0)
cv2.imshow('dst1', lap1)
cv2.waitKey(0)
cv2.destroyAllWindows()
#高斯金字塔取样不可逆
二维统计图表, 可以均衡亮度等
import cv2
import numpy as np
img = cv2.imread('cat.jpeg')
hist = cv2.calcHist([img], [0], None, [256], [0,255])
print(hist)
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('cat.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.hist(gray.ravel(), bins=256, range=[0,255])
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('cat.jpeg')
histb = cv2.calcHist([img], [0], None, [256], [0,255])
histg = cv2.calcHist([img], [1], None, [256], [0,255])
histr = cv2.calcHist([img], [2], None, [256], [0,255])
#plot不传x的时候,会以histb的索引作为x
plt.plot(histb, color='b', label='blue')
plt.plot(histg, color='g', label='green')
plt.plot(histr, color='r', label='red')
plt.legend()
plt.show()
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('cat.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#生成掩膜图像
mask = np.zeros(gray.shape, np.uint8)
#设置想要统计的直方图区域
mask[200:400, 200:400] = 255
cv2.imshow('mask', mask)
cv2.imshow('gray', gray)
#gray,再和mask做与运算
#cv2.imshow('mask_gray', cv2.bitwise_and(gray, gray, mask=mask))
cv2.imshow('mask_gray', cv2.bitwise_and(gray, mask))
cv2.waitKey(0)
cv2.destroyAllWindows()