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);
}
查看全文
相关阅读:
UVALive 4730 Kingdom +段树和支票托收
zoj 3537 Cake (凸包确定+间隔dp)
SVN在branch兼并和游戏patch(1)
拍卖倒计时
java基金会成立
HDU1532 Drainage Ditches 【最大流量】
leetcode-WordLadder
Andriod Studio科普文章——3.大约gradle常见问题插头
android Graphics(三):区域(Range)
android Graphics(二):路径及文字
原文地址:https://www.cnblogs.com/wqj1212/p/1009655.html
最新文章
TRILL浅析
中国大概能用的NTPserver地址
HTML里面Textarea换行总结
SharePoint 2013 搜索SharePoint 特定列和特定文档(自己定义搜索)
WebView无法放大缩小解决方式
Android Wear 开发入门——怎样创建一个手机与可穿戴设备关联的通知(Notification)
生成真正的随机数!
题目1380:lucky number
php课程 8-32 如何使用gd库进行图片裁剪和缩放
thinkphp3.1课程 1-2 thinkphp中入口文件的实质是什么
热门文章
php实现 查找输入整数二进制中1的个数
js进阶 12 jquery事件汇总
js进阶 12-14 jquery的事件触发函数是哪两个
js课程 4-11 表格如何实现隔行换色
js进阶 12-13 jquery中one方法和trigger方法如何使用
bootstrap课程4 bootstrap的css样式有哪些内容需要注意
前端可编辑表格插件有哪些
thinkphp5最最最最简单的ajax实例
spring框架内置笔记本
hdu 4975 最大流问题解决队伍和矩阵,利用矩阵dp优化
Copyright © 2011-2022 走看看