简体   繁体   中英

How to calculate x,y coordinates from bounding box values

I want to crop an image using its x,y coordiates using BufferedImage.getSubimage(x,y,width,height) function in java. But i only have bounding box of an image to crop some part of it.

How can i get x,y coordinates from bounding box using java? Is there any calculation available?

I am giving bounding box values (xMin,yMin,xMax,yMax)(0.46476197,0.46967554,0.8502463,0.67080903 ) 在此处输入图片说明

How can i get x,y coordinates from bounding box using java? Is there any calculation available?

If your calculated bounding box coordinates correspond to the image fractions you will first have to calculate the pixel values for xMin, xMax, yMin and yMax.

Using those it is easy to calculate the necessary parameters for the function BufferedImage.getSubimage(x,y,width,height) .

x and y correspond to the upper left corner of the bounding box, therefore:

x = xMin and y = yMin

The width of the box can be calculated using the image width and substracting the left space length leading to the box as well as the right space length where the box ends, therefore you can calculate it using the formula:

width = imageWidth - xMin - (imageWidth - xMax)

Same goes for the height, just use the y-coordinates instead:

height = imageHeight - yMin - (imageHeight - yMax)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM