opencv二值图像分割——python

opencv⼆值图像分割——python
1. ⼆值图像
图像⼆值化( Image Binarization)就是将图像上的像素点的灰度值设置为0或255,也就是将整个图像呈现出明显的⿊⽩效果的过程。
2. 全局阈值
cv2.threshold(src, thresholdValue, maxVal, thresholdingTechnique)
src:输⼊灰度图像数组。
thresholdValue:提及⽤于对像素值进⾏分类的值。
maxVal:如果像素值⼤于(有时⼩于)阈值,则给出的值。
thresholdingTechnique:要应⽤的阈值类型。
有5种不同的简单阈值技术是:
cv2.THRESH_BINARY:如果像素强度⼤于设置的阈值,则将值设置为 255,否则设置为 0(⿊⾊)。
cv2.THRESH_BINARY_INV:cv2 的反转或相反情况。
cv2.THRESH_TRUNC:如果像素强度值⼤于阈值,则会将其截断为阈值。像素值设置为与阈值相同。所有其他值保持不变。
cv2.THRESH_TOZERO:像素强度设置为 0,对于所有像素强度,⼩于阈值。
cv2.THRESH_TOZERO_INV:cv2 的反转或相反情况。
import cv2
if __name__ =="__main__":
# mentioning absolute path of the image
墙上画大饼img_path ="C:\\Users\\user\\Desktop\\flower.jpg"
# read/load an image in grayscale mode
grey_img = cv2.imread(img_path,0)
# show the Input image on the newly created image window
井冈山大学学报cv2.imshow('Input',grey_img)
# applying cv2.THRESH_BINARY thresholding techniques
ret, thresh_img = cv2.threshold(grey_img,128,255, cv2.THRESH_BINARY)
# show the Output image on the newly created image window
cv2.imshow('Output',thresh_img)
ret, th= cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
该⽅法也被称为⼤津法(OTSU)它是根据图像灰度分布,⾃动选择最佳的阈值,按照⼤津法求得的阈值进⾏图像⼆值化分割后,前景与背景图像的类间⽅差最⼤。适合处理所需提取的前景图像和背景图像差距较⼤的图像。其函数也⼗分简单,只需要把阈值thresh设置为0,然后设置type为cv2.THRESH_BINARY+cv2.THRESH_OTSU,会⾃动返回⼀个合适的阈值。
import cv2
if __name__ =="__main__":
# mentioning absolute path of the image
img_path ="./image-129.png"
# read/load an image in grayscale mode
gray_img = cv2.imread(img_path,0)
粗脉石仙桃# show the Input image on the newly created image window
print(gray_img.shape)生产力研究
cv2.imshow('Input', gray_img)
cv2.namedWindow("Input",0)
cv2.waitKey(0)
# applying cv2.THRESH_BINARY thresholding techniques
ret, thresh_img= cv2.threshold(gray_img,0,255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# show the Output image on the newly created image window
cv2.imshow('Output',thresh_img)
cv2.namedWindow("Output",0)
cv2.waitKey(0)令牌桶
cv2.imwrite('thresh.png', thresh_img)
3. 局部阈值
彩⾊图像⾊彩分布不均匀,使⽤全局阈值处理处理效果较差,通常采⽤局部阈值来进⾏分割,局部阈值的处理原理是,针对每⼀个像素点专门配置⼀个阈值来进⾏处理,这些阈值就构成了和原图像维度相同的矩阵。
cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C, dst=None)
src:灰度化的图⽚
maxValue:满⾜条件的像素点需要设置的灰度值
adaptiveMethod:⾃适应⽅法。有2种分别是均值和⾼斯加权:ADAPTIVE_THRESH_MEAN_C 或
ADAPTIVE_THRESH_GAUSSIAN_C,建议使⽤⾼斯加权和。
thresholdType:⼆值化⽅法,可以设置为THRESH_BINARY或者THRESH_BINARY_INV
blockSize:分割计算的区域⼤⼩,取奇数
C:常数,每个区域计算出的阈值的基础上在减去这个常数作为这个区域的最终阈值,可以为负数
dst:输出图像,可选
import cv2
if __name__ =="__main__":
# mentioning absolute path of the image
img_path ="./image-129.png"
# read/load an image in grayscale mode
gn205# gray_img = cv2.imread(img_path, 0)
color_img = cv2.imread(img_path)
gray_img = cv2.cvtColor(color_img, cv2.COLOR_BGR2GRAY)
# show the Input image on the newly created image window
print(gray_img.shape)
cv2.imshow('Input', gray_img)
cv2.namedWindow("Input",0)
cv2.waitKey(0)
# applying cv2.THRESH_BINARY thresholding techniques
thresh_img = cv2.adaptiveThreshold(gray_img,127, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,-30) # show the Output image on the newly created image window
cv2.imshow('Output',thresh_img)
cv2.namedWindow("Output",0)
cv2.waitKey(0)
cv2.imwrite('thresh.png', thresh_img)
4. 使⽤建议
灰度图像使⽤全局阈值分割:⼤津法 > 普通全局阈值
彩⾊图像使⽤局部阈值分割

本文发布于:2024-09-21 18:53:25,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/414202.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:阈值   图像   设置   处理   像素   分割   灰度
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议