zoukankan
html css js c++ java
OTSU求阈值程序
writen by
wqj1212@yahoo.com.cn
int
otsu (IplImage
*
image,
int
rows,
int
cols,
int
x0,
int
y0,
int
dx,
int
dy,
int
vvv)
{
unsigned
char
*
np;
//
图像指针
int
thresholdValue
=
1
;
//
阈值
int
ihist[
256
];
//
图像直方图,256个点
int
i, j, k;
//
various counters
int
n, n1, n2, gmin, gmax;
double
m1, m2, sum, csum, fmax, sb;
//
对直方图置零
memset(ihist,
0
,
sizeof
(ihist));
gmin
=
255
; gmax
=
0
;
//
生成直方图
/**/
/*
for (i = y0 + 1; i < y0 + dy - 1; i++) {
np = &image[i*cols+x0+1];
for (j = x0 + 1; j < x0 + dx - 1; j++) {
ihist[*np]++;
if(*np > gmax) gmax=*np;
if(*np < gmin) gmin=*np;
np++; /* next pixel
}
}
*/
for
(j
=
y0;j
<
dy;j
++
)
{
for
(i
=
0
;i
<
dx;i
++
)
{
unsigned
char
temp
=
CV_IMAGE_ELEM(image,uchar,j,i);
ihist[temp]
++
;
}
}
//
set up everything
sum
=
csum
=
0.0
;
n
=
0
;
for
(k
=
0
; k
<=
255
; k
++
)
{
sum
+=
(
double
) k
*
(
double
) ihist[k];
/**/
/*
x*f(x) 质量矩
*/
n
+=
ihist[k];
/**/
/*
f(x) 质量
*/
}
if
(
!
n)
{
//
if n has no value, there is problems
fprintf (stderr,
"
NOT NORMAL thresholdValue = 160\n
"
);
return
(
160
);
}
//
do the otsu global thresholding method
fmax
=
-
1.0
;
n1
=
0
;
for
(k
=
0
; k
<
255
; k
++
)
{
n1
+=
ihist[k];
if
(
!
n1)
{
continue
; }
n2
=
n
-
n1;
if
(n2
==
0
)
{
break
; }
csum
+=
(
double
) k
*
ihist[k];
m1
=
csum
/
n1;
m2
=
(sum
-
csum)
/
n2;
sb
=
(
double
) n1
*
(
double
) n2
*
(m1
-
m2)
*
(m1
-
m2);
/**/
/*
bbg: note: can be optimized.
*/
if
(sb
>
fmax)
{
fmax
=
sb;
thresholdValue
=
k;
}
}
//
at this point we have our thresholding value
//
debug code to display thresholding values
if
( vvv
&
1
)
fprintf(stderr,
"
# OTSU: thresholdValue = %d gmin=%d gmax=%d\n
"
,thresholdValue, gmin, gmax);
return
(thresholdValue);
}
查看全文
相关阅读:
[函數] Firemonkey Android 取得系统参数设定的字型大小
[示例] 访问类的私有属性
[修正] 移动平台曲线不平滑的问题(如:TRectangle, TPath...等)
[修正] Firemonkey TSelection 控件等比缩放时,左下角拉动问题
[修正] iOS 10 使用相机及相簿闪退的问题修正
报表之表头
报表字段刷新
tfs解除锁
sql server中排名的问题
TFS查询无法在Excel中打开
原文地址:https://www.cnblogs.com/wqj1212/p/1009655.html
最新文章
[LeetCode in Python] 76 (H) minimum window substring 最小覆盖子串
[LeetCode in Python] 692 (M) top k frequent words 前K个高频单词
[LeetCode in Python] 224 (H) basic calculator 基本计算器
[LeetCode in Python] 17 (M) letter combinations of a phone number 电话号码的字母组合
[LeetCode in Python] 200 (M) number of islands 岛屿数量
[笔记] numpy保存文件的耗时记录
[笔记] 将numpy的操作转移到pytorch的tensor上运行可以加速
c4d 帮助 prime r16 usage
maya 2014帮助手册中 三维概念讲解
c4d 宝典部分二
热门文章
圆 错题
LaTeX新人教程,30分钟从完全陌生到基本入门[转载]
matlab 画图进阶
matlab 初级画图
cinema 4d 包括宝典 --- 改线 循环边 建模布线原则
[修正] Firemonkey 中英文混排折行,省略字符,首字避开标点
[示例] Firemonkey OnTouch 多点触控应用
[修正] Firemonkey Android 显示 Emoji (颜文字)
[函数] Firemonkey Windows 重新计算 Font Baseline
[上架] iOS "app-specific password" 上架问题
Copyright © 2011-2022 走看看