Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1.1 Loading an image The javax.imageio.ImageIO utility class can read various image formats, including png and jpg. It returns a BufferedImage object. BufferedImage image =

1.1 Loading an image The javax.imageio.ImageIO utility class can read various image formats, including png and jpg. It returns a BufferedImage object. BufferedImage image = ImageIO.read(new File("res/lenna.png")); You must also handle the IOException. When running in Eclipse, the path is relative to the root of your project. I usually create a "res" folder for images and other resources. 1.2 Displaying an image The simplest way to display a BufferedImage to the user is to use a message dialog from the JOptionPane utility class. To make it easier to use, Ive written a small wrapper function around it. Pass it the BufferedImage and a title. public static void showImage(Image image, String title) { JOptionPane.showMessageDialog(null, new ImageIcon(image), title, JOptionPane.PLAIN_MESSAGE); } 1 1.3 Processing each pixel The following loop will help you process each pixel. // for each pixel row for (int y = 0; y < image.getHeight(); y++) { // for each pixel in the row for (int x = 0; x < image.getWidth(); x++) { // decompose the color in 4 components int argb = image.getRGB(x, y); int a = (argb >> 24) & 0xFF; int r = (argb >> 16) & 0xFF; int g = (argb >> 8) & 0xFF; int b = (argb >> 0) & 0xFF; // TODO modify the color values // reconstruct the argb integer argb = a << 24 | r << 16 | g << 8 | b; image.setRGB(x, y, argb); } } Note that the source image is modified in-place. Also always make sure that a, r, g, and b are within [0, 255]. 1.4 Image Processing 1.4.1 Grayscale If red, green and blue are all the same value, the color is grayscale (i.e. white, 253 shades of gray, black). It you average the red, green and blue color value, and use that average instead of r, g, b when reconstructing the color, you effectively convert a color image to grayscale. Try it out. There is a better (and almost as simple) method based on human perception of luminosity. For more information, see: https://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/ 1.4.2 Invert If you invert the color values you will get interesting results. Inverting means that a value of 0 becomes 255, and 255 becomes 0. The formula is simple: r = 255 - r; Try it out. 1.4.3 Black and white Similar to grayscale, you can convert an image to stritcly black or white colors. Calculate the gray value, then use a threshold like 128 to decide if the gray value should be 0 or 255 instead. gray = gray < 128? 0 : 255; Try it out.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

305 mg of C6H12O6 in 55.2 mL of solution whats the molarity

Answered: 1 week ago