如何从边界框值计算x,y坐标

时间:2020-03-04 07:09:22

标签: java coordinates bufferedimage bounding-box

我想使用Java中的BufferedImage.getSubimage(x,y,width,height)函数使用其x,y坐标裁剪图像。但是我只有图像的边框可以裁剪其中的一部分。

如何使用Java从边界框获取x,y坐标?有计算可用吗?

我正在提供边界框值(xMin,yMin,xMax,yMax)(0.46476197,0.46967554,0.8502463,0.67080903)enter image description here

2 个答案:

答案 0 :(得分:1)

如何使用Java从边界框获取x,y坐标?在那儿 有可用的计算方法吗?

如果您计算出的边界框坐标对应于图像分数,则首先必须计算xMin,xMax,yMin和yMax的像素值。

使用这些参数很容易为函数BufferedImage.getSubimage(x,y,width,height)计算必要的参数。

x和y对应于边界框的左上角,因此:

x = xMiny = yMin

可以使用图像宽度计算出框的宽度,然后减去导致框的左侧空间长度以及框结束处的右侧空间长度,因此可以使用以下公式进行计算:

width = imageWidth - xMin - (imageWidth - xMax)

高度相同,只需使用y坐标即可:

height = imageHeight - yMin - (imageHeight - yMax)

答案 1 :(得分:0)

I am multiplying bounding box values with image width and height respectively to get its pixel values. 

int y1 = yMin * ImageHeight;
int x1 = xMin * ImageWidth;
int y2 = yMax * ImageHeight;
int x2 = xMax * ImageWidth;

And applied the values to below given formula

BufferedImage.getSubimage((int)x1, (int)y1, (x2-x1), (y2-y1));

Thanks gilbert for giving solution to get pixel values.