我正在使用 Emgu CV 2.4.2 并希望执行以下算法:
- 获取 blob
- 设置 ROI 以加快计算速度
- 从 blob 中获取局部最小值的像素位置
- 划分blob
- 将边界矩形绘制成分割的斑点
我已经完成了步骤 1-2 并使用 BGStatModel 提取了 blob。这是我得到的结果:
我想在垂直投影中获得局部最小值的像素位置。得到它之后,我想划分blob并绘制这样的矩形:
我试图通过检查 blob 区域中的每个像素来获取局部最小值的像素位置,但这会使我的应用程序运行得非常慢。这是我的代码:
Point minPix = new Point(0,0);
//copy the foreground frame
Image<Gray, Byte> foreFrame_copy = foreFrame.Copy();
//find the contour
Contour<Point> contours = foreFrame.FindContours(
CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
RETR_TYPE.CV_RETR_EXTERNAL);
//looping every contour
while (contours != null)
{
double PPixel = contours.Area;
if (PPixel >= 1400)
{
//get the contour width
WR = contours.BoundingRectangle.Width;
//divide the contour using estimated pixel position
Num = Convert.ToInt32(WR / 40);
if (Num > 1)
{
//save the estimated pixel position for ROI in arraylist
ArrayList XList = new ArrayList();
for (int i = 1; i <= Num; i++)
{
int x = i * WR / Num;
XList.Add(x);
}
//get the estimated pixel position
foreach (int pos in Xlist)
{
//roiWidth= 10px
int roiWidth = (pos-5) + (pos+5);
//roiHeight= 20px
int roiHeight = 20;
int pixValue = 0;
//STEP 2: set the ROI to speed up computation
foreFrame_copy.ROI = new Rectangle(contours.BoundingRectangle.X, contours.BoundingRectangle.Y, roiWidth, roiHeight);
for (int i = (pos-5); i < roiWidth; i++)
{
for (int j = (pos-5); j < (pos+5); j++)
{
pixValue = foreFrame_copy.Data[i, j, 0];
//find the white pixel
if (pixValue == 255) {
//find the position of minimum pixel
if (j < j-1) {
minPix.X = i;
minPix.Y = j;
}
}
}
}
}
}
//draw the red rectangle
estimatedFrame.Draw(contours.BoundingRectangle, new Bgr(Color.Red), 1);
contours = contours.HNext;
}
else
{
contours = contours.HNext;
}
}
//show frame in framebox
blobBox.Image = foreFrame_copy;
estimatedBox.Image = estimatedFrame;
请帮助我如何使用 Emgu CV 以最快的方式执行步骤 2-5。如果有人详细说明这三个步骤和一些代码,我将不胜感激。
在此先感谢,大卫