繁体   English   中英

从调整大小的图像中平移/检测图像的矩形部分

[英]Translate/Detect Rectangle portion of Image from Resized Image

我有一个大尺寸的图像,因为要处理高分辨率图像需要很长时间,所以我将其调整大小以保持宽高比。从调整后的图像中我检测到一个矩形并且我具有矩形的坐标。

 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;
        }

有没有一种方法可以将这个矩形转换/映射为大图像,以便获得相同的区域。我这样做是为了节省时间。

一些澄清:我有一个大的原始图像。.我调整它的尺寸,保持长宽比,并使用一些处理,在其中得到一个矩形部分(只是坐标)。由于该部分的图像质量不好,我需要找到一个将此坐标映射到大图像的方法。

好的,所以如果我明白了,这里是:

您在选择矩形的窗口中有一个视口,并且想要将此矩形缩放为未缩放的图像。

因此,我们将具有以下功能:

public RectangleF TranslateScale(RectangleF CropRectangle, Bitmap imgPhoto)

首先,我们需要计算乘数以适合视口上的图像,就像您的函数一样:

        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;

现在我们知道了比例百分比,我们只取函数的倒数,而不是乘以,而是除以矩形的大小和位置:

        CropRectangle.X /= nPercent;
        CropRectangle.Y /= nPercent;
        CropRectangle.Width /= nPercent;
        CropRectangle.Height /= nPercent

        return CropRectangle;

就是这样,现在您已将矩形缩放到原始图像大小,现在可以裁剪该矩形了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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