大津法(otsu)是一種確定影象二值化分割閾值的演算法,由日本學者大津於2023年提出。從大津法的原理上來講,該方法又稱作最大類間方差法,因為按照大津法求得的閾值進行影象二值化分割後,前景與背景影象的類間方差最大。是求影象全域性閾值的最佳方法。
優點:計算簡單快速,不受影象亮度和對比度的影響。
缺點:對影象雜訊敏感;只能針對單一目標分割;當目標和背景大小比例懸殊、類間方差函式可能呈現雙峰或者多峰,這個時候效果不好。
#include
#include
#include
#include
cv::mat binarize
(cv::mat gray,
int th)}}
return out;
}int
otsu
(cv::mat& src, cv::mat& dst,
int thresh)
;int r = src.rows;
int c = src.cols;
for(
int i =
0; i < r;
++i)
}double p[grayscale]=;
double pk[grayscale]=;
double mk[grayscale]=;
double srcpixnum = r*c, sumtmppk =
0, sumtmpmk =0;
for(
int i =
0; i < grayscale;
++i)
//計算類間方差
double var=0;
for(
int k =
0; k < grayscale;
++k)}
src.
copyto
(dst)
; dst =
binarize
(src,thresh)
;return thresh;
}int
main()
if(src.
channels()
>1)
cv::
cvtcolor
(src, src, cv::color_rgb2gray);
cv::mat m_dst,dst;
int thresh=0;
double m_t =
(double
)cv::
gettickcount()
; thresh=
otsu
(src , m_dst, thresh)
;//otsu
std::cout <<
"m_thresh="
<< thresh << std::endl;
m_t =
(double
)cv::
gettickcount()
- m_t;
double m_time =
(m_t *
1000.)/
((double
)cv::
gettickfrequency()
);std::cout <<
"m_process="
<< m_time <<
" ms. "
<< std::endl << std::endl;
double otsuthresh =0;
otsuthresh=cv::
threshold
(src, dst, otsuthresh,
255, cv::thresh_otsu)
; std::cout <<
"thresh="
<< otsuthresh << std::endl;
cv::
imshow
("src"
, src)
; cv::
imshow
("m_dst"
, dst)
; cv::
imshow
("dst"
, dst)
; cv::
waitkey(0
);}
大津二值化演算法(Ostu二值化演算法)及其改進演算法
使用大津演算法來二值化影象!大津演算法,也被稱作最大類間方差法,是一種可以自動確定二值化中閾值的演算法,從類內方差和類間方差的比值計算得來 也就是說 類內方差 sw 2 w0 s0 2 w1 s1 2 類間方差 sb 2 w0 m0 mt 2 w1 m1 mt 2 w0 w1 m0 m1 2 影象所...
大津法加速
int otsu2 accel cv mat src,int width,int height float pixelpro 256 統計灰度級中每個畫素在整幅影象中的個數 for int y 0 y height y float sum u 0 計算每個畫素在整幅影象中的比例 for int i ...
opencv 最大類間方差(大津法OTSU)
參考 otsu 大津演算法 最近在做字元識別,看了很多資料,發現在對影象進行預處理過程中,對影象進行二值化是乙個必不可少的方式。如何才能有效的將目標字元表現出來,opencv提供的閾值化方法有threshold和adaptivethreshold,但這需要自己進行引數調整。在同學那裡了解到乙個很有效...