按矩形

时间:2017-08-19 03:10:50

标签: java sorting colors

我正在制作一个程序,在那里我拍照并找到该照片中的方块,然后确定它们的RGB值。我能够成功地获得这些信息但是当我找到RGB值和我从中得到它们的坐标时,它们按照我发现它们的轮廓顺序排列。我的问题是我如何才能采取颜色并按照我从左上角行到底行的方式对它们进行排序。每种颜色都有从中取出的矩形坐标,我想用它来帮助我订购它们。

我的图像显示了我想要订购的内容。

image I want to order

当我从图像中获取RGB值时,我会打印出颜色和坐标。这是它的样子(注意它们是如何订购的):

279,271
R: 106 G: 255 B: 183 

188,270
R: 107 G: 255 B: 180 

371,267
R: 102 G: 255 B: 169 

274,178
R: 75 G: 255 B: 99 

185,179
R: 84 G: 255 B: 103 

369,175
R: 89 G: 255 B: 105 

277,88
R: 96 G: 255 B: 103 

184,85
R: 101 G: 255 B: 110 

370,84
R: 124 G: 255 B: 126 

我用来获取它们的代码在这里:

我只是不知道如何将颜色分类并存储。

private void getColors(Mat img2read , Rect roi){
    int rAvg = 0 , bAvg = 0, gAvg = 0;
    int rSum = 0, bSum = 0, gSum = 0;

    img2read = new Mat(img2read, roi); //image size of rect (saved contour size and coordinates)
    img2read.convertTo(img2read, CvType.CV_8UC1); //convert to workable type for getting RGB data

    int totalBytes = (int) (img2read.total() * img2read.channels());
    byte buff[] = new byte[totalBytes];
    int channels = img2read.channels();

    img2read.get(0,0,buff);
    int stride = channels * img2read.width();
    for(int i = 0; i < img2read.height();i++){
        for(int x = 0; x < stride; x+= channels){
            int r = unsignedToBytes(buff[(i * stride) + x]);
            int g = unsignedToBytes(buff[(i * stride)+ x + 1]);
            int b = unsignedToBytes(buff[(i * stride)+ x + 2]);

            rSum += r;
            gSum += g;
            bSum += b;

        }
    }
    float[] hsv = new float[3];

    rAvg = (int) (rSum / img2read.total());
    gAvg = (int) (gSum /  img2read.total());
    bAvg = (int) (bSum / img2read.total());


    Color.RGBtoHSB(rAvg, gAvg, bAvg, hsv);

    hsv[2] = 1; //Set to max value

    int rgb = Color.HSBtoRGB(hsv[0], hsv[1], hsv[2]);

    Color brightenedColor = new Color(rgb);
    System.out.println();
    System.out.println(roi.x + "," + roi.y);
    System.out.printf("R: %s G: %s B: %s \n", brightenedColor.getRed(), brightenedColor.getGreen(), brightenedColor.getBlue());     
    Color colorToAdd = new Color(brightenedColor.getRed(), brightenedColor.getGreen(), brightenedColor.getBlue());
    Point coords = new Point(roi.x, roi.y);
    //colorCoords.put(coords, colorToAdd);
    /*counter++;
    if(counter == 9){
        sortColors(colorCoords);
    }*/
}

int counter = 0;

Map <Point, Color> colorCoords = new TreeMap<>();
Color[] colorArray = new Color[54];
int currentIndex = 0;

//Not really sure what I'm doing here but I don't think it is right.
private void sortColors(Map<Point, Color> colorCoords){

    for(int i = 0; i < colorCoords.size();i++){
    //  colorCoords.get(key)
    }

}

2 个答案:

答案 0 :(得分:2)

将这些值放入对象中,并将它们存储到using中。 要进行排序,您可以添加一个静态List字段,或者让它实现Comparator这样的内容:

Comparable

要排序,您可以使用

public class Block implement Comparable<Block> {
   public int x, y;
   public Color color;
   @Override
   public int compareTo(Block other) {
      if (null==other) throw new NullPointerException();
      // implement the logic, like:
      // this will compare X, unless difference in Y is more than EG 10
      return Math.abs(y - other.y) > 10 ? y - other.y : x - other.x;
   }
   public Color getColor() { return color; }
}

public List<Block> blocks = new ArrayList<>();

编辑上述比较以满足您的需求......

使用 Java8 ,应该可以获得Collections.sort(blocks); 的排序数组。

但我不是溪流专家。尝试这样的事情:

Color

为了实现此目的,需要 Color[] colors = blocks.stream().sorted().map(Block::getColor).toArray(); 方法,请参见上文。

答案 1 :(得分:0)

我会将坐标映射到相应的RGB值,然后先按Y然后按X坐标进行排序,以实现您想要的&#34;像书一样阅读&#34;功能。