Hi folks for something so simple this is starting to drive me nuts. Here it is.
All I need to do is to find out how much of a colour in percentage terms is in a photo. I can take photos so that a certain chosen colour is the only one recorded and the rest of the photo is Black and White. So I may have a largely black and white photo with some areas of red - I need to know what is the percentage area of red. Whats your thoughts. |
I'm guessing the images are colour RGB? If so the white and grey areas will contain red, green and blue but in equal amounts; where your red area differs is that it has more red than green or blue.
So I would calculate the % of pixels which have more Red than either of the two other colours. ps. how are you taking photos with some red areas and some greyscale areas? |
Yup thats exactly the problem. I'll give that a go and get back to you.
The photographs - well some canon cameras have a colour accent feature where you can identify a colour and the camera will only use that colour and the rest of the photo is in B&W - very useful for my purposes (paint detachment from steel). You can choose any colour and also put a tolerance in as well. Another feature is that you colour swap. If any one else has any other ideas let me know |
oh well if the camera is doing it digitally from the original image data I would imagine the R&G&B of the grey areas really are equal. That makes things very simple- something like this should do it (untested):
import ij.plugin.PlugIn; import ij.ImagePlus; import ij.IJ; import ij.WindowManager; import ij.process.*; import ij.measure.*; public final class RedAreaCalculator_ implements PlugIn { private ImagePlus imp; private final static String COLUMN_HEADING = "percentage red"; public void run(String arg) { imp = WindowManager.getCurrentImage(); int bitDepth = imp.getBitDepth(); if (bitDepth ==24) { CalculatePercentageRed(); } else { IJ.error("RGB image required"); } } private void CalculatePercentageRed(){ ImageProcessor colours = imp.getProcessor(); int[] RGB = new int[3]; int ImageWidth = colours.getWidth(); int ImageHeight = colours.getHeight(); final int totalPixelNumber = ImageWidth*ImageHeight; int sumOfRedPixels = 0; for (int i = 0 ; i < ImageWidth ; ++i) { for (int j = 0 ; j < ImageHeight ; ++j) { RGB = colours.getPixel(i, j, RGB); if (RGB[0] != RGB[1]) { sumOfRedPixels = sumOfRedPixels +1; } } } double percentage = (sumOfRedPixels*100.00)/totalPixelNumber; ResultsTable rt = ResultsTable.getResultsTable(); int column = rt.getColumnIndex(COLUMN_HEADING); if (column == -1) { rt.addColumns(); column = rt.getFreeColumn(COLUMN_HEADING); } rt.incrementCounter(); rt.addValue(column, percentage); rt.show("Results"); } } |
Ho Ho EHB many thanks - that is correct now I have checked. ok now for the slightly embarrassing bit - how do I use your code
|
it just needs copying and pasting into a compiler, which would produce this
http://dl.dropbox.com/u/2014897/RedAreaCalculator_.class that needs putting in the ImageJ plugins folder. I believe ImageJ has a built in compiler, although I personally use Eclipse (http://www.eclipse.org/). like I said; it hasn't had much testing. BW |
EHB
That seems to be nearly exactly what I have been looking for, many thanks indeed. There might be a couple of changes I might like, it will give me the chance to play about with code. But don't be too surprised if I come sneeking back looking for help |
no worries, was probably one of the smallest plugins ever written. To write your own I'd recommend; http://www.imagingbook.com/
and for basic java; http://workbench.cadenhead.org/book/java-6-21-days/ I think you'll see that your plugin is simpler than any of the examples |
Ta for the advice - I'll give it a go - I am more of a mechanic type, but I like to dabble in all sorts of stuff.
|
Ok I have ordered that book as I do enjoy playing with such things.
One further thing I would ask is if anyone knows of a plugin that would do the following for me: As all my greys are indeed equal amounts of RGB and the red areas have more red - is it possible for me to keep the grey areas as they are but to make the red areas into 'pure red' i.e. R255, G0, B0 |
you don't need a plug in for that, as you'll soon find out when you start learning how to write your own.
to do that with the current plugin all you need to do is but add three lines of code: 1) describe the red only pixel you'd like to replace the partially red pixels with: "private final static int[] REDPIXEL = {255, 0, 0};" 2) as well as counting the non-grey scale pixels also replace them with your red only pixel: "colours.putPixel(i, j, REDPIXEL);" 3) redraw the image: "imp.draw();" which gives: import ij.plugin.PlugIn; import ij.ImagePlus; import ij.IJ; import ij.WindowManager; import ij.process.*; import ij.measure.*; public final class RedAreaCalculator_ implements PlugIn { private ImagePlus imp; private final static String COLUMN_HEADING = "percentage red"; private final static int[] REDPIXEL = {255, 0, 0}; public void run(String arg) { imp = WindowManager.getCurrentImage(); int bitDepth = imp.getBitDepth(); if (bitDepth ==24) { CalculatePercentageRed(); } else { IJ.error("RGB image required"); } } private void CalculatePercentageRed(){ ImageProcessor colours = imp.getProcessor(); int[] RGB = new int[3]; int ImageWidth = colours.getWidth(); int ImageHeight = colours.getHeight(); final int totalPixelNumber = ImageWidth*ImageHeight; int sumOfRedPixels = 0; for (int i = 0 ; i < ImageWidth ; ++i) { for (int j = 0 ; j < ImageHeight ; ++j) { RGB = colours.getPixel(i, j, RGB); if (RGB[0] != RGB[1]) { sumOfRedPixels = sumOfRedPixels +1; colours.putPixel(i, j, REDPIXEL); } } } imp.draw(); double percentage = (sumOfRedPixels*100.00)/totalPixelNumber; ResultsTable rt = ResultsTable.getResultsTable(); int column = rt.getColumnIndex(COLUMN_HEADING); if (column == -1) { rt.addColumns(); column = rt.getFreeColumn(COLUMN_HEADING); } rt.incrementCounter(); rt.addValue(column, percentage); rt.show("Results"); } } which when compiled gives http://dl.dropbox.com/u/2014897/RedAreaCalculator_.class you might like to start with some on-line tutorials in the basics: http://java.sun.com/docs/books/tutorial/ I don't think they are as well written as the book though. |
The following macro code does the same :
count=0; w=getWidth; h=getHeight; for (y=0; y<h; y++) { for (x=0; x<w; x++) { v=getPixel(x,y); r=(v>>16)&0xff; g=(v>>8)&0xff; b=v&0xff; if ((r>b)&&(r>g)) { setPixel(x,y,0xff0000); count++; } } } print ("% more red pixels :"+100*count/(w*h)); Jerome On Tue, Mar 23, 2010 at 12:21 PM, EHB2010 <[hidden email]> wrote: > you don't need a plug in for that, as you'll soon find out when you start > learning how to write your own. > > to do that with the current plugin all you need to do is but add three > lines > of code: > > 1) describe the red only pixel you'd like to replace the partially red > pixels with: > "private final static int[] REDPIXEL = {255, 0, 0};" > 2) as well as counting the non-grey scale pixels also replace them with > your > red only pixel: > "colours.putPixel(i, j, REDPIXEL);" > 3) redraw the image: > "imp.draw();" > > which gives: > > import ij.plugin.PlugIn; > import ij.ImagePlus; > import ij.IJ; > import ij.WindowManager; > import ij.process.*; > import ij.measure.*; > > public final class RedAreaCalculator_ implements PlugIn { > private ImagePlus imp; > private final static String COLUMN_HEADING = "percentage red"; > private final static int[] REDPIXEL = {255, 0, 0}; > > public void run(String arg) { > imp = WindowManager.getCurrentImage(); > int bitDepth = imp.getBitDepth(); > if (bitDepth ==24) { > CalculatePercentageRed(); > } else { > IJ.error("RGB image required"); > } > > } > > private void CalculatePercentageRed(){ > > ImageProcessor colours = imp.getProcessor(); > int[] RGB = new int[3]; > int ImageWidth = colours.getWidth(); > int ImageHeight = colours.getHeight(); > final int totalPixelNumber = ImageWidth*ImageHeight; > > int sumOfRedPixels = 0; > for (int i = 0 ; i < ImageWidth ; ++i) { > for (int j = 0 ; j < ImageHeight ; ++j) { > RGB = colours.getPixel(i, j, RGB); > if (RGB[0] != RGB[1]) { > sumOfRedPixels = sumOfRedPixels +1; > colours.putPixel(i, j, REDPIXEL); > } > } > } > imp.draw(); > double percentage = > (sumOfRedPixels*100.00)/totalPixelNumber; > ResultsTable rt = ResultsTable.getResultsTable(); > int column = rt.getColumnIndex(COLUMN_HEADING); > if (column == -1) { > rt.addColumns(); > column = rt.getFreeColumn(COLUMN_HEADING); > } > rt.incrementCounter(); > rt.addValue(column, percentage); > rt.show("Results"); > } > } > > which when compiled gives > http://dl.dropbox.com/u/2014897/RedAreaCalculator_.class > > you might like to start with some on-line tutorials in the basics: > > http://java.sun.com/docs/books/tutorial/ > > I don't think they are as well written as the book though. > -- > View this message in context: > http://n2.nabble.com/Driving-me-nuts-tp4770479p4783380.html > Sent from the ImageJ mailing list archive at Nabble.com. > |
In reply to this post by EHB2010
EHB
Thankyou very much that is absolutely superb I received my book this afternoon so I will go away and have a good play with a few things and see how I get on. Many thanks again SM |
Free forum by Nabble | Edit this page |