OpenCV學習筆記 4 閾值分割

2021-09-29 10:19:06 字數 2635 閱讀 8871

threshold

(inputarray src, outputarray dst,

double thresh,

double maxval,

int type)

//src-輸入矩陣,資料型別為cv_8u或者cv_32f

//dst-輸出矩陣

//thresh-閾值

//maxval-影象二值化時,一般為255

//type-型別

當type=thresh_binary,

d st

(r,c

)=

maxval,&src(r,c)>thresh\\ 0,&src(r,c)\leq thresh \end

dst(r,

c)=0,&src(r,c)>thresh\\ maxval,&src(r,c)\leq thresh \end

dst(r,

c)=

secondpeak=arg_kmax\

second

peak

=arg

k​ma

x或seco

ndpe

ak=a

rgkm

ax

secondpeak=arg_kmax\

second

peak

=arg

k​ma

x第三步:找到這兩個峰值之間的波谷,若出現兩個或多個波谷,取左側波谷,其對應灰度值即為閾值。

int

threshtwopeaks

(const mat & image, mat & thresh_out)

point secondpeakloc;

minmaxloc

(measuredists,

null

,null

,null

,&secondpeakloc)

;int secondpeak = secondpeakloc.x;

//找到兩個峰值之間最小值對應的灰度值,作為閾值

point threshloc;

int thresh =0;

if(firstpeak < secondpeak)

//第乙個峰值在第二個峰值左側

else

//第乙個峰值在第二個峰值右側

//閾值分割

threshold

(image, thresh_out, thresh,

255, thresh_binary)

;return thresh;

}mat calcgrayhist

(const mat & image)

}return histogram;

}

int

otsu

(const mat & image, mat & otsuthreshimage)

else

}//計算類方間差

mat variance = mat::

zeros

(size

(256,1

), cv_32fc1)

;//總平均值

float mean = onecumumoment.at<

float

>(0

,255);

for(

int i =

0; i <

255; i++)}

//找到閾值

point maxloc;

minmaxloc

(variance,

null

,null

,null

,&maxloc)

;int otsuthresh = maxloc.x;

//閾值處理

threshold

(image, otsuthreshimage, otsuthresh,

255, thresh_binary)

;return otsuthresh;

}mat calcgrayhist

(const mat & image)

}return histogram;

}

void

adaptivethreshold

(inputarray src, outputarray dst,

double maxvalue,

int adaptivemethod,

int thresholdtype,

int blocksize,

double c)

//src-輸入矩陣,資料型別為cv_8u

//dst-輸出矩陣

//maxvalue-一般為255

//adaptivemethod-adaptive_thresh_mean_c採用均值平滑,

// adaptive_thresh_gaussian_c採用高斯平滑

//thresholdtype-thresh_binary,thresh_binary_inv

//blocksize-平滑運算元尺寸,奇數

//c-比例係數

opencv學習筆記 閾值分割

先選定乙個特定的閾值量,比如 127 新的閾值產生規則為 dst x,y maxval quad if quad src x,y thresh 0,otherwise end dst x,y 0 quad if quad src x,y thresh maxval,otherwise end dst...

Opencv學習筆記 4 基本閾值操作

閾值是什麼?簡單點說是把影象分割的標尺,這個標尺是根據什麼產生的,閾值產生演算法?閾值型別。binary segmentation 1.閾值二值化 threshold binary 左下方的圖表示影象畫素點src x,y 值分布情況,藍色水平線表示閾值 2.閾值反二值化 threshold bina...

opencv學習筆記十一 閾值操作

閾值操作型別有 thresh binary 0 二值化,大於閾值的為255,小於閾值的為0 thresh binary inv 1 反二值化,大於閾值的為0,小於閾值的為255 thresh trunc 2 截斷法,大於閾值的取閾值,小於閾值的不變 thresh tozero 3 大於閾值的不變,小...