OpenCVSharp入门教程 特征提取①——MSER区域特征提取Maximally Stable Extremal Regions

朴灿烈づ我的快乐病毒、 2023-01-23 11:50 78阅读 0赞

文章目录

  • 一、前文
  • 二、特征提取流程
  • 三、界面布局
  • 四、功能实现
    • 4.1 打开图片
    • 4.2 特征提取—源码
    • 4.3 特征提取—参数讲解
    • 4.4 特征提取—Detect和DetectRegions
  • 五、运行效果图
  • 六、发现并解决问题
  • 七、其他问题

一、前文

MSER = Maximally Stable Extremal Regions
最大极值稳定区
业界认为是性能最好的仿射不变区域,MSER是当使用不同的灰度阈值对图像进行二值化时得到的最稳定的区域,特点:
1.对于图像灰度的仿射变化具有不变性
2.稳定性,区域的支持集相对灰度变化稳定
3.可以检测不同精细程度的区域

二、特征提取流程

  1. 使用一系列灰度阈值对图像进行二值化处理
  2. 对于每个阈值得到的二值图像,得到相应的黑色区域与白色区域
  3. 在比较宽的灰度阈值范围内保持形状稳定的区域就是MSERs
  4. 评判标准: dA/dtA: 二值图像区域面积,t: 灰度

三、界面布局

  • 一个Label
  • 两个Button
  • 三个Picture

在这里插入图片描述

四、功能实现

4.1 打开图片

  1. private void openFileBtn_Click(object sender, EventArgs e)
  2. {
  3. OpenFileDialog openfiledialog = new OpenFileDialog();
  4. openfiledialog.Filter = "PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
  5. openfiledialog.RestoreDirectory = true;
  6. if (openfiledialog.ShowDialog() == DialogResult.OK)
  7. {
  8. Console.WriteLine(openfiledialog.FileName);
  9. fileName = openfiledialog.FileName;
  10. //Mat src = new Mat("foo.png", LoadMode.Color);
  11. Mat src = new Mat(fileName);
  12. //Mat src = new Mat(fileName, ImreadModes.Color);
  13. var frameBitmap = BitmapConverter.ToBitmap(src);
  14. pictureBox1.Image?.Dispose();
  15. pictureBox1.Image = frameBitmap;
  16. }
  17. }

4.2 特征提取—源码

  1. private void mserBtn_Click(object sender, EventArgs e)
  2. {
  3. if (String.IsNullOrEmpty(fileName))
  4. {
  5. MessageBox.Show("请先打开一个图片");
  6. return;
  7. }
  8. Mat src = new Mat(fileName);
  9. Mat gray = new Mat();
  10. Mat dst = src.Clone();
  11. Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);
  12. int delta = 5;
  13. int minArea = 60;
  14. int maxArea = 14400;
  15. //int maxArea = 100000;
  16. double maxVariation = 0.25;
  17. MSER mser = MSER.Create(delta, minArea, maxArea, maxVariation);
  18. DateTime currentTime = DateTime.Now;
  19. KeyPoint[] contours = mser.Detect(gray, null);
  20. resultLabel.Text = "Detect耗时:" + (System.DateTime.Now - currentTime).TotalMilliseconds + "ms";
  21. Console.WriteLine("contours len="+ contours.Length);
  22. currentTime = DateTime.Now;
  23. Point[][] outPoint;
  24. Rect[] bboxes;
  25. InputArray input = InputArray.Create(gray);
  26. mser.DetectRegions(input, out outPoint, out bboxes);
  27. resultLabel.Text += ", DetectRegions耗时:" + (System.DateTime.Now - currentTime).TotalMilliseconds + "ms";
  28. Console.WriteLine("bboxes len=" + bboxes.Length);
  29. gray = src.Clone();
  30. foreach (KeyPoint pts in contours)
  31. {
  32. gray.Circle((int)pts.Pt.X, (int)pts.Pt.Y, (int)pts.Size, Scalar.Black);
  33. gray.Circle((int)pts.Pt.X, (int)pts.Pt.Y, (int)1, Scalar.Black, 2);
  34. Console.WriteLine("("+pts.Pt.X+" , "+pts.Pt.Y+") Size="+pts.Size+", Angle="+pts.Angle+", Response="+pts.Response + ", ClassId=" + pts.ClassId);
  35. }
  36. foreach (Point[] pts in outPoint)
  37. {
  38. foreach (Point p in pts)
  39. {
  40. dst.Circle(p, 1, Scalar.Black);
  41. }
  42. }
  43. foreach (Rect b in bboxes)
  44. {
  45. dst.Rectangle(b, Scalar.Black, 2);
  46. }
  47. srcPictureBox.Image = BitmapConverter.ToBitmap(src);
  48. grayPictureBox.Image = BitmapConverter.ToBitmap(gray);
  49. dstPictureBox.Image = BitmapConverter.ToBitmap(dst);
  50. }

