区域增长 [英] Region Growing

查看:106
本文介绍了区域增长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有问题.我必须在照片上标记该物体.我的图像以皮肤癌病灶的形式出现.但结果是代码我无法将病变和健康的皮肤分开

I had a problem with my code. I must mark the object in my picture. my image in the form of skin cancer lesions. but the result is code I am not able to separate the lesion and healthy skin

public void markmetdod()
        {
            minq = 100;
            maxq = 150;
            Bitmap EditImage = this.Image;
            // 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 (p[x] <= maxq && p[x] >= minq)
                        {
                            arraynilai[x, y] = 1;
                        }
                        else
                        {
                            arraynilai[x, y] = 0;
                        }

                        p += 3;
                    }
                    p += nOffset;
                }
            }

            unsafe
            {
                byte* p = (byte*)(void*)Scan0;

                int nOffset = stride - EditImage.Width * 3;

                for (int y = 1; y < EditImage.Height; ++y)
                {
                    for (int x = 1; x < EditImage.Width; ++x)
                    {
                        rc1 = arraynilai[x - 1, y - 1];
                        rc2 = arraynilai[x, y - 1];
                        rc3 = arraynilai[x + 1, y - 1];
                        rc4 = arraynilai[x + 1, y];
                        rc5 = arraynilai[x + 1, y + 1];
                        rc6 = arraynilai[x, y + 1];
                        rc7 = arraynilai[x - 1, y + 1];
                        rc8 = arraynilai[x - 1, y];

                        if (rc1 + rc2 + rc3 + rc4 + rc5 + rc6 + rc7 + rc8 < 8)
                        {
                            if (rc1 + rc2 + rc3 + rc4 + rc5 + rc6 + rc7 + rc8 == 0)
                            { 
                            }
                            else
                            {
                                p[x] = p[x + 1] = p[x + 2] = 255;
                            }
                            
                        }
                        
                        p += 3;
                    }
                    p += nOffset;
                }
            }

            EditImage.UnlockBits(bmData);
        }

推荐答案



我认为您的代码并不正确,

我做了一些更改,效果很好;)

几点评论:
-minq和maxq是硬编码的,也许它们应该是计算值.
-当您增加变量p时,您会在每行上添加3的偏移量,从而导致标记错误的区域
-您只需要一个不安全的阻止.
-如前所述if (rc1 + rc2 + rc3 + rc4 + rc5 + rc6 + rc7 + rc8 < 8)不好...

我用各种图片尝试了以下代码,效果很好.


Hi,

I think your code is not far from being correct,

I made a few changes and it works fine ;)

A few remarks:
- minq and maxq are hardcoded, maybe they should be calculated values.
- when you increment your variable p you add an offset of 3 on each line, resulting in the wrong areas being marked
- you only need one unsafe block.
- as previously mentioned if (rc1 + rc2 + rc3 + rc4 + rc5 + rc6 + rc7 + rc8 < 8) is not nice...

I tried the following code with various pictures and it worked fine.


byte minq = 50;
byte maxq = 100;
EditImage = (Bitmap)this.pictureBox1.Image;

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;
int bitsPerPixels = stride / EditImage.Width;

int[,] arraynilai = new int[EditImage.Width+1, EditImage.Height+1];

unsafe
{
    byte* pos;
    byte* scan0 = (byte*)(bmData.Scan0.ToPointer());

    for (int j = 0; j < bmData.Height; j++)
    {
        pos = scan0 + stride * j;
        for (int i = 0; i < bmData.Width; i++)
        {
            *pos = (byte)(255 - *pos);
            if ((pos[i] <= maxq && pos[i] >= minq))
                arraynilai[i, j] = 1;
            else
                arraynilai[i, j] = 0;
            pos += bitsPerPixels;
        }
    }
    for (int j = 1; j < bmData.Height; j++)
    {
        pos = scan0 + stride * j;
        for (int i = 1; i < bmData.Width; i++)
        {
            *pos = (byte)(255 - *pos);
            int rc1 = arraynilai[i - 1, j - 1];
            int rc2 = arraynilai[i, j - 1];
            int rc3 = arraynilai[i + 1, j - 1];
            int rc4 = arraynilai[i + 1, j];
            int rc5 = arraynilai[i + 1, j + 1];
            int rc6 = arraynilai[i, j + 1];
            int rc7 = arraynilai[i - 1, j + 1];
            int rc8 = arraynilai[i - 1, j];
            int tot = rc1 + rc2 + rc3 + rc4 + rc5 + rc6 + rc7 + rc8;
            if (tot < 8 && tot > 0)
            {
                pos[i] = pos[i + 1] = pos[i + 2] = 255;
            }
            pos += bitsPerPixels;
        }
    }
}
EditImage.UnlockBits(bmData);



同样,您可以计算minq和maxq的方法是查看平均像素.但这有点粗糙,也许某些矩阵计算会更有效.



also what you could do to calculate minq and maxq is look at the average pixel. but it is a bit crude, and maybe some matrix calculations would be a lot more efficient.

for (int j = 0; j < bmData.Height; j++)
{
    pos = scan0 + stride * j;
    for (int i = 0; i < bmData.Width; i++)
    {
        *pos = (byte)(255 - *pos);
        if ((pos[i] <= maxq && pos[i] >= minq))
            arraynilai[i, j] = 1;
        else
            arraynilai[i, j] = 0;
        totalValue += pos[i];
        numberPixels += 1;
        pos += bitsPerPixels;
    }
}

//assuming +/- 10% variation... would be better if in a parameter.
minq = Convert.ToByte(totalValue / numberPixels * 0.9);
maxq = Convert.ToByte(totalValue / numberPixels * 1.1);



希望对您有所帮助.

瓦莱里.



Hope it helps.

Valery.


这篇关于区域增长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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