0% found this document useful (0 votes)
5 views

Lec 1,2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lec 1,2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ReadImage.

java

1
2 public class ReadImage {
3
4 public static void main(String[] args) {
5 int pixels[][]={
6 {0,0,0,75,75,75,128,128,128,128,128},
7 {0,75,75,75,128,128,128,128,255,255,255},
8 {75,75,75,200,200,200,255,255,255,200},
9 {128,128,128,200,200,255,255,200,200,200},
10 {128,128,128,255,255,200,200,200,75,75},
11 {175,175,175,255,255,255,75,75,75,100},
12 {175,175,100,100,100,255,255,75,75,100},
13 {75,75,75,35,35,35,0,0,0,35},
14 {35,35,35,0,0,0,35,35,35,75},
15 {35,35,35,0,0,0,35,35,35,75},
16 {75,75,75,100,100,100,200,200,200,200}
17 };// end image
18 Image m1 = new Image(1024,1024,256); // define object of class image and set
Resolution
19 m1.setMatrix(pixels, 10, 10); // read image and its coordinate
20 m1.changeValue(5, 2, 250); // change in gray value
21 m1.printMatrix(); // print pixels of matrix
22 System.out.println();
23 System.out.println(">> The Maximum value is : "+m1.getMaximum());
24 System.out.println(">> The Minimum value is : "+m1.getMinimum());
25 System.out.println(">> The Mean is : "+m1.getMean());
26 System.out.println(">> The Standard Division is : "+m1.getSTDivision());
27 System.out.println(">> The Resolution of Image is : "+m1.getResolution()/
(1024*1024*8)+" MB");
28 System.out.println(">> The Histogram : ");
29 System.out.println("----------------");
30 m1.printHistogram();
31
32
33 }// end main
34
35 }// end class
36

Page 1
Image.java

1 import java.lang.*;
2 public class Image {
3 private int rows ,cols;
4 private int pixels[] [];
5 int M,N,q;
6 private int hists[]= new int [256]; // Gray Level Array has 256 Values
7
8 public Image(int M, int N, int q){
9
10 if(M >0 && N >0 && q>0){
11 this.M=M;
12 this.N=N;
13 this.q=q;
14 }
15 else{
16 this.M=0;
17 this.N=0;
18 this.q=0;
19 }
20 }// end constructor
21
22 public void setMatrix(int pixels[][] ,int rows , int cols ){
23 if(rows>0 && cols>0){
24 this.rows=rows; // pass the rows_value to this.rows
25 this.cols=cols;// pass the cols_value to this.cols
26 this.pixels=pixels; // pass the pixels [][] to this.pixels
27 }// end condition
28 }// end set Matrix
29
30 public void changeValue(int rLoc , int cLoc, int q_Value){
31 if(rLoc>-1 && rLoc<rows&&cLoc>-1 && cLoc<cols ){//check correct range of index
32 if(q_Value>=0 && q_Value<=255){//check the correct value of quantization
33 int old_Value=pixels[rLoc][cLoc];
34 pixels[rLoc][cLoc]=q_Value;
35 System.out.printf(">> The Pixel [%d][%d] was changed from %d to
%d",rLoc,cLoc,old_Value,q_Value);
36 System.out.println();
37 }// end check the correct value of quantization
38 else{
39 System.out.println("!!! Enter Correct Value the range (0-255)");
40 }
41 }// end check correct range of index
42
43 else
44 System.out.println("!!! Check your index please");
45 }// end changeMatrixValues
46
47 public void printMatrix(){
48 System.out.println();
49 for (int i=0; i<rows;i++){
50 for(int j=0;j<cols;j++){
51 if(this.pixels[i][j]<9) // Check if Ones nums
52 System.out.print(" "); // margin Ones numbers
53 else if(this.pixels[i][j]<100)//Check if Tens nums
54 System.out.print(" "); // margin Tens numbers
55 else // Check if Hundreds nums
56 System.out.print(" "); // margin Hundreds numbers
57 System.out.print(this.pixels[i][j]);
58 }// end columns (n)
59 System.out.println();
60 }// end rows (M)

Page 1
Image.java

61 }// end printImage


62
63 public int getMaximum(){
64 int max=0;
65 for (int i=0;i<rows;i++){
66 for(int j=0;j<cols;j++){
67 if(pixels[i][j]>max)
68 max=pixels[i][j];
69 }// end columns
70 }// end rows
71 return max;
72 }// end get maximum value
73
74 public int getMinimum(){
84
85 public int getMean(){// get the average of Image Values
94
95 public int getSTDivision(){
96 int SD=0;
97 for(int i=0; i<rows ; i++){
98 for(int j=0;j<cols;j++){
99 SD+=Math.pow(pixels[i][j]-getMean(),2);
100 }// end cols
101 }// end rows
102 return (int) Math.sqrt(SD/(rows*cols));
103
104
105 }// end getStrDivision
106
107 private int getQuntisation(){
108 return ((int)(Math.log(this.q)/Math.log(2)));
109 }// end getQuntisation
110
111 public int getResolution(){
112 return(this.M*this.N*getQuntisation());
113 }//end getResolution
114
115 private void setHistogram(){
116 int grayValue= 0;
117 for(int i =0;i< rows;i++){
118 for(int j=0; j <cols;j++){
119 grayValue=pixels[i][j];
120 hists[grayValue]++;
121 }// end cols
122 }// end rows
123 }// end setHistogram
124
125 public void printHistogram(){
126 setHistogram(); // call setHistogram
127 System.out.println("Gray\t| Count");
128 for (int i=0; i<256;i++){
129 if(i>0&& hists[i] ==0)continue;
130 System.out.println(i + "\t| "+hists[i]);
131 }// end print histogram
132
133 }// end print Histogram
134 }// end class
135

Page 2

You might also like