版权所有 详情咨询qq 849495327
内容和要求如下:
任务:提取一幅数字影像中的特征点
内容:使用Moravec算子编写程序,从一幅数字影像中自动提取出50个以上的特征点
要求:程序能读取数字图像、在窗口中显示整幅图像、在图像中显示所提取的特征点(+),并列表显示各特征点的像素坐标。
算法流程及计算原理
1.选取一个合理的邻域遍历图像,这里是5*5邻域的。在邻域中依次计算,垂直,水平,对角与反对角四个相邻像素灰度的差的平方和,作为该邻域特征值。
大致就是下面这个样子:
公式:
这里k是窗口的半径。
2.从四个特征值中选最小的值作为该像素初次候选特征值。
公式:
3.设定一个阈值,将大于该阈值初次候选特征值的选为二次候选特征值。
4.设定一个邻域,将该邻域最大的二次候选特征值作为最终要选择的特征值。
界面调试
主要界面的代码:
FORM1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace shuzituxiangchuli
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string curFilename;
private System.Drawing.Bitmap curbitmap;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openglg = new OpenFileDialog();
openglg.Filter = "所有图像文件|*.bmp;*.png;*.pcx;*.jpg;*.gif;*.tif;*.ico";
openglg.Title = "打开图像文件";
openglg.ShowHelp = true;
if (openglg.ShowDialog() == DialogResult.OK)
{
curFilename = openglg.FileName;
try
{
curbitmap = (Bitmap)Image.FromFile(curFilename);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.panel1.CreateGraphics();
if (curbitmap != null)
{
int x, y;
x = curbitmap.Width;
y = curbitmap.Height;
while (x > this.Width - 160 && y > this.Height - 20)
{
x = x / 2;
y = y / 2;
}
g.DrawImage(curbitmap, 160, 20, x, y);
//g.DrawImage(curbitmap, 160, 20, curbitmap.Width, curbitmap.Height);
}
g.Dispose();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
//this.panel1.Left = 10;
this.panel1.BackColor = Color.CadetBlue;
}
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
//curbitmap = new Bitmap(curbitmap, new Size(curbitmap.Width / 2, curbitmap.Height / 2));
Graphics g = this.panel1.CreateGraphics();
g.Clear(this.panel1.BackColor);
}
private void Form1_Scroll(object sender, ScrollEventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
FrmhistR f = new FrmhistR(curbitmap);
f.Show();
FrmhistG G = new FrmhistG(curbitmap);
G.Show();
FrmhistB B = new FrmhistB(curbitmap);
B.Show();
}
private void button5_Click(object sender, EventArgs e)
{
if (curbitmap != null)
{
Frmliner finearfrom = new Frmliner();
if (finearfrom.ShowDialog() == DialogResult.OK)
{
Rectangle rect = new Rectangle(0, 0, curbitmap.Width, curbitmap.Height);
System.Drawing.Imaging.BitmapData bmpdata = curbitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
curbitmap.PixelFormat);
IntPtr ptr = bmpdata.Scan0;
int bytes = 3 * curbitmap.Width * curbitmap.Height;
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
int temp = 0;
double a = Convert.ToDouble(finearfrom.Getscaling);
double b = Convert.ToDouble(finearfrom.Getoffset);
for (int i = 0; i < bytes; i++)
{
temp = (int)(a * grayValues[i] + b + 0.5);
if (temp > 255)
{
grayValues[i] = 255;
}
else
if (temp < 0)
grayValues[i] = 0;
else
grayValues[i] = (byte)temp;
}
System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
curbitmap.UnlockBits(bmpdata);
}
Invalidate();
}
}
private void button6_Click(object sender, EventArgs e)
{
if (curbitmap != null)
{
Rectangle rect = new Rectangle(0, 0, curbitmap.Width, curbitmap.Height);
System.Drawing.Imaging.BitmapData bmpdata = curbitmap.LockBits(rect,
System.Drawing.Imaging.ImageLockMode.ReadWrite, curbitmap.PixelFormat);
IntPtr ptr = bmpdata.Scan0;
int bytes = 2 * curbitmap.Width * curbitmap.Height;
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
byte a = 255;
byte b = 0;
double p;
for (int i = 0; i < bytes; i++)
{
if (a > grayValues[i])
{
a = grayValues[i];
}
if (b < grayValues[i])
{
b = grayValues[i];
}
}
p = 255.0 / (b - a);
for (int i = 0; i < bytes; i++)
{
grayValues[i] = (byte)(p * (grayValues[i] - a) + 0.5);
}
System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
curbitmap.UnlockBits(bmpdata);
Invalidate();
}
}
private void button7_Click(object sender, EventArgs e)
{
if (curbitmap != null)
{
Rectangle rect = new Rectangle(0, 0, curbitmap.Width, curbitmap.Height);
System.Drawing.Imaging.BitmapData bmpdata = curbitmap.LockBits(rect,
System.Drawing.Imaging.ImageLockMode.ReadWrite, curbitmap.PixelFormat);
IntPtr ptr = bmpdata.Scan0;
int bytes = 3 * curbitmap.Width * curbitmap.Height;
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
for (int i = 0; i < bytes; i += 3)
{
grayValues[i] = 0;
grayValues[i + 1] = 0;
}
System.Runtime.InteropServices.Marshal.Copy(grayValues, 0, ptr, bytes);
curbitmap.UnlockBits(bmpdata);
Invalidate();
}
}
private void button8_Click(object sender, EventArgs e)
{
if (curbitmap != null)
{
Rectangle rect = new Rectangle(0, 0, curbitmap.Width, curbitmap.Height);
System.Drawing.Imaging.BitmapData bmpdata = curbitmap.LockBits(rect,
System.Drawing.Imaging.ImageLockMode.ReadWrite, curbitmap.PixelFormat);
IntPtr ptr = bmpdata.Scan0;
int bytes = 3 * curbitmap.Width * curbitmap.Height;
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
double[] junzhi = new double[3];
junzhi[0] = 0; junzhi[1] = 0; junzhi[2] = 0;
//double j = 0;
for (int i = 0; i < bytes; i += 3)
{
junzhi[0] += grayValues[i];
junzhi[1] += grayValues[i + 1];
junzhi[2] += grayValues[i + 2];
// grayValues[i + 1] = 0;
}
junzhi[0] = junzhi[0] / bytes * 3;
junzhi[1] = junzhi[1] / bytes * 3;
junzhi[2] = junzhi[2] / bytes * 3;
double[] var = new double[3];
var[0] = 0; var[1] = 0; var[2] = 0;
for (int i = 0; i < bytes; i += 3)
{
var[0] += (grayValues[i] - junzhi[0]) * (grayValues[i] - junzhi[0]);
var[1] += (grayValues[i + 1] - junzhi[1]) * (grayValues[i + 1] - junzhi[1]);
var[2] += (grayValues[i + 2] - junzhi[2]) * (grayValues[i + 2] - junzhi[2]);
// grayValues[i + 1] = 0;
}
var[0] = var[0] / bytes * 3;
var[1] = var[1]