从缩放的小尺寸图像中获取相同的矩形位置
Getting Same Rectangle Position from Scaled Small Size Image
我正在尝试处理大尺寸 image.Since 处理需要太多时间才能完成 我正在调整图像大小 processing.After 处理 我正在小尺寸上绘制一个矩形image.How 我可以将这个矩形的坐标转换为原始未缩放图像 ie:Draw 未缩放图像上相同位置的矩形。
我正在使用以下代码调整图像大小
public static Size ResizeKeepAspect(Size CurrentDimensions, int maxWidth, int maxHeight)
{
int newHeight = CurrentDimensions.Height;
int newWidth = CurrentDimensions.Width;
if (maxWidth > 0 && newWidth > maxWidth) //WidthResize
{
Decimal divider = Math.Abs((Decimal)newWidth / (Decimal)maxWidth);
newWidth = maxWidth;
newHeight = (int)Math.Round((Decimal)(newHeight / divider));
}
if (maxHeight > 0 && newHeight > maxHeight) //HeightResize
{
Decimal divider = Math.Abs((Decimal)newHeight / (Decimal)maxHeight);
newHeight = maxHeight;
newWidth = (int)Math.Round((Decimal)(newWidth / divider));
}
return new Size(newWidth, newHeight);
}
这就是我要实现的目标
这是一个简单的关系计算。例如:
Image A 100 (w) x 100 (h): Pixel x = 10, y = 30
Image B 200 (w) x 200 (h): Pixel x = a, y = b
10 / 100 (w) = a / 200 (w)
200 (w) * 10 / 100 (w) = a //Image B's x value
a = 20
30 / 100 (h) = b / 200 (h)
200 (h) * 30 / 100 (h) = b //Image A's y value
b = 60
Rectangle ConvertToLargeRect(Rectangle smallRect, Size largeImageSize, Size smallImageSize)
{
double xScale = (double)largeImageSize.Width / smallImageSize.Width;
double yScale = (double)largeImageSize.Height / smallImageSize.Height;
int x = (int)(smallRect.X * xScale + 0.5);
int y = (int)(smallRect.Y * yScale + 0.5);
int right = (int)(smallRect.Right * xScale + 0.5);
int bottom = (int)(smallRect.Bottom * yScale + 0.5);
return new Rectangle(x, y, right - x, bottom - y);
}
我正在尝试处理大尺寸 image.Since 处理需要太多时间才能完成 我正在调整图像大小 processing.After 处理 我正在小尺寸上绘制一个矩形image.How 我可以将这个矩形的坐标转换为原始未缩放图像 ie:Draw 未缩放图像上相同位置的矩形。
我正在使用以下代码调整图像大小
public static Size ResizeKeepAspect(Size CurrentDimensions, int maxWidth, int maxHeight)
{
int newHeight = CurrentDimensions.Height;
int newWidth = CurrentDimensions.Width;
if (maxWidth > 0 && newWidth > maxWidth) //WidthResize
{
Decimal divider = Math.Abs((Decimal)newWidth / (Decimal)maxWidth);
newWidth = maxWidth;
newHeight = (int)Math.Round((Decimal)(newHeight / divider));
}
if (maxHeight > 0 && newHeight > maxHeight) //HeightResize
{
Decimal divider = Math.Abs((Decimal)newHeight / (Decimal)maxHeight);
newHeight = maxHeight;
newWidth = (int)Math.Round((Decimal)(newWidth / divider));
}
return new Size(newWidth, newHeight);
}
这就是我要实现的目标
这是一个简单的关系计算。例如:
Image A 100 (w) x 100 (h): Pixel x = 10, y = 30
Image B 200 (w) x 200 (h): Pixel x = a, y = b
10 / 100 (w) = a / 200 (w)
200 (w) * 10 / 100 (w) = a //Image B's x value
a = 20
30 / 100 (h) = b / 200 (h)
200 (h) * 30 / 100 (h) = b //Image A's y value
b = 60
Rectangle ConvertToLargeRect(Rectangle smallRect, Size largeImageSize, Size smallImageSize)
{
double xScale = (double)largeImageSize.Width / smallImageSize.Width;
double yScale = (double)largeImageSize.Height / smallImageSize.Height;
int x = (int)(smallRect.X * xScale + 0.5);
int y = (int)(smallRect.Y * yScale + 0.5);
int right = (int)(smallRect.Right * xScale + 0.5);
int bottom = (int)(smallRect.Bottom * yScale + 0.5);
return new Rectangle(x, y, right - x, bottom - y);
}