資源簡介
根據Harris算法 CSS算法寫的一個簡單圖像角點檢測類
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Drawing;
using?System.Drawing.Imaging;
using?System.Linq;
using?System.Text;
using?System.Threading.Tasks;
namespace?Postrue_Correction.ClassLib.Image
{
????class?MyImageCorner
????{
????????#region?Need
????????///?
????????///?生成并初始化一個8位灰度圖像
????????///?
????????public?static?Bitmap?CreateGrayscaleImage(int?width?int?height)
????????{
????????????Bitmap?bmp?=?new?Bitmap(width?height?PixelFormat.Format8bppIndexed);
????????????//?設置調色板
????????????SetGrayscalePalette(bmp);
????????????return?bmp;
????????}
????????///?
????????///?設置位圖的調色板至灰度模式
????????///?
????????private?static?void?SetGrayscalePalette(Bitmap?srcImg)
????????{
????????????//?檢查像素格式
????????????if?(srcImg.PixelFormat?!=?PixelFormat.Format8bppIndexed)
????????????????throw?new?ArgumentException();
????????????//?獲取調色板
????????????ColorPalette?cp?=?srcImg.Palette;
????????????//?初始化調色板
????????????for?(int?i?=?0;?i?256;?i++)
????????????{
????????????????cp.Entries[i]?=?Color.FromArgb(i?i?i);
????????????}
????????????//?設置調色板
????????????srcImg.Palette?=?cp;
????????}
????????#endregion
????????///?
????????///?Harris角點檢測
????????///?
????????///?
????????///?
????????///?
????????public?static?Bitmap?Harris(Bitmap?srcBitmap?byte?threshold)
????????{
????????????int?w?=?srcBitmap.Width;
????????????int?h?=?srcBitmap.Height;
????????????Bitmap?rs?=?CreateGrayscaleImage(w?h);
????????????Rectangle?rect?=?new?Rectangle(0?0?srcBitmap.Width?srcBitmap.Height);
????????????BitmapData?bmpData?=?srcBitmap.LockBits(rect?ImageLockMode.ReadOnly?srcBitmap.PixelFormat);
????????????BitmapData?rsData?=?rs.LockBits(rect?ImageLockMode.WriteOnly?PixelFormat.Format8bppIndexed);
????????????int?step?=?1;
????????????switch?(srcBitmap.PixelFormat)
????????????{
????????????????case?PixelFormat.Format24bppRgb:?step?=?3;
????????????????????break;
????????????????case?PixelFormat.Format32bppArgb:?step?=?4;
????????????????????break;
????????????????case?PixelFormat.Format8bppIndexed:?step?=?1;
????????????????????break;
????????????}
????????????int?istride?=?bmpData.Stride;
????????????int?ostride?=?rsData.Stride;
????????????double?gradX?gradY;
????????????double[]?gradXX?=?new?double[ostride?*?h];//x方向梯度的平方的數組
????????????double[]?gradYY?=?new?double[ostride?*?h];//y方向梯度的平方的數組
????????????double[]?gradXY?=?new?double[ostride?*?h];//x方向梯度與y方向梯度的乘積的數組
????????????double[]?CRF?=?new?double[ostride?*?h];//存儲可能的角點
????????????unsafe
????????????{
????????????????byte*?pin?=?(byte*)bmpData.Scan0.ToPointer();
????????????????byte*?pout?=?(byte*)rsData.Scan0.ToPointer();
????????????????//計算梯度
????????????????for?(int?y?=?0;?y?????????????????{
????????????????????for?(int?x?=?0;?x?
評論
共有 條評論