## Converting multiple banded image into single banded image... Image enhancement

I am trying to do image enhancement by histogram equalization using the below code. As of now i am not able to view the output be it correct or wrong.

The error thrown is regarding the input image being a 3 banded image. And ROI needs a single banded image. So i guess i need to convert my 3 banded image into 1 banded image. So how should i do that?

If you have a better code for image enhancement please provide me.

Here goes the code and the input image is "ww.jpg"

import java.awt.image.*;
import javax.media.jai.*;
import javax.media.jai.iterator.*;

class SubstitutePixelValues
{
public static void main (String[] args)
{

// Get the image dimensions.
int width = img.getWidth();
int height = img.getHeight();

// Get the number of bands on the image.
SampleModel sm = img.getSampleModel();
int numBands = sm.getNumBands();

int binCount=1;
System.out.println(numBands);

// Allocate histogram memory.
int[] numBins = new int[numBands];
double[] lowValue = new double[numBands];
double[] highValue = new double[numBands];
for(int i = 0; i < numBands; i++)
{
numBins[i] = binCount;
lowValue[i] = 0.0;
highValue[i] = 255.0;
}

// Create the Histogram object.
Histogram hist = new Histogram(numBins, lowValue, highValue);

// Set the ROI to the entire image.
ROI roi = new ROI(img);

// Create the histogram op.
RenderedOp histImage = JAI.create("histogram", img, hist, roi, new Integer(1), new Integer(1));

// Retrieve the histogram.
hist = (Histogram)histImage.getProperty("histogram");

// Create an equalization CDF.
float[][] CDFeq = new float[numBands][];
for(int b = 0; b < numBands; b++) {
CDFeq[b] = new float[binCount];
for(int i = 0; i < binCount; i++) {
CDFeq[b][i] = (float)(i+1)/(float)binCount;
}
}

// Create a normalization CDF.
double[] mean = new double[] {128.0, 128.0, 128.0};
double[] stDev = new double[] {64.0, 64.0, 64.0};
float[][] CDFnorm = new float[numBands][];
for(int b = 0; b < numBands; b++) {
CDFnorm[b] = new float[binCount];
double mu = mean[b];
double twoSigmaSquared = 2.0*stDev[b]*stDev[b];
CDFnorm[b] =
(float)Math.exp(-mu*mu/twoSigmaSquared);
for(int i = 1; i < binCount; i++) {
double deviation = i - mu;
CDFnorm[b][i] = CDFnorm[b][i-1] +
(float)Math.exp(-deviation*deviation/twoSigmaSquared);
}
}
for(int b = 0; b < numBands; b++) {
double CDFnormLast = CDFnorm[b][binCount-1];
for(int i = 0; i < binCount; i++) {
CDFnorm[b][i] /= CDFnormLast;
}
}

// Create a histogram-equalized image.
RenderedOp eq = JAI.create("matchcdf", ".jpg", CDFeq);

// Create a histogram-normalized image.
RenderedOp nm = JAI.create("matchcdf", ".jpg", CDFnorm);

}
}