vendredi 10 avril 2015

Select all pixels of one color in ImageJ

I want to develop a plugIn for ImageJ that clicking over a pixel (of a determined color) it selects all the pixels of that color and the rest of the image in black (except if the selected pixel is black).


I coded the next:



import ij.ImagePlus;
import ij.gui.ImageCanvas;
import ij.gui.ImageWindow;
import ij.plugin.filter.PlugInFilter;
import ij.process.ImageProcessor;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class PlugInMouse_ implements PlugInFilter, MouseListener {
ImagePlus imp;
ImagePlus ni;
ImageCanvas canvas;

@Override
public int setup(String arg, ImagePlus imp) {
this.imp = imp;
return DOES_RGB + DOES_STACKS + SUPPORTS_MASKING;
}

@Override
public void run(ImageProcessor ip) {
ni = new ImagePlus();
ImageWindow win = imp.getWindow();
canvas = win.getCanvas();
canvas.addMouseListener(this);
}

@Override
public void mouseClicked(MouseEvent arg0) {
int x = arg0.getLocationOnScreen().x; // seleciona a posição do clique
int y = arg0.getLocationOnScreen().y;
setDrawingColor(x, y, false);
}

public void setDrawingColor(int x1, int y1, boolean setBackground) {
int bi = imp.getBufferedImage().getRGB(x1, y1);
for (int corte = 1; corte <= imp.getNSlices(); corte++) {
int[] pix = (int[]) imp.getStack().getProcessor(corte).getPixels();
for (int i = 0; i < imp.getWidth() * imp.getHeight(); i++) {
if (pix[i] != bi & pix[i] != 0x000000) {
pix[i] = 0x000000;
}else{
pix[i]=pix[i];
}
}
}
imp.updateAndDraw();
}

@Override
public void mouseReleased(MouseEvent arg0) {
}

@Override
public void mouseEntered(MouseEvent arg0) {
}

@Override
public void mouseExited(MouseEvent arg0) {
}

@Override
public void mousePressed(MouseEvent arg0) {
}
}


The thing is: It works pretty well except that it draws a kind of a box outside the color and it doesn't always work. Sometimes I click over a black box and it puts all the other boxes black. Original Image After clicking over the black square (you can see that "box" outside the square


Can you help me?


Aucun commentaire:

Enregistrer un commentaire