基于C#的眼部瞳孔追踪 与 双图片融合成像

2023-11-04 08:20

本文主要是介绍基于C#的眼部瞳孔追踪 与 双图片融合成像,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前期我们先用画饼充饥的方法来做研发与测试

手动画人眼

测试算法

蓝线中心点为瞳孔中心,可以看到比较准。

具体代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;namespace WindowsFormsApplication8
{public partial class Form1 : Form{Point p = new Point();static Bitmap b;int h;int w;Bitmap b1;BitmapData bd;BitmapData bd1;public Form1(){InitializeComponent();this.Show();String s = "";b = null;OpenFileDialog ofd = new OpenFileDialog();if (ofd.ShowDialog() == DialogResult.OK){s = ofd.FileName;b = (Bitmap)Bitmap.FromFile(s, false);int[] point = getdivimg(b);try{//Bitmap bmp = new Bitmap(b.Width, b.Height, PixelFormat.Format32bppArgb);MessageBox.Show(point[0] + "---" + point[1]);p.X = point[1];p.Y = point[0];this.pictureBox1.Load(s);//g = Graphics.FromImage(bmp);//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;//Image img = Image.FromFile(s);//g.DrawImage(img, 0, 0);pictureBox1.Refresh();//g.FillEllipse(Brushes.Red, 0, 0, 100, 100);this.pictureBox1.Image = img;//g.Dispose();}catch (Exception e1){MessageBox.Show(e1.Message);}}}private void Form1_Load(object sender, EventArgs e){this.Show();}public void setgray(Bitmap b){ColorPalette cp = b.Palette;for (int i = 0; i < 256; i++){cp.Entries[i] = Color.FromArgb(i, i, i);}b.Palette = cp;}public int[] getdivimg(Bitmap b){h = b.Height;w = b.Width;Rectangle rect = new Rectangle(0, 0, w, h);bd = b.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);b1 = new Bitmap(b.Width, b.Height, PixelFormat.Format8bppIndexed);bd1 = b1.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);int len = bd.Stride * h;byte[] imgbyte = new byte[len];Marshal.Copy(bd.Scan0, imgbyte, 0, len);int i = 0;int j = 0;int[] hist = new int[256];int maxgray = 0;int maxpos = 0;int bogugray = 0;int bogupos = 0;try{for (i = 0; i < h; i++){for (j = 0; j < w; j++){int t = imgbyte[i * bd.Stride + j];hist[t]++;if (hist[t] > maxgray){maxgray = hist[t];maxpos = t;}}}bogugray = maxgray;for (int k = 9; k < maxpos; k++){if (hist[k] < bogugray){bogugray = hist[k];bogupos = k;}}int[] apoint = getcenterpoint(imgbyte, 0, h, 0, w, bogupos);MessageBox.Show(apoint[0] + "---" + apoint[1]);int[] point = getcenterpoint(imgbyte, apoint[0] - 100, apoint[0] + 100, apoint[1] - 100, apoint[1] + 100, bogupos);MessageBox.Show(point[0] + "---" + point[1]);return point;}catch (Exception e1){MessageBox.Show(e1.Message);return null;}}double r;public int[] getcenterpoint(byte[] imgbyte, int xstart = 0, int xend = 378, int ystart = 0, int yend = 681, int thresh=0){if(xstart < 0)xstart=0;if (xend > h)xend = h;if (ystart < 0)ystart = 0;if (yend > w)yend = w;bool firstpoint = false;int firstx = 0;int firsty = 0;int xpossum = 0, ypossum = 0, count = 0;for (int i = xstart; i < xend; i++){for (int j = ystart; j < yend; j++){//Console.WriteLine(i * bd.Stride + j);int t = imgbyte[i * bd.Stride + j];if (t <= thresh){if (!firstpoint) {firstx = i;firsty = j;firstpoint = true;}imgbyte[i * bd.Stride + j] = (byte)0;xpossum += i;ypossum += j;count++;}else{imgbyte[i * bd.Stride + j] = (byte)255;}}}//b.UnlockBits(bd);int[] point = new int[2];point[0] = xpossum / count;point[1] = ypossum / count;r = Math.Sqrt(Math.Abs(firstx - point[0]) * Math.Abs(firstx - point[0]) + Math.Abs(firsty - point[1]) * Math.Abs(firsty - point[1]));Console.WriteLine(r);return point;}private void pictureBox1_Paint(object sender, PaintEventArgs e){}private void pictureBox1_Paint_1(object sender, PaintEventArgs e){Pen pen=new Pen (Color .Blue,50);Point q = new Point(p.X + 1, p.Y + 1);e.Graphics.DrawLine(pen, p, q);}private void pictureBox1_Click(object sender, EventArgs e){}}
}

那么对真实人眼准不准呢

 


接下来是读入两副眼睛图片并做拼接融合

上面是先用picturebox做的伪融合,真正的融合需要在内存地址中融合,用到Marshal

伪融合代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;namespace WindowsFormsApplication8
{public partial class Form1 : Form{Point p = new Point();Point p2 = new Point();static Bitmap b;static Bitmap b2;int h;int w;Bitmap b1;BitmapData bd;BitmapData bd1;public Form1(){InitializeComponent();this.Show();String s = "";b = null;OpenFileDialog ofd = new OpenFileDialog();if (ofd.ShowDialog() == DialogResult.OK){s = ofd.FileName;b = (Bitmap)Bitmap.FromFile(s, false);int[] point = getdivimg(b);try{//Bitmap bmp = new Bitmap(b.Width, b.Height, PixelFormat.Format32bppArgb);MessageBox.Show(point[0] + "---" + point[1]);p.X = point[1];p.Y = point[0];this.pictureBox1.Load(s);//g = Graphics.FromImage(bmp);//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;//Image img = Image.FromFile(s);//g.DrawImage(img, 0, 0);pictureBox1.Refresh();//g.FillEllipse(Brushes.Red, 0, 0, 100, 100);this.pictureBox1.Image = img;//g.Dispose();}catch (Exception e1){MessageBox.Show(e1.Message);}}if (ofd.ShowDialog() == DialogResult.OK){s = ofd.FileName;b2 = (Bitmap)Bitmap.FromFile(s, false);int[] point = getdivimg(b2);try{//Bitmap bmp = new Bitmap(b.Width, b.Height, PixelFormat.Format32bppArgb);MessageBox.Show(point[0] + "---" + point[1]);p2.X = point[1];p2.Y = point[0];this.pictureBox2.Load(s);//g = Graphics.FromImage(bmp);//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;//Image img = Image.FromFile(s);//g.DrawImage(img, 0, 0);pictureBox2.Refresh();//g.FillEllipse(Brushes.Red, 0, 0, 100, 100);this.pictureBox1.Image = img;//g.Dispose();}catch (Exception e1){MessageBox.Show(e1.Message);}}}private void Form1_Load(object sender, EventArgs e){this.Show();}public void setgray(Bitmap b){ColorPalette cp = b.Palette;for (int i = 0; i < 256; i++){cp.Entries[i] = Color.FromArgb(i, i, i);}b.Palette = cp;}public int[] getdivimg(Bitmap b){h = b.Height;w = b.Width;Rectangle rect = new Rectangle(0, 0, w, h);bd = b.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);b1 = new Bitmap(b.Width, b.Height, PixelFormat.Format8bppIndexed);bd1 = b1.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);int len = bd.Stride * h;byte[] imgbyte = new byte[len];Marshal.Copy(bd.Scan0, imgbyte, 0, len);int i = 0;int j = 0;int[] hist = new int[256];int maxgray = 0;int maxpos = 0;int bogugray = 0;int bogupos = 0;try{for (i = 0; i < h; i++){for (j = 0; j < w; j++){int t = imgbyte[i * bd.Stride + j];hist[t]++;if (hist[t] > maxgray){maxgray = hist[t];maxpos = t;}}}bogugray = maxgray;for (int k = 9; k < maxpos; k++){if (hist[k] < bogugray){bogugray = hist[k];bogupos = k;}}int[] apoint = getcenterpoint(imgbyte, 0, h, 0, w, bogupos);MessageBox.Show(apoint[0] + "---" + apoint[1]);int[] point = getcenterpoint(imgbyte, apoint[0] - 100, apoint[0] + 100, apoint[1] - 100, apoint[1] + 100, bogupos);MessageBox.Show(point[0] + "---" + point[1]);return point;}catch (Exception e1){MessageBox.Show(e1.Message);return null;}}double r;public int[] getcenterpoint(byte[] imgbyte, int xstart = 0, int xend = 378, int ystart = 0, int yend = 681, int thresh=0){if(xstart < 0)xstart=0;if (xend > h)xend = h;if (ystart < 0)ystart = 0;if (yend > w)yend = w;bool firstpoint = false;int firstx = 0;int firsty = 0;int xpossum = 0, ypossum = 0, count = 0;for (int i = xstart; i < xend; i++){for (int j = ystart; j < yend; j++){//Console.WriteLine(i * bd.Stride + j);int t = imgbyte[i * bd.Stride + j];if (t <= thresh){if (!firstpoint) {firstx = i;firsty = j;firstpoint = true;}imgbyte[i * bd.Stride + j] = (byte)0;xpossum += i;ypossum += j;count++;}else{imgbyte[i * bd.Stride + j] = (byte)255;}}}//b.UnlockBits(bd);int[] point = new int[2];point[0] = xpossum / count;point[1] = ypossum / count;r = Math.Sqrt(Math.Abs(firstx - point[0]) * Math.Abs(firstx - point[0]) + Math.Abs(firsty - point[1]) * Math.Abs(firsty - point[1]));Console.WriteLine(r);return point;}private void pictureBox1_Paint(object sender, PaintEventArgs e){}private void pictureBox1_Paint_1(object sender, PaintEventArgs e){Pen pen = new Pen(Color.Blue, 50);Point q = new Point(p.X, p.Y + 1);e.Graphics.DrawLine(pen, p, q);Point q2 = new Point(p.X, p.Y - 1);e.Graphics.DrawLine(pen, p, q2);Point q3 = new Point(p.X+1, p.Y );e.Graphics.DrawLine(pen, p, q3);Point q4 = new Point(p.X-1, p.Y);e.Graphics.DrawLine(pen, p, q4);}private void pictureBox1_Click(object sender, EventArgs e){}private void pictureBox2_Paint(object sender, PaintEventArgs e){Pen pen = new Pen(Color.Blue, 50);Point q = new Point(p2.X, p2.Y + 1);e.Graphics.DrawLine(pen, p2, q);Point q2 = new Point(p2.X, p2.Y - 1);e.Graphics.DrawLine(pen, p2, q2);Point q3 = new Point(p2.X + 1, p2.Y);e.Graphics.DrawLine(pen, p2, q3);Point q4 = new Point(p2.X - 1, p2.Y);e.Graphics.DrawLine(pen, p2, q4);}}
}

真正融合代码:

在自己开发的程序中,初次尝试byte上的融合

出现了这种情况

右边为融合之后的,出现了间隔栅格+噪声


做了一些调试以后,感觉问题应该是在于 变量没上锁,变量还没进入 就被读出,所以会产生噪声和黑色蒙版

尝试在变量上加锁

这篇关于基于C#的眼部瞳孔追踪 与 双图片融合成像的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/344444

相关文章

C#实现文件读写到SQLite数据库

《C#实现文件读写到SQLite数据库》这篇文章主要为大家详细介绍了使用C#将文件读写到SQLite数据库的几种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录1. 使用 BLOB 存储文件2. 存储文件路径3. 分块存储文件《文件读写到SQLite数据库China编程的方法》博客中,介绍了文

使用C#如何创建人名或其他物体随机分组

《使用C#如何创建人名或其他物体随机分组》文章描述了一个随机分配人员到多个团队的代码示例,包括将人员列表随机化并根据组数分配到不同组,最后按组号排序显示结果... 目录C#创建人名或其他物体随机分组此示例使用以下代码将人员分配到组代码首先将lstPeople ListBox总结C#创建人名或其他物体随机分组

在C#中合并和解析相对路径方式

《在C#中合并和解析相对路径方式》Path类提供了几个用于操作文件路径的静态方法,其中包括Combine方法和GetFullPath方法,Combine方法将两个路径合并在一起,但不会解析包含相对元素... 目录C#合并和解析相对路径System.IO.Path类幸运的是总结C#合并和解析相对路径对于 C

C#中字符串分割的多种方式

《C#中字符串分割的多种方式》在C#编程语言中,字符串处理是日常开发中不可或缺的一部分,字符串分割是处理文本数据时常用的操作,它允许我们将一个长字符串分解成多个子字符串,本文给大家介绍了C#中字符串分... 目录1. 使用 string.Split2. 使用正则表达式 (Regex.Split)3. 使用

使用 Python 和 LabelMe 实现图片验证码的自动标注功能

《使用Python和LabelMe实现图片验证码的自动标注功能》文章介绍了如何使用Python和LabelMe自动标注图片验证码,主要步骤包括图像预处理、OCR识别和生成标注文件,通过结合Pa... 目录使用 python 和 LabelMe 实现图片验证码的自动标注环境准备必备工具安装依赖实现自动标注核心

C# Task Cancellation使用总结

《C#TaskCancellation使用总结》本文主要介绍了在使用CancellationTokenSource取消任务时的行为,以及如何使用Task的ContinueWith方法来处理任务的延... 目录C# Task Cancellation总结1、调用cancellationTokenSource.

C# dynamic类型使用详解

《C#dynamic类型使用详解》C#中的dynamic类型允许在运行时确定对象的类型和成员,跳过编译时类型检查,适用于处理未知类型的对象或与动态语言互操作,dynamic支持动态成员解析、添加和删... 目录简介dynamic 的定义dynamic 的使用动态类型赋值访问成员动态方法调用dynamic 的

C#如何优雅地取消进程的执行之Cancellation详解

《C#如何优雅地取消进程的执行之Cancellation详解》本文介绍了.NET框架中的取消协作模型,包括CancellationToken的使用、取消请求的发送和接收、以及如何处理取消事件... 目录概述与取消线程相关的类型代码举例操作取消vs对象取消监听并响应取消请求轮询监听通过回调注册进行监听使用Wa

通过C#和RTSPClient实现简易音视频解码功能

《通过C#和RTSPClient实现简易音视频解码功能》在多媒体应用中,实时传输协议(RTSP)用于流媒体服务,特别是音视频监控系统,通过C#和RTSPClient库,可以轻松实现简易的音视... 目录前言正文关键特性解决方案实现步骤示例代码总结最后前言在多媒体应用中,实时传输协议(RTSP)用于流媒体服

C#图表开发之Chart详解

《C#图表开发之Chart详解》C#中的Chart控件用于开发图表功能,具有Series和ChartArea两个重要属性,Series属性是SeriesCollection类型,包含多个Series对... 目录OverviChina编程ewSeries类总结OverviewC#中,开发图表功能的控件是Char