Wednesday, December 8, 2010

generateButtons

private void generateButtons(){
circle.setBounds(buttonWidth, screenHeight, buttonHeight, buttonHeight);
circle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
setInputImage("/Users/kyla/Desktop/circle_jpg.jpg");
}
});
square.setBounds(buttonWidth*2, screenHeight, buttonWidth, buttonHeight);
square.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
setInputImage("/Users/kyla/Desktop/anothercircle.jpg");
}
});
triangle.setBounds(buttonWidth*3, screenHeight, buttonWidth, buttonHeight);
triangle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
setInputImage("/Users/kyla/Desktop/shapes.jpg");
}
});
add(circle);
add(square);
add(triangle);
}


Part 2  <--- Back to Part 2

(3) SetInputImage(String file)

public void setInputImage(String file){  file name is set as a parameter 
try {
inputSourceImage = ImageIO.read(new File(file)); Tries to read image
}
catch (Exception e) {
e.printStackTrace();
}
wpixel = inputSourceImage.getWidth(null); //num of pixels  
sets pixel width to width of image
hpixel = inputSourceImage.getHeight(null); //num of pixels
sets pixel height to hight of image

screenWidth = wpixel*mag;
screenHeight = (hpixel*mag + buttonHeight + titleHeight);
setSize(screenWidth*2+space, screenHeight+100);
graphStartX = screenWidth + space; //maybe *mag or (screenWidth+space);
i=0;
edgecounter =0;

totaldata = new int[(wpixel*hpixel)*4]; set length total data
edgepoints = new int[(wpixel*hpixel)*4]; set length edge points
processedImage = pixelArray(); processedImage[][] = pixelArray()

             ~~~~~~~~~~~~~~ PixelArray() <-- CLICK! ~~~~~~~~~~~~~~~
FindEdge(processedImage);

            ~~~~~~~~~~~~~~ FindEdge() <-- CLICK! ~~~~~~~~~~~~~~

//printPoints(edgepoints); THIS IS CALLED IN FINDEDGE()
//printDerivative(totaldata); THIS IS CALLED IN FINDEDGE()
//System.out.println("totaldatalength" + totaldata.length);
//System.out.println("w: " + wpixel);
//System.out.println("h: " + hpixel);
efficiency = findEfficiency(totaldata);
 findEfficiency()






System.out.println("E:" + efficiency + "%");

}

(5) FindEdge()

public void FindEdge(int[][] list){ //list of whole image (may include noise)
// System.out.println("listSizeFindEdge(): " + list.length);
//each list of Z values in one row of Y need a derivative.

// System.out.println("YDERIVATIVE...");
for (int y = 0; ylength;y++){
yDerivativeList = new int[list.length];
for (int x=0;xlength;x++){
yDerivativeList[derivativeCounter] = list[y][x];
derivativeCounter++;
}
YDerivative(yDerivativeList,y);
derivativeCounter = 0;
}

// System.out.println("XDERIVATIVE...");
derivativeCounter = 0;
for (int x = 0; xlength;x++){
xDerivativeList = new int[list.length];
for (int y=0;ylength;y++){
xDerivativeList[derivativeCounter] = list[y][x];
derivativeCounter++;
}
XDerivative(xDerivativeList,x);
derivativeCounter = 0;
}

printPoints(edgepoints);
printDerivative(totaldata);



}

Part 3  <--- Back to Part 3

(4) PixelArray()

public int[][] pixelArray() {
rgbs = new int[wpixel*hpixel];
// System.out.println("rgbs length: " + rgbs.length);
int[][] pixelArrayData = new int[wpixel][hpixel];
int colorcounter = 0;

/* 24 bit color
16777216 = 2^24

16 bit color
65536 = 2^16

(2^24)/(2^24) = 1bit --> 0 or 1
(2^24) / (2^16) = 2^8bits --> 0-225
*/

inputSourceImage.getRGB(0, 0, wpixel, hpixel, rgbs, 0, wpixel);
for (int r=0;r<wpixel;r++) {
for (int c=0;c<hpixel;c++) {
pixelArrayData[r][c] = -(rgbs[colorcounter])/65536; //greyscale
colorcounter++;
// System.out.print(pixelArrayData[r][c]+ " ");
}
// System.out.println();
}
if (noiseOn){
pixelArrayData = addNoise(pixelArrayData,percentNoise);
}

/* System.out.println("WITH NOISE");

for (int r=0;r
for (int c=0;c
// System.out.print(pixelArrayData[r][c] + " ");
}
// System.out.println();
}*/

return pixelArrayData;
}

Part 3  <-- Back to Part 3

(1) OpenJFrame

JFRAME:


import javax.swing.JFrame;


public class FinalScienceProject extends JFrame {

