C#开发的人脸左右相似度计算软件源码分析开发的人脸左右相似度计算软件源码分析
主要介绍了C#开发的人脸左右相似度计算软件,较为详细的分析了相似度计算的相关原理与具体实现技巧,需要的朋友可以参考下
本文实例讲述了C#开发的人脸左右相似度计算软件。分享给大家供大家参考。具体分析如下:
模仿湖南卫视快乐大本营中所使用的一款人脸左右对称相似度计算软件,自己写的一个小软件,使用语言是C#,希望跟喜欢这个软件的同志们共享!
1. FaceClass类程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace FaceSmile
{
class FaceClass
{
/// <summary>
/// 左脸对称函数
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
public static Bitmap FaceFlipLeft(Bitmap a)
{
Rectangle rect = new Rectangle(0, 0, a.Width, a.Height);
System.Drawing.Imaging.BitmapData srcData = a.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, a.PixelFormat);
IntPtr ptr = srcData.Scan0;
int bytes = 0;
bytes = srcData.Stride * a.Height;
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
byte[] temp = new byte[bytes];
temp = (byte[])grayValues.Clone();
for (int j = 0; j < a.Height; j++)
{
for (int i = 0; i < (int)(a.Width/2); i++)
{
temp[(a.Width - 2 - i) * 3 + j * srcData.Stride] = temp[i * 3 + j * srcData.Stride];
temp[(a.Width - 2 - i) * 3 + 1 + j * srcData.Stride] = temp[i * 3 + 1 + j * srcData.Stride];
temp[(a.Width - 2 - i) * 3 + 2 + j * srcData.Stride] = temp[i * 3 + 2 + j * srcData.Stride];
}
}
grayValues = (byte[])temp.Clone();
System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
a.UnlockBits(srcData);
return a;
}
/// <summary>
/// 右脸对称函数
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
public static Bitmap FaceFlipRight(Bitmap a)
{
Rectangle rect = new Rectangle(0, 0, a.Width, a.Height);
System.Drawing.Imaging.BitmapData srcData = a.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, a.PixelFormat);
IntPtr ptr = srcData.Scan0;
int bytes = 0;
bytes = srcData.Stride * a.Height;
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
byte[] temp = new byte[bytes];
temp = (byte[])grayValues.Clone();
for (int j = 0; j < a.Height; j++)
{
for (int i = 0; i < (int)(a.Width / 2); i++)
{
temp[i * 3 + j * srcData.Stride] = temp[(a.Width - 2 - i) * 3 + j * srcData.Stride];
temp[i * 3 + 1 + j * srcData.Stride] = temp[(a.Width - 2 - i) * 3 + 1 + j * srcData.Stride];
temp[i * 3 + 2 + j * srcData.Stride] = temp[(a.Width - 2 - i) * 3 + 2 + j * srcData.Stride];
}
}
grayValues = (byte[])temp.Clone();
System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
a.UnlockBits(srcData);
return a;
}
/// <summary>
/// 定义肤色检测函数
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
public static Bitmap SkinDetect(Bitmap a)
{
Rectangle rect = new Rectangle(0, 0, a.Width, a.Height);
System.Drawing.Imaging.BitmapData bmpData = a.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
int stride = bmpData.Stride;
unsafe
{
byte* pIn = (byte*)bmpData.Scan0.ToPointer();
byte* P;
int R, G, B;
double r, g, Fupr, Flor, Wrg;
for (int y = 0; y < a.Height; y++)
{
for (int x = 0; x < a.Width; x++)
{
P = pIn;
B = P[0];
G = P[1];
R = P[2];
if (R + G + B == 0)
{
r = 0;