Reputation: 1
Hi in this code in java I want to sort the pixels of the image through their brightness in java by using inversed selection sort however there is no update in the result image. how can i make it work?
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class Sort {
private BufferedImage image;
private int rgb;
int iterationCount ;
public Sort(BufferedImage image){
this.image = image;
this.iterationCount = 0;
}
public void ChainListWithBrightness(BufferedImage image) {
double[][] points = new double[image.getWidth()][image.getHeight()];
for (int i = 0; i < image.getWidth(); i++) {
for (int j = 0; j < image.getHeight(); j++) {
rgb = image.getRGB(i, j);
points[i][j] = pixelBrightness(rgb);
}
}
diagonalListingOfPoints(points);
}
private void diagonalListingOfPoints(double[][] points) {
ArrayList<Double> diagonalBrightnessValues = new ArrayList<>();
for (int m = 0; m < image.getWidth() + image.getHeight() - 1; m++) {
int i = Math.min(0, image.getWidth() - 1);
int j = Math.max(0, m - image.getWidth() + 1);
while (i >= 0 && j < image.getHeight()) {
diagonalBrightnessValues.add(points[i][j]);
i--;
j++;
}
}
selectionSort(diagonalBrightnessValues);
// Apply sorted brightness values back to the image
int index = 0;
for (int m = 0; m < image.getWidth() + image.getHeight() - 1; m++) {
int i = Math.min(0, image.getWidth() - 1);
int j = Math.max(0, m - image.getWidth() + 1);
while (i >= 0 && j < image.getHeight()) {
points[i][j] = diagonalBrightnessValues.get(index);
index++;
i--;
j++;
}
//iterationCount++;
}
for (int i = 0; i < image.getWidth(); i++) {
for (int j = 0; j < image.getHeight(); j++) {
Color oldColor = new Color(image.getRGB(i, j));
int red = (int) points[i][j];
int green = (int) points[i][j];
int blue = (int) points[i][j];
Color newColor = new Color(red, green, blue);
image.setRGB(i, j, newColor.getRGB());
}
}
iterationCount++;
}
public static void main(String[] args) throws IOException {
BufferedImage img = ImageIO.read(new File("C:\\Users\\jafar\\Documents\\semester3\\cs102-1\\Lab\\lab6\\skyy.jpg"));
Sort sort = new Sort(img);
sort.ChainListWithBrightness(img);
System.out.println("Sorting completed with " + sort.iterationCount + " iterations.");
// Display the sorted image
showImage(sort.image);
}
private static void showImage(BufferedImage image) {
JFrame frame = new JFrame("Sorted Image");
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
I was expecting an image which is sorted through its pixel brightness but the result is displaying the original image again to me
Upvotes: 0
Views: 30