给出上图直方图均衡化的过程和结果,并用程序检验。
)
给出上图直方图均衡化的过程和结果,并用程序检验。
int [][] a={{5,5,4,4,2,0,7}
,{5,5,4,2,0,0,7}
,{4,4,4,2,0,7,7}
,{1,3,3,0,7,7,6}
,{1,0,0,0,7,7,6}
,{7,7,7,7,7,7,6}
,{7,7,7,7,7,7,6} }
源代码
package math;
import java.util.*;
public class Main {
public static void main(String[] args) {
//声明Scanner对象,获取键盘输入值
Scanner sc = new Scanner(System.in);
/* int n = sc.nextInt();
int m = sc.nextInt();
//创建动态二维数组
int[][] ary = new int[n][];
for (int i = 0; i < n; i++){
ary[i] = new int[m];
}
//输出二维数组
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++)
ary[i][j]=sc.nextInt();
*//*System.out.print(ary[i][j]+" ");
System.out.println();*//*
}
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++)
// ary[i][j]=sc.nextInt();
System.out.print(ary[i][j]+" ");
System.out.println();
}*/
int[][] a={
{4,5,5,5,5,6,7},
{4,2,2,2,2,2,2},
{4,2,1,1,1,1,2},
{4,2,1,0,0,2,2},
{3,2,1,0,0,1,2},
{3,2,1,0,0,1,2},
{3,3,1,1,1,1,3}
};
/* int [][] a={{5,5,4,4,2,0,7}
,{5,5,4,2,0,0,7}
,{4,4,4,2,0,7,7}
,{1,3,3,0,7,7,6}
,{1,0,0,0,7,7,6}
,{7,7,7,7,7,7,6}
,{7,7,7,7,7,7,6} };*/
Table table[]=new Table[100];
for (int i=0;i<100;i++){
table[i]=new Table();
}
//初始化
table[0].grayScale=a[0][0];
int count=0;
for(int i=0;i<a.length;i++){
for (int j=0;j<a[0].length;j++){
int flag=0;
for (int k=0;k<count;k++){
if(table[k].grayScale==a[i][j]) {
flag = 1;
table[k].nk++;
}
}
//加入新的灰度等级
if(flag==0){
table[count].grayScale=a[i][j];
table[count].nk++;
count++;
}
}
}
for (int i=0;i<count-1;i++){
for (int j=0;j<count-1-i;j++){
if(table[j].grayScale>table[j+1].grayScale){
Table temp=table[j];
table[j]=table[j+1];
table[j+1]=temp;
}
}
}
int sum=0;
int mxn=a.length*a[0].length;
// System.out.println(count );
for (int i=0;i<count;i++){
sum+=table[i].nk;
table[i].sk=(float)sum/mxn;
table[i].rk=(float)table[i].grayScale/(count-1);
// System.out.println(table[i].sk+" "+table[i].rk);
}
for (int i=0;i<count;i++){
for (int j=0;j<count;j++){
table[j].f=Math.abs(table[i].sk-table[j].rk);
// System.out.println(table[j].f);
}
table[i].newGrayScale=table[getMin(count,table)].grayScale;
table[i].skb=table[getMin(count,table)].rk;
}
sum=0;
for (int i=0;i<count;i++){
sum+=table[i].nk;
System.out.println(table[i].grayScale+"\t"+table[i].grayScale+"/"+(count-1)+"\t "+table[i].nk+" "
+table[i].nk+"/"+mxn+" "+sum+"/"+mxn+" "+table[i].sk+" "+table[i].newGrayScale
);
}
print(table,count,a);
}
private static void print(Table[] table, int count, int[][] a) {
System.out.println("原矩阵");
for (int i=0;i<a.length;i++) {
for (int j = 0; j < a[0].length; j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("平衡化:");
for (int i=0;i<a.length;i++){
for (int j=0;j<a[0].length;j++){
a[i][j]=getGray(i,j,table,a,count);
}
}
for (int i=0;i<a.length;i++) {
for (int j = 0; j < a[0].length; j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
//老像素 转换成对应的 新像素
private static int getGray(int i, int j, Table[] table, int[][] a, int count) {
for (int k=0;k<count;k++){
if(a[i][j]==table[k].grayScale)return table[k].newGrayScale;
}
return 0;
}
private static int getMin(int count, Table[] table) {
float flag = 1;
int num=0;
for (int i=0;i<count;i++){
if(flag>table[i].f){
flag=table[i].f;
num=i;
}
}
return num;
}
}
class Table {
public int grayScale=-1;//灰度等级
public float sk; //大S计
public float f;
public float skb;
public float rk; //归一化rk
public int nk=0; //像素个数
public int newGrayScale=-1;//新灰度等级
public int newnk=0; //新像素个数
public Table() {
}
public Table(int grayScale, int nk, int newGrayScale, int newnk) {
this.grayScale = grayScale;
this.nk = nk;
this.newGrayScale = newGrayScale;
this.newnk = newnk;
}
}
试例数字图像矩阵的直方图均衡化运行结果:
0 0/7 6 6/49 6/49 0.12244898 1
1 1/7 13 13/49 19/49 0.3877551 3
2 2/7 15 15/49 34/49 0.6938776 5
3 3/7 5 5/49 39/49 0.79591835 6
4 4/7 4 4/49 43/49 0.877551 6
5 5/7 4 4/49 47/49 0.9591837 7
6 6/7 1 1/49 48/49 0.97959185 7
7 7/7 1 1/49 49/49 1.0 7
原矩阵
4 5 5 5 5 6 7
4 2 2 2 2 2 2
4 2 1 1 1 1 2
4 2 1 0 0 2 2
3 2 1 0 0 1 2
3 2 1 0 0 1 2
3 3 1 1 1 1 3
平衡化:
6 7 7 7 7 7 7
6 5 5 5 5 5 5
6 5 3 3 3 3 5
6 5 3 1 1 5 5
6 5 3 1 1 3 5
6 5 3 1 1 3 5
6 6 3 3 3 3 6
图中直方图均衡化运行结果:
0 0/7 8 8/49 8/49 0.1632653 1
1 1/7 2 2/49 10/49 0.20408164 1
2 2/7 3 3/49 13/49 0.26530612 2
3 3/7 2 2/49 15/49 0.30612245 2
4 4/7 6 6/49 21/49 0.42857143 3
5 5/7 4 4/49 25/49 0.5102041 4
6 6/7 4 4/49 29/49 0.59183675 4
7 7/7 20 20/49 49/49 1.0 7
原矩阵
5 5 4 4 2 0 7
5 5 4 2 0 0 7
4 4 4 2 0 7 7
1 3 3 0 7 7 6
1 0 0 0 7 7 6
7 7 7 7 7 7 6
7 7 7 7 7 7 6
平衡化:
4 4 3 3 2 1 7
4 4 3 2 1 1 7
3 3 3 2 1 7 7
1 2 2 1 7 7 4
1 1 1 1 7 7 4
7 7 7 7 7 7 4
7 7 7 7 7 7 4