Jump to content
  • Advertisement
Sign in to follow this  
jaypeebacol

[.net] How to feather edges of a subject in picture using bitmapdata in c#?

This topic is 3267 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am rushing to develop feathering feature of my program. Please help me to feather the edges of a subject to make it smoother. For example, a chicken in a picture. Only the edges of chicken should be feathered. Jaypee

Share this post


Link to post
Share on other sites
Advertisement
Hi, Thanks for your quick reply.

I already tried this code and modified. The feathering is ok at the left and top side of bitmap. But the right side is not OK. Please help me.

public class ConvMatrix
{
public int TopLeft = 0, TopMid = 0, TopRight = 0;
public int MidLeft = 0, Pixel = 1, MidRight = 0;
public int BottomLeft = 0, BottomMid = 0, BottomRight = 0;
public int Factor = 1;
public int Offset = 0;
public void SetAll(int nVal)
{
TopLeft = TopMid = TopRight = MidLeft = Pixel = MidRight = BottomLeft = BottomMid = BottomRight = nVal;
}

private void Smooth(int nWeight)
{
ConvMatrix m = new ConvMatrix();

m.SetAll(1);
m.Pixel = nWeight;
m.Factor = nWeight + 8;

Conv3x3(m);
}
Bitmap bSrc = (Bitmap)orig.Clone();

// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = orig.LockBits(new Rectangle(0, 0, orig.Width, orig.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
BitmapData bmSrc = bSrc.LockBits(new Rectangle(0, 0, bSrc.Width, bSrc.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

int stride = bmData.Stride;
int stride2 = stride * 2;
System.IntPtr Scan0 = bmData.Scan0;
System.IntPtr SrcScan0 = bmSrc.Scan0;



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

//int nOffset = stride + 6 - b.Width*3;
int nOffset = stride + 8 - (orig.Width * 4);
int nWidth = orig.Width - 2;
int nHeight = orig.Height - 2;

int nPixel;

bool blur_flag;
int range = 0;

Int32 x, y, n, f, i=0;

for (y = 0; y < nHeight; ++y)
{
for (x = 0; x < nWidth; ++x)
{
//for (n = 0; n <= 3; ++n)
//{
f = i + 3;

nPixel = ((((pSrc[f] * m.TopLeft) +
(pSrc[f + 4] * m.TopMid) +
(pSrc[f + 8] * m.TopRight) +
(pSrc[f + stride] * m.MidLeft) +
(pSrc[f + 4 + stride] * m.Pixel) +
(pSrc[f + 8 + stride] * m.MidRight) +
(pSrc[f + stride2] * m.BottomLeft) +
(pSrc[f + 4 + stride2] * m.BottomMid) +
(pSrc[f + 8 + stride2] * m.BottomRight)) / m.Factor) + m.Offset);

if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;

//if (p[(f - 20) + 4 + stride] == (byte)0 || p[(f + 20) + 4 + stride] == (byte)0)
//{

p[f + 8 + stride] = (byte)nPixel;

//}
//}

i += 4;
}

i += nOffset;
}
}
orig.UnlockBits(bmData);
bSrc.UnlockBits(bmSrc);

GC.Collect();

pictureBox1.Image = orig;
pictureBox1.Refresh();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!