Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Logical operation on Image

sachin upatil
Greenhorn
Posts: 29

In Java How to do logical operation on Image ?

e.g ANDing, ORing ....

Rob Spoor
Sheriff
Posts: 20709
68
What do you want to achieve exactly? Take the (A)RGB value of each pixel (x,y) in both images, perform the operation on those two values, then set the result of that operation for a new image?

If so, look at BufferedImage. It allows you to get and set individual pixel values using getRGB and setRGB. Use ImageIO.read to create BufferedImage objects from files.

Darryl Burke
Bartender
Posts: 5149
11
What do you want to achieve exactly?

Whatever it is he wants, it's probably achievable with AlphaComposite or BlendComposite. If so, there's no need to reinvent the wheel (unless of course if this is homework).

sachin upatil
Greenhorn
Posts: 29
I want to do this logical operation (as shown in image screenshot) on binary image ?

I did it using BufferedImage. I take the each pixel from two images and do logical operation on that pixel and setting that pixel.(used getRGB and setRGB).
the result i get attached as LogicalOPeration.

what's wrong with AND and OR operation in my result ?
Screenshot.png
LogicalOperation.PNG

Rob Spoor
Sheriff
Posts: 20709
68
Now we see what you want, AlphaComposite is what you should use. If we define IMG_1 as the source and IMG_2 as the destination, you can use the following rules:
- SRC_IN for AND
- SRC_OVER for OR
- CLEAR for XOR
- DST_OUT for NOT-AND

sachin upatil
Greenhorn
Posts: 29
Problem solved ....

First i converted image as Binary : if getRGB if RGB = 255: pixel = 0 else 1

Do operation on it then setRGB() : if result 1 = setRGB black else white.