Hello,
I am using the hough center plugin (http://w3.umons.ac.be/perso/Villers.Didier/Hough/Hough_Center.html) and I would like to get the coordinate result as a point roi selection instead of having a cross drew on my picture. I think that it should not be so difficult but unfortunately my programing skill is too bad. Does anyone can help me? Script is displayed below. Thanks, With regards, JB Forien import ij.*; import ij.plugin.filter.PlugInFilter; import ij.process.*; import ij.gui.*; //import java.util.*; //import java.io.*; public class Hough_ implements PlugInFilter { private static int N=5, percent=1, dimFiltre=9; private static double threshold=0.85, precision=0.5; private static String backGroundColor = new String("white"); public int setup(String arg, ImagePlus imp) { boolean ok = false; if(imp==null) { IJ.error("Please load an image"); return -1; } while(!ok) { String st[] = new String[2]; st[0] = new String("white"); st[1] = new String("black"); GenericDialog gen = new GenericDialog("Configuration :"); gen.addNumericField("accumulator dimension : ", N, 0); gen.addChoice("backGroundColor : ", st, st[0]); gen.addNumericField("filter dimension : ", dimFiltre, 0); gen.addNumericField("percentage of pixels : ", percent, 0); gen.addNumericField("precision : ", precision, 1); gen.addNumericField("threshold : ", threshold, 2); gen.showDialog(); if (!gen.wasCanceled()) { N = (int)gen.getNextNumber(); backGroundColor = gen.getNextChoice(); dimFiltre = (int)gen.getNextNumber(); percent = (int)gen.getNextNumber(); precision = (double)gen.getNextNumber(); threshold = (double)gen.getNextNumber(); if( (N%2 != 0) && (N>1) && (dimFiltre%2!=0) && (dimFiltre>0) && (percent<=100) && (percent>0) && (threshold>0) && (threshold<=1) && (precision>=0.0625) ) { if(imp.getType()!=0) { gen = new GenericDialog("Warning"); gen.addMessage("This plug-in works on 8-bit grayscaled images."); gen.addMessage("Would you like to continue in this format?"); gen.showDialog(); if(gen.wasCanceled()) { return -1; } else { ImageConverter imc = new ImageConverter(imp); imc.convertToGray8(); ok = true; } } else { ok = true; } } else { gen = new GenericDialog("Configuration error"); if(N%2 == 0) { gen.addMessage("N must be uneven. "); } if(N <= 1) { gen.addMessage("N must be greater than 1. "); } if(dimFiltre%2 == 0) { gen.addMessage("Filter dimension must be uneven."); } if(dimFiltre <= 0) { gen.addMessage("Filter dimension must be positive."); } if((percent>100) || (percent<=0)) { gen.addMessage("Percentage must be included between 0 and 100."); } if((threshold<=0) || (threshold>1)) { gen.addMessage("Threshold must be included between 0 and 1."); } if(precision<0.0625) { gen.addMessage("Precision must be greater than 0.0625."); } gen.showDialog(); if(gen.wasCanceled()) { return -1; } } } else { return -1; } } return DOES_8G; } public void run(ImageProcessor ip) { int middleX, middleY; int average=0,i=0, j=0; boolean blackBackGround = true; RectDouble finalAccumulator = new RectDouble(0,0,0,0); if(backGroundColor.compareTo("white")==0) { blackBackGround = false; } Hough hough = new Hough(ip, N, blackBackGround, dimFiltre, percent, precision, threshold); for(i=0; i<10; ++i) { finalAccumulator = hough.getCenter(); } if( ((int)(finalAccumulator.getCentreX()))!=((int)(finalAccumulator.getCentreX()+0.5)) ) { middleX = (int)(finalAccumulator.getCentreX()+1); } else { middleX = (int)(finalAccumulator.getCentreX()); } if( ((int)(finalAccumulator.getCentreY()))!=((int)(finalAccumulator.getCentreY()+0.5)) ) { middleY = (int)(finalAccumulator.getCentreY()+0.5); } else { middleY = (int)(finalAccumulator.getCentreY()); } for(i=-2; i<3; ++i) { for(j=-2; j<3; ++j) { average = average + ip.getPixel(middleX+i, middleY+j); } } average = (int)(average/25); if(average<128) { ip.setColor(255); } else { ip.setColor(0); } ip.drawLine(middleX-5, middleY-5, middleX+5, middleY+5); ip.drawLine(middleX+5, middleY-5, middleX-5, middleY+5); GenericDialog gen = new GenericDialog("Result"); gen.addMessage("center : ( "+finalAccumulator.getCentreX()+" , "+finalAccumulator.getCentreY()+" )"); gen.showDialog(); } } |
Hi Jean,
On 24.05.2012 3:38 PM, jforien wrote: > I am using the hough center plugin ( > http://w3.umons.ac.be/perso/Villers.Didier/Hough/Hough_Center.html > http://w3.umons.ac.be/perso/Villers.Didier/Hough/Hough_Center.html ) and I > would like to get the coordinate result as a point roi selection instead of > having a cross drew on my picture. I think that it should not be so > difficult but unfortunately my programing skill is too bad. Does anyone can > help me? Script is displayed below. Replacing these two lines in the script you pasted: > ip.drawLine(middleX-5, middleY-5, middleX+5, middleY+5); > ip.drawLine(middleX+5, middleY-5, middleX-5, middleY+5); by something like that: PointRoi roi = new PointRoi(middleX,middleY); imp.setRoi(roi); imp.updateAndDraw(); should do the trick. I didn't test the script you provided though. Cheers, Jan > > import ij.*; > import ij.plugin.filter.PlugInFilter; > import ij.process.*; > import ij.gui.*; > //import java.util.*; > > //import java.io.*; > > > > public class Hough_ implements PlugInFilter > { > private static int N=5, percent=1, dimFiltre=9; > private static double threshold=0.85, precision=0.5; > private static String backGroundColor = new String("white"); > > > public int setup(String arg, ImagePlus imp) > { > boolean ok = false; > > if(imp==null) > { > IJ.error("Please load an image"); > return -1; > } > > > while(!ok) > { > String st[] = new String[2]; > st[0] = new String("white"); > st[1] = new String("black"); > > > GenericDialog gen = new GenericDialog("Configuration :"); > gen.addNumericField("accumulator dimension : ", N, 0); > gen.addChoice("backGroundColor : ", st, st[0]); > gen.addNumericField("filter dimension : ", dimFiltre, 0); > gen.addNumericField("percentage of pixels : ", percent, 0); > gen.addNumericField("precision : ", precision, 1); > gen.addNumericField("threshold : ", threshold, 2); > gen.showDialog(); > > if (!gen.wasCanceled()) > { > N = (int)gen.getNextNumber(); > backGroundColor = gen.getNextChoice(); > dimFiltre = (int)gen.getNextNumber(); > percent = (int)gen.getNextNumber(); > precision = (double)gen.getNextNumber(); > threshold = (double)gen.getNextNumber(); > > if( (N%2 != 0) && (N>1) && (dimFiltre%2!=0) && (dimFiltre>0) && > (percent<=100) && (percent>0) && (threshold>0) && (threshold<=1) && > (precision>=0.0625) ) > { > if(imp.getType()!=0) > { > gen = new GenericDialog("Warning"); > gen.addMessage("This plug-in works on 8-bit grayscaled images."); > gen.addMessage("Would you like to continue in this format?"); > gen.showDialog(); > > if(gen.wasCanceled()) > { > return -1; > } > > else > { > ImageConverter imc = new ImageConverter(imp); > imc.convertToGray8(); > ok = true; > } > } > > else > { > ok = true; > } > } > > else > { > gen = new GenericDialog("Configuration error"); > > if(N%2 == 0) > { > gen.addMessage("N must be uneven. "); > } > > if(N <= 1) > { > gen.addMessage("N must be greater than 1. "); > } > > if(dimFiltre%2 == 0) > { > gen.addMessage("Filter dimension must be uneven."); > } > > if(dimFiltre <= 0) > { > gen.addMessage("Filter dimension must be positive."); > } > > > if((percent>100) || (percent<=0)) > { > gen.addMessage("Percentage must be included between 0 and 100."); > } > > if((threshold<=0) || (threshold>1)) > { > gen.addMessage("Threshold must be included between 0 and 1."); > } > > if(precision<0.0625) > { > gen.addMessage("Precision must be greater than 0.0625."); > } > > gen.showDialog(); > > if(gen.wasCanceled()) > { > return -1; > } > } > } > > else > { > return -1; > } > } > > return DOES_8G; > } > > > public void run(ImageProcessor ip) > { > int middleX, middleY; > int average=0,i=0, j=0; > boolean blackBackGround = true; > RectDouble finalAccumulator = new RectDouble(0,0,0,0); > > if(backGroundColor.compareTo("white")==0) > { > blackBackGround = false; > } > > Hough hough = new Hough(ip, N, blackBackGround, dimFiltre, percent, > precision, threshold); > > for(i=0; i<10; ++i) > { > finalAccumulator = hough.getCenter(); > } > > > if( > ((int)(finalAccumulator.getCentreX()))!=((int)(finalAccumulator.getCentreX()+0.5)) > ) > { > middleX = (int)(finalAccumulator.getCentreX()+1); > } > > else > { > middleX = (int)(finalAccumulator.getCentreX()); > } > > if( > ((int)(finalAccumulator.getCentreY()))!=((int)(finalAccumulator.getCentreY()+0.5)) > ) > { > middleY = (int)(finalAccumulator.getCentreY()+0.5); > } > > else > { > middleY = (int)(finalAccumulator.getCentreY()); > } > > for(i=-2; i<3; ++i) > { > for(j=-2; j<3; ++j) > { > average = average + ip.getPixel(middleX+i, middleY+j); > } > } > > average = (int)(average/25); > > > if(average<128) > { > ip.setColor(255); > } > > else > { > ip.setColor(0); > } > > ip.drawLine(middleX-5, middleY-5, middleX+5, middleY+5); > ip.drawLine(middleX+5, middleY-5, middleX-5, middleY+5); > > > GenericDialog gen = new GenericDialog("Result"); > gen.addMessage("center : ( "+finalAccumulator.getCentreX()+" , > "+finalAccumulator.getCentreY()+" )"); > gen.showDialog(); > } > } > > > -- > View this message in context: http://imagej.1557.n6.nabble.com/Help-required-for-scripting-tp4998786.html > Sent from the ImageJ mailing list archive at Nabble.com. |
Hello Jan,
Thank you for your quick reply. I tried your advice but it doesn't work in this state. I did declare ImagePlus imp; and PointRoi roi; after the public class but it is still not working. Do I miss something? Thanks, JB |
Hi,
On 24.05.2012 6:11 PM, jforien wrote: > Hello Jan, > Thank you for your quick reply. I tried your advice but it doesn't work in > this state. I did declare > ImagePlus imp; and PointRoi roi; after the public class but it is still not > working. Do I miss something? I got it to work by adding an IJ.getImage(); to get the current open image from ImageJ: PointRoi roi = new PointRoi(middleX,middleY); ImagePlus image = IJ.getImage(); image.setRoi(roi); image.updateAndDraw(); Of course, that might not be what you want if you run the plugin by scripting... Ideally, the run() method should be aware of the ImagePlus, but it only gets the current ImageProcessor ip. Maybe someone else could help here? Jan PS: I'm re-pasting the rest of the conversation here, so that others can still follow: > On 24.05.2012 3:38 PM, jforien wrote: >> I am using the hough center plugin ( >> http://w3.umons.ac.be/perso/Villers.Didier/Hough/Hough_Center.html >> http://w3.umons.ac.be/perso/Villers.Didier/Hough/Hough_Center.html ) and I >> would like to get the coordinate result as a point roi selection instead of >> having a cross drew on my picture. I think that it should not be so >> difficult but unfortunately my programing skill is too bad. Does anyone can >> help me? Script is displayed below. > > Replacing these two lines in the script you pasted: > >> ip.drawLine(middleX-5, middleY-5, middleX+5, middleY+5); >> ip.drawLine(middleX+5, middleY-5, middleX-5, middleY+5); > > by something like that: > > PointRoi roi = new PointRoi(middleX,middleY); > imp.setRoi(roi); > imp.updateAndDraw(); > > should do the trick. I didn't test the script you provided though. |
Free forum by Nabble | Edit this page |