So I have looked at these questions
Change the color of each pixel in an image java
How to convert a Drawable to a Bitmap?
How to oepn an image to draw on that
So I have a program that takes a screen shots in a while loop and then checks the color of the image per pixel, which works fine, but the problem is it detects all the color in the image. I used this answer: Java colour detection
But when for example taking this image
I only want to see the green square but not the loose pixels, and then draw something over it so I can see if my program understands that it only needs to target that square.
So what would be the best way to approach this problem?
pseudo code
Step 1: Detect the color
Step 2: Save the position of the pixel
Step 3: Then when ever the same pixel is found Look for the distance between ?
Step 4: if the distance is below certain amount look at the third up coming pixel locations
Step 5: if step 4 fails look at the last found pixel and repeat step 2
Step 6: if its a row of mutiple pixels in a row save the locations and start looking at the same X but different y to see if its true (But should also work for circles ;-;)
Is the best way to address this problem? Or is there an external java libary that can detect this for you?
Update:
So after doing some programming I came up with this what is prob not the best way to do this in anyway but its seems to be finding the yellow square
package src;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.*;
import java.awt.*;
import javax.swing.*;;
import java.util.List;
import java.util.Arrays;
public class ColorDectection
{
private static ArrayList<ArrayList<Integer>> yellowList = new ArrayList<ArrayList<Integer>>();
private static ArrayList<Integer> tmpYellowList = new ArrayList<Integer>();
private static int tmpYellowPixelX = -1;
/*
@params Image img
@params int w
@params int h
Confert notmal image to BufferedImage
Then set the width and height to image pixels cause image is always full screen
Loop all the pixels
*/
public void detectColorInImage(Image img, int w, int h) {
BufferedImage conImg = convertImage(img);
int[][] pixels = new int[w][h];
for( int i = 0; i < w; i++ )
for( int j = 0; j < h; j++ )
selectColor(conImg, i,j);
handleYellowList();
}
/*
@params Image img
@params int pixelX
@params int pixelY
get int rgb of pixel on location
transfer RGB to HSF
if color is found give pixel X and Y to the corresponding function
*/
private void selectColor(BufferedImage img, int pixelX, int pixelY){
int rgb = img.getRGB(pixelX,pixelY);
float hsb[] = new float[3];
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = (rgb ) & 0xFF;
Color.RGBtoHSB(r, g, b, hsb);
if (hsb[1] < 0.1 && hsb[2] > 0.9) whiteFound();
else if (hsb[2] < 0.1) blackFound();
else {
float deg = hsb[0]*360;
if (deg >= 30 && deg < 90) yellowFound(pixelX, pixelY);
else if (deg >= 90 && deg < 150) greenFound();
else if (deg >= 150 && deg < 210) cyanFound();
else if (deg >= 210 && deg < 270) blueFound();
else if (deg >= 270 && deg < 330) magentaFound();
else redFound();
}
}
private void handleYellowList(){
// System.out.println(yellowList);
// Step 1: Check if there is an item in the array
// Step 2: Save the position of the pixel
// Step 3: Then when ever the same pixel is found Look for the distance between ?
// Step 4: if the distance is below certain amount look at the third up coming pixel locations
// Step 5: if step 4 fails look at the last found pixel and repeat step 2
// Step 6: if its a row of mutiple pixels in a row save the locations and start looking at the same X but different y to see if its true (But should also work for circles ;-;)
}
private void blackFound(){
//
}
private void whiteFound(){
//
}
/*
@params int pixelX
@params int pixelY
if is this is NOT the first time in the loop && of the pixelX and tmp pixel are not the same
then set tmpYellowPixelX to pixelX
1. add Pixel y to the list!
2. and if the tempArray list is smaller then 20 dont add them to the current list cuz its prob alone pixel!
3. add the new pixelY!
4. create new arraylist cuz TempPixel and PixelX were not the same so we must have been on a new row!
else if Check if its the first iteration
1. just add set tmpYellowPixelX to pixelX
2. add pixelY to the tmpYellowList
else just add pixelY to tmpYellowList
if color is found give pixel X and Y to the corresponding function
*/
private void yellowFound(int pixelX, int pixelY){
if (tmpYellowPixelX != pixelX && tmpYellowPixelX != -1){
tmpYellowPixelX = pixelX;
tmpYellowList.add(pixelY);
if(tmpYellowList.size() > 50) {
yellowList.add(tmpYellowList);
System.out.println(pixelX);
System.out.println(tmpYellowList.size());
}
tmpYellowList = new ArrayList<Integer>();
} else if (tmpYellowPixelX != pixelX && tmpYellowPixelX == -1 ) {
tmpYellowPixelX = pixelX;
tmpYellowList.add(pixelY);
} else {
tmpYellowList.add(pixelY);
}
}
private void greenFound(){
//
}
private void cyanFound(){
//
}
private void blueFound(){
//
}
private void magentaFound(){
//
}
private void redFound(){
//
}
private static BufferedImage convertImage(Image img) {
if (img instanceof BufferedImage) return (BufferedImage) img;
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
return bimage;
}
}
Still Need to make yellowFound function modulair so I can use it on all of them and more propper testing