-
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
}
-
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.
-
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?
-
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..
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks