Search for color in BufferedImage Java - java

I currently have a script that can create a bufferedimage of the screen, and then list the value for a specific pixel. However, I'm trying to search the entire bufferedimage for a specific color. Is there a way to do this?
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class Main {
public static void main(String args[]) throws IOException, AWTException {
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
int x = 10;
int y = 10;
int clr = image.getRGB(x, y);
int red = (clr & 0x00ff0000) >> 16;
int green = (clr & 0x0000ff00) >> 8;
int blue = clr & 0x000000ff;
System.out.println("Red = " + red);
System.out.println("Green = " + green);
System.out.println("Blue = " + blue);
}
}

You could use nested for loops for every (x, y) coordinate of the image (with x between 0 and image.getWidth(), and y between 0 and image.getHeight()) and compare if the color at the given position is equal to the color you are looking for.

Related

How do I make the for loops for this code? (I need to check a region of an image for a certain color.)

According to Get color of each pixel of an image using BufferedImages, the following code needs for loops for its intended purpose to work.
public class GetPixelColor
{
public static void main(String args[]) throws IOException{
File file= new File("your_file.jpg");
BufferedImage image = ImageIO.read(file);
// Getting pixel color by position x and y
int clr= image.getRGB(x,y);
int red = (clr & 0x00ff0000) >> 16;
int green = (clr & 0x0000ff00) >> 8;
int blue = clr & 0x000000ff;
System.out.println("Red Color value = "+ red);
System.out.println("Green Color value = "+ green);
System.out.println("Blue Color value = "+ blue);
}
}
How do I define the region of the image I would like to check for its color with for loops?
the region you want to read it's color is a rectangle between two points p1(x1,y1), p2(x2,y2)
and you scan that rectangle by two nested for loops like this
for(int x=x1; x<=x2; x++)
for(int y=y1; y<=y2; y++){
// Getting pixel color by position x and y
int clr= image.getRGB(x,y);
int red = (clr & 0x00ff0000) >> 16;
int green = (clr & 0x0000ff00) >> 8;
int blue = clr & 0x000000ff;
System.out.println("Red Color value = "+ red);
System.out.println("Green Color value = "+ green);
System.out.println("Blue Color value = "+ blue);
}
You can simply use two nested for loops to loop through the image, something like this:
public static void printPixelColors(BufferedImage img) {
int imageWidth = img.getWidth();
int imageHeight = img.getHeight();
for (int y = 0; y < imageHeight; y++) {
for (int x = 0; x < imageWidth; x++) {
// Getting pixel color by position x and y
int clr = img.getRGB(x, y);
int red = (clr & 0x00ff0000) >> 16;
int green = (clr & 0x0000ff00) >> 8;
int blue = clr & 0x000000ff;
System.out.println("Red Color value = " + red);
System.out.println("Green Color value = " + green);
System.out.println("Blue Color value = " + blue);
}
}
}

Converting certain pixels' RGB color to another color

Programming Language: Java
I am trying to convert certain pixels of this image to a different color to reveal a "Secret Message."
Most of the pixels are:
Red = 0, Green = 64, Blue = 0
The pixels that I want changed to R = 255, G = 255, B = 255 are:
Red = 5, Green = 64, Blue = 5
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.Color;
public class ASSN2p2
{
private static int makeRGBColor(int red, int green, int blue)
{
int rgb = 0;
rgb = red*65536 + green*256 + blue;
return rgb;
}
private static int getRed(int pixel)
{
return (pixel >> 16) & 0xFF;
}
private static int getGreen(int pixel)
{
return (pixel >> 8) & 0xFF;
}
private static int getBlue(int pixel)
{
return (pixel) & 0xFF;
}
public static void main(String args[]) throws IOException
{
// int width = 300;
// int height = 200;
BufferedImage image = null;
File f = null;
try
{
f = new File("D:\\2016-2017\\Fall2016\\201_CSCE_Programming\\Assignment 2\\secretmessage.png");
image = ImageIO.read(f);
image = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
System.out.println("Reading Complete");
BufferedImage output = new BufferedImage(image.getWidth(), image.getHeight(),BufferedImage.TYPE_INT_ARGB);
//--------------------------------------
//-------------------------------------------
for (int y = 0; y < image.getHeight(); y++)
{
for (int x = 0; x < image.getWidth(); x++)
{
int pixel = image.getRGB(x, y);
int r,g,b;
r = getRed(pixel);
g = getGreen(pixel);
b = getBlue(pixel);
if ((r == 5) && (g == 64) && (b == 5))
{
r = 64;
b = 64;
b = 64;
image.setRGB(x,y,makeRGBColor(r,g,b));
} }
}
}
catch(IOException e)
{
System.out.println("Error: "+e);
}
// printPixelARGB(pixel);
// System.out.println("");
try
{
f = new File("D:\\2016-2017\\Fall2016\\201_CSCE_Programming\\Assignment 2\\output.png");
ImageIO.write(image, "png", f);
System.out.println("Writing Complete");
}
catch(IOException e)
{
System.out.println("Error: "+e);
}
}
}
This produces this:
I am having trouble understanding what has gone wrong!
Any assistance will be appreciated!
I believe that you're missing the alpha component of your new color. Change your code to:
private static int makeRGBColor(int red, int green, int blue) {
int rgb = 0xff000000 | (red << 16) | (green << 8) | blue;
return rgb;
}
to make sure that the alpha is set to full, otherwise your image will look completely transparent.

How to clear pixel of BufferedImage in java?

In java, I read an image and then go through the pixels and if its color distance is < 30, then I want to clear the image by changing its alpha to 0. This is my code:
But this is not working. It is having no effect...
Does anyone see the problem?
Thanks
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.imageio.ImageIO;
public class Recognize {
public static void main(String args[]) throws IOException {
Path path = Paths.get("images/fish.png");
File file = path.toFile();
if (file.exists()) {
InputStream stream = Files.newInputStream(path);
BufferedImage bufferedImage = ImageIO.read(stream);
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
if (width > 0 && height > 0) {
int TLpixel = bufferedImage.getRGB(0, 0);
Color TLcolor = new Color(TLpixel);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int pixel = bufferedImage.getRGB(i, j);
Color color = new Color(pixel);
double distance = ColourDistance(TLcolor, color);
//System.out.println(distance);
if (distance < 30) {
int mc = (0 << 24) | 0x00ffffff;
int newcolor = pixel & mc;
bufferedImage.setRGB(i, j, newcolor);
}
}
}
File outputfile = new File("images/fish_new.png");
ImageIO.write(bufferedImage, "png", outputfile);
}
}
}
public static int[] printPixelARGB(int pixel) {
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
return new int[] {red, green, blue, alpha};
}
public static double ColourDistance(Color c1, Color c2) {
double rmean = ( c1.getRed() + c2.getRed() )/2;
int r = c1.getRed() - c2.getRed();
int g = c1.getGreen() - c2.getGreen();
int b = c1.getBlue() - c2.getBlue();
double weightR = 2 + rmean/256;
double weightG = 4.0;
double weightB = 2 + (255-rmean)/256;
return Math.sqrt(weightR*r*r + weightG*g*g + weightB*b*b);
}
}
By "clearing" the pixel you obviously meant "to make the pixel transparent".
In order to be able to make a pixel transparent, the image has to support transparent pixels. Whether or not a BufferedImage supports transparent pixels depends on the type of the BufferedImage. After loading a BufferedImage with ImageIO, you hardly know the type of the image. But you can easily convert the image into an image with a known type (that supports transparency) by passing it to a method like this:
public static BufferedImage convertToARGB(BufferedImage image)
{
BufferedImage newImage = new BufferedImage(
image.getWidth(), image.getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = newImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return newImage;
}

how to mask color channels in java?

I'm trying to write a code that masks red and blue channel from the image inputed. I have retrieved R, G, B values but however stuck up in proceeding further. Can anyone help me in this please?
public class Green {
public static void main(String args[]) throws IOException {
BufferedImage bi = ImageIO.read(new File("image.jpg"));
for (int x = 0; x <= bi.getWidth(); x++) {
for (int y = 0; y <= bi.getHeight(); y++) {
int pixelCol = bi.getRGB(x, y);
int r = (pixelCol >> 16) & 0xff;
int b = pixelCol & 0xff;
int g = (pixelCol >> 8) & 0xff;
int px = 0;
px = (px | (g << 8));
bi.setRGB(x, y, px);
}
}
}
}
Some remarks:
Use an IDE (Integrated Development Environment) like Eclipse or NetBeans.
Use < instead of <= as condition in your for loops.
Use code formatting (that is a feature of the IDE)
For clearness, order r, g and b.
int r = (color >> 16) & 0xff;
int g = (color >> 8) & 0xff;
int b = (color >> 0) & 0xff;
Since you said you were stuck, the only thing left to do is save the manipulated image:
ImageIO.write(bi, "JPG", new File("green.jpg"));
A little trick to perform the mask quickly is this:
bi.setRGB(x, y, bi.getRGB(x, y) & 0xff00ff00);
So, the clean working code should be this:
public class Green
{
public static void main(String args[]) throws IOException
{
/* Read the image */
BufferedImage bi= ImageIO.read(new File("image.jpg"));
/* Loop through all the pixels */
for (int x=0; x < bi.getWidth(); x++)
{
for (int y = 0; y < bi.getHeight(); y++)
{
/* Apply the green mask */
bi.setRGB(x, y, bi.getRGB(x, y) & 0xff00ff00);
}
}
/* Save the image */
ImageIO.write(bi, "JPG", new File("green_mask.jpg"));
}
}
In addition to copying and scaling images, the Java 2D API also filter an image. Filtering is drawing or producing a new image by applying an algorithm to the pixels of the source image.
Image filters can be applied by using the following method:
void Graphics2D.drawImage(BufferedImage img,
BufferedImageOp op,
int x, int y)
The BufferedImageOp parameter implements the filter.
See this document for Image filter examples: http://ptgmedia.pearsoncmg.com/images/9780132413930/samplechapter/0132413930_CH08.pdf

Writing java program about RGB-CMY

How would you write a simple Java program that converts RGB to CMY?...or you could give me some hints on how to write it?
RGB to/from CMY
Converting from RGB to CMY is simply the following
C = 1 - R
M = 1 - G
Y = 1 - B
Please refer the below for further information
http://paulbourke.net/texture_colour/convert/
Will add in
CYAN - 1 = RED
MAGENTA - 1 = GREEN
YELLOW - 1 = BLUE
from will the will
trade from rgb
RED - 1 = CYAN
GREEN - 1 = MAGENTA
BLUE - 1 = CYAN
I am tring to convert a specifig picture as well, which is given. I kbow whow ic can calculate the CMY Value from RGB. I am reading the given picture (what is in RGB color space) pixelwise, calculate it to CMY and then whant to save it as a CMY picture.
Here is my code for Java:
import java.awt.Color;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Aufgabe2c {
public static void main(String[] args) {
try {
BufferedImage image = ImageIO.read(new File("blumen.bmp"));
iterateThroughImageToGetPixel(image);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void iterateThroughImageToGetPixel(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
System.out.println("width, height: " + width + ", " + height);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
System.out.println("x,y: " + j + ", " + i);
int pixel = image.getRGB(j, i);
getPixelARGB(pixel);
System.out.println("");
}
}
}
/*
* Quelle: https://alvinalexander.com/blog/post/java/getting-rgb-values-for-each-pixel-in-image-using-java-bufferedi
* http://openbook.rheinwerk-verlag.de/javainsel9/javainsel_20_006.htm#mj4c12381d5bacf8fb6ee31448d26890bb
*/
public static void getPixelARGB(int pixel) {
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
convertRGBToCMY(red, green, blue);
System.out.println("argb: " + alpha + ", " + red + ", " + green + ", " + blue);
}
public static void convertRGBToCMY(int red, int green, int blue) {
int[] cmyArray = new int[3];
//cyan
cmyArray[0] = 255 - red;
//magenta
cmyArray[1] = 255 - green;
//yellow
cmyArray[3] = 255 - blue;
Color col = new Color(new ColorSpace(), components, alpha)
// BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
// ImageIO.write(image, "bmp", new File("blumen_cym.bmp") ); // Save as BMP
// System.out.println("argb: "+ red + ", " + green + ", " + blue);
}
}

Categories