Can you please check what is wrong with my project. I am trying to do a convolution on the following picture, but the error is connected with bounds. Please correct my code.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Can you please check what is wrong with my project. I am trying to do a convolution on the following picture, but the error is connected with bounds. Please correct my code.

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.Scanner;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class project {
public static void main(String[] args) throws IOException {
String imagePath = "C:\\Users\\hasib\\Downloads\\11″ TUFTEX BLACK.jpg";
int[][] matrix = {{1,0,-1},{2,0,-2},{1,0,-1}}; //edge detection
BufferedImage image = ImageIO.read(new File(imagePath));

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.setSize(750,750);
frame.setVisible(true);
BufferedImage image2 = Convolution(image,matrix);

JLabel label = new JLabel(new ImageIcon(image2));
frame.add(label);



}

public static BufferedImage Convolution (BufferedImage img, int[][] matrix){
BufferedImage convoluted = new BufferedImage(img.getWidth(),img.getHeight(),BufferedImage.TYPE_INT_ARGB);
for (int i=0; i<img.getWidth();i++) {
for (int j=0; j< img.getHeight(); j++) {
int red=0;
int blue=0;
int green=0;
int x= matrix.length/2;
int width= img.getWidth();
int height = img.getHeight();
for (int k = 0; k < matrix.length; k++) {
for (int l = 0; l < matrix[0].length; l++) {
int newX = (i - x)+k;
int newY = (j - x) + l;
if (newX>=0 && newY<width && newY>=0 && newY<height){
int a= img.getRGB(newX,newY);//not sure about coordinates
int ared = ((a>>16)& 0xff) * matrix[k][l];
int agreen = ((a>>8)& 0xff) * matrix[k][l];
int ablue = (a & 0xff) * matrix[k][l];
red += ared;
green += agreen;
blue += ablue;
}
}
}
red = Math.min(Math.max(red, 0), 255);
green = Math.min(Math.max(green, 0), 255);
blue = Math.min(Math.max(blue, 0), 255);
Color color = new Color(red, green, blue);
convoluted.setRGB(i, j, color.getRGB());
}
}
return convoluted;
}//last in the function convolution

}
Expert Solution
steps

Step by step

Solved in 5 steps with 6 images

Blurred answer
Knowledge Booster
Fundamentals of Testing Strategies
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education