Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按矩形的坐标对颜色进行排序_Java_Sorting_Colors - Fatal编程技术网

Java 按矩形的坐标对颜色进行排序

Java 按矩形的坐标对颜色进行排序,java,sorting,colors,Java,Sorting,Colors,我正在制作一个程序,在这个程序中,我拍摄了一张照片,找到照片中的正方形,然后确定它们的RGB值。我能够成功地获得这些信息,但当我找到RGB值和坐标时,它们按照我找到的轮廓的顺序出现。我的问题是,我怎样才能把颜色分类,让我像看书一样从左上角一行读到底行。每种颜色都有矩形坐标,我想用它来帮我排序 我有一张显示我想要点什么的图片 当我从图像中获得RGB值时,我打印出颜色和坐标。下面是它的外观(注意它们是如何不被订购的): 我用来获取它们的代码如下: 我只是不知道怎样才能把颜色分类存放 private

我正在制作一个程序,在这个程序中,我拍摄了一张照片,找到照片中的正方形,然后确定它们的RGB值。我能够成功地获得这些信息,但当我找到RGB值和坐标时,它们按照我找到的轮廓的顺序出现。我的问题是,我怎样才能把颜色分类,让我像看书一样从左上角一行读到底行。每种颜色都有矩形坐标,我想用它来帮我排序

我有一张显示我想要点什么的图片

当我从图像中获得RGB值时,我打印出颜色和坐标。下面是它的外观(注意它们是如何不被订购的):

我用来获取它们的代码如下:

我只是不知道怎样才能把颜色分类存放

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

}
private void getColors(Mat img2read、Rect roi){
int-rAvg=0,bAvg=0,gAvg=0;
int rSum=0,bSum=0,gSum=0;
img2read=新Mat(img2read,roi);//rect的图像大小(保存的轮廓大小和坐标)
img2read.convertTo(img2read,CvType.CV_8UC1);//转换为用于获取RGB数据的可用类型
int totalBytes=(int)(img2read.total()*img2read.channels());
字节buff[]=新字节[totalBytes];
int channels=img2read.channels();
img2read.get(0,0,buff);
int stride=channels*img2read.width();
对于(int i=0;i
我会将坐标映射到相应的RGB值,然后先按Y再按X坐标排序,以实现所需的“像书一样阅读”功能。

我会将坐标映射到相应的RGB值,然后先按Y再按X坐标排序,以实现所需的“像书一样阅读”功能功能。

将这些值放入对象中,并将其存储到
列表中。
要进行排序,您可以添加一个静态
比较器
字段,或者使其实现类似以下内容的
可比较

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<>();
 Color[] colors = blocks.stream().sorted().map(Block::getColor).toArray();
编辑以上比较,以满足您的需要。。。
使用Java8应该可以获得
颜色
的排序数组

但我不是一个河流专家。试着这样做:

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<>();
 Color[] colors = blocks.stream().sorted().map(Block::getColor).toArray();

要使其正常工作,需要使用Block.getColor()
方法,请参见上文。

将这些值放入对象,并将其存储到
列表中。
要进行排序,您可以添加一个静态
比较器
字段,或者使其实现类似以下内容的
可比较

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<>();
 Color[] colors = blocks.stream().sorted().map(Block::getColor).toArray();
编辑以上比较,以满足您的需要。。。
使用Java8应该可以获得
颜色
的排序数组

但我不是一个河流专家。试着这样做:

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<>();
 Color[] colors = blocks.stream().sorted().map(Block::getColor).toArray();


要使其正常工作,需要使用
Block.getColor()
方法,请参见上文。

将这些值放入对象中,并将其存储到
列表中
…是否需要对对象使用getter和setter@UsagiMiyamoto?你可以使用getter和setter,但不是必需的……我的问题是,我该如何像我在问题中所问的那样对它们进行排序,即基于与平均颜色相关的坐标的“书本效应”@usagimiyamoto将这些值放入一个对象中,并将它们存储到一个
列表中
…我需要为该对象使用getter和setter吗@UsagiMiyamoto?你可以使用getter和setter,但不是必需的……我的问题是,我该如何像我在问题中所问的那样对它们进行排序,即基于与平均颜色相关的坐标的“书本效应”@这就是我想做的,但不知道怎么做。我需要x和y在一本书中对它们进行分类,比如功能,我想我误解了。RGB值上方的数字是多少?这些是RGB值取自的坐标。这是那个正方形的xy。那么,按这些坐标排序不会仍然产生同样的效果吗?对坐标排序确实会产生同样的效果。每一个坐标都属于它所来自的图像,所以如果我根据它的x和y移动一个正方形,我必须用它移动那个颜色。这就是我想做的,但不知道如何做。我需要x和y在一本书中对它们进行分类,比如功能,我想我误解了。RGB值上方的数字是多少?这些是RGB值取自的坐标。它是那个正方形的xy,所以不会