Login  Register

solution to Program 4.3?

Posted by Yisong Zhen on Jul 20, 2013; 12:20pm
URL: http://imagej.273.s1.nabble.com/solution-to-Program-4-3-tp5004050.html

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.

Yisong

//

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