HARRIS角点检测

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.Util;

namespace WindowsFormsApplication28
{
    public partial class Form1 : Form
    {
        private Capture _capture = null;
        private bool _captureInProgress;
        public Form1()
        {
            InitializeComponent();  

            CvInvoke.UseOpenCL = false;
            try
            {
                _capture = new Capture();
                _capture.ImageGrabbed += ProcessFrame;
            }
            catch (NullReferenceException excpt)
            {
                MessageBox.Show(excpt.Message);
            }
        }
        private void ProcessFrame(object sender, EventArgs arg)
        {
            Mat frame = new Mat();
            _capture.Retrieve(frame, 0);
            Mat grayFrame = new Mat();
            CvInvoke.CvtColor(frame, grayFrame, ColorConversion.Bgr2Gray);
            
            Mat smallGrayFrame = new Mat();
            CvInvoke.PyrDown(grayFrame, smallGrayFrame);
            Mat smoothedGrayFrame = new Mat();
            CvInvoke.PyrUp(smallGrayFrame, smoothedGrayFrame);

            //Image<Gray, Byte> smallGrayFrame = grayFrame.PyrDown();
            //Image<Gray, Byte> smoothedGrayFrame = smallGrayFrame.PyrUp();
            Mat cannyFrame = new Mat();
            CvInvoke.Canny(smoothedGrayFrame, cannyFrame, 100, 60);
           
            //Image<Gray, Byte> cannyFrame = smoothedGrayFrame.Canny(100, 60);

            captureImageBox.Image = frame;
            grayscaleImageBox.Image = grayFrame;
            smoothedGrayscaleImageBox.Image = smoothedGrayFrame;
            cannyImageBox.Image = cannyFrame;
        }

         private void captureButton_Click(object sender, EventArgs e)
        { 
            if (_capture != null)
            {
                if (_captureInProgress)
                {  //stop the capture
                    captureButton.Text = "Start Capture";
                    _capture.Pause();
                }
                else
                {
                    //start the capture
                    captureButton.Text = "Stop";
                    _capture.Start();
                }
               
                _captureInProgress = !_captureInProgress;
            }
        }
        
        private void ReleaseData()
        {
            if (_capture != null)
                _capture.Dispose();
        }

      

      
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void FlipHorizontalButton_Click(object sender, EventArgs e)
        {
            
            if (_capture != null) _capture.FlipHorizontal = !_capture.FlipHorizontal;
            grayscaleImageBox.Image.Save("GrayFrame.JPG");
        }

        private void FlipVerticalButton_Click(object sender, EventArgs e)
        {
          
            if (_capture != null) _capture.FlipVertical = !_capture.FlipVertical;
       
        }

        private void FindChessboardCorners_Click(object sender, EventArgs e)
        {

            //Image<Bgr, byte> img = (Image<Bgr, byte>)imageBox_Ori.Image;
            //Image<Gray, byte> ImgGray = img.Convert<Gray, byte>();
            //Size  patternSize;
            //PointF[] corners =  CvInvoke.CornerHarris( ImgGray, patternSize,
            //    CALIB_CB_TYPE.ADAPTIVE_THRESH
            //    //| CALIB_CB_TYPE.FILTER_QUADS
            //    // CALIB_CB_TYPE.NORMALIZE_IMAGE
            //    //CALIB_CB_TYPE.DEFAULT
            //    );

            //if (corners != null) //chess board found
            //{
            //    //make mesurments more accurate by using FindCornerSubPixel   使通过FindCornerSubPix测量更精确
            //    ImgGray.FindCornerSubPix(new PointF[1][] { corners }, new Size(11, 11), new Size(-1, -1), new MCvTermCriteria(30, 0.1));

            //    //fill line colour array
            //    Random R = new Random();
            //    for (int i = 0; i < corners.Length; i++)
            //    {
            //        Bgr  line_colour_array[i] = new Bgr(R.Next(0, 255), R.Next(0, 255), R.Next(0, 255)) ;
            //    }

            //    //dram the results
            //    img.Draw(new CircleF(corners[0], 3), new Bgr(Color.Yellow), 1);
            //    for (int i = 1; i < corners.Length; i++)
            //    {
            //        img.Draw(new LineSegment2DF(corners[i - 1], corners[i]), line_colour_array[i], 2);
            //        img.Draw(new CircleF(corners[i], 5), new Bgr(Color.Yellow), 2);
            //    }

 

            Image<Bgr , byte> image = new Image<Bgr , byte>("GrayFrame.JPG");
            Image<Gray, float> b = new Image<Gray, float>(image.Width, image.Height);
            Image<Gray, byte> c = new Image<Gray, byte>(image.Width, image.Height);
            CvInvoke.CornerHarris(image.Convert<Gray, byte>(), b, 2);     //注意:角点检测传出的为Float类型的数据
            CvInvoke.Normalize(b, b, 0, 255, NormType.MinMax, DepthType.Cv32F);  //正常化输入数组,使得它的范数或值范围取一定值(多个)。
            double min = 0, max = 0;
            Point minp = new Point(0, 0);
            Point maxp = new Point(0, 0);
            CvInvoke.MinMaxLoc(b, ref min, ref max, ref minp, ref maxp);//查找最小和最大的元素值和它们的位置。
            double scale = 255 / (max - min);
            double shift = min * scale ;
            CvInvoke.ConvertScaleAbs(b, c, scale, shift);//进行缩放,转化为byte类型 //类似于cvCvtScale但它存储转换结果的绝对值:
            byte[] data = c.Bytes; //获取或设置表示此数组中的数据的字节数组
            for (int i = 0; i < b.Height; i++)
            {
                for (int j = 0; j < b.Width; j++)
                {
                    int k = i * image.Width + j;
                    if (data[k] > 200)    //通过阈值判断
                    {
                        CvInvoke.Circle(image, new Point(j, i), 1, new MCvScalar(0, 0, 255, 255), 2);
                    }
                }
            }

                //corners_points_list[0] = corners;
            imageBox_Ori.Image = image;
            imageBox_Ori.Refresh();
            }

        
        }

      
    }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值