OpenCV库学习心得(六)
1. 图像二值化
1.1 二值图像(Binary Image)
1.2 图像二值化方式
1.3 OpenCV有关API
1.3.1 OTSU
1.3.2 Triangle
1.3.3 全自动与手动式
1.3.4 部分阀值
2. 图像金字塔
图像金字塔基本原理
高斯函数金字塔式和拉普拉斯金字塔式
1. 图像二值化
1.1 二值图像(Binary Image)
(1)二值图像的定义
一张图象仅有黑与白2个色调
下边图象是便是一个二值图像,在其中的1意味着灰黑色,在其中的0意味着白色
(2)二值图像的储存
应用四叉树储存平面图图象
1.2 图像二值化方式
图像二值化的方式:
全局性阀值
部分阀值
1.3 OpenCV有关API
1.3.1 OTSU
import cv2 as cv
import numpy
def threshold_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
print("threshold value %s" % ret)
cv.imshow("Binary IMage", binary)
image = cv.resize(cv.imread("01.jpg"), (300,400))
cv.imshow("image", image)
threshold_demo(image)
cv.waitKey(0)
cv.destroyAllWindows()
threshold value 171.0
1.3.2 Triangle
import cv2 as cv
import numpy
def threshold_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_TRIANGLE)
print("threshold value %s" % ret)
cv.imshow("Binary IMage", binary)
image = cv.resize(cv.imread("01.jpg"), (300,400))
cv.imshow("image", image)
threshold_demo(image)
cv.waitKey(0)
cv.destroyAllWindows()
threshold value 189.0
1.3.3 全自动与手动式
import cv2 as cv
import numpy
def threshold_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 150, 255, cv.THRESH_BINARY)
print("threshold value %s" % ret)
cv.imshow("Binary IMage", binary)
image = cv.resize(cv.imread("01.jpg"), (300,400))
cv.imshow("image", image)
threshold_demo(image)
cv.waitKey(0)
cv.destroyAllWindows()
threshold value 150.0
1.3.4 部分阀值
应用ADAPTIVE_THRESH_MEAN_C
import cv2 as cv
import numpy
def local_threshold_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
binary = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 25, 10)
cv.imshow("Binary IMage", binary)
image = cv.resize(cv.imread("01.jpg"), (300,400))
cv.imshow("image", image)
local_threshold_demo(image)
cv.waitKey(0)
cv.destroyAllWindows()
应用ADAPTIVE_THRESH_GAUSSIAN_C
import cv2 as cv
import numpy
def local_threshold_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
binary = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 25, 10)
cv.imshow("Binary IMage", binary)
image = cv.resize(cv.imread("01.jpg"), (300,400))
cv.imshow("image", image)
local_threshold_demo(image)
cv.waitKey(0)
cv.destroyAllWindows()
2. 图像金字塔
图像金字塔基本原理
图像金字塔是图象多尺度表述的一种,是一种以多屏幕分辨率来表述图象的合理但定义简易的构造。
一幅图象的金字塔式是一系列以金字塔式样子排序的屏幕分辨率逐渐减少,且来自同一张初始图的图象结合。其根据专业化向下采样得到,直至做到某一停止标准才终止取样。
大家将一层一层的图象形容成金字塔式,等级越高,则图象越小,屏幕分辨率越低。
高斯函数金字塔式和拉普拉斯金字塔式
Reduce:先向图象开展高斯模糊,再取下全部列和行中的双数行和双数列开展降取样
Expand:先向图象开展扩张,再对扩张的图象开展卷积和
import cv2 as cv
def pyramid_demo(image):
level = 3
temp = image.copy()
pyramid_images = []
for i in range(level):
dst = cv.pyrDown(temp)
pyramid_images.append(dst)
cv.imshow("pyramid_down_" + str(i), dst)
temp = dst.copy()
return pyramid_images
def ladpalian_demo(image):
pyramid_images = pyramid_demo(image)
level = len(pyramid_images)
for i in range(level - 1, -1, -1):
if (i - 1) < 0:
expand = cv.pyrUp(pyramid_images[i], dstsize=image.shape[:2])
lpls = cv.subtract(image, expand)
cv.imshow("laplalian_down_" + str(i), lpls)
else:
expand = cv.pyrUp(pyramid_images[i], dstsize=pyramid_images[i - 1].shape[:2])
lpls = cv.subtract(pyramid_images[i - 1], expand)
cv.imshow("laplalian_down_" + str(i), lpls)
image = cv.imread("01.jpg")
pyramid_demo(image)
cv.waitKey(0)
cv.destroyAllWindows()
pyramid_demo
ladpalian_demo