-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubImage.java
More file actions
119 lines (96 loc) · 2.76 KB
/
Copy pathSubImage.java
File metadata and controls
119 lines (96 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.awt.image.*;
import javax.swing.*;
import java.awt.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
/**
* @Jonathan Ke
* @9/13/2019
*
* Creates a subImage object with average rgb for the image
*/
public class SubImage {
private BufferedImage img;
//colors
private int r;
private int g;
private int b;
private int index;
public SubImage(BufferedImage img, int i){
this.img = img;
index = i;
//find average of each pixel
int r = 0, g = 0, b = 0;
for (int x = 0; x < img.getWidth(); x++){
for (int y = 0; y < img.getHeight(); y++){
r += findR(x,y);
g += findG(x,y);
b += findB(x,y);
}
}
this.r = (r/(img.getWidth()*img.getHeight()));
this.g = (g/(img.getWidth()*img.getHeight()));
this.b = (b/(img.getWidth()*img.getHeight()));
}
private int findR(int x,int y){
int pixel = img.getRGB(x,y);
return (pixel >> 16) & 0xff;
}
private int findB(int x, int y){
int pixel = img.getRGB(x,y);
return (pixel) & 0xff;
}
private int findG(int x, int y){
int pixel = img.getRGB(x,y);
return (pixel >> 8) & 0xff;
}
public BufferedImage getImage(){
return img;
}
public int getRed(){
return r;
}
public int getGreen(){
return g;
}
public int getBlue(){
return b;
}
public void setRGB(int newRed, int newGreen, int newBlue){
r = newRed;
g = newGreen;
b = newBlue;
}
//finds the distance between r,g,b for this image and another
public int getDistance(int red,int green,int blue){
int rs = red-r;
int gs = green-g;
int bs = blue-b;
return rs*rs+gs*gs+bs*bs;
}
//implementation of v2 distance function for comparision
public void reweight(int red, int green, int blue, int k, int out){
r = yangFunc(r,red,k,out);
g = yangFunc(g,green,k,out);
b = yangFunc(b,blue,k,out);
}
//Yangs special function for magical shit
public static int yangFunc(int x, int x1, int k, int d){
d = (int) Math.sqrt(d);
return (int)(x + (k-d)/d*(x-x1));
}
public int getIndex(){
return index;
}
public static void main(String[] args){
SubImage test = new SubImage(LargeImage.loadImage("black.jpg"));
JFrame f = new JFrame("SUBIMAGE");
JLabel l = new JLabel(new ImageIcon(test.getImage()));
f.add(l);
f.pack();
f.setFocusable(true);
f.setVisible(true);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}