Emgucv_滑鼠選取感興趣區域_觀察小狗狗的眼睛區塊Histogram




Visual studio 2013

emgucv 2.4.10


建議
 因為 到  3.0 以上  語法很多會不一樣

容易造成   初學者   不開心

建議   可以  用   visual 2012   2013   去搭配   emgu 2.4.10



效果示意




可以用  emgucv 的  ImageBox --> 右鍵 --> 去 顯示濾波 或  秀 直方圖等功能

還可以查看屬性喔!!!!



放大來看 直方圖



這就是小狗狗眼睛區塊的三通道顏色直方圖!!!!!!



Form1.cs  


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 Emgu.CV;
using Emgu.CV.Util;
using Emgu.CV.Structure;

namespace WakeUpDetect
{    
    public partial class Form1 : Form
    {
        private Image<Bgr, byte> imgEntrada;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            imgEntrada = new Image<Bgr, byte>("C:\\img_res\\boy_and_dog.jpg");
            picOutputROI.Image = picInput.Image = imgEntrada;

        }
        private Point RectStartPoint;
        private Rectangle Rect = new Rectangle();
        private Rectangle RealImageRect = new Rectangle();
        private Brush selectionBrush = new SolidBrush(Color.FromArgb(128, 64, 64, 64));
        private int thickness = 3;

        private void pictureBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void pictureBox_Paint(object sender, PaintEventArgs e)
        {
            // Draw the rectangle...
            if (picInput.Image != null)
            {
                if (Rect != null && Rect.Width > 0 && Rect.Height > 0)
                {
                    //Seleciona a ROI
                    e.Graphics.SetClip(Rect, System.Drawing.Drawing2D.CombineMode.Exclude);
                    e.Graphics.FillRectangle(selectionBrush, new Rectangle(0, 0, ((PictureBox)sender).Width, ((PictureBox)sender).Height));
                    //e.Graphics.FillRectangle(selectionBrush, Rect);
                }
            }
        }

        private void picInput_MouseDown(object sender, MouseEventArgs e)
        {
            RectStartPoint = e.Location;
            Invalidate();
        }

        private void picInput_MouseMove(object sender, MouseEventArgs e)
        {
            
                #region SETS COORDINATES AT INPUT IMAGE BOX
                int X0, Y0;

                Utilities.ConvertCoordinates(picInput, out X0, out Y0, e.X, e.Y);
                labelPostionXY.Text = "Last Position: X:" + X0 + "  Y:" + Y0;

                //Coordinates at input picture box
                if (e.Button != MouseButtons.Left)
                    return;
                Point tempEndPoint = e.Location;
                Rect.Location = new Point(
                    Math.Min(RectStartPoint.X, tempEndPoint.X),
                    Math.Min(RectStartPoint.Y, tempEndPoint.Y));
                Rect.Size = new Size(
                    Math.Abs(RectStartPoint.X - tempEndPoint.X),
                    Math.Abs(RectStartPoint.Y - tempEndPoint.Y));
                #endregion

                #region SETS COORDINATES AT REAL IMAGE
                //Coordinates at real image - Create ROI
                Utilities.ConvertCoordinates(picInput, out X0, out Y0, RectStartPoint.X, RectStartPoint.Y);
                int X1, Y1;
                Utilities.ConvertCoordinates(picInput, out X1, out Y1, tempEndPoint.X, tempEndPoint.Y);
                RealImageRect.Location = new Point(
                    Math.Min(X0, X1),
                    Math.Min(Y0, Y1));
                RealImageRect.Size = new Size(
                    Math.Abs(X0 - X1),
                    Math.Abs(Y0 - Y1));

                imgEntrada = new Image<Bgr, byte>("boy_and_dog.jpg");
                imgEntrada.Draw(RealImageRect, new Bgr(Color.Red), thickness);
                picOutputROI.Image = imgEntrada;
                #endregion
            
                ((PictureBox)sender).Invalidate();
        }

        private void picInput_MouseUp(object sender, MouseEventArgs e)
        {
            //Define ROI. Valida altura e largura para evitar index range exception.
            if (RealImageRect.Width > 0 && RealImageRect.Height > 0)
            {
                imgEntrada.ROI = RealImageRect;
                picROI.Image = imgEntrada;
            }
        }


    }
}



Utilities class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WakeUpDetect
{
    public class Utilities
    {
        
        public static void ConvertCoordinates(PictureBox pic,
            out int X0, out int Y0, int x, int y)
        {
                int pic_hgt = pic.ClientSize.Height;
                int pic_wid = pic.ClientSize.Width;
                int img_hgt = pic.Image.Height;
                int img_wid = pic.Image.Width;

                X0 = x;
                Y0 = y;
                switch (pic.SizeMode)
                {
                    case PictureBoxSizeMode.AutoSize:
                    case PictureBoxSizeMode.Normal:
                        // These are okay. Leave them alone.
                        break;
                    case PictureBoxSizeMode.CenterImage:
                        X0 = x - (pic_wid - img_wid) / 2;
                        Y0 = y - (pic_hgt - img_hgt) / 2;
                        break;
                    case PictureBoxSizeMode.StretchImage:
                        X0 = (int)(img_wid * x / (float)pic_wid);
                        Y0 = (int)(img_hgt * y / (float)pic_hgt);
                        break;
                    case PictureBoxSizeMode.Zoom:
                        float pic_aspect = pic_wid / (float)pic_hgt;
                        float img_aspect = img_wid / (float)img_wid;
                        if (pic_aspect > img_aspect)
                        {
                            // The PictureBox is wider/shorter than the image.
                            Y0 = (int)(img_hgt * y / (float)pic_hgt);

                            // The image fills the height of the PictureBox.
                            // Get its width.
                            float scaled_width = img_wid * pic_hgt / img_hgt;
                            float dx = (pic_wid - scaled_width) / 2;
                            X0 = (int)((x - dx) * img_hgt / (float)pic_hgt);
                        }
                        else
                        {
                            // The PictureBox is taller/thinner than the image.
                            X0 = (int)(img_wid * x / (float)pic_wid);

                            // The image fills the height of the PictureBox.
                            // Get its height.
                            float scaled_height = img_hgt * pic_wid / img_wid;
                            float dy = (pic_hgt - scaled_height) / 2;
                            Y0 = (int)((y - dy) * img_wid / pic_wid);
                        }
                        break;
                }
            
        }
    }
}




介面布局

容器 --> TableLayoutPanel





下面兩個就是  Label  了

其中一個寫滑鼠移動  更新位置 的 顯示

使用的不是一般的   pictureBox

而是使用   Emgucv 的 ImageBox 物件


















留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header