在路上
后劲十足
BlogJava
联系
聚合
管理
0 Posts :: 1 Stories :: 0 Comments :: 0 Trackbacks
留言簿
给我留言
查看公开留言
查看私人留言
文章档案
2009年5月 (1)
搜索
最新评论
自己用java写的自适应中值滤波器,没有用的模式,写的不是很好,只是模拟
/**/
/*
school:jxust.cn
author:bravekingzhang;
time;2009.5.14
environment:JDK1.60
*/
import
java.util.Random;
class
fastsort
{
void
quickSort(
int
a[],
int
p,
int
r)
{
if
(p
<
r)
{
int
q
=
partition(a,p,r);
quickSort(a,p,q
-
1
);
//
对左半段排序
quickSort(a,q
+
1
,r);
//
对右半段排序
}
}
int
partition(
int
a[],
int
p,
int
r)
{
int
i
=
p,j
=
r
+
1
;
int
x
=
a[p];
//
将<X的元素放在X的左边
//
将>X的元素放在X的右边
while
(
true
)
{
while
(a[
++
i]
<
x
&&
i
<
r);
//
i++;
while
(a[
--
j]
>
x);
//
j--;
if
(i
>=
j)
break
;
swap(a,i,j);
}
a[p]
=
a[j];
a[j]
=
x;
return
j;
}
void
swap(
int
a[],
int
i,
int
j)
{
int
x;
x
=
a[i];
a[i]
=
a[j];
a[j]
=
x;
}
int
Randomizedpartition(
int
a[],
int
p,
int
r)
//
随机快速排序
{
newrandom rad
=
new
newrandom();
int
h
=
a.length
-
1
;
int
i
=
rad.newNextInt(p,r);
//
System.out.println(i);
swap(a,i,p);
return
partition(a,p,r);
}
void
randomizedquicksort(
int
a[],
int
p,
int
r)
{
if
(p
<
r)
{
int
q
=
Randomizedpartition(a,p,r);
randomizedquicksort(a,p,q
-
1
);
randomizedquicksort(a,q
+
1
,r);
}
}
}
class
newrandom
extends
Random
//
取得一个随机数,降低快速排序算发出现最坏情况的概率
{
public
int
newNextInt(
int
j,
int
k)
{
while
(
true
)
{
Random rd
=
new
Random();
int
i1
=
rd.nextInt(k
+
1
);
int
i2
=
rd.nextInt(j
+
1
);
int
i
=
i1
-
i2;
if
(j
<=
i
&&
i
>=
k)
return
i;
}
}
}
public
class
SelfAdaptmiddlefilter
{
public
static
void
main(String args[])
{
int
a[]
=
new
int
[
9
];
//
滤波算子为3*3大小
int
b[]
=
new
int
[
25
];
//
滤波算子为5*5大小
int
c[]
=
new
int
[
49
];
//
滤波算子为7*7大小
fastsort fs
=
new
fastsort();
//
实例化一个快速排序对象
int
oringin[][]
=
{
{
2
,
3
,
4
,
2
,
3
,
4
,
4
,
5
}
,
{
2
,
3
,
4
,
2
,
3
,
4
,
4
,
5
}
,
{
2
,
3
,
4
,
2
,
3
,
4
,
4
,
5
}
,
{
2
,
3
,
4
,
2
,
3
,
4
,
4
,
5
}
,
{
2
,
3
,
4
,
2
,
3
,
4
,
4
,
5
}
,
{
2
,
3
,
4
,
2
,
3
,
4
,
4
,
5
}
,
{
2
,
3
,
4
,
2
,
3
,
4
,
4
,
5
}
,
{
2
,
3
,
4
,
2
,
3
,
4
,
4
,
5
}
,
{
2
,
3
,
4
,
2
,
3
,
4
,
4
,
5
}
,
{
2
,
3
,
4
,
2
,
3
,
4
,
4
,
5
}
,
{
2
,
3
,
4
,
2
,
3
,
4
,
4
,
5
}
,
{
2
,
3
,
4
,
2
,
3
,
4
,
4
,
5
}
,
{
2
,
3
,
4
,
2
,
3
,
4
,
4
,
5
}
,
{
2
,
3
,
4
,
2
,
3
,
4
,
4
,
5
}
,
{
2
,
3
,
4
,
2
,
3
,
4
,
4
,
5
}
,
{
2
,
3
,
4
,
2
,
3
,
4
,
4
,
5
}
,
{
2
,
3
,
4
,
2
,
3
,
4
,
4
,
5
}
,
{
2
,
3
,
4
,
2
,
3
,
4
,
4
,
5
}
}
;
int
m,n;
m
=
oringin.length;
n
=
oringin[
0
].length;
int
arry[][]
=
new
int
[m][n];
int
zmin,zmax,zmed,zxy,smax
=
1
;
for
(
int
i
=
0
;i
<
m;i
++
)
//
对每个像素进行处理
{
for
(
int
j
=
0
;j
<
n;j
++
)
{
zxy
=
oringin[i][j];
smax
=
1
;
while
(smax
<
4
)
{
if
(smax
==
1
)
//
初始化一个大小为3*3的滤波器算子
{
int
index
=
0
;
for
(
int
chushi
=-
1
;chushi
<=
1
;chushi
++
)
//
对与一个边界临界问题,如果超出了数组范围,让它取0
{
for
(
int
chushi2
=-
1
;chushi2
<=-
1
;chushi2
++
)
{
if
((i
+
chushi)
<
0
||
(j
+
chushi2)
<
0
||
(i
+
chushi
>
m
-
1
)
||
(j
+
chushi2
>
n
-
1
))
{
a[index]
=
0
;
index
++
;
}
else
//
否则,如果没有越界就取到这个值。
{
a[index]
=
oringin[i
+
chushi][j
+
chushi2];
index
++
;
}
}
}
fs.randomizedquicksort(a,
0
,
8
);
zmin
=
a[
0
];
zmax
=
a[
8
];
zmed
=
a[
4
];
int
A1,A2;
A1
=
zmed
-
zmin;
A2
=
zmed
-
zmax;
if
(A1
>
0
&&
A2
<
0
)
{
int
B1
=
zxy
-
zmin;
int
B2
=
zxy
-
zmax;
if
(B1
>
0
&&
B2
<
0
)
{
arry[i][j]
=
zxy;
smax
=
4
;
}
arry[i][j]
=
zmed;
//
to do
smax
=
4
;
}
else
smax
++
;
}
if
(smax
==
2
)
{
int
index
=
0
;
for
(
int
chushi
=-
2
;chushi
<=
2
;chushi
++
)
{
for
(
int
chushi2
=-
2
;chushi2
<=
2
;chushi2
++
)
{
if
((i
+
chushi)
<
0
||
(j
+
chushi2)
<
0
||
(i
+
chushi
>
m
-
1
)
||
(j
+
chushi2
>
n
-
1
))
{
b[index]
=
0
;
index
++
;
}
else
{
b[index]
=
oringin[i
+
chushi][j
+
chushi2];
index
++
;
}
}
}
fs.randomizedquicksort(b,
0
,
24
);
zmin
=
b[
0
];
zmax
=
b[
24
];
zmed
=
b[
12
];
zxy
=
oringin[i][j];
int
A1,A2;
A1
=
zmed
-
zmin;
A2
=
zmed
-
zmax;
if
(A1
>
0
&&
A2
<
0
)
{
int
B1
=
zxy
-
zmin;
int
B2
=
zxy
-
zmax;
if
(B1
>
0
&&
B2
<
0
)
{
arry[i][j]
=
zxy;
smax
=
4
;
}
arry[i][j]
=
zmed;
//
todo
smax
=
4
;
}
else
smax
++
;
}
if
(smax
==
3
)
{
int
index
=
0
;
for
(
int
chushi
=-
3
;chushi
<=
3
;chushi
++
)
{
for
(
int
chushi2
=-
3
;chushi2
<=
3
;chushi2
++
)
{
if
((i
+
chushi)
<
0
||
(j
+
chushi2)
<
0
||
(i
+
chushi
>
m
-
1
)
||
(j
+
chushi2
>
n
-
1
))
{
c[index]
=
0
;
index
++
;
}
else
{
c[index]
=
oringin[i
+
chushi][j
+
chushi2];
index
++
;
}
}
}
fs.randomizedquicksort(c,
0
,
48
);
zmin
=
c[
0
];
zmax
=
c[
48
];
zmed
=
c[
24
];
zxy
=
oringin[i][j];
int
A1,A2;
A1
=
zmed
-
zmin;
A2
=
zmed
-
zmax;
if
(A1
>
0
&&
A2
<
0
)
{
int
B1
=
zxy
-
zmin;
int
B2
=
zxy
-
zmax;
if
(B1
>
0
&&
B2
<
0
)
{
arry[i][j]
=
zxy;
}
else
arry[i][j]
=
zmed;
//
todo
smax
=
4
;
}
arry[i][j]
=
zxy;
smax
=
4
;
//
让while循环结束
}
}
}
}
for
(
int
i
=
0
;i
<
m;i
++
)
{
for
(
int
j
=
0
;j
<
n;j
++
)
{
System.out.print(arry[i][j]
+
"
"
);
}
System.out.println(
"
"
);
}
//
System.out.println(n);
//
for(int i=0;i<a.length;i++)
//
System.out.println(a[i]);
//
for(int i=0;i<b.length;i++)
//
System.out.println(b[i]);
//
for(int i=0;i<c.length;i++)
//
System.out.println(c[i]);
//
测试用的,对程序没有影响,可以查看
}
}
posted on 2009-05-19 18:54
braveking
阅读(801)
评论(0)
编辑
收藏
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
Chat2DB
C++博客
博问
管理
Powered by:
BlogJava
Copyright © braveking