    public FinalScienceProject() {
    add(new FinalScienceProjectPart2());

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500*2, 650); (MAY CHANGE LATER --> SET TO DEFALT)
        setLocationRelativeTo(null);
        setTitle("Science Project");
        setResizable(true);
        setVisible(true);
    }
public static void main(String[] argv){
FinalScienceProject i = new FinalScienceProject();
}

}

Part2 --> FinalScienceProjectPart2()


(2) JPanel

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JPanel;

public class FinalScienceProjectPart2 extends JPanel {
BufferedImage inputSourceImage; JPG IMAGE (100*100)
Random generator = new Random(); RANDOM NUMBER GENERATOR
int[][] processedImage;2-D Array, holds color values of image pixels
int[] edgepoints, rgbs; list of edge points, list of values of color
int[] totaldata, xDerivativeList,yDerivativeList; //derivatives 
       List of total derivative data, list of X Derivatives, list of Y Derivatives
int wpixel, hpixel, average, numofaverages
- Width # of pixels in image
- Height # of pixels in image
- Average of averages of line of derivatives
- number of slope averages
int i = 0; Counts "totaldata"
int derivativeCounter = 0; Counts "XDerivativeList" and "YDerivativeList"
int edgecounter = 0; Counts "edgepoints"
double efficiency; Percent Efficiency (signal to noise ratio)

double percentNoise = 10; Noise value, usually out of 256
// Noise value. Total value is usually 256.

int edgeColor = 255; Color that the edges will be outlined in
double maxpercent = .80; All numbers above 80% of max, are an edge
Boolean noiseOn = true; Tells to add noise or not


//SCREEN SETTINGS

int titleHeight=20, buttonHeight=30, screenWidth, screenHeight, graphStartX, space=20, mag=5,buttonWidth = 12, PAD = 10;


1. height of title bar
2. height required for buttons
3. Width of Screen
4. Height of Screen
5. X coordinate in which the graph starts at
6. certain amount of space added
7. magnifies the image by 5
8. width of buttons
9. space in between graph

JButton circle = new JButton("Circle"); Button to show circle
JButton square = new JButton("Square"); Button to show square
JButton triangle = new JButton("Triangle"); Button to show triangle

public FinalScienceProjectPart2(){ 
setFocusable(true);
  setBackground(color.WHITE);
setDoubleBuffered(true);
setVisible(true);
generateButtons();
~~~~~~~~~~~~~~~~~~~GENERAGEBUTTONS() <-- CLICK HERE~~~~~~~~~~~~~~~
setInputImage("/Users/kyla/Desktop/circle_jpg.jpg");
}




Part 3 <-- PART 3!

Tuesday, December 7, 2010

Program Details

SETINPUTIMAGE --> wpixel, hpixel, totaldata[(wpixel*hpixel)*2], edgepoints[{wpixel*hpixel)*4]

processedImage --> PixelArray() --> rgbs[wpixel*hpixel], pixelArrayData[wpixel][hpixel] (get value of each pixel color in the image. Print out the Array. 

If "noiseOn" is "true" addNoise() --->   for each pixel in the image, generate a random double from 0-1. With the random number subtract .5 from it. Then multiply it by 256*the percent value
The color = the color of the pixel. If the color + the random number is greater than 256, then set the pixel to 256.
If the color - the random number is less than 0, then set the pixel to 0
else set the pixel to its original color + the random number;   return the image with noise  

print again with noise. return pixelArrayData………………. processedImage = pixelArrayData (with or without noise)

FindEdge(processedImage) --> 
  • find y derivative: in each Y row, take each X color value and add it to yDerivativeLIst and add 1 to the DerivativeCounter. Once all X values are filled, take the YDerivative of the list and that y value
    • slope[list.length], counter=0, sum = 0;
    • Add to the slope, for each number in the list, the absolute value of list[y] - list [y-1], add 1 to the counter
    • Find the sum, the average, and the max
    • for each number in the slope list, if the number is greater than the max*maxpercet, change the pixel to blue, and add the x and y value to the edgepoints[] add 1 to the edgecounter.
    • add the slope to the totaldata;
  • Find x derivative: in each X row, take each Y color value and add it to xDerivativeLIst and add 1 to the DerivativeCounter. Once all Y values are filled, take the XDerivative of the list and that x value
    • slope[list.length], counter=0, sum = 0;
    • Add to the slope, for each number in the list, the absolute value of list[x] - list [x-1], add 1 to the counter
    • Find the sum, the average, and the max
    • for each number in the slope list, if the number is greater than the max*maxpercet, change the pixel to blue, and add the x and y value to the edgepoints[] add 1 to the edgecounter.
    • add the slope to the totaldata;
  • Print points of the edge points
  • Print list of derivatives
  • Find Efficiency - find max and total average of derivatives. Efficiency = (max-totalAverage)/totalAverage