药片检测

#include "mainwindow.h"
#include <QApplication>
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
    QApplication a(argc, argv);

    Mat src, src_binary,dst,src_distance;
    src = imread("E:/088.png");
    imshow("213", src);
     Mat kernel = getStructuringElement(MORPH_RECT, Size(16, 16), Point(-1, -1));//于创建结构元素的函数。它接受三个参数:矩形结构。表示一个16x16的矩形结构元素。指定为(-1, -1),表示将中心位置设置为结构元素的中心。
    morphologyEx(src, dst, MORPH_OPEN, kernel);//对图像src进行了开运算操作,并将结果存储在图像dst中。
    imshow("xingtraixue",dst);
    cvtColor(dst, dst, COLOR_BGR2GRAY);
    threshold(dst, src_binary, 100, 255, THRESH_OTSU);
    imshow("erzhihua", src_binary);



    vector<vector<Point>> contours;//名为contours的二维向量,用于存储轮廓的点集。
    findContours(src_binary, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point(0, 0));//在二值图像中查找轮廓。
    RNG rng(12345);//创建了一个名为rng的随机数生成器对象,使用了种子值12345。
    double area;
    Point2i PL;
    for (size_t i = 0; i < contours.size(); i++)
    {
        area = contourArea(contours[i]);
        if (area < 500)continue;
        PL = contours[i].front();
        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        drawContours(src, contours, i, color, 2, 8);
        putText(src, to_string(i), PL, FONT_HERSHEY_COMPLEX, 1, color, 2);
    }
    imshow("jishujieguo", src);
    waitKey(0);
    return 0;

}

posted @ 2023-10-16 07:14  blackstrom  阅读(18)  评论(0)    收藏  举报