本文介绍了使用Emgu识别图像中的黄点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用EMGUCV识别图像中的黄斑

How shall i identify yellow spot in Images using EMGUCV

推荐答案


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.Structure;

namespace ColorDet
{
    public partial class Form1 : Form
    {
        private Capture _capture;
        private bool _captureInProgress;
        private Rectangle m_Rect;
        private Point m_ClickPoint;
        private Hsv m_TargetColor;
        private bool m_TrackColor;
        private Hsv m_Lower;
        private Hsv m_Higher;

        public Form1()
        {
            m_Rect = Rectangle.Empty;
            m_ClickPoint = Point.Empty;
            m_TrackColor = false;

            InitializeComponent();
        }


        private void ProcessFrame(object sender, EventArgs arg)
        {
            Image<Bgr, Byte> frame = _capture.QueryFrame();

            frame = frame.SmoothGaussian(3, 3, 1, 1);

            if (!m_ClickPoint.Equals(Point.Empty))
            {
                Rectangle Rect = new Rectangle(m_ClickPoint.X - 5, m_ClickPoint.Y - 5, 10, 10);

                if (Rect.X < 0)
                    Rect.X = 0;

                if (Rect.Y < 0)
                    Rect.Y = 0;

                using (Image<Bgr, Byte> TouchRegion = frame.GetSubRect(Rect))
                {
                    using (Image<Hsv, Byte> TouchRegionHsv = TouchRegion.Convert<Hsv, byte>())
                    {
                        int Points = TouchRegion.Width*TouchRegion.Height;

                        m_TargetColor = TouchRegionHsv.GetAverage();
                        Bgr Colr = Convert(m_TargetColor);

                        m_Lower = new Hsv(m_TargetColor.Hue - 30, m_TargetColor.Satuation - 30, m_TargetColor.Value - 30);
                        m_Higher = new Hsv(m_TargetColor.Hue + 30, m_TargetColor.Satuation + 30, m_TargetColor.Value + 30);

                        panel2.BackColor = Color.FromArgb((int)Colr.Red, (int)Colr.Green, (int)Colr.Blue);

                        frame.Draw(Rect, new Bgr(Color.Red), 1);
                        m_TrackColor = true;
                    }
                }

                m_ClickPoint = Point.Empty;
            }

            if (m_TrackColor)
            {
                Image<Hsv, Byte> HsvFrame = frame.Convert<Hsv, byte>();
                HsvFrame = HsvFrame.SmoothGaussian(5, 5, 0.1, 0.1);
                Image<Gray, byte> InRageFrame = HsvFrame.InRange(m_Lower, m_Higher);
                processedImageBox.Image = InRageFrame;
            }

            captureImageBox.Image = frame;
        }


        const float FLOAT_TO_BYTE = 255.0f;
        const float BYTE_TO_FLOAT = 1.0f / FLOAT_TO_BYTE;

        private Bgr Convert(Hsv hsv)
        {
            int H = (int)hsv.Hue;
            int S = (int)hsv.Satuation;
            int V = (int)hsv.Value;
            // HSV to RGB color conversion in OpenCV

            int bH = H; // H component
            int bS = S; // S component
            int bV = V; // V component
            float fH, fS, fV;
            float fR, fG, fB;

            // Convert from 8-bit integers to floats
            fH = (float)bH * BYTE_TO_FLOAT;
            fS = (float)bS * BYTE_TO_FLOAT;
            fV = (float)bV * BYTE_TO_FLOAT;

            // Convert from HSV to RGB, using float ranges 0.0 to 1.0
            int iI;
            float fI, fF, p, q, t;

            if (bS == 0)
            {
                // achromatic (grey)
                fR = fG = fB = fV;
            }
            else
            {
                // If Hue == 1.0, then wrap it around the circle to 0.0
                if (fH >= 1.0f)
                    fH = 0.0f;

                fH *= (float)6.0; // sector 0 to 5
                fI = (float)Math.Floor(fH); // integer part of h (0,1,2,3,4,5 or 6)
                iI = (int)fH; // " " " "
                fF = fH - fI; // factorial part of h (0 to 1)

                p = fV * (1.0f - fS);
                q = fV * (1.0f - fS * fF);
                t = fV * (1.0f - fS * (1.0f - fF));

                switch (iI)
                {
                    case 0:
                        fR = fV;
                        fG = t;
                        fB = p;
                        break;

                    case 1:
                        fR = q;
                        fG = fV;
                        fB = p;
                        break;

                    case 2:
                        fR = p;
                        fG = fV;
                        fB = t;
                        break;

                    case 3:
                        fR = p;
                        fG = q;
                        fB = fV;
                        break;

                    case 4:
                        fR = t;
                        fG = p;
                        fB = fV;
                        break;

                    default: // case 5 (or 6):
                        fR = fV;
                        fG = p;
                        fB = q;
                        break;
                }
            }

            // Convert from floats to 8-bit integers
            int bR = (int)(fR * FLOAT_TO_BYTE);
            int bG = (int)(fG * FLOAT_TO_BYTE);
            int bB = (int)(fB * FLOAT_TO_BYTE);

            // Clip the values to make sure it fits within the 8bits.
            if (bR > 255)
                bR = 255;
            if (bR < 0)
                bR = 0;
            if (bG > 255)
                bG = 255;
            if (bG < 0)
                bG = 0;
            if (bB > 255)
                bB = 255;
            if (bB < 0)
                bB = 0;

            // Set the RGB cvScalar with G B R, you can use this values as you want too..
            return new Bgr(bB, bG, bR); // R component
        }


        private void m_btnCapture_Click(object sender, EventArgs e)
        {
            if (_capture == null)
            {
                try
                {
                    _capture = new Capture(1);
                }
                catch (NullReferenceException excpt)
                {
                    MessageBox.Show(excpt.Message);
                }
            }

            if (_capture != null)
            {
                _captureInProgress = !_captureInProgress;

                if (!_captureInProgress)
                {  //stop the capture
                    m_btnCapture.Text = "Capture";
                    Application.Idle -= ProcessFrame;
                }
                else
                {
                    //start the capture
                    m_btnCapture.Text = "Stop";
                    Application.Idle += ProcessFrame;
                }
            }
        }


        private void ReleaseData()
        {
            if (_capture != null)
            {
                _capture.Dispose();
                _capture = null;
            }
        }

        private void captureImageBox_MouseClick(object sender, MouseEventArgs e)
        {
            //e.X;
            m_ClickPoint = new Point(e.X, e.Y);
        }
    }
}


这篇关于使用Emgu识别图像中的黄点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 10:02