methods - replacing green pixels in a picture using java -
i have picture , trying remove green pixels. how do using simple java , 2d array?
so far, code looks this:
public void removegreen() { picture pic = new picture("img_7320.jpg"); pixel pixel = null; (int row = 0; row < pic.getheight(); row++) { (int col = 0; col < pic.getwidth(); col++) { pixel = getpixel(row,col); pixel.getcolor(); if(pixel.getred() < 40 & pixel.getblue() < 160 & pixel.getgreen() > 220) { color white = new color(255,255,255); pixel.setcolor(white); } } } } (right trying replacing green pixel white pixel because i'm not sure how remove pixel altogether.)
and code in main method using test removegreen() method,looks this:
//method test removegreen public static void testremovegreen() { picture me = new picture("img_7320.jpg"); me.explore(); me.removegreen(); me.explore(); } so, code looks this:
public void removegreen(picture pic) {
for (int row = 0; row < pic.getheight(); row++) {
(int col = 0; col < pic.getwidth(); col++) { pixel pixel = pic.getpixel(row,col); if((pixel.getred() < 40) && (pixel.getblue() < 160) && (pixel.getgreen() > 220)) { color white = new color(255,255,255); pixel.setcolor(white); } } } }
and main method still same. still not understand why method not working properly.
the following makes clear passed pic parameter altered.
public static void removegreen(picture pic) { (int row = 0; row < pic.getheight(); row++) { (int col = 0; col < pic.getwidth(); col++) { pixel pixel = pic.getpixel(row,col); if (pixel.getred() < 40 && pixel.getblue() < 160 && pixel.getgreen() > 220) { pixel.setcolor(color.white); } } } } the && short-cut and: x && y() not call y x false.
mind pic.getpixel. if removegreen intended method of picture, remove parameter , static keyword, , pic..
public static void testremovegreen() { picture me = new picture("img_7320.jpg"); me.explore(); removegreen(me); me.explore(); } as .jpg, jpeg, format not have transparency, colour white needed shown "background."
Comments
Post a Comment