Getting rid of small lines in an image!

Here is the image that I am working with, (it has been skeletonized in binary to yield as follows:

What I want to do is get rid of all objects that contain less than 7 pixels. (Any pixels that share any edge or diagonal are part of the same object). In order to do so, I have downloaded G. Landini’s imageJ plugins, which lable objects.

scroll down until you find something that looks like this

Click the blue link to download his package.

Anyways, I used BinaryLabel8 to create an image that looks like this:

The cool thing about this image is that every object is labeled with the same pixel intensity in this 16 bit image. What I want to do is sort through the whole thing find the objects that contain less than 7 pixels and delete them. I’m almost able to do this with my code here:

//Problem to solve: the getpixel command returns the pixel value as a number from 0-255, whereas I want instead the value in the actual image, (0-xxxxx).  If i solve this, the program will run.

setForegroundColor(0, 0, 0);
setBackgroundColor(0, 0, 0);
run("BinaryLabel8 ", "white");//labels all the axons with a number
getDimensions(width, height, channels, slices, frames);
getHistogram(values,counts,100); //sorts all of the numbered axons into a histogram
killcount=0; //counter for how many pixels in a small axon are destroyed

for(iii=0; iii<counts.length; iii++) //searches counts for labelled axons with small number of pixels
  if(counts[iii]>7) //this is the cutoff value for what will be deleted
    destroyvalue=values[iii]; //destroy value is the label number of the axon that will be deleted

    for(iiii=0;iiii<width;iiii++) //this loop searches the image for the axon we want to destroy
        if(currentvalue==destroyvalue) //checks to see if current pixel is a target to kill.
            killcount=killcount+1; //kill count so that we can stop the loop early.

However, whenever I use the getPixel command, it returns that value as seen from a 8 bit image, instead of returning the value that is actually on the image. How do I adapt my program so that I can access the actual value on the image that has labeled all of the objects? The labeled image is a 16 bit image.

Hi @UCResearchAssistant,

not sure if I completely get you right regarding the following statement…

…but here is what you could do in addition which is not a solution for your workflow approach directly bit can be recorded and hopefully gives you what you need in the end:

Analyze Particles allows you to get rid of all objects of a certiain size which in a skeletonized image also reflects the length of those skeletons.

Alternatively, if you also need to shorten the branches on the longer object by 7 pixels (while I guess that is not really what you want at the moment but it might help others) is the following under >Process >Binary >Options…:

But careful, this starts to prune the ends of all skeletonized objects from all sites, thus with an iteration of 7 taking of 7 pixels at each side and additionally making the longer object shorter. nevertheless, under certain circumstancis this might be helpful.

Sorry if this approach passes by what you actually want to achieve but if so, hopefully it serves somone else :wink:


Thank you so much! This completely solves the problem. I had no idea that there was such a simple solution available.

If you downloaded the morphology zip, that also includes the Particles8 plugin.
Run it and specify “Filter by size” and change the minimum size to 7.