从调整大小的图像中转换/检测图像的矩形部分 [英] Translate/Detect Rectangle portion of Image from Resized Image

查看:105
本文介绍了从调整大小的图像中转换/检测图像的矩形部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于从调整大小的图像中转换/检测图像的矩形部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