dlibdotnet 人脸相似度源代码_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
{
///
/// 左脸对称函数
///
///
///
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;
}
///
/// 右脸对称函数
///
///
///
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;
}
///
/// 定义肤色检测函数
///
///
///
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;
g = 0;
}
else
{
r = (R / (R + G + B));
g = (G / (R + G + B));
}
Fupr = (1.0743 * r + 0.1452 - 1.3767 * r * r);
Flor = (0.5601 * r + 0.1766 - 0.776 * r * r);
Wrg = (r - 0.33) * (r - 0.33) + (g - 0.33) * (g - 0.33);
if ((R - G >= 45) && ((R > G) && (G > B)) && (Fupr > g) && (Wrg >= 0.0004))
{
P[0] = (byte)B;
P[1] = (byte)G;
P[2] = (byte)R;
}
else
{
P[0] = 0;
P[1] = 0;
P[2] = 0;
}
pIn += 3;
}
pIn += stride - a.Width * 3;
}
}
a.UnlockBits(bmpData);
return a;
}
///
/// 定义图像灰度化函数
///
///
///
public static Bitmap ImageGray(Bitmap src)
{
int w = src.Width;
int h = src.Height;
//构建与原图像大小一样的模版图像
Bitmap dstBitmap = new Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
//将原图像存入内存
System.Drawing.Imaging.BitmapData srcData = src.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Imaging.BitmapData dstData = dstBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
unsafe
{
byte* pIn = (byte*)srcData.Scan0.ToPointer();
byte* pOut = (byte*)dstData.Scan0.ToPointer();
byte* p;
int stride = srcData.Stride;
int r, g, b;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
p = pIn;
r = p[2];
g = p[1];
b = p[0];
//调用图像灰度化公式
pOut[0] = pOut[1] = pOut[2] = (byte)(b * 0.114 + g * 0.587 + r * 0.299);
pIn += 3;
pOut += 3;
}
pIn += srcData.Stride - w * 3;
pOut += srcData.Stride - w * 3;
}
src.UnlockBits(srcData);
dstBitmap.UnlockBits(dstData);
return dstBitmap;
}
}
}
}
2. SameRatioClass类程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace FaceSmile
{
class SameRatioClass
{
///
/// 左右脸相似度函数
///
///
///
///
public static double SameRatio(Bitmap src, Bitmap dst)
{
byte[] srcData = GetBytes(src);
byte[] dstData = GetBytes(dst);
double ratio = 0;
int sum = 0;
int std=0;
for (int i = 0; i < srcData.Length; i++)
{
sum += Math.Abs(srcData[i] - dstData[i]);
std += srcData[i];
}
ratio = 100-(double)(100*sum / std);
return ratio;
}
///
/// 得到图像信息函数
///
///
///
private static byte[] GetBytes(Bitmap src)
{
int w = src.Width;
int h = src.Height;
byte[] dataImage = new byte[w * h];
System.Drawing.Imaging.BitmapData srcData = src.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
unsafe
{
byte* pIn = (byte*)srcData.Scan0.ToPointer();
byte* p;
int stride = srcData.Stride;
int r, g, b;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
p = pIn;
r = p[2];
g = p[1];
b = p[0];
dataImage[x + y * x] = (byte)((r + g + b) / 3);
pIn += 3;
}
pIn += srcData.Stride - w * 3;
}
src.UnlockBits(srcData);
return dataImage;
}
}
}
}
3. 主窗体程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FaceSmile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
groupBox1.Visible = true;
groupBox2.Visible = false;
}
#region 全局变量定义
//定义原始图像变量
private Bitmap src ;
//定义图像相似度变量
private double ratio = 0;
//定义图像路径变量
private string curFileName;
//定义人脸位置图像调整变量
private int numAdjust = 0;
#endregion
#region 软件操作
//左脸对称
private void button1_Click(object sender, EventArgs e)
{
if (src != null)
{
Bitmap temp = (Bitmap)src.Clone();
Bitmap a = FaceClass.FaceFlipLeft(temp);
pictureBox1.Image = (Image)a;
ratio = SameRatioClass.SameRatio(a, src);
label1.Text = ratio.ToString();
}
else
{
MessageBox.Show("Please open one image!");
}
}
//右脸对称
private void button2_Click(object sender, EventArgs e)
{
if (src != null)
{
Bitmap temp = (Bitmap)src.Clone();
Bitmap a = FaceClass.FaceFlipRight(temp);
pictureBox1.Image = (Image)a;
ratio = SameRatioClass.SameRatio(a, src);
label1.Text = ratio.ToString();
}
else
{
MessageBox.Show("Please open one image!");
}
}
//打开图像
private void button3_Click(object sender, EventArgs e)
{
OpenImage();
if (src != null)
{
pictureBox1.Image = (Image)src;
pictureBox1.Width = src.Width;
pictureBox1.Height = src.Height;
}
else
{
MessageBox.Show("Please open one image!");
}
}
//保存图像
private void button4_Click(object sender, EventArgs e)
{
SaveImage();
}
//图像打开函数
private void OpenImage()
{
try
{
ofd.Filter = "All files (*.*)|*.*|bmp files (*.bmp)|*.bmp|jpeg files (*.jpg)|*.jpg|png files (*.png)|*.png";
ofd.Title = "打开";
ofd.ShowHelp = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
curFileName = ofd.FileName;
src = new Bitmap(curFileName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//图像保存函数
private void SaveImage()
{
try
{
sfd.Filter = "保存(*.bmp)|*.bmp";
sfd.Title = "保存";
sfd.ShowHelp = true;
if (sfd.ShowDialog() == DialogResult.OK)
{
Bitmap temp = (Bitmap)pictureBox1.Image;
temp.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//其他操作
private void button5_Click(object sender, EventArgs e)
{
groupBox2.Location = new Point(groupBox1.Location.X, groupBox1.Location.Y);
groupBox2.Visible = true;
groupBox1.Visible = false;
}
//肤色检测
private void button6_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
{
pictureBox1.Image = (Image)FaceClass.SkinDetect((Bitmap)pictureBox1.Image);
}
else
{
MessageBox.Show("Please open one image!");
}
}
//返回操作
private void button7_Click(object sender, EventArgs e)
{
groupBox1.Visible = true;
groupBox2.Visible = false;
}
//灰度化
private void button8_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
{
pictureBox1.Image = (Image)FaceClass.ImageGray((Bitmap)pictureBox1.Image);
}
else
{
MessageBox.Show("Please open one image!");
}
}
//博客连接
private void label2_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("IEXPLORE.EXE", "http://dongtingyueh.blog.163.com/");
}
//修正人脸位置
private void button9_Click(object sender, EventArgs e)
{
if (numAdjust != 0)
{
int a = numAdjust;
int b = src.Width - a;
int result = a < b ? a : b;
if (result == b)
{
src = src.Clone(new Rectangle(src.Width - 2 * result, 0, 2 * result, src.Height), src.PixelFormat);
}
else
{
src = src.Clone(new Rectangle(0, 0, 2 * result, src.Height), src.PixelFormat);
}
pictureBox1.Image = (Image)src;
pictureBox1.Width = src.Width;
pictureBox1.Height = src.Height;
}
trackBar1.Value = 0;
label4.Text = "0";
}
#endregion
#region 人脸位置修正
private void trackBar1_Scroll(object sender, EventArgs e)
{
if (src != null)
{
trackBar1.Maximum = src.Width;
trackBar1.Minimum = 0;
numAdjust = trackBar1.Value;
label4.Text = numAdjust.ToString();
}
else
{
MessageBox.Show("Please open one image!");
}
}
private void trackBar1_ValueChanged(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
private void trackBar1_MouseUp(object sender, MouseEventArgs e)
{
if (src != null)
{
Graphics g = pictureBox1.CreateGraphics();
g.DrawLine(new Pen(Color.Red, 2), new Point((int)(numAdjust), 0), new Point((int)(numAdjust), src.Height));
g.Dispose();
}
else
{
MessageBox.Show("Please open one image!");
}
}
#endregion
}
}
希望本文所述对大家的C#程序设计有所帮助。
dlibdotnet 人脸相似度源代码_C#开发的人脸左右相似度计算软件源码分析相关推荐
- SpringBoot-web开发(四): SpringMVC的拓展、接管(源码分析)
[SpringBoot-web系列]前文: SpringBoot-web开发(一): 静态资源的导入(源码分析) SpringBoot-web开发(二): 页面和图标定制(源码分析) SpringBo ...
- pythonweb项目源码下载_最新Python WEB开发在线教育项目之谷粒教育 软件源码齐全...
[课程内容]项目准备 01.根据模板页面抽象app 02.app当中模型类(表)的抽象(1) 03.app当中模型类(表)的抽象(2) 04.项目的创建和配置 05.创建其余app配置子路由,创建自主 ...
- SNMP功能开发简介 二 net-snmp源码分析报文处理流程图
最近在开发snmp功能,核心实现是基于net-snmp,将net-snmp的代理基本功能移植到自己的程序中去,因为需要修改一些定制化的内容,所以需要对net-snmp的流程有所了解,网上这方面的资料比 ...
- java 源代码 分析_Java Collections 源码分析
Java Collections API源码分析 侯捷老师剖析了不少Framework,如MFC,STL等.侯老师有句名言: 源码面前,了无秘密 这句话还在知乎引起广泛讨论. 我对教授程序设计的一点想 ...
- 云瞻外卖江湖外卖满天星外卖美赚外卖CPS系统小程序软件源码开发
云瞻外卖江湖外卖满天星外卖美赚外卖CPS系统小程序软件源码开发 外卖CPS红包小程序源码分享 外卖券外卖省省外卖探探美团饿了么外卖联盟优惠券小程序系统软件开发源码 美团/饿了么外卖CPS联盟返利公众号 ...
- JSP汽车销售管理系统myeclipse开发sql计算机程序web结构java编程网页源码
一.源码特点 JSP 汽车销售管理系统 是一套完善的web设计系统,对理解JSP java编程开发语言有帮助,系统具有完整的源代码和数据库,系统主要采用B/S模式开发 JSP汽车销售管理系 ...
- java rfid demo_UHF-DEMO-JAVA RFID UHF超高频设备的 版本软件源码 238万源代码下载- www.pudn.com...
文件名称: UHF-DEMO-JAVA下载 收藏√ [ 5 4 3 2 1 ] 所属分类: RFID 开发工具: Java 文件大小: 218 KB 上传时间: 2013-09-10 下载次 ...
- 基于比原链开发Dapp(三)-Dapp-demo前端源码分析
# 简介 本章内容会针对比原官方提供的dapp-demo,分析里面的前端源码,分析清楚整个demo的流程,然后针对里面开发过程遇到的坑,添加一下个人的见解还有解决的方案. ### 储蓄分红合 ...
- Bytom Dapp 开发笔记(三):Dapp Demo前端源码分析
本章内容会针对比原官方提供的dapp-demo,分析里面的前端源码,分析清楚整个demo的流程,然后针对里面开发过程遇到的坑,添加一下个人的见解还有解决的方案. 储蓄分红合约简述 为了方便理解,这里简 ...
- SpringBoot-web开发(二): 页面和图标定制(源码分析)
[SpringBoot-web系列]前文: SpringBoot-web开发(一): 静态资源的导入(源码分析) 目录 一.首页 1. 源码分析 2. 访问首页测试 二.动态页面 1. 动态资源目录t ...
最新文章
- 《JavaScript高级程序设计》(第2版)上市
- oracle技术之使用rman找回被误删除表空间
- python3爬虫入门教程-有什么好的python3爬虫入门教程或书籍吗?
- 如何快速成长为技术大牛?
- 三星android5.0基带,三星首款5G手机没用高通!问题来了:5G基带哪家强?
- c#如何通过ftp上传文件_定时上传文件到ftp,如何使用工具定时上传文件到ftp
- 计算机网络及公文写作知识,计算机网络期末复习题
- Centos6.5 安装Vim7.4
- [编织消息框架][设计协议]位运算
- 前端程序员简历模板整理和下载
- meta camp+21春季PAT乙级反思
- C语言求解一元二次方程组的代码
- Linux三剑客 grep sed awk 详细使用方法
- Matlab做一个刚体运动仿真,模拟小车避障
- 阿里P8整理Mysql面试题答案,助你面试“脱颖而出”
- Unity3D: 给字符串中的部分字体添加颜色突出显示
- 【天梯赛 - L2习题集】啃题(12 / 44)
- 真心话大冒险,一款小程序让你看清你朋友的内心
- Spring Boot 2.X + Shiro 优雅解决 session 跨域问题
- 什么是区块链?CNS积分怎么赚钱
热门文章
- 抽象代数 01.06 变换群与置换群
- [渝粤教育] 深圳职业技术学院 安全教育与应急处理训练 参考 资料
- 腾讯云TDSQL TCP干货
- winxp如何打开计算机的端口,xp系统怎么打开445端口呢?开启445端口的教程
- mysql sql 字符串拼接_深入讲解SQL中的字符串拼接
- postgresql 查询sql字符串拼接相关
- 修复 VM Player 断网问题 20121215T1402
- 【转】将 azw3 格式转换为 mobi 格式并保持原有排版格式
- ThinkPHP(TP框架)的归纳与总结(一)----基于TP开发手册
- 程序员八大排序算法之直接选择排序算法(java版)