使用opencv进行Ostu二值化

来源:互联网 发布:室内照明设计软件 编辑:程序博客网 时间:2024/05/29 05:00

Otsu’s Binarization
Otsu 二值化

在第一部分,我说过这里有第二个参数retVal。如果我们使用Otsu我们将使用这个参数。怎么用呢?
In the first section, I told you there is a second parameter retVal. Its use comes when we go for Otsu’s Binarization. So what is it?

在全局阈值中,我们使用一个固定的值作为阈值。对吧?那么我们如何知道我们选的值好不好呢?答案是:反复试验。但是如果考虑双峰图像(简单的说,双峰图像就是直方图有两个波峰的图像)。对于这种图像,我们可以在两个波峰中间大约的取一个值作为阈值,对吧?这就是Ostu算法。简单的说,他可以在直方图中给双峰图像自动计算出一个阈值。(如果图像不是双峰图,这种办法并不准确)

In global thresholding, we used an arbitrary value for threshold value, right? So, how can we know a value we selected is good or not? Answer is, trial and error method. But consider a bimodal image (In simple words, bimodal image is an image whose histogram has two peaks). For that image, we can approximately take a value in the middle of those peaks as threshold value, right ? That is what Otsu binarization does. So in simple words, it automatically calculates a threshold value from image histogram for a bimodal image. (For images which are not bimodal, binarization won’t be accurate.)

我们可以用cv2.threshold()来实现,但是需要一个额外的标记cv2.THRESH_OTSU阈值可以简单设为0。这个算法找到一个优化的阈值,并返回您作为第二个输出,retVal。如果Ostu没有使用,retVal仍然作为阈值被使用。(???)
For this, our cv2.threshold() function is used, but pass an extra flag, cv2.THRESH_OTSU. For threshold value, simply pass zero. Then the algorithm finds the optimal threshold value and returns you as the second output, retVal. If Otsu thresholding is not used, retVal is same as the threshold value you used.

检查下面的例子,输入一个带有噪声的图片,在第一行例子中,我是用了全局阈值127,在第二个例子中,我使用了Ostu算法。在第三行我使用5*5的高斯核来进行降噪,然后在进行Ostu,看一下滤波对结果的影响。
Check out below example. Input image is a noisy image. In first case, I applied global thresholding for a value of 127. In second case, I applied Otsu’s thresholding directly. In third case, I filtered image with a 5x5 gaussian kernel to remove the noise, then applied Otsu thresholding. See how noise filtering improves the result.

import cv2import numpy as npfrom matplotlib import pyplot as pltimg = cv2.imread('noisy2.png',0)# global thresholdingret1,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)# Otsu's thresholdingret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)# Otsu's thresholding after Gaussian filteringblur = cv2.GaussianBlur(img,(5,5),0)ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)# plot all the images and their histogramsimages = [img, 0, th1,          img, 0, th2,          blur, 0, th3]titles = ['Original Noisy Image','Histogram','Global Thresholding (v=127)',          'Original Noisy Image','Histogram',"Otsu's Thresholding",          'Gaussian filtered Image','Histogram',"Otsu's Thresholding"]for i in xrange(3):    plt.subplot(3,3,i*3+1),plt.imshow(images[i*3],'gray')    plt.title(titles[i*3]), plt.xticks([]), plt.yticks([])    plt.subplot(3,3,i*3+2),plt.hist(images[i*3].ravel(),256)    plt.title(titles[i*3+1]), plt.xticks([]), plt.yticks([])    plt.subplot(3,3,i*3+3),plt.imshow(images[i*3+2],'gray')    plt.title(titles[i*3+2]), plt.xticks([]), plt.yticks([])plt.show()

ostu

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 如果地震了你会怎么办 地震来了怎么办60字 如果迷路了你会怎么办 吃多了抽烟想吐怎么办 晚上牙疼得要命怎么办 楼梯被火封锁后怎么办 牙齿疼怎么办能快速不疼 我被短信轰炸了怎么办 火警响了在家该怎么办 痔疮肉球变大了怎么办 痔疮长了好几个怎么办 苹果7手机丢了怎么办 如果油锅着火了怎么办 家里电气著火了怎么办 你家油锅起火了怎么办 交通事故后对方不肯去处理怎么办 租的车出了事故怎么办 借的车出了事故怎么办 台风来了怎么办小知识 台中班台风来了怎么办 地震时在五楼怎么办 小事故对方不来怎么办 发生交通事故对方不处理怎么办 当地震来了该怎么办 地震来了该怎么办教案 地震来了怎么办的问题 住30楼的地震了怎么办 在家里地震来了怎么办? 乐高地震来了怎么办? 地震来了该怎么办300字 地震来了该怎么办200 外地在北京生孩子建档怎么办 怀孕2个月头疼怎么办 怀孕6个月头疼怎么办 轮胎扎了个钉子怎么办 顶菅遇到填土区怎么办 12墙没砌在梁上怎么办 孕32周胎盘二级怎么办 孕36周胎盘三级怎么办 孕24周胎盘一级怎么办 孕24周 胎盘1级怎么办