We had to program this for the school:
1: stuff the start pixel into a queue, note its color. note it as added. 2: begin picking a pixel off the queue. If it similar to the start pixel: 2: put all its neighbours into the queue for each added pixel, note it added. if already noted for a pixel, don't add it anymore. 3: color it with the destination color. 3: nonempty => jump back to 2 4: empty => we are finished
Depending on whether we make an 8-neighboring or 4-neighboring, we check all 8 neighboring pixels or only pixels left / right or above / below a certain pixel. Here is the code (using ImageJ. I deleted some code that doesn't matter). Hope this makes sense, this is Java. Just ask questions:
public class Uebung1_2 implements PlugInFilter, MouseListener { private ImageProcessor ip; boolean[] state; int[] pixels; Queue<Integer> nextPixels; int threshould; void addNextPixel(int p) { if(!state[p]) { nextPixels.add(p); state[p] = true; } } boolean pixelsSimilar(int color1, int color2) { int dr = Math.abs(((color1 >> 16) & 0xff) - ((color2 >> 16) & 0xff)); int dg = Math.abs(((color1 >> 8) & 0xff) - ((color2 >> 8) & 0xff)); int db = Math.abs(((color1 >> 0) & 0xff) - ((color2 >> 0) & 0xff)); return ((double)(dr + dg + db) / 3.0) <= threshould; } private void doFill(int x, int y, boolean connect8) {
Johannes Schaub - litb
source share