本文介绍了自动切出物体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在申请裁剪图像
自动剪切图像中的对象
该图片链接: http://gunk-rico.deviantart.com/art/cancer-193284261 [ ^ ]
其目标是癌症病变.创建自动剪切图像的代码时,我已经编写了一个手动剪切图像的代码,我很难确定对象坐标的终点.如何确定对象最外端的坐标?以下是我的代码:

I''m making an application to crop the image
cut out objects in the image automatically
This link to image : http://gunk-rico.deviantart.com/art/cancer-193284261[^]
and its object is the cancer lesions. I''ve made a code to cut the image manually when I create a code for cutting the images automatically, I have difficulty in determining the ends of the object coordinate. How do I determine the coordinates of the outermost tip of the object? The following is my code:

x0 = 0;
y0 = 0;
xmax = 0;
ymax = 0;
// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = EditImage.LockBits(new Rectangle(0, 0, EditImage.Width, EditImage.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;

unsafe
{
    byte* p = (byte*)(void*)Scan0;
    int nOffset = stride - EditImage.Width * 3;

    for (int y = 0; y < EditImage.Height; ++y)
    {
        for (int x = 0; x < EditImage.Width; ++x)
        {
            if ((y == 0) && (x == 0))
            {
                awalB = p[0];
                awalG = p[1];
                awalR = p[2];
            }
            else
            {
                if ((awalB == p[0]) && (awalG == p[1]) && (awalR == p[2]))
                {
                }
                else if (x0 == 0)
                {
                    x0 = x;
                }
                else
                {
                    xmax = x;
                }
            }
            p += 3;
        }
        p += nOffset;
    }
}

unsafe
{
    byte* p = (byte*)(void*)Scan0;
    int nOffset = stride - EditImage.Height * 3;

    for (int x = 0; x < EditImage.Width; ++x)
    {
        for (int y = 0; y < EditImage.Height; ++y)
        {
            if ((y == 0) && (x == 0))
            {
                awalB = p[0];
                awalG = p[1];
                awalR = p[2];
            }
            else
            {
                if ((awalB == p[0]) && (awalG == p[1]) && (awalR == p[2]))
                {
                }
                else if (y0 == 0)
                {
                    y0 = y;
                }
                else
                {
                    ymax = y;
                }
            }
            p += 3;
        }
        p += nOffset;
    }
}

EditImage.UnlockBits(bmData);

x1 = xmax - x0;
y1 = ymax - y0;
_selection = new Rectangle(new Point(x0, y0), new Size());
_selection.Width = x1;
_selection.Height = y1;

// Create cropped image:
Image img = pbox_hasil.Image.Crop(_selection);
// Fit image to the picturebox:
pbox_hasil.Image = img.Fit2PictureBox(pbox_hasil);

推荐答案


这篇关于自动切出物体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 15:00