Filters for an Image


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Filters for an Image

  1. #1
    Join Date
    Sep 2004
    Posts
    103

    Filters for an Image

    K i'm having a small problem, I have a filter that can take out pixels from the picture and that's shown down below. I can't figure out how to alter the filter to make the image black and white, I figure I have to change the value by subtracting 255 by the old value, Can't figure out how to do it? any ideas


    package filters;
    import imagelab.*;
    import javax.swing.JOptionPane;

    public class Mesh implements ImageFilter {

    ImgProvider filteredImage;

    public void filter (ImgProvider ip) {
    short tmp;
    short [][] al = ip.getAlpha();
    int npixels = getMeshSize();
    for (int r = 0; r<al.length; r++) {
    for (int c=0; c<al[0].length; c++) {
    if ((c/npixels+r/npixels) % 2 == 1) al[r][c] = 0;
    }//for c
    }//for r;
    filteredImage = new ImgProvider();
    filteredImage.setColors(ip.getRed(), ip.getGreen(), ip.getBlue(), al);
    filteredImage.showPix("Transparent");
    }//filter

    public ImgProvider getImgProvider() {
    return filteredImage;
    }//getImgProvider

    public String getMenuLabel() {
    return "Mesh";
    } //getMenuLabel

    protected int getMeshSize() {
    String response = JOptionPane.showInputDialog(null, "Enter Mesh Size", "");
    int num;
    try {
    num = Integer.parseInt(response);
    if (num <= 0) num = 2;
    } catch (NumberFormatException ex) {
    num = 2;
    }//catch
    return num;
    }//getMeshSize

    }

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    Do you want a b&w image? or a greyscale image?

    for greyscale:
    add the 3 color components together, then divide by 255. then use that value for all the color channels of the pixel

    for b&w:
    if the image is in greyscale mode, just check the color value of the pixel. if its less than 128, make it black. if its greater that 128, make it white.

  3. #3
    Join Date
    Sep 2004
    Posts
    103

    Question Black and white

    How can I code from this filter a black and white image? can't figure it out, I know I have to subtract by 255 and loop through the array but can't get the for loop to work?

  4. #4
    Join Date
    Feb 2004
    Posts
    808
    Originally posted by Phaelax
    Do you want a b&w image? or a greyscale image?

    for greyscale:
    add the 3 color components together, then divide by 255. then use that value for all the color channels of the pixel
    im not sure i agree with div by 255.. div by 3 more like, to give the average value!

    note though that the human eye is less sensitive to blue than other colours.. i suggest reading about yuv 422 and other such things.. youll get a be3tter idea of how to convert to grayscale. black and white are affected by similar issues..
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  5. #5
    Join Date
    Mar 2004
    Posts
    635
    oops, yea divide by 3. After cjard mentioned the eye's sensitivity is different with each color, I remembered an image library I worked on with someone else. Here's what we did:

    r = red * 0.3
    g = green *0.59
    b = blue *0.11
    grey = r + g + b

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center