※我用的環境 Visual Studio professional 2017 + EMGU.CV.3.4.3.3016 + WPF+ release build on Any CPU
安裝Emgu.CV在Visual Studio
開啟已建立的專案(我是用WPF) ->在Solution Explorer裡面的project name上按右鍵
-> 選 Manage NuGet Packages... -> 按Browse ->輸入 EMGU.CV ->安裝
程式碼片段
以下程式碼我跑過沒有問題
若是那種程式碼看都沒看直接複製貼上
(ex: 需修改成自己電腦環境的 比如圖檔路徑)
然後跑不起來的智障問題恕我無法回答
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using System;
using System.Windows;
using System.Windows.Forms;
using Point = System.Drawing.Point;
namespace VirtualTouch
{
public partial class MainWindow : Window
{
Image<Gray, byte> grayImage;
Image<Gray, byte> threshImage;
double area;
MCvScalar color = new MCvScalar(0,0, 255);
public MainWindow()
{
InitializeComponent();
// Hide MainWindow
WindowState = (WindowState)FormWindowState.Minimized;
ShowInTaskbar = false;
using (var image = new Image<Bgr, Byte>(@"C:\Users\Gary\Desktop\IR.jpg"))// IR camera image
{
// Convert BGR to Gray
grayImage = image.Convert<Gray, Byte>();
// Binary image
threshImage = grayImage.CopyBlank();
CvInvoke.Threshold(
grayImage,
threshImage,
170, //Threshold
255, //maximum
ThresholdType.Binary);
// Draw the object contour of the green(0, 255, 0) rectangle
System.Drawing.Rectangle bounding_rect;
VectorOfVectorOfPoint con = new VectorOfVectorOfPoint();
CvInvoke.FindContours(threshImage, con, null, RetrType.Ccomp, ChainApproxMethod.ChainApproxSimple);
for (int i = 0; i < con.Size; i++)// Iterate through each contour
{
//CvInvoke.DrawContours(d, con, i, new MCvScalar(255, 0, 255, 255), 1);
area = CvInvoke.ContourArea(con[i], false); // Find the area of contour
if (area > 0)
{
bounding_rect = CvInvoke.BoundingRectangle(con[i]);
CvInvoke.Rectangle(image, bounding_rect, new MCvScalar(0, 255, 0), 1);
//Bounding Box Centroid
Point center = Center(bounding_rect);
CvInvoke.Circle(image, center, 1, color, 2, LineType.Filled, 0);
}
}
CvInvoke.Imshow("", image);
}
}
public static Point Center(System.Drawing.Rectangle rect)
{
return new Point(rect.Left + rect.Width / 2,
rect.Top + rect.Height / 2);
}
}
}
結果
input
output
參考資料
關於findCoutours我覺得這人寫的很詳盡
留言列表