简单的形态学处理流程

简单的形态学处理流程–标出图中硬币的位置

通过一个简单的例子——找出图像中的硬币轮廓,来简单的介绍一下形态学处理的流程。

下面是例子用到的图片:

示例图片

流程

  • 灰度化—cvtColor(sourceImage, grayImage, CV_BGR2GRAY);

  • 阈值化—threshold(grayImage, thresholdImage, 100, 255, 0);

  • 形态学处理 — morphologyEx()

  • 找到并标出轮廓— findContours() 和 drawContours()

代码

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
void main(){
    //【1】 
    Mat sourceImage = imread("coin.jpg");
    if (sourceImage.empty()){ 
        cout << "can't grab a picture " << endl;  
        return; 
    }
    imshow("source image", sourceImage);
    //【2】 转换为灰度图
    Mat grayImage;
    cvtColor(sourceImage, grayImage, CV_BGR2GRAY);
    imshow("gray image", grayImage);
    //【3】 阈值化
    Mat thresholdImage;
    threshold(grayImage, thresholdImage, 100, 255, 0);
    imshow("threshold image", thresholdImage);
    //【4】 通过开闭运算,进行形态学处理
    Mat element1 = getStructuringElement(MORPH_ELLIPSE,Size(15, 15));       //将硬币的内部凸显出来
    morphologyEx(thresholdImage, thresholdImage, MORPH_CLOSE, element1);
    Mat element2 = getStructuringElement(MORPH_RECT ,Size(5, 5));               //滤掉周围的小点
    morphologyEx(thresholdImage, thresholdImage, MORPH_OPEN, element2);
    imshow("filtered image", thresholdImage);
    //【5】 找到轮廓并标出
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    findContours(thresholdImage, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0,0));
    RNG g_rng(999999);
    for (int i = 0; i< contours.size() ; i++)
    {
        Scalar color = Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255));//任意值
        drawContours(sourceImage, contours, i, color, 2, 8, hierarchy, 0, Point());
    }
    imshow("finished", sourceImage);
    waitKey(0);
    return;
}

结果截图:
阈值化后
阈值化
形态学处理后
形态学处理后
最后结果
最后结果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值