图像处理之常见二值化方法汇总

图像二值化是图像分析与处理中最常见最重要的处理手段,二值处理方法也非常多。越

精准的方法计算量也越大。本文主要介绍四种常见的二值处理方法,通常情况下可以满

足大多数图像处理的需要。主要本文讨论的方法仅针对RGB色彩空间。

 

方法一:

该方法非常简单,对RGB彩色图像灰度化以后,扫描图像的每个像素值,值小于127的

像素值设为0(黑色),值大于等于127的像素值设为255(白色)。该方法的好处是计算

量少速度快。缺点更多首先阈值为127没有任何理由可以解释,其次完全不考虑图像的

像素分布情况与像素值特征。可以说该方法是史最弱智的二值处理方法一点也不为过。

 

方法二:

最常见的二值处理方法是计算像素的平均值K,扫描图像的每个像素值如像素值大于K

像素值设为255(白色),值小于等于K像素值设为0(黑色)。该方法相比方法一,阈值的

选取稍微有点智商,可以解释。但是使用平均值作为二值化阈值同样有个致命的缺点,

可能导致部分对象像素或者背景像素丢失。二值化结果不能真实反映源图像信息。

 

方法三:

使用直方图方法来寻找二值化阈值,直方图是图像的重要特质,直方图方法选择二值

化阈值主要是发现图像的两个最高的峰,然后在阈值取值在两个峰之间的峰谷最低处。

该方法相对前面两种方法而言稍微精准一点点。结果也更让人可以接受。

 

方法四:http://en.wikipedia.org/wiki/Thresholding_(image_processing)

使用近似一维Means方法寻找二值化阈值,该方法的大致步骤如下:

1.      一个初始化阈值T,可以自己设置或者根据随机方法生成。

2.      根据阈值图每个像素数据P(n,m)分为对象像素数据G1与背景像素数据G2。(n为

行,m为列)

3.      G1的平均值是m1, G2的平均值是m2

4.      一个新的阈值T’ = (m1 + m2)/2

5.      回到第二步,用新的阈值继续分像素数据为对象与北京像素数据,继续2~4步,

直到计算出来的新阈值等于上一次阈值。

前面三种在以前的博文中都有涉及,最后一种二值化方法的代码如下:

[java]  view plain copy
  1. package com.gloomyfish.filter.study;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. public class ThresholdBinaryFilter extends GrayFilter {  
  8.   
  9.     @Override  
  10.     public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
  11.         int width = src.getWidth();  
  12.         int height = src.getHeight();  
  13.   
  14.         if ( dest == null )  
  15.             dest = createCompatibleDestImage( src, null );  
  16.   
  17.         int[] inPixels = new int[width*height];  
  18.         int[] outPixels = new int[width*height];  
  19.         src = super.filter(src, null); // we need to create new one  
  20.         getRGB( src, 00, width, height, inPixels );  
  21.         int index = 0;  
  22.         int means = getThreshold(inPixels, height, width);  
  23.         for(int row=0; row<height; row++) {  
  24.             int ta = 0, tr = 0, tg = 0, tb = 0;  
  25.             for(int col=0; col<width; col++) {  
  26.                 index = row * width + col;  
  27.                 ta = (inPixels[index] >> 24) & 0xff;  
  28.                 tr = (inPixels[index] >> 16) & 0xff;  
  29.                 tg = (inPixels[index] >> 8) & 0xff;  
  30.                 tb = inPixels[index] & 0xff;  
  31.                 if(tr > means) {  
  32.                     tr = tg = tb = 255//white  
  33.                 } else {  
  34.                     tr = tg = tb = 0// black  
  35.                 }  
  36.                 outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;  
  37.             }  
  38.         }  
  39.         setRGB( dest, 00, width, height, outPixels );  
  40.         return dest;  
  41.     }  
  42.   
  43.     private int getThreshold(int[] inPixels, int height, int width) {  
  44.         // maybe this value can reduce the calculation consume;   
  45.         int inithreshold = 127;  
  46.         int finalthreshold = 0;  
  47.         int temp[] = new int[inPixels.length];  
  48.         for(int index=0; index<inPixels.length; index++) {  
  49.             temp[index] = (inPixels[index] >> 16) & 0xff;  
  50.         }  
  51.         List<Integer> sub1 = new ArrayList<Integer>();  
  52.         List<Integer> sub2 = new ArrayList<Integer>();  
  53.         int means1 = 0, means2 = 0;  
  54.         while(finalthreshold != inithreshold) {  
  55.             finalthreshold = inithreshold;  
  56.             for(int i=0; i<temp.length; i++) {  
  57.                 if(temp[i] <= inithreshold) {  
  58.                     sub1.add(temp[i]);  
  59.                 } else {  
  60.                     sub2.add(temp[i]);  
  61.                 }  
  62.             }  
  63.             means1 = getMeans(sub1);  
  64.             means2 = getMeans(sub2);  
  65.             sub1.clear();  
  66.             sub2.clear();  
  67.             inithreshold = (means1 + means2) / 2;  
  68.         }  
  69.         long start = System.currentTimeMillis();  
  70.         System.out.println("Final threshold  = " + finalthreshold);  
  71.         long endTime = System.currentTimeMillis() - start;  
  72.         System.out.println("Time consumes : " + endTime);  
  73.         return finalthreshold;  
  74.     }  
  75.   
  76.     private static int getMeans(List<Integer> data) {  
  77.         int result = 0;  
  78.         int size = data.size();  
  79.         for(Integer i : data) {  
  80.             result += i;  
  81.         }  
  82.         return (result/size);  
  83.     }  
  84.   
  85. }  

