Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: What's wrong in this code?

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    21
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What's wrong in this code?

    Main.java

    import java.io.File;
    import java.io.IOException;
    import java.io.FileNotFoundException;


    public class Main
    {
    /**
    * Entry point
    */
    public static void main(String[] args) {


    try {
    final String filename = args[0];
    final File imageFile = new File(filename);

    //Start the JFrame
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new ImageJFrame(imageFile);
    }
    });


    } catch (ArrayIndexOutOfBoundsException e) {
    System.err.println("Usage: java -jar ImageProcessor.jar <filename>");
    System.exit(-1);
    }

    }
    }


    ImageJFrame.java



    import javax.swing.JFrame;
    import java.io.File;


    public class ImageJFrame extends JFrame
    {
    public ImageJPanel panel;

    /**
    * Constructor
    *
    * Sets up a JFrame which contains an image
    */
    public ImageJFrame(File imageFile) {
    super("Image processing frame");

    panel = new ImageJPanel(imageFile);

    getContentPane().add(panel);
    setSize(panel.getWidth(), panel.getHeight());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setVisible(true);
    }
    }

    ImageJPanel


    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import javax.swing.JPanel;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.*;

    public class ImageJPanel extends JPanel {

    public BufferedImage image;

    public int getWidth() {
    return image.getWidth();
    }

    public int getHeight() {
    return image.getHeight();
    }

    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    g.drawImage(image, 0, 0, null); //draws the image
    }

    public void loadImage(File imageFile) {
    try {
    image = ImageIO.read(imageFile);
    } catch (IOException ex) {
    String directory = new File(".").getAbsolutePath();
    System.err.println("Could not open " + imageFile.getAbsolutePath() + " at " + directory);
    System.exit(-1);
    }
    }

    public void saveToFile(String filename) {
    try {
    // Save as PNG
    String fn = filename + ".png";
    File file = new File(fn);
    ImageIO.write(image, "png", file);
    } catch (IOException e) {}
    }


    public ImageJPanel(File imageFile) {
    loadImage(imageFile);
    image = Transformations.noise(image);
    String filename = imageFile.getAbsolutePath() + "1";
    saveToFile(filename);

    }
    }



    import java.awt.image.BufferedImage;
    import java.util.Random;


    public class Transformations
    {


    public static BufferedImage noise(BufferedImage img, int quantity, int threshold) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    //good values are
    //quantity = 10;
    //threshold = 50;

    Random r = new Random();

    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);

    int ran = r.nextInt(quantity);
    if (ran <= 1) {

    int amount = r.nextInt(threshold);
    int red = ((px >> 16) & 0xFF) + amount;

    amount = r.nextInt(threshold);
    int green = ((px >> 8) & 0xFF) + amount;

    amount = r.nextInt(threshold);
    int blue = (px & 0xFF) + amount;

    //Overflow fix
    if (red > 255) { red = 255; }
    if (green > 255) { green = 255; }
    if (blue > 255) { blue = 255; }

    px = (0xFF<<24) + (red<<16) + (green<<8) + blue;
    }

    dest.setRGB(x, y, px);
    }
    }

    return dest;
    }






    public static BufferedImage pixelate(BufferedImage img, int size) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    for (int x = 0; x < img.getWidth(); x+=size) {
    for (int y = 0; y < img.getHeight(); y+=size) {

    int px = 0;

    for (int xi = 0; xi < size; xi++) {
    for (int yi = 0; yi < size; yi++) {
    px += img.getRGB(x, y);
    px = px / 2;
    }
    }

    for (int xi = 0; xi < size; xi++) {
    for (int yi = 0; yi < size; yi++) {
    dest.setRGB(x+xi, y+yi, px);
    }
    }
    }
    }

    return dest;
    }





    public static BufferedImage histogramThreshold(BufferedImage img, int threshold) {

    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    int reds[] = new int[256];
    int greens[] = new int[256];
    int blues[] = new int[256];


    //Count the occurance of each pixel's red, green and blue
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);


    int red = ((px >> 16) & 0xFF);
    reds[red]++;

    int green = ((px >> 8) & 0xFF);
    greens[green]++;

    int blue = (px & 0xFF);
    blues[blue]++;

    dest.setRGB(x, y, px);
    }
    }

    //Analyse the results
    int mostCommonRed = 0;
    int mostCommonBlue = 0;
    int mostCommonGreen = 0;

    for (int i = 0; i < 256; i++) {
    if (reds[i] > mostCommonRed) {
    mostCommonRed = i;
    }

    if (blues[i] > mostCommonBlue) {
    mostCommonBlue = i;
    }

    if (greens[i] > mostCommonGreen) {
    mostCommonGreen = i;
    }
    }

    //Set the destination to pixels that are in a range +/- threshold from mostCommon value
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);

    int red = ((px >> 16) & 0xFF);
    int green = ((px >> 8) & 0xFF);
    int blue = (px & 0xFF);
    int val = 0;


    if (((red - 20 < mostCommonRed) && (red + threshold > mostCommonRed)) || ((blue - threshold < mostCommonBlue) && (blue + threshold > mostCommonBlue)) || ((green - threshold < mostCommonGreen) && (green + threshold > mostCommonGreen))) {
    val = (0xFF<<24) + (red<<16) + (green<<8) + blue;
    } else {
    val = (0xFF<<24) + (0xFF<<16) + (0xFF<<8) + 0xFF;
    }


    dest.setRGB(x, y, val);
    }
    }

    return dest;
    }



    public static BufferedImage invert(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);

    //Subtracting the channels value from 0xFF effectively inverts it
    int red = 0xFF - ((px >> 16) & 0xFF);
    int green = 0xFF - ((px >> 8) & 0xFF);
    int blue = 0xFF - (px & 0xFF);

    int inverted = (0xFF<<24) + (red<<16) + (green<<8) + blue;
    dest.setRGB(x, y, inverted);
    }
    }

    return dest;
    }



    public static BufferedImage greyScale(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {

    int px = img.getRGB(x, y);

    int alpha = (px >> 24) & 0xFF;
    int red = (px >> 16) & 0xFF;
    int green = (px >> 8) & 0xFF;
    int blue = px & 0xFF;

    //average of RGB
    int avg = (red + blue + green) / 3;

    //set R, G & B with avg color
    int grey = (alpha<<24) + (avg<<16) + (avg<<8) + avg;

    dest.setRGB(x, y, grey);
    }
    }

    return dest;
    }


    public static BufferedImage burn(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);

    int burn = px << 8; //this was a lucky guess. not sure why it works

    dest.setRGB(x, y, burn);
    }
    }

    return dest;
    }


    public static BufferedImage gaussianFilter(BufferedImage img) {
    int cuttoff = 2000;
    double magic = 1.442695;
    int xcenter = img.getWidth() / 2 - 1;
    int ycenter = img.getHeight() / 2 - 1;

    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);

    double distance = Math.sqrt(x*x+y*y);
    double value = px*255*Math.exp((-1*distance*distance)/(magic*cuttoff*cuttoff));
    dest.setRGB(x, y, (int) value);

    }
    }

    return dest;

    }


    public static BufferedImage flipVerticalHorizontal(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    //Flip vertical and horizontal
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
    int destX = img.getWidth() - x - 1;
    int destY = img.getHeight() - y - 1;
    dest.setRGB(destX, destY, px);
    }
    }

    return dest;
    }



    public static BufferedImage flipVertical(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    //Flip vertical and horizontal
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
    dest.setRGB(x, img.getHeight() - y - 1, px);
    }
    }

    return dest;
    }


    public static BufferedImage flipHorizontal(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    //Flip horizontal
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
    dest.setRGB(img.getWidth() - x - 1, y, px);
    }
    }

    return dest;
    }

    }

    Transformations.java



    import java.awt.image.BufferedImage;
    import java.util.Random;


    public class Transformations
    {


    public static BufferedImage noise(BufferedImage img, int quantity, int threshold) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    //good values are
    //quantity = 10;
    //threshold = 50;

    Random r = new Random();

    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);

    int ran = r.nextInt(quantity);
    if (ran <= 1) {

    int amount = r.nextInt(threshold);
    int red = ((px >> 16) & 0xFF) + amount;

    amount = r.nextInt(threshold);
    int green = ((px >> 8) & 0xFF) + amount;

    amount = r.nextInt(threshold);
    int blue = (px & 0xFF) + amount;

    //Overflow fix
    if (red > 255) { red = 255; }
    if (green > 255) { green = 255; }
    if (blue > 255) { blue = 255; }

    px = (0xFF<<24) + (red<<16) + (green<<8) + blue;
    }

    dest.setRGB(x, y, px);
    }
    }

    return dest;
    }






    public static BufferedImage pixelate(BufferedImage img, int size) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    for (int x = 0; x < img.getWidth(); x+=size) {
    for (int y = 0; y < img.getHeight(); y+=size) {

    int px = 0;

    for (int xi = 0; xi < size; xi++) {
    for (int yi = 0; yi < size; yi++) {
    px += img.getRGB(x, y);
    px = px / 2;
    }
    }

    for (int xi = 0; xi < size; xi++) {
    for (int yi = 0; yi < size; yi++) {
    dest.setRGB(x+xi, y+yi, px);
    }
    }
    }
    }

    return dest;
    }





    public static BufferedImage histogramThreshold(BufferedImage img, int threshold) {

    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    int reds[] = new int[256];
    int greens[] = new int[256];
    int blues[] = new int[256];


    //Count the occurance of each pixel's red, green and blue
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);


    int red = ((px >> 16) & 0xFF);
    reds[red]++;

    int green = ((px >> 8) & 0xFF);
    greens[green]++;

    int blue = (px & 0xFF);
    blues[blue]++;

    dest.setRGB(x, y, px);
    }
    }

    //Analyse the results
    int mostCommonRed = 0;
    int mostCommonBlue = 0;
    int mostCommonGreen = 0;

    for (int i = 0; i < 256; i++) {
    if (reds[i] > mostCommonRed) {
    mostCommonRed = i;
    }

    if (blues[i] > mostCommonBlue) {
    mostCommonBlue = i;
    }

    if (greens[i] > mostCommonGreen) {
    mostCommonGreen = i;
    }
    }

    //Set the destination to pixels that are in a range +/- threshold from mostCommon value
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);

    int red = ((px >> 16) & 0xFF);
    int green = ((px >> 8) & 0xFF);
    int blue = (px & 0xFF);
    int val = 0;


    if (((red - 20 < mostCommonRed) && (red + threshold > mostCommonRed)) || ((blue - threshold < mostCommonBlue) && (blue + threshold > mostCommonBlue)) || ((green - threshold < mostCommonGreen) && (green + threshold > mostCommonGreen))) {
    val = (0xFF<<24) + (red<<16) + (green<<8) + blue;
    } else {
    val = (0xFF<<24) + (0xFF<<16) + (0xFF<<8) + 0xFF;
    }


    dest.setRGB(x, y, val);
    }
    }

    return dest;
    }



    public static BufferedImage invert(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);

    //Subtracting the channels value from 0xFF effectively inverts it
    int red = 0xFF - ((px >> 16) & 0xFF);
    int green = 0xFF - ((px >> 8) & 0xFF);
    int blue = 0xFF - (px & 0xFF);

    int inverted = (0xFF<<24) + (red<<16) + (green<<8) + blue;
    dest.setRGB(x, y, inverted);
    }
    }

    return dest;
    }



    public static BufferedImage greyScale(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {

    int px = img.getRGB(x, y);

    int alpha = (px >> 24) & 0xFF;
    int red = (px >> 16) & 0xFF;
    int green = (px >> 8) & 0xFF;
    int blue = px & 0xFF;

    //average of RGB
    int avg = (red + blue + green) / 3;

    //set R, G & B with avg color
    int grey = (alpha<<24) + (avg<<16) + (avg<<8) + avg;

    dest.setRGB(x, y, grey);
    }
    }

    return dest;
    }


    public static BufferedImage burn(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);

    int burn = px << 8; //this was a lucky guess. not sure why it works

    dest.setRGB(x, y, burn);
    }
    }

    return dest;
    }


    public static BufferedImage gaussianFilter(BufferedImage img) {
    int cuttoff = 2000;
    double magic = 1.442695;
    int xcenter = img.getWidth() / 2 - 1;
    int ycenter = img.getHeight() / 2 - 1;

    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);

    double distance = Math.sqrt(x*x+y*y);
    double value = px*255*Math.exp((-1*distance*distance)/(magic*cuttoff*cuttoff));
    dest.setRGB(x, y, (int) value);

    }
    }

    return dest;

    }


    public static BufferedImage flipVerticalHorizontal(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    //Flip vertical and horizontal
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
    int destX = img.getWidth() - x - 1;
    int destY = img.getHeight() - y - 1;
    dest.setRGB(destX, destY, px);
    }
    }

    return dest;
    }



    public static BufferedImage flipVertical(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    //Flip vertical and horizontal
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
    dest.setRGB(x, img.getHeight() - y - 1, px);
    }
    }

    return dest;
    }


    public static BufferedImage flipHorizontal(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);

    //Flip horizontal
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
    dest.setRGB(img.getWidth() - x - 1, y, px);
    }
    }

    return dest;
    }

    }


    Can anybody help please how to run and display noise part ?Its showing error in "image = Transformations.noisel(image);"


  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: What's wrong in this code?

    Main.java

    import java.io.File;
    import java.io.IOException;
    import java.io.FileNotFoundException;
     
     
    public class Main
    {
    /**
    * Entry point
    */
    public static void main(String[] args) {
     
     
    try {
    final String filename = args[0];
    final File imageFile = new File(filename);
     
    //Start the JFrame
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new ImageJFrame(imageFile);
    }
    });
     
     
    } catch (ArrayIndexOutOfBoundsException e) {
    System.err.println("Usage: java -jar ImageProcessor.jar <filename>");
    System.exit(-1);
    }
     
    }
    }

    ImageJFrame.java

     
    import javax.swing.JFrame;
    import java.io.File;
     
     
    public class ImageJFrame extends JFrame
    {
    public ImageJPanel panel;
     
    /**
    * Constructor
    *
    * Sets up a JFrame which contains an image
    */
    public ImageJFrame(File imageFile) {
    super("Image processing frame");
     
    panel = new ImageJPanel(imageFile);
     
    getContentPane().add(panel);
    setSize(panel.getWidth(), panel.getHeight());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setVisible(true);
    }
    }

    ImageJPanel

    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import javax.swing.JPanel;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.*;
     
    public class ImageJPanel extends JPanel {
     
    public BufferedImage image;
     
    public int getWidth() {
    return image.getWidth();
    }
     
    public int getHeight() {
    return image.getHeight();
    }
     
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    g.drawImage(image, 0, 0, null); //draws the image
    }
     
    public void loadImage(File imageFile) {
    try {
    image = ImageIO.read(imageFile);
    } catch (IOException ex) {
    String directory = new File(".").getAbsolutePath();
    System.err.println("Could not open " + imageFile.getAbsolutePath() + " at " + directory);
    System.exit(-1);
    }
    }
     
    public void saveToFile(String filename) {
    try {
    // Save as PNG
    String fn = filename + ".png";
    File file = new File(fn);
    ImageIO.write(image, "png", file);
    } catch (IOException e) {}
    }
     
     
    public ImageJPanel(File imageFile) {
    loadImage(imageFile);
    image = Transformations.noise(image);
    String filename = imageFile.getAbsolutePath() + "1";
    saveToFile(filename);
     
    }
    }

    Transformation

     
    import java.awt.image.BufferedImage;
    import java.util.Random;
     
     
    public class Transformations
    {
     
     
    public static BufferedImage noise(BufferedImage img, int quantity, int threshold) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    //good values are
    //quantity = 10;
    //threshold = 50;
     
    Random r = new Random();
     
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
     
    int ran = r.nextInt(quantity);
    if (ran <= 1) {
     
    int amount = r.nextInt(threshold);
    int red = ((px >> 16) & 0xFF) + amount;
     
    amount = r.nextInt(threshold);
    int green = ((px >> 8) & 0xFF) + amount;
     
    amount = r.nextInt(threshold);
    int blue = (px & 0xFF) + amount;
     
    //Overflow fix
    if (red > 255) { red = 255; }
    if (green > 255) { green = 255; }
    if (blue > 255) { blue = 255; }
     
    px = (0xFF<<24) + (red<<16) + (green<<8) + blue;
    }
     
    dest.setRGB(x, y, px);
    }
    }
     
    return dest;
    }
     
     
     
     
     
     
    public static BufferedImage pixelate(BufferedImage img, int size) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    for (int x = 0; x < img.getWidth(); x+=size) {
    for (int y = 0; y < img.getHeight(); y+=size) {
     
    int px = 0;
     
    for (int xi = 0; xi < size; xi++) {
    for (int yi = 0; yi < size; yi++) {
    px += img.getRGB(x, y);
    px = px / 2;
    }
    }
     
    for (int xi = 0; xi < size; xi++) {
    for (int yi = 0; yi < size; yi++) {
    dest.setRGB(x+xi, y+yi, px);
    }
    }
    }
    }
     
    return dest;
    }
     
     
     
     
     
    public static BufferedImage histogramThreshold(BufferedImage img, int threshold) {
     
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    int reds[] = new int[256];
    int greens[] = new int[256];
    int blues[] = new int[256];
     
     
    //Count the occurance of each pixel's red, green and blue
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
     
     
    int red = ((px >> 16) & 0xFF);
    reds[red]++;
     
    int green = ((px >> 8) & 0xFF);
    greens[green]++;
     
    int blue = (px & 0xFF);
    blues[blue]++;
     
    dest.setRGB(x, y, px);
    }
    }
     
    //Analyse the results
    int mostCommonRed = 0;
    int mostCommonBlue = 0;
    int mostCommonGreen = 0;
     
    for (int i = 0; i < 256; i++) {
    if (reds[i] > mostCommonRed) {
    mostCommonRed = i;
    }
     
    if (blues[i] > mostCommonBlue) {
    mostCommonBlue = i;
    }
     
    if (greens[i] > mostCommonGreen) {
    mostCommonGreen = i;
    }
    }
     
    //Set the destination to pixels that are in a range +/- threshold from mostCommon value
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
     
    int red = ((px >> 16) & 0xFF);
    int green = ((px >> 8) & 0xFF);
    int blue = (px & 0xFF);
    int val = 0;
     
     
    if (((red - 20 < mostCommonRed) && (red + threshold > mostCommonRed)) || ((blue - threshold < mostCommonBlue) && (blue + threshold > mostCommonBlue)) || ((green - threshold < mostCommonGreen) && (green + threshold > mostCommonGreen))) {
    val = (0xFF<<24) + (red<<16) + (green<<8) + blue;
    } else {
    val = (0xFF<<24) + (0xFF<<16) + (0xFF<<8) + 0xFF;
    }
     
     
    dest.setRGB(x, y, val);
    }
    }
     
    return dest;
    }
     
     
     
    public static BufferedImage invert(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
     
    //Subtracting the channels value from 0xFF effectively inverts it
    int red = 0xFF - ((px >> 16) & 0xFF);
    int green = 0xFF - ((px >> 8) & 0xFF);
    int blue = 0xFF - (px & 0xFF);
     
    int inverted = (0xFF<<24) + (red<<16) + (green<<8) + blue;
    dest.setRGB(x, y, inverted);
    }
    }
     
    return dest;
    }
     
     
     
    public static BufferedImage greyScale(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
     
    int px = img.getRGB(x, y);
     
    int alpha = (px >> 24) & 0xFF;
    int red = (px >> 16) & 0xFF;
    int green = (px >> 8) & 0xFF;
    int blue = px & 0xFF;
     
    //average of RGB
    int avg = (red + blue + green) / 3;
     
    //set R, G & B with avg color
    int grey = (alpha<<24) + (avg<<16) + (avg<<8) + avg;
     
    dest.setRGB(x, y, grey);
    }
    }
     
    return dest;
    }
     
     
    public static BufferedImage burn(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
     
    int burn = px << 8; //this was a lucky guess. not sure why it works
     
    dest.setRGB(x, y, burn);
    }
    }
     
    return dest;
    }
     
     
    public static BufferedImage gaussianFilter(BufferedImage img) {
    int cuttoff = 2000;
    double magic = 1.442695;
    int xcenter = img.getWidth() / 2 - 1;
    int ycenter = img.getHeight() / 2 - 1;
     
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
     
    double distance = Math.sqrt(x*x+y*y);
    double value = px*255*Math.exp((-1*distance*distance)/(magic*cuttoff*cuttoff));
    dest.setRGB(x, y, (int) value);
     
    }
    }
     
    return dest;
     
    }
     
     
    public static BufferedImage flipVerticalHorizontal(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    //Flip vertical and horizontal
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
    int destX = img.getWidth() - x - 1;
    int destY = img.getHeight() - y - 1;
    dest.setRGB(destX, destY, px);
    }
    }
     
    return dest;
    }
     
     
     
    public static BufferedImage flipVertical(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    //Flip vertical and horizontal
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
    dest.setRGB(x, img.getHeight() - y - 1, px);
    }
    }
     
    return dest;
    }
     
     
    public static BufferedImage flipHorizontal(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    //Flip horizontal
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
    dest.setRGB(img.getWidth() - x - 1, y, px);
    }
    }
     
    return dest;
    }
     
    }

    Transformations.java

     
    import java.awt.image.BufferedImage;
    import java.util.Random;
     
     
    public class Transformations
    {
     
     
    public static BufferedImage noise(BufferedImage img, int quantity, int threshold) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    //good values are
    //quantity = 10;
    //threshold = 50;
     
    Random r = new Random();
     
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
     
    int ran = r.nextInt(quantity);
    if (ran <= 1) {
     
    int amount = r.nextInt(threshold);
    int red = ((px >> 16) & 0xFF) + amount;
     
    amount = r.nextInt(threshold);
    int green = ((px >> 8) & 0xFF) + amount;
     
    amount = r.nextInt(threshold);
    int blue = (px & 0xFF) + amount;
     
    //Overflow fix
    if (red > 255) { red = 255; }
    if (green > 255) { green = 255; }
    if (blue > 255) { blue = 255; }
     
    px = (0xFF<<24) + (red<<16) + (green<<8) + blue;
    }
     
    dest.setRGB(x, y, px);
    }
    }
     
    return dest;
    }
     
     
     
     
     
     
    public static BufferedImage pixelate(BufferedImage img, int size) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    for (int x = 0; x < img.getWidth(); x+=size) {
    for (int y = 0; y < img.getHeight(); y+=size) {
     
    int px = 0;
     
    for (int xi = 0; xi < size; xi++) {
    for (int yi = 0; yi < size; yi++) {
    px += img.getRGB(x, y);
    px = px / 2;
    }
    }
     
    for (int xi = 0; xi < size; xi++) {
    for (int yi = 0; yi < size; yi++) {
    dest.setRGB(x+xi, y+yi, px);
    }
    }
    }
    }
     
    return dest;
    }
     
     
     
     
     
    public static BufferedImage histogramThreshold(BufferedImage img, int threshold) {
     
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    int reds[] = new int[256];
    int greens[] = new int[256];
    int blues[] = new int[256];
     
     
    //Count the occurance of each pixel's red, green and blue
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
     
     
    int red = ((px >> 16) & 0xFF);
    reds[red]++;
     
    int green = ((px >> 8) & 0xFF);
    greens[green]++;
     
    int blue = (px & 0xFF);
    blues[blue]++;
     
    dest.setRGB(x, y, px);
    }
    }
     
    //Analyse the results
    int mostCommonRed = 0;
    int mostCommonBlue = 0;
    int mostCommonGreen = 0;
     
    for (int i = 0; i < 256; i++) {
    if (reds[i] > mostCommonRed) {
    mostCommonRed = i;
    }
     
    if (blues[i] > mostCommonBlue) {
    mostCommonBlue = i;
    }
     
    if (greens[i] > mostCommonGreen) {
    mostCommonGreen = i;
    }
    }
     
    //Set the destination to pixels that are in a range +/- threshold from mostCommon value
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
     
    int red = ((px >> 16) & 0xFF);
    int green = ((px >> 8) & 0xFF);
    int blue = (px & 0xFF);
    int val = 0;
     
     
    if (((red - 20 < mostCommonRed) && (red + threshold > mostCommonRed)) || ((blue - threshold < mostCommonBlue) && (blue + threshold > mostCommonBlue)) || ((green - threshold < mostCommonGreen) && (green + threshold > mostCommonGreen))) {
    val = (0xFF<<24) + (red<<16) + (green<<8) + blue;
    } else {
    val = (0xFF<<24) + (0xFF<<16) + (0xFF<<8) + 0xFF;
    }
     
     
    dest.setRGB(x, y, val);
    }
    }
     
    return dest;
    }
     
     
     
    public static BufferedImage invert(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
     
    //Subtracting the channels value from 0xFF effectively inverts it
    int red = 0xFF - ((px >> 16) & 0xFF);
    int green = 0xFF - ((px >> 8) & 0xFF);
    int blue = 0xFF - (px & 0xFF);
     
    int inverted = (0xFF<<24) + (red<<16) + (green<<8) + blue;
    dest.setRGB(x, y, inverted);
    }
    }
     
    return dest;
    }
     
     
     
    public static BufferedImage greyScale(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
     
    int px = img.getRGB(x, y);
     
    int alpha = (px >> 24) & 0xFF;
    int red = (px >> 16) & 0xFF;
    int green = (px >> 8) & 0xFF;
    int blue = px & 0xFF;
     
    //average of RGB
    int avg = (red + blue + green) / 3;
     
    //set R, G & B with avg color
    int grey = (alpha<<24) + (avg<<16) + (avg<<8) + avg;
     
    dest.setRGB(x, y, grey);
    }
    }
     
    return dest;
    }
     
     
    public static BufferedImage burn(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
     
    int burn = px << 8; //this was a lucky guess. not sure why it works
     
    dest.setRGB(x, y, burn);
    }
    }
     
    return dest;
    }
     
     
    public static BufferedImage gaussianFilter(BufferedImage img) {
    int cuttoff = 2000;
    double magic = 1.442695;
    int xcenter = img.getWidth() / 2 - 1;
    int ycenter = img.getHeight() / 2 - 1;
     
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
     
    double distance = Math.sqrt(x*x+y*y);
    double value = px*255*Math.exp((-1*distance*distance)/(magic*cuttoff*cuttoff));
    dest.setRGB(x, y, (int) value);
     
    }
    }
     
    return dest;
     
    }
     
     
    public static BufferedImage flipVerticalHorizontal(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    //Flip vertical and horizontal
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
    int destX = img.getWidth() - x - 1;
    int destY = img.getHeight() - y - 1;
    dest.setRGB(destX, destY, px);
    }
    }
     
    return dest;
    }
     
     
     
    public static BufferedImage flipVertical(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    //Flip vertical and horizontal
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
    dest.setRGB(x, img.getHeight() - y - 1, px);
    }
    }
     
    return dest;
    }
     
     
    public static BufferedImage flipHorizontal(BufferedImage img) {
    BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
     
    //Flip horizontal
    for (int x = 0; x < img.getWidth(); x++) {
    for (int y = 0; y < img.getHeight(); y++) {
    int px = img.getRGB(x, y);
    dest.setRGB(img.getWidth() - x - 1, y, px);
    }
    }
     
    return dest;
    }
     
    }

    Please use [highlight=java] Your code [/highlight] around your code. It really makes it easier to read.


    As for your error, please post your full error text. It will make it easier to try and guess what is causing it.

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: What's wrong in this code?

    I dont know what is wrong with your program, but maybe you could tell us what you expect it to do and what is actually happening.

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    21
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong in this code?

    When I want to run the noise function its showing error .How to solve this problem??The error are
    "]Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: Transformations.noise
    at ImageJPanel.<init>(ImageJPanel.java:49)
    at ImageJFrame.<init>(ImageJFrame.java:17)
    at Main$1.run(Main.java:19)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:744)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 714)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:82)[/COLOR]
    BUILD SUCCESSFUL (total time: 1 second)"

    Below is the attach file of screenshot
    noise.jpg

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    15
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Default Re: What's wrong in this code?

    what I can see is that in your imageJPanel code
    public ImageJPanel(File imageFile) {
    loadImage(imageFile);
    image = Transformations.noise(image);
    String filename = imageFile.getAbsolutePath() + "1";
    saveToFile(filename);
     
    }

    you call the Transformations.noise(image); with one argument, but in the method definition there are three arguments.

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    21
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong in this code?

    Quote Originally Posted by Zavael View Post
    what I can see is that in your imageJPanel code
    public ImageJPanel(File imageFile) {
    loadImage(imageFile);
    image = Transformations.noise(image);
    String filename = imageFile.getAbsolutePath() + "1";
    saveToFile(filename);
     
    }

    you call the Transformations.noise(image); with one argument, but in the method definition there are three arguments.


    Then how to correct this code??

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What's wrong in this code?

    Too much unformatted code that is too hard to read. Next time, post your formatted code correctly.

    This is the method signature:

    public static BufferedImage noise(BufferedImage img, int quantity, int threshold)

    notice that it takes 3 parameters, BufferedImage, int, and int. Supply the required parameters when calling the method. If you have not yet acquired this foundational Java knowledge, then I suggest you spend more time studying the basics.

Similar Threads

  1. What's wrong with my code?
    By deejaypanininini in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 8th, 2014, 08:50 PM
  2. Please HELP! my code is wrong somewhere.
    By mcpanthers22 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: September 19th, 2012, 01:12 PM
  3. what's wrong with this code
    By gcsekhar in forum Java Networking
    Replies: 5
    Last Post: July 21st, 2011, 06:01 AM
  4. What is Wrong with my code
    By xew123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2011, 04:59 AM
  5. What's wrong with my code ?
    By mithani in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2010, 08:57 AM