基于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

相关文章

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

python实现svg图片转换为png和gif

《python实现svg图片转换为png和gif》这篇文章主要为大家详细介绍了python如何实现将svg图片格式转换为png和gif,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录python实现svg图片转换为png和gifpython实现图片格式之间的相互转换延展:基于Py

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

Python实现图片分割的多种方法总结

《Python实现图片分割的多种方法总结》图片分割是图像处理中的一个重要任务,它的目标是将图像划分为多个区域或者对象,本文为大家整理了一些常用的分割方法,大家可以根据需求自行选择... 目录1. 基于传统图像处理的分割方法(1) 使用固定阈值分割图片(2) 自适应阈值分割(3) 使用图像边缘检测分割(4)

C# foreach 循环中获取索引的实现方式

《C#foreach循环中获取索引的实现方式》:本文主要介绍C#foreach循环中获取索引的实现方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、手动维护索引变量二、LINQ Select + 元组解构三、扩展方法封装索引四、使用 for 循环替代

C# Where 泛型约束的实现

《C#Where泛型约束的实现》本文主要介绍了C#Where泛型约束的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录使用的对象约束分类where T : structwhere T : classwhere T : ne

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

C#中async await异步关键字用法和异步的底层原理全解析

《C#中asyncawait异步关键字用法和异步的底层原理全解析》:本文主要介绍C#中asyncawait异步关键字用法和异步的底层原理全解析,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录C#异步编程一、异步编程基础二、异步方法的工作原理三、代码示例四、编译后的底层实现五、总结C#异步编程

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

C#TextBox设置提示文本方式(SetHintText)

《C#TextBox设置提示文本方式(SetHintText)》:本文主要介绍C#TextBox设置提示文本方式(SetHintText),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录C#TextBox设置提示文本效果展示核心代码总结C#TextBox设置提示文本效果展示核心代