效果如下:

图像的二值化(需要是灰度图像) import cv2 as cv import numpy as np def threshold_demo(image): gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY) #cv.THRESH_TRIANGLE与cv.THRESH_OTSU是自动寻找阈值,这个时候threshold值必须是0 #如果threshold不为0,即是自己指定的(如127),那么type就直接THRESH_BINARY就够了 ret,binary = cv.threshold(gray,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU) print(ret) cv.imshow("binary",binary) def local_threshold(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("binary1", binary) src =cv.imread("E:/opencv/picture/dog.jpg") cv.imshow("initial_window",src) threshold_demo(src) local_threshold(src) cv.waitKey(0) cv.destroyAllWindows() 分析: 1. 全局阈值 def threshold_demo(image): gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY) #cv.THRESH_TRIANGLE与cv.THRESH_OTSU是自动寻找阈值,这个时候threshold值必须是0 #如果threshold不为0,即是自己指定的(如127),那么type就直接THRESH_BINARY就够了 ret,binary = cv.threshold(gray,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU) print(ret) cv.imshow("binary",binary) threshold其函数原型为:threshold(src, thresh, maxval, type[, dst]) -> retval, dst src参数表示输入图像(多通道,8位或32位浮点)。 thresh参数表示阈值。(如果type是自动二值化就设置为0) maxval参数表示与THRESH_BINARY和THRESH_BINARY_INV阈值类型一起使用设置的最大值。 type参数表示阈值类型。 retval参数表示返回的阈值。若是全局固定阈值算法,则返回thresh参数值。若是全局自适应阈值算法,则返回自适应计算得出的合适阈值。 dst参数表示输出与src相同大小和类型以及相同通道数的图像。 type阈值类型说明 cv.THRESH_BINARY | cv.THRESH_OTSU)#大律法,全局自适应阈值 参数0可改为任意数字但不起作用 cv.THRESH_BINARY | cv.THRESH_TRIANGLE)#TRIANGLE法,,全局自适应阈值, 参数0可改为任意数字但不起作用,适用于单个波峰 cv.THRESH_BINARY)# 自定义阈值为127,大于127的是白色 小于的是黑色 cv.THRESH_BINARY_INV)# 自定义阈值为127,大于127的是黑色 小于的是白色 cv.THRESH_TRUNC)# 截断 大于127的是改为127 小于127的保留 cv.THRESH_TOZERO)# 截断 小于127的是改为127 大于127的保留 2. 局部阈值法 3. def local_threshold(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("binary1", binary) adaptiveThreshold函数进行局部阈值 函数原型为:adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) -> dst src参数表示输入图像(8位单通道图像)。 maxValue参数表示使用 THRESH_BINARY 和 THRESH_BINARY_INV 的最大值. adaptiveMethod参数表示自适应阈值算法, 平均— cv2.ADAPTIVE_THRESH_MEAN_C :领域内均值 高斯—cv2.ADAPTIVE_THRESH_GAUSSIAN_C :领域内像素点加权和,权 重为一个高斯窗口 thresholdType参数表示阈值类型,必须为THRESH_BINARY或THRESH_BINARY_INV的阈值类型。 blockSize参数表示块大小,规定领域大小(奇数且大于1,比如3,5,7........ )。 C参数是常数,表示从平均值或加权平均值中减去的数。 通常情况下,这是正值,但也可能为零或负值。 二.对超大图像进行二值化 如果这个时候只是单纯的用二值化api,图像上会出现很多噪声,所以我们特地介绍了一种对大图像进行二值化方法import cv2 as cv import numpy as np from matplotlib import pyplot as plt #对超大图像进行二值化 def big_image_threshold(image): 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): #gray[0:3] 从索引0开始取,直到索引3为止,但不包括索引3。即索引0,1,2,正好是3个元素。 roi = gray[row:row+ch,col:col+ch]#表示从[row,col]到[row+ch,col+ch]的所有元素所组成的矩阵 #推荐使用局部阈值二值化 dst = cv.adaptiveThreshold(roi,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,127,20) gray[row:row+ch,col:col+ch] =dst cv.imwrite("E:/opencv/picture/bigpicture1.jpg",gray) src = cv.imread("E:/opencv/picture/bigpicture.jpg") t1 = cv.getTickCount() big_image_threshold(src) t2 = cv.getTickCount() time= (t2-t1)/cv.getTickFrequency() #print("time =%sms\n"%(time)*1000) cv.waitKey(0) cv.destroyAllWindows()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值