首页 > 解决方案 > Java 使用 jframe 在图像上绘制

问题描述

所以我看过这些问题

更改图像java中每个像素的颜色

如何将 Drawable 转换为位图?

如何打开图像以在其上绘图

所以我有一个程序,它在一个while循环中拍摄屏幕截图,然后检查每个像素的图像颜色,效果很好,但问题是它检测到图像中的所有颜色。我使用了这个答案:Java color detection 但是当例如拍摄这张图片时在此处输入图像描述

我只想看到绿色方块而不是松散的像素,然后在上面画一些东西,这样我就可以看到我的程序是否理解它只需要定位那个方块。

那么解决这个问题的最佳方法是什么?

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 ;-;)

是解决这个问题的最好方法吗?或者是否有可以为您检测到的外部 Java 库?

更新:

所以在做了一些编程之后,我想出了这个问题,无论如何,这可能不是最好的方法,但它似乎是在找到黄色方块

在此处输入图像描述

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;
}
}

仍然需要制作 YellowFound 功能模块,以便我可以在所有这些和更合适的测试上使用它

标签: javaimageimage-processing

解决方案


推荐阅读