Help with a loop in java

BlueAcolyte

Platinum Member
Nov 19, 2007
2,793
2
0
Ok, so in response to my previous question, ImageJ can get an array of pixels and each pixel's value with certain commands.

So I want to search the array with a loop...
I don't know if it searches horizontally first or second.

Currently I have it set up like this:

import ij.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.filter.PlugInFilter;
import ij.process.*;

//Superclass that works with ImageJ images
public class Line_ implements PlugInFilter {

//Setup
public int setup(String arg, ImagePlus imp) {
if (arg.equals("about"))
{showAbout(); return DONE;}
return DOES_RGB+NO_CHANGES;

}

//Actual Plugin runs here
public void run(ImageProcessor ip) {

// get width, height
int w = ip.getWidth();
int h = ip.getHeight();

// create a new image with the same size and copy the pixels of the original image
ImagePlus inverted = NewImage.createRGBImage ("Black and White image", w, h, 1, NewImage.FILL_BLACK);
ImageProcessor inv_ip = inverted.getProcessor();
inv_ip.copyBits(ip,0,0,Blitter.COPY);

//this is an array of pixel values, apparently
int[] pixels = (int[]) inv_ip.getPixels();

// loop under construction. According to Sun's tutorials, item represents the current variable being worked on.
for (int item : pixels) {
int pixelvalue = item;
//IJ.showMessage("int pixelvalue");

//This should do something, I will figure it out
ip.setPixel(1, 1, 0);
}

//redraws new image
inverted.show();
inverted.updateAndDraw();

}

void showAbout() {
IJ.showMessage("Line_",
"Creates a new black and white version of an image,\n" +
"finds the left-most edge of an object, and measures\n" +
"to the right-most edge. Useful for making sure objects\n" +
"are within a certain length."
);
}

}

Edit: Gah, lack of whitespace makes _BLUE sad.

I meant to ask, how do you recommend I go about this?

I think the correct command to get the specified pixel is getPixel(int x, int y)
 

flashbacck

Golden Member
Aug 3, 2001
1,921
0
76
Try the Attach Code button.

It's a little unclear to me what's going on. You have a picture of pipes, you're converting it to black and white. Then you want to take extract the black/white color info into 2D arrays? Then go through it horizontally to measure the length in pixels and convert that to real world inches?