我正在使用以下代码将一个较小的矩形坐标转换为一个较大的坐标,即:将一幅小图像上的矩形位置转换为同一图像的较大分辨率上的相同位置
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);
}
但是某些图像似乎有问题。转换后的矩形坐标似乎不在图像上。
更新:
img.Draw(rect, new Bgr(232, 3, 3), 2);
Rectangle transret= ConvertToLargeRect(rect, orgbitmap.Size, bit.Size);
target = new Bitmap(transret.Width, transret.Height);
using (Graphics g = Graphics.FromImage(target))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawImage(orgbitmap, new Rectangle(0, 0, target.Width, target.Height),
transret, GraphicsUnit.Pixel);
}
在小分辨率图像上绘制的矩形
{X=190,Y=2,Width=226,Height=286}
矩形转换为原始高分辨率图像{X=698,Y=7,Width=830,Height=931}
原始图片
答案 0 :(得分:0)
首先,如果您调整形状的大小,则不应移动位置。那不是放大形状的结果。这意味着不应更改左上角的X,Y点。
第二,您不应该在操作中手动添加0.5,这不是一个干净的方法。使用@RezaAghaei建议的上限功能
第三,您应该不从高度/宽度中减去X / Y,您的计算应以宽度*比例进行。
请更正那些错误,如果它不起作用,我将通过其他步骤来更新答案。