基于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#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

MySQL追踪数据库表更新操作来源的全面指南

《MySQL追踪数据库表更新操作来源的全面指南》本文将以一个具体问题为例,如何监测哪个IP来源对数据库表statistics_test进行了UPDATE操作,文内探讨了多种方法,并提供了详细的代码... 目录引言1. 为什么需要监控数据库更新操作2. 方法1:启用数据库审计日志(1)mysql/mariad

利用Python脚本实现批量将图片转换为WebP格式

《利用Python脚本实现批量将图片转换为WebP格式》Python语言的简洁语法和库支持使其成为图像处理的理想选择,本文将介绍如何利用Python实现批量将图片转换为WebP格式的脚本,WebP作为... 目录简介1. python在图像处理中的应用2. WebP格式的原理和优势2.1 WebP格式与传统

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

Python中图片与PDF识别文本(OCR)的全面指南

《Python中图片与PDF识别文本(OCR)的全面指南》在数据爆炸时代,80%的企业数据以非结构化形式存在,其中PDF和图像是最主要的载体,本文将深入探索Python中OCR技术如何将这些数字纸张转... 目录一、OCR技术核心原理二、python图像识别四大工具库1. Pytesseract - 经典O

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

Go语言如何判断两张图片的相似度

《Go语言如何判断两张图片的相似度》这篇文章主要为大家详细介绍了Go语言如何中实现判断两张图片的相似度的两种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 在介绍技术细节前,我们先来看看图片对比在哪些场景下可以用得到:图片去重:自动删除重复图片,为存储空间"瘦身"。想象你是一个