图片内容对象计数

这里写自定义目录标题


此篇主要为临时记录代码后续编写过程慢慢补上

图片内容对象计数

原图片

在这里插入图片描述

图形计数

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <math.h>
#include <iostream>

using namespace std;
using namespace cv;

//double threshold = 200.0;

int main()
{
    Mat grayimg, BinaryImg, morhImage;
    Mat img = imread("data/corn.png");
    imshow("原图", img);
    //二值化操作
    cvtColor(img, grayimg, COLOR_BGR2GRAY);
    threshold(grayimg, BinaryImg, 245, 255, THRESH_BINARY);
    imshow("二值化", BinaryImg);

    //形态学腐蚀
    Mat kernel = getStructuringElement(MORPH_RECT, Size(15, 15));
    dilate(BinaryImg, morhImage, kernel);
    imshow("morphology", morhImage);

    //距离变换:用于二值化图像中的每一个非零点距自己最近的零点的距离,距离变换图像上越亮的点,代表了这一点距离零点的距离越远。
    Mat dist;
    bitwise_not(morhImage, morhImage);
    distanceTransform(morhImage, dist, DIST_L2, 3);
    normalize(dist, dist, 0, 1.0, NORM_MINMAX); //范围在0~1之间
    imshow("distance", dist);

    //形态学处理
    Mat MorphImg;
    dist.convertTo(MorphImg, 8U);
    // threshold(MorphImg, MorphImg, 0.99, 255, THRESH_BINARY); //上面像素值在0~1之间
    adaptiveThreshold(MorphImg, MorphImg, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 85, 0);
    kernel = getStructuringElement(MORPH_RECT, Size(11, 11), Point(-1, -1));
    // morphologyEx(MorphImg, MorphImg, MORPH_OPEN, kernel); //开操作
    dilate(MorphImg, MorphImg, kernel, Point(-1, -1), 1);
    imshow("t-distance", MorphImg);

    //找到种子的轮廓区域
    vector<vector<Point>> contours;
    findContours(MorphImg, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
    // draw result
    Mat markers = Mat::zeros(img.size(), CV_8UC3);
    RNG rng(12345); //随机产生颜色
    for (int i = 0; i < contours.size(); i++)
    {
        drawContours(markers, contours, i, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), -1, 8, Mat());
    }
    cout << "number of corns: " << contours.size() << endl;
    imshow("result Img", markers);

    waitKey(0);
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值