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);
}
查看全文
相关阅读:
写给Web开发人员看的Nginx介绍
Nginx安装及配置详解
Intellij IDEA 热部署处理
JSP自定义标签就是如此简单
过滤器入门看这一篇就够了
Caffe FCN:可视化featureMaps和Weights(C++)、获取FCN结果
Caffe RPN :error C2220: warning treated as error
实例分割:MaskXRCnn 与Visual Genome数据集
Detectron-MaskRCnn: 用于抠图的FCNN
CaffeMFC:caffe.pb.h(2525): error C2059: syntax error : 'constant'
原文地址:https://www.cnblogs.com/wqj1212/p/1009655.html
最新文章
网页图片宽度和高度自适应的方法
canvas 画六边形
canvas 画六边形边框
css绘制六边形
前端工程师
一段token
关于金额的正则
vue-cli 结局图片打包路径错误问题
nginx的简单反向配置
element 表格 远程后台排序 sort-change
热门文章
element 表格中使用if语句,scope写法
VUE 获取窗口改变时的大小
vue element 分页页面初始请求两次的问题
拖拽的几个事件。
vue element 注册回车事件
Spring基础篇——DI/IOC和AOP原理初识
Spring基础篇——bean的自动化装配
volatile关键字
从jvm源码看synchronized
通俗化理解Spring3 IoC的原理和主要组件
Copyright © 2011-2022 走看看