Click here to Skip to main content
16,020,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
        }
Posted
Updated 22-Dec-10 21:41pm
v2
Comments
Abhinav S 23-Dec-10 3:41am    
Code blocks added.
[no name] 23-Dec-10 4:27am    
I doubt that anyone will have much idea of what this is about. Please explain, or show, where in the above code you have a problem and what that problem is.
Pete O'Hanlon 26-Dec-10 17:29pm    
if (rc1 + rc2 + rc3 + rc4 + rc5 + rc6 + rc7 + rc8 < 8)
{
if (rc1 + rc2 + rc3 + rc4 + rc5 + rc6 + rc7 + rc8 == 0). A small suggestion, precompute this and use a short circuit condition here, e.g. if (myVal < 8 && myVal > 0).

1 solution

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);


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.

C#
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.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 13-Jan-11 9:48am    
[Moved here for OP]
Thanks! I will try it, sorry for late reply.
sara hisham 30-Apr-11 6:52am    
Can I have the source code of this program because I am also implementing a program but it calculates the volume of brain tumor so I need the part of region growing segmentation,, if possible :)
Thanks Alot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900