标签:技术 tle class rgb2gray targe 图片 知识 ada info
1.可以采用分块方法,
2.先缩放处理就行二值化,然后还原大小
def big_image_binary(image): print(image.shape) #(4208, 2368, 3) #超大图像,屏幕无法显示完整 cw,ch = 256,256 h,w = image.shape[:2] gray = cv.cvtColor(image,cv.COLOR_RGB2GRAY) #要二值化图像,要先进行灰度化处理 for row in range(0,h,ch): for col in range(0,w,cw): roi = gray[row:row+ch,col:col+cw] #获取分块 # ret,binary = cv.threshold(roi,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU) #全局阈值 binary = cv.adaptiveThreshold(roi,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,127,20) #局部阈值 gray[row:row + ch, col:col + cw] = binary #分块覆盖 print(np.std(binary),np.mean(binary)) cv.imwrite("binary2.jpg",gray)
ret,binary = cv.threshold(roi,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU) #全局阈值
binary = cv.adaptiveThreshold(roi,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,127,20) #局部阈值
def big_image_binary(image): print(image.shape) #(4208, 2368, 3) cw,ch = 128,128 h,w = image.shape[:2] gray = cv.cvtColor(image,cv.COLOR_RGB2GRAY) #要二值化图像,要先进行灰度化处理 for row in range(0,h,ch): for col in range(0,w,cw): roi = gray[row:row+ch,col:col+cw] #获取分块 dev = np.std(roi) avg = np.mean(roi) if dev < 15 and avg > 200: #满足条件,接近空白区域,让他变黑 gray[row:row + ch, col:col + cw] = 0 #全部都赋值为0 else: ret,binary = cv.threshold(roi,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU) gray[row:row + ch, col:col + cw] = binary print(np.std(binary), np.mean(binary)) cv.imwrite("binary.jpg",gray)
(一)numpy中相关方法介绍
print(np.std(binary),np.mean(binary))
通过上面获取图像的标准差和平均值,当标准差为0,平均值为255时,代表该区域为空白区域
我们可以将该空白区域(或者满足一定条件的区域),直接设置为0或者255或者其他想要获取的图像,不需要进行多余的阈值二分
标签:技术 tle class rgb2gray targe 图片 知识 ada info
原文地址:https://www.cnblogs.com/ssyfj/p/9273116.html