I am trying to take a colored image and make two different verisons of this imag
ID: 3593047 • Letter: I
Question
I am trying to take a colored image and make two different verisons of this image. I have figured out how to output a proper grayscale image but I cannot understand the logic of outputting the second verison which is a threshold image. I have identified my threshold value but cannot figure out how to properly output the image. In my code the threshold value is "maxInd". I have tried using an if else statement to make the output work but it is making an all white copy of the new grayscale and the new image. The code needs to look at the pixels and determine if the current pixel value is greater than or equal to the threshold amount, it would assign that pixel to 255, if it is less, it would assign it to zero. I have attached my code below:
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class GrayScale
{
BufferedImage image, thresHold;
int width;
int height;
int grayScale;
int maxInd = 0;
public GrayScale(){
try{
File input = new File("mysteryBird.jpg");
image = ImageIO.read(input);
width = image.getWidth();
height= image.getHeight();
int hist[] = new int[256];
for (int i = 0; i <height; i++){
for(int j = 0; j < width; j++){
Color c = new Color(image.getRGB(j,i));
int red = (int)(c.getRed() * 0.299);
int green =(int)(c.getGreen() * 0.587);
int blue = (int)(c.getBlue() * 0.114);
Color newColor = new Color(red+green+blue,red+green+blue,red+green+blue);
grayScale=red+blue+green;
hist[grayScale] += 1; // increment hist[grayScale] by 1
image.setRGB(j,i,newColor.getRGB());
if(grayScale >= maxInd){
Color thresh = new Color(255,255,255);
thresHold.setRGB(j,i,thresh.getRGB());
}
else {
Color thresh = new Color(0,0,0);
thresHold.setRGB(j,i,thresh.getRGB());
}
}
}
for(int k = 0;k<hist.length;k++){
System.out.println("pixel value: " +k+ " found " + hist[k]+" times");
}
// calculating threshold value
for (int i = 0; i < hist.length; ++i) {
if(hist[i] > hist[maxInd]) {
maxInd = i;
}
}
System.out.println(maxInd + " appeared most (" + hist[maxInd] + ") number of times");
System.out.println("So, Threshold value is " + maxInd);
File output = new File("grayscale.jpg");
ImageIO.write(image, "jpg", output);
File output2 = new File("threshold.jpg");
ImageIO.write(image, "jpg", output2);
} catch (Exception e){}
}
public static void main(String args[]) throws Exception{
GrayScale obj = new GrayScale();
}
}
Explanation / Answer
There were some errors in your code. First of all, the BufferedImage variable threshold was not initialized, and it was returning NullPointerException . And at the last, the statements ‘ImageIO.write(image, "jpg", output1);’ and ‘ImageIO.write(image, "jpg", output2);’ both were trying to save the same image in two names. But the main problem that was causing the image go full ‘white’ or ‘black’ was the maxInd variable which holds the threshold value. Actually, the initial value of maxInd = 0 ; is not changed before finding the thresholded image. i.e It remained as 0. Only after exiting from the loop, before printing the maxInd value; it got its real value. Therefore I’ve defined a function thresholdImage(BufferedImage image, int thresholdvalue) which takes an image and threshold value as inputs and returns a thresholded image. And called it after finding the real value of maxInd. Run the following code now.
//GrayScale.java file
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class GrayScale
{
BufferedImage image, thresHold;
int width;
int height;
int grayScale;
int maxInd = 0;
public GrayScale(){
try{
File input = new File("mysteryBird.jpg");
image = ImageIO.read(input);
width = image.getWidth();
height= image.getHeight();
int hist[] = new int[256];
for (int i = 0; i <height; i++){
for(int j = 0; j < width; j++){
Color c = new Color(image.getRGB(j,i));
int red = (int)(c.getRed() * 0.299);
int green =(int)(c.getGreen() * 0.587);
int blue = (int)(c.getBlue() * 0.114);
Color newColor = new Color(red+green+blue,red+green+blue,red+green+blue);
grayScale=red+blue+green;
hist[grayScale] += 1; // increment hist[grayScale] by 1
image.setRGB(j,i,newColor.getRGB());
}
}
for(int k = 0;k<hist.length;k++){
System.out.println("pixel value: " +k+ " found " + hist[k]+" times");
}
// calculating threshold value
for (int i = 0; i < hist.length; ++i) {
if(hist[i] > hist[maxInd]) {
maxInd = i;
}
} /*only now; we can say that the real threshold value is saved in maxInd*/
System.out.println(maxInd + " appeared most (" + hist[maxInd] + ") number of times");
System.out.println("So, Threshold value is " + maxInd);
thresHold=thresholdImage(image, maxInd); /* calling the function to convert an image into threshold; and it'll return the thresholded image*/
File output = new File("grayscale.jpg");
ImageIO.write(image, "jpg", output); /*saving the grayscale image*/
File output2 = new File("threshold.jpg");
ImageIO.write(thresHold, "jpg", output2); /*saving the thresholded image*/
} catch (IOException e){
System.out.println(""+e.getMessage());
}
}
public BufferedImage thresholdImage(BufferedImage image,int threshold){ /*Thresholding function*/
BufferedImage im=new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); /*creating a blank image with same height and width as the original image*/
int width = image.getWidth();
int height= image.getHeight();
int grayScale;
for (int i = 0; i <height; i++){
for(int j = 0; j < width; j++){
Color c = new Color(image.getRGB(j,i));
int red = (int)(c.getRed() * 0.299);
int green =(int)(c.getGreen() * 0.587);
int blue = (int)(c.getBlue() * 0.114);
Color newColor = new Color(red+green+blue,red+green+blue,red+green+blue);
grayScale=red+blue+green;
if(grayScale>=threshold){
Color thresh = new Color(255,255,255);
im.setRGB(j,i,thresh.getRGB());
}else{
if(grayScale>=threshold){
Color thresh = new Color(0,0,0);
im.setRGB(j,i,thresh.getRGB());
}
}
}
}
return im; /* thresholded image*/
}
public static void main(String args[]) throws Exception{
GrayScale obj = new GrayScale();
}
}
input image
grayscale image
thresholded image
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.