• 29 OpenCV 图像距


    距的概念

    距的概念
    在这里插入图片描述在这里插入图片描述

    API函数

    moments(
    InputArray  array,//输入数据
    bool   binaryImage=false // 是否为二值图像
    )
    
    contourArea(
    InputArray  contour,//输入轮廓数据
    bool   oriented// 默认false、返回绝对值)
    
    arcLength(
    InputArray  curve,//输入曲线数据
    bool   closed// 是否是封闭曲线)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    示例

    #include 
    #include 
    #include 
    
    using namespace std;
    using namespace cv;
    
    Mat src, gray_src;
    int threshold_value = 80;
    int threshold_max = 255;
    const char* output_win = "image moents demo";
    RNG rng(12345);
    void Demo_Moments(int, void*);
    int main(int argc, char** argv) {
    	src = imread("D:/vcprojects/images/circle.png");
    	if (!src.data) {
    		printf("could not load image...\n");
    		return -1;
    	}
    	cvtColor(src, gray_src, CV_BGR2GRAY);
    	GaussianBlur(gray_src, gray_src, Size(3, 3), 0, 0);
    
    	char input_win[] = "input image";
    	namedWindow(input_win, CV_WINDOW_AUTOSIZE);
    	namedWindow(output_win, CV_WINDOW_AUTOSIZE);
    	imshow(input_win, src);
    
    	createTrackbar("Threshold Value : ", output_win, &threshold_value, threshold_max, Demo_Moments);
    	Demo_Moments(0, 0);
    
    	waitKey(0);
    	return 0;
    }
    
    void Demo_Moments(int, void*) {
        // 定义变量
        Mat canny_output; // Canny边缘检测的输出
        vector<vector<Point>> contours; // 图像中找到的轮廓
        vector<Vec4i> hierachy; // 轮廓的层级结构
    
        // 应用Canny边缘检测
        Canny(gray_src, canny_output, threshold_value, threshold_value * 2, 3, false);
        // 在二值图像中找到轮廓
        findContours(canny_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
    
        // 计算轮廓的矩和质心
        vector<Moments> contours_moments(contours.size()); // 轮廓的矩
        vector<Point2f> ccs(contours.size()); // 轮廓的质心
        for (size_t i = 0; i < contours.size(); i++) {
       		 // 计算轮廓的矩
            contours_moments[i] = moments(contours[i]); 
            // 计算质心
            ccs[i] = Point(static_cast<float>(contours_moments[i].m10 / contours_moments[i].m00), static_cast<float>(contours_moments[i].m01 / contours_moments[i].m00)); 
        }
    
        // 在原始图像上绘制轮廓和质心
        Mat drawImg; // 用于绘制轮廓和质心的图像
        src.copyTo(drawImg); // 复制原始图像以进行绘制
        for (size_t i = 0; i < contours.size(); i++) {
            // 跳过小轮廓
            if (contours[i].size() < 100) {
                continue;
            }
            // 为每个轮廓生成随机颜色
            Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
            // 打印质心坐标和轮廓属性
            printf("center point x : %.2f y : %.2f\n", ccs[i].x, ccs[i].y);
            printf("contours %d area : %.2f   arc length : %.2f\n", i, contourArea(contours[i]), arcLength(contours[i], true));
            // 在图像上绘制轮廓和质心
            drawContours(drawImg, contours, i, color, 2, 8, hierachy, 0, Point(0, 0));
            circle(drawImg, ccs[i], 2, color, 2, 8);
        }
    
        // 显示绘制了轮廓和质心的图像
        imshow(output_win, drawImg);
        return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    在这里插入图片描述

  • 相关阅读:
    go 1.22 增强 http.ServerMux 路由能力
    【区块链】讲解
    gatk4中的interval是什么??
    JVM 访问对象的两种方式
    HFI-脉振法
    C#实现检测打印机状态(包括打印机是否缺纸、打印队列任务数)
    正则表达式
    基于hydra库实现yaml配置文件的读取(支持命令行参数)
    【iOS】简单的网络请求
    微信开发者工具C盘占用大的问题
  • 原文地址:https://blog.csdn.net/weixin_45672157/article/details/138169713