Posted by
Rasband, Wayne (NIH/NIMH) [E] on
Jul 20, 2013; 7:21pm
URL: http://imagej.273.s1.nabble.com/solution-to-Program-4-3-tp5004050p5004058.html
On Jul 20, 2013, at 8:20 AM, Yisong Zhen wrote:
> Dear all,
>
> I just read the book 'Digital Image Processing - An Algorithmic
> Introduction using Java'. There is an assignment on program 4.3 which ask
> to finish a task to compute the histogram of 8-bit-gray-scale image and
> display it.. I finished the left part and completed the code to create the
> histogram image. But failed (compiled successfully) and the result did not
> show histogram as expected. Here is the code:
>
> the red colored is my own code for this project. Since my understanding of
> Java and imageJ is limited, please give me some suggestions of why there is
> no output image of histogram. Thanks in advance.
One reason there is no output is that the plugin is drawing in white on a white background. Here is a version that works with all image types. It uses the histogram returned by getStatistics() because that histogram always has 256 bins. It implements the PlugIn interface because it is not a filter and because the PlugIn interface is simpler.
import ij.*;
import ij.plugin.PlugIn;
import ij.process.*;
import java.awt.Color;
public class Create_New_Image implements PlugIn {
public void run(String arg) {
ImagePlus imp = IJ.getImage();
int w = 256;
int h = 100;
ImageStatistics stats = imp.getStatistics();
int[] hist = stats.histogram;
ImageProcessor histIp = new ByteProcessor(w, h);
histIp.setColor(Color.white);
histIp.fill();
histIp.setColor(Color.black);
int max = hist[stats.mode];
double scale = (double)h/max;
for (int i=0; i<hist.length; i++)
histIp.drawLine(i, h, i, h-(int)(hist[i]*scale));
String title = "Histogram of " + imp.getTitle();
new ImagePlus(title, histIp).show();
}
}
> import ij.ImagePlus;
> import ij.plugin.filter.PlugInFilter;
> import ij.process.ByteProcessor;
> import ij.process.ImageProcessor;
>
> /*
> * This plugin demonstrates how to create and display a
> * new byte image
> */
>
> public class Create_New_Image implements PlugInFilter {
> String title = null;
>
> public int setup(String arg, ImagePlus im) {
> title = im.getTitle();
> return DOES_8G + NO_CHANGES;
> }
>
> public void run(ImageProcessor ip) {
> int w = 256;
> int h = 100;
> int[] hist = ip.getHistogram();
> int[] hist2= new int[256];
> ImageProcessor histIp = new ByteProcessor(w, h);
> histIp.setValue(255); // white = 255
> histIp.fill();
>
> // draw the histogram values as black bars in ip2 here,
> // for example, using histIp.putpixel(u,v,0)
> // ...
> int max = hist[0];
> for (int i =1;i<hist.length;i++) {
> if(hist[i] > max) {
> max = hist[i];
> }
> }
> // shrink to 0.8 of the original;
> for(int i=0;i<hist2.length;i++){
> hist2[i]= (hist[i]/max)*4*100/5;
> }
> for(int i=0;i<hist2.length;i++){
> for(int j= 100 - hist2[i];j<h;j++){
> histIp.putPixel(i,hist2[j],0);
> }
> }
> // display histogram:
> String hTitle = "Histogram of " + title;
> ImagePlus histIm = new ImagePlus(hTitle, histIp);
> histIm.show();
> //histIm.updateAndDraw();
> }
> }
--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html