圖像處理之三角法圖像二值化
三角法求閾值最早見於Zack的論文《Automatic measurement of sister chromatid exchange frequency》主要是用於染色體的研究,該方法是使用直方圖數據,基於純幾何方法來尋找最佳閾值,它的成立條件是假設直方圖最大波峯在靠近最亮的一側,然後通過三角形求得最大直線距離,根據最大直線距離對應的直方圖灰度等級即爲分割閾值,圖示如下:
對上圖的詳細解釋:
在直方圖上從最高峯處bmx到最暗對應直方圖bmin(p=0)%構造一條直線,從bmin處開始計算每個對應的直方圖b到直線的垂直距離,知道bmax爲止,其中最大距離對應的直方圖位置即爲圖像二值化對應的閾值T。
擴展情況:
有時候最大波峯對應位置不在直方圖最亮一側,而在暗的一側,這樣就需要翻轉直方圖,翻轉之後求得值,用255減去即得到爲閾值T。擴展情況的直方圖表示如下:
二:算法步驟
1. 圖像轉灰度
2. 計算圖像灰度直方圖
3. 尋找直方圖中兩側邊界
4. 尋找直方圖最大值
5. 檢測是否最大波峯在亮的一側,否則翻轉
6. 計算閾值得到閾值T,如果翻轉則255-T
三:代碼實現
package com.gloomyfish.filter.study;
import java.awt.image.BufferedImage;
public class TriangleBinaryFilter extends AbstractBufferedImageOp{
public TriangleBinaryFilter() {
System.out.println("triangle binary filter");
}
@Override
public BufferedImage filter(BufferedImage src, BufferedImage dest) {
int width = src.getWidth();
int height = src.getHeight();
if ( dest == null )
dest = createCompatibleDestImage( src, null );
// 圖像灰度化
int[] inPixels = new int[width*height];
int[] outPixels = new int[width*height];
getRGB( src, 0, 0, width, height, inPixels );
int index = 0;
for(int row=0; row<height; row++) {
int ta = 0, tr = 0, tg = 0, tb = 0;
for(int col=0; col<width; col++) {
index = row * width + col;
ta = (inPixels[index] >> 24) & 0xff;
tr = (inPixels[index] >> 16) & 0xff;
tg = (inPixels[index] >> 8) & 0xff;
tb = inPixels[index] & 0xff;
int gray= (int)(0.299 *tr + 0.587*tg + 0.114*tb);
inPixels[index] = (ta << 24) | (gray << 16) | (gray << 8) | gray;
}
}
// 獲取直方圖
int[] histogram = new int[256];
for(int row=0; row<height; row++) {
int tr = 0;
for(int col=0; col<width; col++) {
index = row * width + col;
tr = (inPixels[index] >> 16) & 0xff;
histogram[tr]++;
}
}
int left_bound = 0, right_bound = 0, max_ind = 0, max = 0;
int temp;
boolean isflipped = false;
int i=0, j=0;
int N = 256;
// 找到最左邊零的位置
for( i = 0; i < N; i++ )
{
if( histogram[i] > 0 )
{
left_bound = i;
break;
}
}
// 位置再移動一個步長,即爲最左側零位置
if( left_bound > 0 )
left_bound--;
// 找到最右邊零點位置
for( i = N-1; i > 0; i-- )
{
if( histogram[i] > 0 )
{
right_bound = i;
break;
}
}
// 位置再移動一個步長,即爲最右側零位置
if( right_bound < N-1 )
right_bound++;
// 在直方圖上尋找最亮的點Hmax
for( i = 0; i < N; i++ )
{
if( histogram[i] > max)
{
max = histogram[i];
max_ind = i;
}
}
// 如果最大值落在靠左側這樣就無法滿足三角法求閾值,所以要檢測是否最大值是否靠近左側
// 如果靠近左側則通過翻轉到右側位置。
if( max_ind-left_bound < right_bound-max_ind)
{
isflipped = true;
i = 0;
j = N-1;
while( i < j )
{
// 左右交換
temp = histogram[i]; histogram[i] = histogram[j]; histogram[j] = temp;
i++; j--;
}
left_bound = N-1-right_bound;
max_ind = N-1-max_ind;
}
// 計算求得閾值
double thresh = left_bound;
double a, b, dist = 0, tempdist;
a = max; b = left_bound-max_ind;
for( i = left_bound+1; i <= max_ind; i++ )
{
// 計算距離 - 不需要真正計算
tempdist = a*i + b*histogram[i];
if( tempdist > dist)
{
dist = tempdist;
thresh = i;
}
}
thresh--;
// 對已經得到的閾值T,如果前面已經翻轉了,則閾值要用255-T
if( isflipped )
thresh = N-1-thresh;
// 二值化
System.out.println("final threshold value : " + thresh);
for(int row=0; row<height; row++) {
for(int col=0; col<width; col++) {
index = row * width + col;
int gray = (inPixels[index] >> 8) & 0xff;
if(gray > thresh)
{
gray = 255;
outPixels[index] = (0xff << 24) | (gray << 16) | (gray << 8) | gray;
}
else
{
gray = 0;
outPixels[index] = (0xff << 24) | (gray << 16) | (gray << 8) | gray;
}
}
}
// 返回二值圖像
setRGB(dest, 0, 0, width, height, outPixels );
return dest;
}
}
四:運行結果
2016年最後一篇,這裏祝大家元旦快樂,歡迎在2017繼續關注本博客,分享有用實用的圖像處理知識本人會一直堅持到永遠!
學習圖像處理基礎入門課程 - 點擊這裏