请各位大虾帮帮忙

yandg 2005-05-14 02:21:50
在c# 环境下怎样用程序自动将照片中的人物抠出来,即去除照片的背景

谢谢
...全文
49 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yandg 2005-05-15
  • 打赏
  • 举报
回复
这个算法是怎样的
可否提供源代码学习学习
pingnt 2005-05-14
  • 打赏
  • 举报
回复
这个可要算法实现了。
新鲜鱼排 2005-05-14
  • 打赏
  • 举报
回复
mark
JasonHeung 2005-05-14
  • 打赏
  • 举报
回复
那背景必须是纯色的,然后使用如下方法:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace Jh.UserControls
{

/// <summary>
/// determines the meaning of the transparencyKey argument to the Convert method
/// </summary>
public enum TransparencyMode
{
/// <summary>
/// the color key is used to define the transparent region of the bitmap
/// </summary>
ColorKeyTransparent,
/// <summary>
/// the color key is used to define the area that should _not_ be transparent
/// </summary>
ColorKeyOpaque
}

/// <summary>
/// a class to convert a color-keyed bitmap into a region
/// </summary>
public class BitmapToRegion
{
/// <summary>
/// ctor made private to avoid instantiation
/// </summary>
private BitmapToRegion()
{}


/// <summary>
/// the meat of this class
/// converts the bitmap to a region by scanning each line one by one
/// this method will not affect the original bitmap in any way
/// </summary>
/// <param name="bitmap">The bitmap to convert</param>
/// <param name="transparencyKey">The color which will indicate either transparency or opacity</param>
/// <param name="mode">Whether the transparency key should indicate the transparent or the opaque region</param>
public unsafe static Region Convert( Bitmap bitmap, Color transparencyKey,TransparencyMode mode )
{
//sanity check
if ( bitmap == null )
throw new ArgumentNullException( "Bitmap", "Bitmap cannot be null!" );

//flag = true means the color key represents the opaque color
bool modeFlag = ( mode == TransparencyMode.ColorKeyOpaque );

GraphicsUnit unit = GraphicsUnit.Pixel;
RectangleF boundsF = bitmap.GetBounds( ref unit );
Rectangle bounds = new Rectangle( (int)boundsF.Left, (int)boundsF.Top,
(int)boundsF.Width, (int)boundsF.Height );

uint key = (uint)((transparencyKey.A << 24) | (transparencyKey.R << 16) |
(transparencyKey.G << 8) | (transparencyKey.B << 0));


//get access to the raw bits of the image
BitmapData bitmapData = bitmap.LockBits( bounds, ImageLockMode.ReadOnly,
PixelFormat.Format32bppArgb );
uint* pixelPtr = (uint*)bitmapData.Scan0.ToPointer();

//avoid property accessors in the for
int yMax = (int)boundsF.Height;
int xMax = (int)boundsF.Width;

//to store all the little rectangles in
GraphicsPath path = new GraphicsPath();

for ( int y = 0; y < yMax; ++y )
{
//store the pointer so we can offset the stride directly from it later
//to get to the next line
byte* basePos = (byte*)pixelPtr;

for ( int x = 0; x < xMax; ++x, ++pixelPtr )
{
//is this transparent? if yes, just go on with the loop
if ( modeFlag ^ ( *pixelPtr == key ) )
continue;

//store where the scan starts
int x0 = x;

//not transparent - scan until we find the next transparent byte
while( x < xMax && !( modeFlag ^ ( *pixelPtr == key ) ) )
{
++x;
pixelPtr++;
}

//add the rectangle we have found to the path
path.AddRectangle( new Rectangle( x0, y, x-x0, 1 ) );
}
//jump to the next line
pixelPtr = (uint*)(basePos + bitmapData.Stride);
}

//now create the region from all the rectangles
Region region = new Region( path );

//clean up
path.Dispose();
bitmap.UnlockBits( bitmapData );

return region;
}

}


/*
Exsample:
Bitmap bm = (Bitmap)Bitmap.FromStream( this.GetType().Assembly.GetManifestResourceStream("BitmapToRegion.Image1.bmp" ) );
Color col = bm.GetPixel( 1, 1 );

this.Width = bm.Width;
this.Height = bm.Height;

this.Region = BitmapToRegion.Convert( bm, col, TransparencyMode.ColorKeyOpaque);//.ColorKeyTransparent );//
this.BackgroundImage = bm;

//*/
}
viyo 2005-05-14
  • 打赏
  • 举报
回复
这个可要算法实现了。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