切透明部分的图像 [英] Cut transparent parts image

查看:161
本文介绍了切透明部分的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新的映像:

非常感谢你的人,但我想要做财产以后有点不同,削减每长方形这里作为一个sperate image.Lets先找到蓝色的块边界试试。听起来很难,但它其实很简单

thanks you very much man but i wanted to do somthing little different, to cut each rectangle here as a sperate image.Lets try first to find the blue block Bounds. Sounds hard but its actually simple.

当我迄今所做的样子:

  private unsafe Bitmap CodeImage(Bitmap bmp)
    {

        Bitmap bmpRes = new Bitmap(bmp.Width, bmp.Height);

        BitmapData bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);


        IntPtr scan0 = bmData.Scan0;


        int stride = bmData.Stride;


        int nWidth = bmp.Width;
        int nHeight = bmp.Height;
        int minX = 10000 ;
        int maxX = -10000;
        int minY = 10000;
        var maxY = -10000;

        bool found = false;
        for (int y = 0; y < nHeight; y++)
        {
            byte* p = (byte*)scan0.ToPointer();
            p += y * stride;


            for (int x = 0; x < nWidth; x++)
            {




                if (p[3]!=0)  //Check if pixel is transpert;
                {


                    found = true;
                  if (x < minX)
                      minX = x;
                  if (y < minY)
                      minY = y;
                   if (x > maxX)
                      maxX = x;
                  if (y > maxY)
                      maxY = y;

                }

                else
                {
                    if (found)
                    {


                        Rectangle temp = new Rectangle(minX, minY, maxX - minX,  maxY-minY);


                        return bmp.Clone(temp, bmp.PixelFormat);
                    }
                }



                p += 4;


            }
        }


        return null;

    }

您实际上是写,我应该算这样的宽度:
INT宽度= MAXX - 其minX; 键,它的实际工作..但高度为0 ....

you actually was write and i should calculate the width like this : int width = maxX - minX; and it actually works.. but the height is 0....

这些界限尝试了这一点的人几乎允许输出矩形正确:
(200800400,和 0 的高度)。
I只是用你的部分代码在我的算法是啊,你是对的,但现在有一个与高度的小问题,我将非常感激,如果你看看

try this out man its outputing almost correct rectangle with these bounds: (200,800,400, and 0 on the height). i just used parts of your code in my algorithm and yea you were right but now there is a little problem with the height i' would very appreciate if you will have a look

推荐答案

我将在伪上下的代码写这个,因为你应该能够然后将它应用到任何类型的图像或语言...

I'll write this in pseudo-ish-code since you should be able then to apply it to any type of image or language...

var minX = 10000
var maxX = -10000
var minY = 10000
var maxY = -10000

for (var y = 0 to height)
{
    for (var x = 0 to width)
    {
        if (pixel(x,y).Color != transparent)
        {
            if (x < minX) minX = x
            if (y < minY) minY = y
            if (x > maxX) maxX = x
            if (y > maxY) maxY = y
        }
    }
}

var cropRectangle = (minX, minY, maxX, maxY)

您现在可以使用标准功能的位图来获得该领域,这应该是该地区由非透明像素界

You can now use standard functions on the bitmap to get that area, which should be the area bounded by the non-transparent pixels.

这篇关于切透明部分的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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