Login  Register

Re: solution to Program 4.3?

Posted by Yisong Zhen on Jul 21, 2013; 2:26am
URL: http://imagej.273.s1.nabble.com/solution-to-Program-4-3-tp5004050p5004060.html

Dear Wayne,

The program4.3 in the book does set the background to white
(histIp.setValue(255)), but I then change the pixel value of the
corresponding histogram  by using the method , histIp.putPixel(u,v,0). My
understanding is that in this way, I change white color to black in
correspond region. You code about this histogram task is beautiful, but I
do want to know why I am wrong. Any further suggestion is greatly
appreciated.

Yisong

On Sun, Jul 21, 2013 at 3:21 AM, Rasband, Wayne (NIH/NIMH) [E] <
[hidden email]> wrote:

> 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
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html