編寫環境:
OS: Win 11
C#: VS2010
EMGU:2.4.10
構想: 利用滑鼠down->move->up在圖形上的事件, 抓取ROI區塊上的顏色, 進而去判斷整個影像中相對應的顏色物件並將其以最小矩形框出.
利用滑鼠的事件
private void imageBox1_MouseDown(object sender, MouseEventArgs e)
private void imageBox1_MouseMove(object sender, MouseEventArgs e)
private void imageBox1_MouseUp(object sender, MouseEventArgs e)
去抓取imageBox1中的ROI區塊的影像去做處理
利用到的函式
// bgr 轉 hsv
TEmgu._EmguPara.TouchRegionHsv = CommonData.ROIImageSource.Convert<Hsv, Byte>();
TEmgu._EmguPara.TouchRegionHsv.SmoothGaussian(5);
TEmgu._EmguPara.m_Lower = new Hsv(TEmgu._EmguPara.m_TargetColor.Hue - 30, TEmgu._EmguPara.m_TargetColor.Satuation - 30, TEmgu._EmguPara.m_TargetColor.Value - 30);
TEmgu._EmguPara.m_Higher = new Hsv(TEmgu._EmguPara.m_TargetColor.Hue + 30, TEmgu._EmguPara.m_TargetColor.Satuation + 30, TEmgu._EmguPara.m_TargetColor.Value + 30);
//搜尋的顏色範圍
TEmgu._EmguPara.InRageFrame = TEmgu._EmguPara.ImageHsv.InRange(TEmgu._EmguPara.m_Lower, TEmgu._EmguPara.m_Higher);
//抓取ROI顏色的最小矩形
using (MemStorage storage = new MemStorage())
{
for (Contour<Point> contours = TEmgu._EmguPara.InRageFrame.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL, storage); contours != null; contours = contours.HNext)
{
Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.010, storage);
if (currentContour.BoundingRectangle.Width < 10 || currentContour.BoundingRectangle.Height < 10) //对于太小的外接矩形,删除掉
{
continue;
}
if (currentContour.BoundingRectangle.Width > 50 && currentContour.BoundingRectangle.Height > 50)
{
TEmgu._EmguPara.ImageSource.Draw(currentContour.GetMinAreaRect(), new Bgr(0, 0, 255), 1);
MCvBox2D rect;
rect = currentContour.GetMinAreaRect();
CommonData.Angle = rect.angle;
cnti++;
}
}
}
沒有留言:
張貼留言