[英]Translate/Detect Rectangle portion of Image from Resized Image
I have a Large size image.Since it takes long to process high res images i resize it keeping the aspect ratio.From the resized image i detect a rectangle and i have the coordinates of the rectangle. 我有一个大尺寸的图像,因为要处理高分辨率图像需要很长时间,所以我将其调整大小以保持宽高比。从调整后的图像中我检测到一个矩形并且我具有矩形的坐标。
Bitmap ResizekeepAspectRatio(Bitmap imgPhoto, int Width, int Height)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((Width -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((Height -
(sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(Width, Height,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Red);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
Is there a way by which i can translate/Map this rectangle to the Large image so that i get the same area.Im doing this to save time. 有没有一种方法可以将这个矩形转换/映射为大图像,以便获得相同的区域。我这样做是为了节省时间。
Some clarification: I have a Large Original Image.. i resize it keeping the aspect ratio and using some processing i get a rectangle portion in it( Just the coordinates).Since the Image quality of this portion is not good i need to find a way to map this coordinate to the large image. 一些澄清:我有一个大的原始图像。.我调整它的尺寸,保持长宽比,并使用一些处理,在其中得到一个矩形部分(只是坐标)。由于该部分的图像质量不好,我需要找到一个将此坐标映射到大图像的方法。
Ok, so if I understand it clear here it is: 好的,所以如果我明白了,这里是:
You have a viewport in what you select a rectangle and you want to scale this rectangle to the unscaled image. 您在选择矩形的窗口中有一个视口,并且想要将此矩形缩放为未缩放的图像。
So we will have a function like this: 因此,我们将具有以下功能:
public RectangleF TranslateScale(RectangleF CropRectangle, Bitmap imgPhoto)
First of all what we need is to calculate the multiplier to fit the image on the viewport, exactly like your function: 首先,我们需要计算乘数以适合视口上的图像,就像您的函数一样:
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
Now that we know the scale percentage we just take the inverse of your function, instead of multiply just divide the rectangle size and position: 现在我们知道了比例百分比,我们只取函数的倒数,而不是乘以,而是除以矩形的大小和位置:
CropRectangle.X /= nPercent;
CropRectangle.Y /= nPercent;
CropRectangle.Width /= nPercent;
CropRectangle.Height /= nPercent
return CropRectangle;
And that's it, now you have the rectangle scaled to the original image size, you now can crop that rectangle. 就是这样,现在您已将矩形缩放到原始图像大小,现在可以裁剪该矩形了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.