Question: Can you check my Histogram.java? My logic there might be wrong and that's leading to incorrect similarity measurements Expected output for 'input _ files /

Can you check my Histogram.java? My logic there might be wrong and that's leading to incorrect similarity measurements
Expected output for 'input_files/correctfiles.txt''3''1'"
0.857143 or 0.714286
My output: for 'input_files/correctfiles.txt''3''1'"
0.428571
import java.util.List;
import java.util.ArrayList;
public class Histogram {
private double[] histogram;
public Histogram(double[] histogram){
this.histogram = histogram;
}
public double[] getData(){
return histogram;
}
public boolean isEmpty(){
return histogram == null || histogram.length ==0;
}
// Method to calculate the similarity score
public double similarity(Histogram other){
double similarityScore =0.0;
if (this.histogram.length != other.histogram.length){
throw new IllegalArgumentException("Histograms must have the same length for comparison.");
}
for (int i =0; i < this.histogram.length; i++){
similarityScore += Math.min(this.histogram[i], other.histogram[i]);
}
return similarityScore;
}
// Additional method to split the image into 4 quarters and calculate histograms for each
public static List getQuartersHistogram(double[][] imagePixels){
List quarters = new ArrayList<>();
// Divide the image into 4 quarters (each 64x64)
for (int quarter =0; quarter <4; quarter++){
double[][] quarterPixels = getQuarterPixels(imagePixels, quarter);
Histogram quarterHistogram = calculateNormalizedHistogram(quarterPixels);
quarters.add(quarterHistogram);
}
return quarters;
}
private static double[][] getQuarterPixels(double[][] imagePixels, int quarter){
double[][] quarterPixels = new double[64][64];
int startX =(quarter ==1|| quarter ==3)?64 : 0;
int startY =(quarter ==2|| quarter ==3)?64 : 0;
// Copy the 64x64 block of pixels into quarterPixels
for (int x =0; x <64; x++){
for (int y =0; y <64; y++){
quarterPixels[x][y]= imagePixels[startX + x][startY + y];
}
}
return quarterPixels;
}
public static Histogram calculateNormalizedHistogram(double[][] quarterPixels){
int[] histogram = new int[256]; // For 256 intensity levels
double totalPixels =64*64; // Total number of pixels in the quarter
// Ensure the pixel values fall within the 0-255 range
for (int x =0; x <64; x++){
for (int y =0; y <64; y++){
int pixelValue = Math.min(255, Math.max(0,(int) quarterPixels[x][y]));
histogram[pixelValue]++;
}
}
// Normalize the histogram
double[] normalizedHistogram = new double[256];
for (int i =0; i < histogram.length; i++){
normalizedHistogram[i]= histogram[i]/ totalPixels;
}
return new Histogram(normalizedHistogram);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!