簡體   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