4.3 特征提取—参数讲解

MSER在detect的时候不需要传特别的参数
只需要在Create的时候传递参数

  1. //
  2. // 摘要:
  3. // Creates MSER parameters
  4. //
  5. // 参数:
  6. // delta:
  7. // delta, in the code, it compares (size_{i}-size_{i-delta})/size_{i-delta}
  8. //
  9. // minArea:
  10. // prune the area which smaller than min_area
  11. //
  12. // maxArea:
  13. // prune the area which bigger than max_area
  14. //
  15. // maxVariation:
  16. // prune the area have simliar size to its children
  17. //
  18. // minDiversity:
  19. // trace back to cut off mser with diversity < min_diversity
  20. //
  21. // maxEvolution:
  22. // for color image, the evolution steps
  23. //
  24. // areaThreshold:
  25. // the area threshold to cause re-initialize
  26. //
  27. // minMargin:
  28. // ignore too small margin
  29. //
  30. // edgeBlurSize:
  31. // the aperture size for edge blur
  32. public static MSER Create(int delta = 5, int minArea = 60, int maxArea = 14400, double maxVariation = 0.25, double minDiversity = 0.2, int maxEvolution = 200, double areaThreshold = 1.01, double minMargin = 0.003, int edgeBlurSize = 5);
  • delta,区域的灰度差
  • minArea,检测区域面积(像素数)的下限
  • maxArea,检测区域面积(像素数)的上限
  • maxVariation,区域与子区域间的偏差的上限

剩下的参数是MSCR使用,这个CColorC
若输入的是灰度图像,则使用MSER
若输入的是彩色图像,则使用MSCR

  • minDiversity,For color image, trace back to cut off mser with diversity less than min_diversity
  • maxEvolution,For color image, the evolution steps
  • areaThreshold, For color image, the area threshold to cause re-initialize
  • minMargin,For color image, ignore too small margin
  • edgeBlurSize,For color image, the aperture size for edge blur

4.4 特征提取—Detect和DetectRegions

OpenCV库中存在两个检测方法,DetectDetectRegions

Detect返回的是KeyPoint数组,包含了:

  • 中心点Point2f
  • 特征大小Size
  • 特征角度Angle
  • 特征强度Response
  • OctaveClassId

DetectRegions返回的是二维数组Point和一位数组Rect

  • 二位数组Point,有N个区域,每个区域的所有点Point
  • 一位数组Rect,有N个区域,每个区域的Rect

五、运行效果图

  • 从左到右
  • 第一张是原图
  • 第二张是Detect结果图

    • 将检测发现的结果的中间描黑,按照结果的size画个圆
  • 第三张是DetectRegions结果图

    • 将检测发现的区域全部描黑
    • 将检测发现的区域Rect框起来

在这里插入图片描述

六、发现并解决问题

这时候会发现一个问题,为啥右上角的绿色长方形没有被识别出来

思考了一下,大胆猜测是因为面积太大了。

因为,也想不到别的因素了,小正方形的绿色都识别出来了,为啥更大的长方形绿色没有被识别出来。颜色因素排除,那只有面积了。

更改了下代码

  1. //int maxArea = 14400;
  2. int maxArea = 100000;

运行:
在这里插入图片描述

堪称完美

七、其他问题

当然还是存在其他问题没解决

  • 最后两张,是一样的图,but一个是jpg,一个是png,但是提取的效果就完全不一样
  • 猜测可能是数据转换过程有不一样的地方
  • 仅仅是猜测后续再花时间验证,如有大佬看到并晓得原因,不吝赐教
    在这里插入图片描述
    在这里插入图片描述

觉得好,就一键三连呗(点赞+收藏+关注)

发表评论

表情:
评论列表 (有 0 条评论,78人围观)

还没有评论,来说两句吧...

相关阅读