分块
全局阈值和局部阈值
from matplotlib import pyplot as plt
from cv2 import cv2 as cv
import numpy as np
def big_image_binary(image):
print(image.shape) #超大图像,屏幕无法显示完整
cw = 256 # 分块的大小
ch = 256
h,w = image.shape[:2]
gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY) #要二值化图像,要先进行灰度化处理
for row in range(0,h,ch):
for col in range(0,w,cw):
'''
roi = gray[row:row+ch,col:cw+col]#获取分块
print(np.std(roi),np.mean(roi))
# ret,dst=cv.threshold(roi,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU)#全局阈值
# 局部阈值
dst = cv.adaptiveThreshold(roi,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,127,20)
gray[row:row+ch,col:cw+col] = dst #分块覆盖
print(np.std(dst),np.mean(dst))
# 方差小于15就令为0 # 空白图像过滤
'''
roi = gray[row:row+ch,col:cw+col]#获取分块
print(np.std(roi),np.mean(roi))
dev = np.std(roi)
if dev < 15:
gray[row:row+ch,col:cw+col] = 255
else:
ret,dst=cv.threshold(roi,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU)#全局阈值
gray[row:row+ch,col:cw+col] = dst #分块覆盖
cv.imshow("big_image_binary",gray)
cv.imwrite("C:\\pictures\\result_binary.png",gray)
if __name__ == "__main__":
filepath = "C:\\pictures\\1(1)(1).jpg" # 超级大图 比如5477*7777 在其他图上放大尺寸就行
img = cv.imread(filepath) # blue green red
# cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
# cv.imshow("input image",img)
big_image_binary(img)
cv.waitKey(0)
cv.destroyAllWindows()
本文介绍了一种处理超大图像的分块二值化算法,通过将图像分割成多个小块并分别应用全局或局部阈值,实现图像的高效处理。文章详细展示了使用Python、OpenCV和Matplotlib等工具进行图像处理的具体步骤。
27万+

被折叠的 条评论
为什么被折叠?



