分割区域 [英] segmentation area

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

问题描述

我为此算法编写了代码
(我的照片之前是灰度的)
1.获取种子像素的值像素(0,0)
2.将值种子像素与一个相邻像素进行比较
3.如果No.3的值小于阈值(T),则转到下一个像素并转到No.2
4.如果No.3的值大于阈值(T),则将像素更改为白色(也用于下一个10像素),并获得新的种子值像素.

这是我的代码

i make a code for this algorithm
(my picture have been greyscale before it)
1. get value pixel (0,0) for seed pixel
2. compare value seed pixel with one neighbor pixel
3. if value of no.3 less than treshold (T), go to next pixel and go to no.2
4. if value of no.3 more than treshold (T), change pixel to white(also for next 10 pixel), and get new seed value pixel.

this is my code

private void button4_Click(object sender, EventArgs e)
        {
            // GDI+ still lies to us - the return format is BGR, NOT RGB.
            BitmapData bmData = RImage.LockBits(new Rectangle(0, 0, RImage.Width, RImage.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

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

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

                int nOffset = stride - RImage.Width * 3;

                for (int y = 0; y < RImage.Height; ++y)
                {                   
                    for (int x = 0; x < RImage.Width; ++x)
                    {  
                        if (x == 0)
                        {
                            //get value of seed pixel if x==0
                            Color col = new Color();
                            col = RImage.GetPixel(x, y);
                            seedR = Convert.ToByte(col.R.ToString());
                            seedG = Convert.ToByte(col.G.ToString());
                            seedB = Convert.ToByte(col.B.ToString());
                        }
                        //compare value seed pixel and pixel now
                        if ((seedR - p[0] >= tred) || (p[0] - seedR >= tred))
                        {
                            //change value pixel now
                            p[0] = p[1] = p[2] = 0;
                            //change value pixel for next 10 pixel
                            for (x = (x+1); x <= 10; ++x)
                            {
                                p[0] = p[1] = p[2] = 0;                                
                            }
                            //get new value of new seed pixel
                            Color col = new Color();
                            col = RImage.GetPixel(x, y);
                            seedR = Convert.ToByte(col.R.ToString());
                            seedG = Convert.ToByte(col.G.ToString());
                            seedB = Convert.ToByte(col.B.ToString());
                        }                        
                    }
                    p += nOffset;
                }
            }

            RImage.UnlockBits(bmData);
        }




但是如果运行则出现此代码错误
我的代码有什么错误??
(此程序可用于分割性lesi皮肤癌)




but this code error if running
what false with my code??
(this program for segmented lesi skin cancer)

推荐答案

首先,您不需要:
First of all you dont need:
Color col = new Color();


因为GetPixel(x,y)将返回Color类的实例.

在同一位图上调用LockBits()后,不能使用GetPixel().这就是为什么您会遇到内存错误.

一种解决方案是使用安全"代码-不要调用LockBits()并使用GetPixel()从位图读取,而使用SetPixel()对其进行写入.在较大的位图上,这可能会很慢,但是您不必担心每个像素的位数(GDI会这样做).

更快(不安全)的解决方案是调用LockBits(),然后通过指针读取像素数据:


because GetPixel(x,y) will return instance of Color class.

You can not use GetPixel() after you call LockBits() on same bitmap. That''s why you get memory errors.

One solution is to use ''safe'' code - do not call LockBits() and use GetPixel() to read from bitmap and SetPixel() to write to it. This can be slow on larger bitmaps, but you don''t have to worry about bits per pixel (GDI does that).

Faster (unsafe) solution is to call LockBits() and then read pixel data through pointer:

seedR = p[x]
seedR = p[x+1]
seedR = p[x+2]


并写入像素数据:


and write pixel data:

p[x]=p[x+1]=p[x+2]=0


另外,您的内循环不好,请将其更改为:


Also your inner loop is not good, change it to something like:

for(int i=0; i<10; i++)
{
    p[x]=p[x+1]=p[x+2]=0; 
    x++;
}



这仅适用于24bpp位图.对于其他任何情况,您将需要更多的指针算法:)



This will only work for 24bpp bitmaps. For any other you''ll need more pointer arithmetic :)


如果vshost停止工作,则表明您在这里遇到了内存问题.当您使用不安全的代码时,您的代码很可能正在使用不应该使用的内存.
If vshost has stopped working, this would suggest that you have a memory problem here. As you are using unsafe code, it''s quite likely that your code is working with memory that it shouldn''t be.


这应该有所帮助:

This Should Help:

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

                int nOffset = stride - RImage.Width * 3;

                for (int y = 0; y < RImage.Height; ++y)
                {
                    for (int x = 0; x < RImage.Width; ++x)
                    {
                        int offset = y * RImage.Width * 3 + x * 3;
                        if (x == 0)
                        {
                            //get value of seed pixel if x==0
                            Color col = Color.FromArgb(p[offset], p[offset + 1], p[offset + 2]);
                            //col = bmData.GetPixel(x, y);
                            seedR = Convert.ToByte(col.R.ToString());
                            c = Convert.ToByte(col.G.ToString());
                            seedB = Convert.ToByte(col.B.ToString());
                        }
                        //compare value seed pixel and pixel now
                        if ((seedR - p[0] >= tred) || (p[0] - seedR >= tred))
                        {
                            //change value pixel now
                            p[0] = p[1] = p[2] = 0;
                            //change value pixel for next 10 pixel
                            for (x = (x + 1); x <= 10; ++x)
                            {
                                p[0] = p[1] = p[2] = 0;
                            }
                            //get new value of new seed pixel
                            Color col = Color.FromArgb(p[offset], p[offset + 1], p[offset + 2]);
                            seedR = Convert.ToByte(col.R.ToString());
                            seedG = Convert.ToByte(col.G.ToString());
                            seedB = Convert.ToByte(col.B.ToString());
                        }
                    }
                    p += nOffset;
                }
            }


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

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