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);
}
查看全文
相关阅读:
补题列表
task list
UVa 11809
UVA 272 TEX Quotes 题解
莱州一中2016高考加油视频
POJ2367-Genealogical tree-拓扑排序
POJ1094-Sorting It All Out-拓扑排序
POJ3660-Permutations-传递闭包FLOYD
POJ3687- Labeling Balls-优先队列拓扑排序
POJ1201-Intervals- 差分约束
原文地址:https://www.cnblogs.com/wqj1212/p/1009655.html
最新文章
证明一下 三角和 公式
我画了一个 数学 的 架构图
做一道 初三题
用 微积分 做 一道 小学生 寒假作业 题
推导 一个 圆锥 的 体积
微分几何 随便一说
数学库 图形库 机器学习库 随想
球面几何 和 张量 在 物理学 中 毫无意义
和 aa 老师 关于 0.9999…… = 1 的 一段 对话
大神们 看看 这个 微分方程 怎么解
热门文章
自适应页面
sql 操作,
工作十年 为何只拿毕业三年的工资
2014.春
json 解析
row_number()over(order by id) SQL顺序排列
让浏览器滚动条居中
根据不同的分辨率调用不同的css
DIV的垂直居中
鼠标移上,移开的另一种写法
Copyright © 2011-2022 走看看