Is there a way to write C/C++ plugins for ImageJ to do pixel-level number crunching?
Thanks, Neil -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Why write C/C++ for ImageJ? Why not just use Java and the ImageJ API? At
one time, people would have done this for speed, but these days Java is supposed to be nearly as fast as C/C++ On Mon, Aug 27, 2012 at 4:25 PM, Neil Fazel <[hidden email]>wrote: > Is there a way to write C/C++ plugins for ImageJ to do pixel-level number > crunching? > > Thanks, > Neil > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by Neil Fazel
Hi Neil,
On Mon, 27 Aug 2012, Neil Fazel wrote: > Is there a way to write C/C++ plugins for ImageJ to do pixel-level > number crunching? Have a look at http://fiji.sc/JNI, where I described very briefly what you need to do to use C/C++ code in ImageJ. Note, however, that it is in general a bad idea to write C/C++ plugins from scratch; if you do not have any pre-existing code, it is much better to write things in Java. There are pros and cons to Java, of course, but since ImageJ is *already* written in Java, you have those cons anyway. No need to incur the cons of native code, too... Ciao, Johannes -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Thanks, Johannes. I will follow your suggestion and try Java first.
Background: I have scanned images from Fuji imaging plate having 2000x6000 pixels. To process the image (convert from QL to PSL), I apply a power law formula to every pixel. I've done it in ijm and it's very slow. Neil On Aug 28, 2012, at 8:43 AM, Johannes Schindelin wrote: > Hi Neil, > > On Mon, 27 Aug 2012, Neil Fazel wrote: > >> Is there a way to write C/C++ plugins for ImageJ to do pixel-level >> number crunching? > > Have a look at http://fiji.sc/JNI, where I described very briefly what you > need to do to use C/C++ code in ImageJ. > > Note, however, that it is in general a bad idea to write C/C++ plugins > from scratch; if you do not have any pre-existing code, it is much better > to write things in Java. There are pros and cons to Java, of course, but > since ImageJ is *already* written in Java, you have those cons anyway. No > need to incur the cons of native code, too... > > Ciao, > Johannes -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
On 28/08/2012 16:27, Neil Fazel wrote:
> Background: I have scanned images from Fuji imaging plate having > 2000x6000 pixels. To process the image (convert from QL to PSL), I > apply a power law formula to every pixel. I've done it in ijm and > it's very slow. Could it be a problem with the particular algorithm that you are applying to each voxel? Best, José María Mateos. -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by Neil Fazel
Hi Neil,
On Tue, 28 Aug 2012, Neil Fazel wrote: > Thanks, Johannes. I will follow your suggestion and try Java first. > > Background: I have scanned images from Fuji imaging plate having > 2000x6000 pixels. To process the image (convert from QL to PSL), I apply > a power law formula to every pixel. I've done it in ijm and it's very > slow. It is probably not surprising that I'd recommend to use Fiji's Script Editor to develop that plugin? :-) (I am one of the authors, and solely responsible for its ability to compile and run Java classes). In your case, I would recommend to download Fiji from http://fiji.sc/, unpack it (if you're on Windows, I'd suggest to unpack it to the Desktop rather than Program Files, to avoid problems with Windows' funny permission system), start Fiji, hit 'l' to bring up the Command Finder (which used to be called the Command Launcher, hence the letter 'l'), type 'editor' and double-click on the Script Editor. In that Editor, choose Templates>Java>Process Pixels to get you started with a plugin processing, ahem, pixels. You can compile and run it by clicking on the appropriate menu item in the Run menu. The code should be pretty well-documented, and Java is pretty close to C anyway. There are a few caveats which I listed here, though: http://fiji.sc/Tips_for_C++_developers Ciao, Johannes -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Neil,
Have you tried implementing your power law via the build in/menu function Process/Math/Gamma? I tried this on my PC using a 2000x6000 image and it took ~.05 seconds. Also Process/Math other pixels functions that you have use in conjunction with Gamma. The documentation is as below. David Webster Applies the function *f*(*p*) = (*p*⁄255)*γ* × 255 to each pixel (*p*) in the image or selection, where 0.1 ≤ *γ* ≤ 5.0. For RGB images, this function is applied to all three color channels. For 16-bit images, the image min and max are used for scaling instead of 255. On Tue, Aug 28, 2012 at 8:42 AM, Johannes Schindelin < [hidden email]> wrote: > Hi Neil, > > On Tue, 28 Aug 2012, Neil Fazel wrote: > > > Thanks, Johannes. I will follow your suggestion and try Java first. > > > > Background: I have scanned images from Fuji imaging plate having > > 2000x6000 pixels. To process the image (convert from QL to PSL), I apply > > a power law formula to every pixel. I've done it in ijm and it's very > > slow. > > It is probably not surprising that I'd recommend to use Fiji's Script > Editor to develop that plugin? :-) (I am one of the authors, and solely > responsible for its ability to compile and run Java classes). > > In your case, I would recommend to download Fiji from http://fiji.sc/, > unpack it (if you're on Windows, I'd suggest to unpack it to the Desktop > rather than Program Files, to avoid problems with Windows' funny > permission system), start Fiji, hit 'l' to bring up the Command Finder > (which used to be called the Command Launcher, hence the letter 'l'), type > 'editor' and double-click on the Script Editor. In that Editor, choose > Templates>Java>Process Pixels to get you started with a plugin processing, > ahem, pixels. You can compile and run it by clicking on the appropriate > menu item in the Run menu. The code should be pretty well-documented, and > Java is pretty close to C anyway. There are a few caveats which I listed > here, though: http://fiji.sc/Tips_for_C++_developers > > Ciao, > Johannes > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by Neil Fazel
Hi Neil,
> I apply a power law formula to every pixel. I've done it in ijm and > it's very slow. The macro language is simply slow at accessing a large number of pixels. You will get several (at least 2, likely 3, more if parallelized) orders of magnitude speedup just by using Java. I did this several years ago and never looked back: going from 5 minutes to less than 1 second processing time was worth learning Java for. Michael -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by dscho
Hi Neil,
On Tue, 28 Aug 2012, Neil Fazel wrote: > Thanks for telling me about fiji. I downloaded fiji (on Mac) and > followed the steps you provided to open the template file. (It was > very straightforward.) I made some changes to the template, then > saved, and compiled. Then I opened a .txt image, and ran the > program. It popped up a dialog prompting for a couple of things, > which I don't think I use; I pressed OK, to use the default > values. It appeared to have done something (really fast), because > it displayed a message on the menu bar almost immediately about > the time it took. But the image pixel values seems unchanged. (I > moved the cursor over the image to check pixel values.) The change > I made to the code was simple; just a few lines. I also renamed > the class name to match the file name: > > // processing of GRAY16 images > public void process(short[] pixels) { > for (int y=0; y < height; y++) { > for (int x=0; x < width; x++) { > // process each pixel of the line > // example: add 'number' to each pixel > // pixels[x + y * width] += (short)value; > resolution=100; > sensitivity=4000; > ql=pixels[x + y * width]; > pixels[x + y * width] = (short) (Math.pow(resolution/100, 2) * (4000/sensitivity) * Math.pow(10, 5*(ql/65535 - 0.5)) ); > } > } > } Did you call image.updateAndDraw() after processing the pixels? Ciao, Johannes -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Hi Neil,
On Tue, 28 Aug 2012, Neil Fazel wrote: > On Aug 28, 2012, at 5:27 PM, Johannes Schindelin wrote: > > > Did you call image.updateAndDraw() after processing the pixels? > > Yes, that was already in the template and I left it there. Can you hover over the pixel values and verify that they changed? > How does one debug? I used Run->Start Debugging, with the image open, > but it popped up a dialog with the message "There is no image open." I > tried to quite, but it became unresponsive and I had to kill it. I tried > this several times. > > If I get the debugger to work, I should be able to figure out the rest. The debug mode of the script editor was never completed. The script editor itself was a Google Summer of Code project, and I always hoped that the student who worked on it would come back to fix the loose ends. If you want proper debugging, I'd like to suggest to use a proper full-blown IDE such as Netbeans or IntelliJ. To support these, we added some convenience functions and I described their usage here: http://fiji.sc/Debugging_intro#Debugging_plugins_in_an_IDE_.28Netbeans.2C_IntelliJ.2C_Eclipse.2C_etc.29 Ciao, Johannes -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Hi Neil,
On Tue, 28 Aug 2012, Neil Fazel wrote: > OK. I must be misunderstanding something about image types. I load a > .txt image in which all pixel values are smaller than 2^16. Still it > loads as a 32-bit image. Since I wasn't able to change anything on the > 32-bit image, I converted it to 16-bit image, and then was able to set > all pixels to 0 and get a dark image. So far so good. But when I try to > set the 16-bit pixels to a non-zero value (e.g. 100), the pixel values > remain 0. Don't understand this behavior. The best bet is now probably to send the code to the list (not me personally, I am not really a private helpdesk ;-). That way, you get the combined expertise of the ImageJ mailing list to help you... Ciao, Johannes -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by Neil Fazel
On Aug 28, 2012, at 10:27 AM, Neil Fazel wrote:
> Thanks, Johannes. I will follow your suggestion and try Java first. > > Background: I have scanned images from Fuji imaging plate having 2000x6000 pixels. To process the image (convert from QL to PSL), I apply a power law formula to every pixel. I've done it in ijm and it's very slow. Here is an example plugin that applies a formula to every pixel in an image or stack. It works with all image types (except RGB), it is multi-threaded when processing stacks and it supports Undo. import ij.*; import ij.process.*; import ij.plugin.filter.*; public class Apply_Formula implements PlugInFilter { ImagePlus imp; public int setup(String arg, ImagePlus imp) { this.imp = imp; return DOES_ALL-DOES_RGB+DOES_STACKS+PARALLELIZE_STACKS; } public void run(ImageProcessor ip) { int size = ip.getWidth()*ip.getHeight(); double scale = 1; if (imp.getBitDepth()==8) scale = 255; if (imp.getBitDepth()==16) scale = 65535; for (int i=0; i<size; i++) { double v1 = ip.getf(i); double v2 = Math.pow(10, v1/scale); ip.setf(i, (float)v2); } ip.resetMinAndMax(); } } -wayne > On Aug 28, 2012, at 8:43 AM, Johannes Schindelin wrote: > >> Hi Neil, >> >> On Mon, 27 Aug 2012, Neil Fazel wrote: >> >>> Is there a way to write C/C++ plugins for ImageJ to do pixel-level >>> number crunching? >> >> Have a look at http://fiji.sc/JNI, where I described very briefly what you >> need to do to use C/C++ code in ImageJ. >> >> Note, however, that it is in general a bad idea to write C/C++ plugins >> from scratch; if you do not have any pre-existing code, it is much better >> to write things in Java. There are pros and cons to Java, of course, but >> since ImageJ is *already* written in Java, you have those cons anyway. No >> need to incur the cons of native code, too... >> >> Ciao, >> Johannes > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by dscho
Hi Johannes,
You're right; I think I was trying to avoid being the source of excessive emails to the list. Wayne Rasband sent the following Java code snippet that does the job of rapidly converting a raw QL image to a PSL image. So I'm good now; thanks for your help. import ij.*; import ij.process.*; import ij.plugin.filter.*; public class Filter_Plugin implements PlugInFilter { ImagePlus imp; public int setup(String arg, ImagePlus imp) { this.imp = imp; return DOES_ALL; } public void run(ImageProcessor ip) { int size = ip.getWidth()*ip.getHeight(); double resolution = 100; double sensitivity = 4000; for (int i=0; i < size; i++) { double v1 = ip.getf(i); double v2 = Math.pow(resolution/100, 2) * (4000/sensitivity) * Math.pow(10, 5*(v1/65535-0.5)); ip.setf(i, (float)v2); } ip.resetMinAndMax(); } } Best regards, Neil On Aug 29, 2012, at 10:57 AM, Johannes Schindelin wrote: > Hi Neil, > > On Tue, 28 Aug 2012, Neil Fazel wrote: > >> OK. I must be misunderstanding something about image types. I load a >> .txt image in which all pixel values are smaller than 2^16. Still it >> loads as a 32-bit image. Since I wasn't able to change anything on the >> 32-bit image, I converted it to 16-bit image, and then was able to set >> all pixels to 0 and get a dark image. So far so good. But when I try to >> set the 16-bit pixels to a non-zero value (e.g. 100), the pixel values >> remain 0. Don't understand this behavior. > > The best bet is now probably to send the code to the list (not me > personally, I am not really a private helpdesk ;-). That way, you get the > combined expertise of the ImageJ mailing list to help you... > > Ciao, > Johannes -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Free forum by Nabble | Edit this page |