1 均值法2 otsu
3 三角法
#include
#include
using
namespace cv;
using
namespace std;
intmain
(int argc,
char
** ar**)
namedwindow
("image"
, window_freeratio)
;imshow
("image"
, src)
;mat gray, binary;
cvtcolor
(src, gray, color_bgr2gray)
;imshow
("gray"
, gray)
;// 1.均值法
scalar m =
mean
(gray)
;printf
("means:%0.2f\n"
, m[0]
);threshold
(gray, binary, m[0]
,255
, thresh_binary)
;imshow
("mean"
, binary)
;// 2.otsu
double t1 =
threshold
(gray, binary,0,
255, thresh_binary | thresh_otsu)
;printf
("ostu:%0.2f\n"
, t1)
;imshow
("thresh_otsu"
, binary)
;// 3.三角法
double t2 =
threshold
(gray, binary,0,
255, thresh_binary | thresh_********)
;printf
("********:%0.2f\n"
, t2)
;imshow
("thresh_********"
, binary)
;waitkey(0
);destroyallwindows()
;return0;
}
全域性閾值的侷限性,對光照度不均勻的影象容易錯誤的二值化分割盒子模糊影象 - d
原圖 + 加上偏置常量c得 - s
t = s - d + 255
分割的好壞取決於模糊視窗block大小和常量c
#include
#include
using
namespace cv;
using
namespace std;
intmain
(int argc,
char
** ar**)
namedwindow
("src"
, window_autosize)
;imshow
("src"
, src);
mat gray, binary;
cvtcolor
(src, gray, color_bgr2gray)
;// 自適應閾值分割
;// 自適應閾值分割
基於邊緣改進的全域性閾值 opencv
99.97 時候的二值圖 為了視覺化,灰度值範圍設定為0 255 90 的二值圖 核心演算法 void edge threshold mat src uchar d data dst.data for int j 0 j dst.rows j long sum 0,amount 0 double p...
UILabel和UITextView自適應高度
uilabel自動換行,自適應高度 uilabel label uilabel alloc initwithframe cgrectzero label setbackgroundcolor uicolor clearcolor label setfont uifont systemfontofsi...
在OpenCV裡實現全域性閾值分割3
接著下來看thresh trunc型別,這個型別與前面兩個型別的區別在於最大值不起作用,而是讓大於閾值的畫素值全部等於閾值。演示例子如下 python 3.7.4,opencv4.1 蔡軍生 import cv2 import numpy as np 影象資料 src np.array 100,15...