在图像的边缘裁剪/删除不需要的空间 [英] Crop/Remove Unwanted Space at the edges of image

查看:255
本文介绍了在图像的边缘裁剪/删除不需要的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多,除去不需要的空间,但未能找到。我才发现,它可以用来去除黑色和白色背景空间的链接。但我的背景图片可以是任何东西。所以,如果我有这些图像,

I have search a lot to remove the unwanted space but could not found. I only found links which can used to remove black and white background space. But my background images can be anything. So, If I have these images,

我怎么能提取图像的,我需要的部分。例如,

How I can extract the part of image which I required. For example,



推荐答案

下面是我送给你的问题​​的解决:

Here's my solution for your question :

我已经宣布它通过检查提供的图像的角落得到原始图像,然后它看起来的背景颜色,如果至少3个角落有类似颜色的(最多偏移10%)的方法那么,我们已经找到了背景颜色,然后尝试查找在图像这些形状当然也有不同的颜色背景颜色

I have declared a method which gets the Original Image then It looks for the background Color by checking the corners of the provided Image , if at least 3 corners have similar Color (10% offset at most) then we've found the background color then It tries to find the bounds of those shapes in the Image which of course have different color than Background Color

查找边界的功能后,剪切图像,并返回新的裁剪区域作为一个新的位图!

after Finding the Bounds The Function Crops the Image and Return the new Cropped Area as a new Bitmap !

这是演示文件:下载

完整的解决方案:下载

这里的结果:

图像1

Image 1 :

图像2

Image 2 :

图像3

Image 3 :

这里的 ImageProcessingTools 类中的函数
简体,

here's the Function inside ImageProcessingTools class Simplified,

public class ImageHelper
{
    #region CropUnwantedBackground
    public static Bitmap CropUnwantedBackground(Bitmap bmp)
    {
        var backColor = GetMatchedBackColor(bmp);
        if (backColor.HasValue)
        {
            var bounds = GetImageBounds(bmp, backColor);
            var diffX = bounds[1].X - bounds[0].X + 1;
            var diffY = bounds[1].Y - bounds[0].Y + 1;
            var croppedBmp = new Bitmap(diffX, diffY);
            var g = Graphics.FromImage(croppedBmp);
            var destRect = new Rectangle(0, 0, croppedBmp.Width, croppedBmp.Height);
            var srcRect = new Rectangle(bounds[0].X, bounds[0].Y, diffX, diffY);
            g.DrawImage(bmp, destRect, srcRect, GraphicsUnit.Pixel);
            return croppedBmp;
        }
        else
        {
            return null;
        }
    }
    #endregion

    #region Private Methods

    #region GetImageBounds
    private static Point[] GetImageBounds(Bitmap bmp, Color? backColor)
    {
        //--------------------------------------------------------------------
        // Finding the Bounds of Crop Area bu using Unsafe Code and Image Proccesing
        Color c;
        int width = bmp.Width, height = bmp.Height;
        bool upperLeftPointFounded = false;
        var bounds = new Point[2];
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                c = bmp.GetPixel(x, y);
                bool sameAsBackColor = ((c.R <= backColor.Value.R * 1.1 && c.R >= backColor.Value.R * 0.9) &&
                                        (c.G <= backColor.Value.G * 1.1 && c.G >= backColor.Value.G * 0.9) &&
                                        (c.B <= backColor.Value.B * 1.1 && c.B >= backColor.Value.B * 0.9));
                if (!sameAsBackColor)
                {
                    if (!upperLeftPointFounded)
                    {
                        bounds[0] = new Point(x, y);
                        bounds[1] = new Point(x, y);
                        upperLeftPointFounded = true;
                    }
                    else
                    {
                        if (x > bounds[1].X)
                            bounds[1].X = x;
                        else if (x < bounds[0].X)
                            bounds[0].X = x;
                        if (y >= bounds[1].Y)
                            bounds[1].Y = y;
                    }
                }
            }
        }
        return bounds;
    } 
    #endregion

    #region GetMatchedBackColor
    private static Color? GetMatchedBackColor(Bitmap bmp)
    {
        // Getting The Background Color by checking Corners of Original Image
        var corners = new Point[]{
            new Point(0, 0),
            new Point(0, bmp.Height - 1),
            new Point(bmp.Width - 1, 0),
            new Point(bmp.Width - 1, bmp.Height - 1)
        }; // four corners (Top, Left), (Top, Right), (Bottom, Left), (Bottom, Right)
        for (int i = 0; i < 4; i++)
        {
            var cornerMatched = 0;
            var backColor = bmp.GetPixel(corners[i].X, corners[i].Y);
            for (int j = 0; j < 4; j++)
            {
                var cornerColor = bmp.GetPixel(corners[j].X, corners[j].Y);// Check RGB with some offset
                if ((cornerColor.R <= backColor.R * 1.1 && cornerColor.R >= backColor.R * 0.9) &&
                    (cornerColor.G <= backColor.G * 1.1 && cornerColor.G >= backColor.G * 0.9) &&
                    (cornerColor.B <= backColor.B * 1.1 && cornerColor.B >= backColor.B * 0.9))
                {
                    cornerMatched++;
                }
            }
            if (cornerMatched > 2)
            {
                return backColor;
            }
        }
        return null;
    }  
    #endregion

    #endregion
}

和这里是ASP.NET中的一个简单的使用,

and here is a simple one usage in ASP.NET,

if (IsPostBack && Request.Files.Count > 0)
{
    var file = Request.Files[0];
    var bmp = new Bitmap(file.InputStream);
    var croppedBmp = ImageHelper.CropUnwantedBackground(bmp);
    Response.ContentType = file.ContentType;
    croppedBmp.Save(Response.OutputStream, ImageFormat.Jpeg);
    Response.End();
}

和最后,我要提的是,这些美妙的教程已经帮了我很多在图像处理:

And Finally I should mention that , these Fantastic Tutorials have Helped me a lot in Image Processing :

<一个href=\"http://www.$c$cproject.com/Articles/1989/Image-Processing-for-Dummies-with-C-and-GDI-Part-1\">Image处理与C#和GDI +傻瓜

图片处理使用C#

希望它帮助:)

这篇关于在图像的边缘裁剪/删除不需要的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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