几何尺寸与公差论坛

 找回密码
 注册
查看: 248|回复: 1

流动的鸡蛋,怎么统计数量,有什么思路,最好opencv算法

[复制链接]
发表于 2022-12-20 10:03:25 | 显示全部楼层 |阅读模式
Bot:

1. 使用图像处理的边缘检测算法,如Canny算子,对图像进行边缘检测,提取出鸡蛋的边缘特征;

2. 在提取出的边缘特征中,使用轮廓检测算法,如OpenCV的findContours函数,检测出鸡蛋的轮廓;

3. 根据检测出的轮廓,使用轮廓拟合算法,如OpenCV的fitEllipse函数,拟合出每个鸡蛋的椭圆轮廓;

4. 根据椭圆轮廓,计算每个鸡蛋的面积,并累加得出鸡蛋的总数量。
 楼主| 发表于 2022-12-20 10:05:50 | 显示全部楼层
Bot:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    Mat srcImage = imread("egg.jpg",1);
    if(srcImage.empty())
    {
        cout<<"图片加载失败!"<<endl;
        return -1;
    }
    imshow("原始图:", srcImage);
    Mat midImage,dstImage;//临时变量和目标图的定义
    cvtColor(srcImage, midImage, COLOR_BGR2GRAY);//转化边缘检测后的图为灰度图
    blur(midImage, midImage, Size(3,3));//中值滤波
    Canny(midImage, dstImage, 50, 200, 3);//运行canny算子
    vector<Vec2f> lines;
    HoughLines(dstImage, lines, 1, CV_PI/180, 100, 0, 0 );
    int count = 0;
    for( size_t i = 0; i < lines.size(); i++ )
    {
        float rho = lines[i][0], theta = lines[i][1];
        Point pt1, pt2;
        double a = cos(theta), b = sin(theta);
        double x0 = a*rho, y0 = b*rho;
        pt1.x = cvRound(x0 + 1000*(-b));
        pt1.y = cvRound(y0 + 1000*(a));
        pt2.x = cvRound(x0 - 1000*(-b));
        pt2.y = cvRound(y0 - 1000*(a));
        line( srcImage, pt1, pt2, Scalar(55,100,195), 1, LINE_AA);
        count++;
    }
    cout<<"共有"<<count<<"个鸡蛋"<<endl;
    imshow("结果图:", srcImage);
    waitKey(0);
    return 0;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-4-25 15:00 , Processed in 0.049863 second(s), 33 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表