opencv 通过连通域得到外接矩形及外接圆

本程序通过对图像二值化、连通域拆解,然后提取目标连通域的坐标及外接矩形参数,最终通过中心坐标为圆心,外接矩形的二分之一为半径进行画圆。

#include <stdio.h>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
	Mat img = imread("E:\\opencv\\opencv_roll_nihe\\1.png");
    if (img.empty())
    {
        cout << "请输入图像文件名称是否正确" << endl;
        return -1;
    }
    Mat gray;
    cvtColor(img, gray, CV_BGR2GRAY);
    Mat rice, riceBW;
   threshold(gray, riceBW, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
   		//生成随机颜色,用于区分不同连通域
       	RNG rng(10086);
   		Mat out, stats, centroids;
   		//统计图像中连通域的个数
        int number = connectedComponentsWithStats(riceBW, out, stats, centroids, 8, CV_16U);
   		vector<Vec3b> colors;
   		for (int i = 0; i < number; i++)
       		{
       		//使用均匀分布的随机数确定颜色
           	Vec3b vec3 = Vec3b(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));
       		colors.push_back(vec3);
       		}

   		//以不同颜色标记出不同的连通域
   		Mat result = Mat::zeros(rice.size(), img.type());
        //imshow("0", result);
   		int w = result.cols;
   		int h = result.rows;

        for (int i = 0; i < 1; i++)
        {
            // 中心位置
            int center_x = centroids.at<double>(i, 0);
            int center_y = centroids.at<double>(i, 1);
            //矩形边框
            int x = stats.at<int>(i, CC_STAT_LEFT);
            int y = stats.at<int>(i, CC_STAT_TOP);
            int w = stats.at<int>(i, CC_STAT_WIDTH);
            int h = stats.at<int>(i, CC_STAT_HEIGHT);
            int area = stats.at<int>(i, CC_STAT_AREA);

            // 中心位置绘制
            circle(img, Point(center_x, center_y), 2, Scalar(0, 255, 0), 2, 8, 0);
            //外接圆绘制
            circle(img, Point(center_x, center_y), w/2, Scalar(0, 255, 0), 2, 8, 0);
            // 外接矩形
            Rect rect(x, y, w, h);
            rectangle(img, rect, colors[i], 1, 8, 0);
            putText(img, format("%d", i), Point(center_x, center_y),
                FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255), 1);
            cout << "number: " << i << ",area: " << area << ",center_x: " << center_x << ",center_y: "<< center_y <<endl;
        }
       		//显示结果
          	imshow("标记后的图像", img);
    waitKey(0);
}

原始图像:
在这里插入图片描述
二值化后图像:
在这里插入图片描述
对连通域中背景(黑色)进行中心点、方框坐标提取并根据以上数据进行画圆:
在这里插入图片描述
进一步的,对整幅图像提取ROI,然后进行提取目标中心。整幅图像如下:
在这里插入图片描述


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

using namespace std;
using namespace cv;

int main()
{

	Mat img = imread("E:\\opencv\\opencv_roll_nihe\\13.png");

    if (img.empty())
    {
        cout << "请输入图像文件名称是否正确" << endl;
        return -1;
    }

    //创建ROI
    int roi_x =205 ;
    int roi_y =85 ;
    int roi_w = 120;
    int roi_h = 120;

    Rect roi(roi_x, roi_y, roi_w, roi_h);
 
    Mat roi_img = img(roi);
    imshow("ROI",roi_img);
    Mat gray;
    cvtColor(roi_img, gray, CV_BGR2GRAY);

    Mat rice, riceBW;
   threshold(gray, riceBW, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
   		//生成随机颜色,用于区分不同连通域
       	RNG rng(10086);
   		Mat out, stats, centroids;
   		//统计图像中连通域的个数
        int number = connectedComponentsWithStats(riceBW, out, stats, centroids, 8, CV_16U);
   		vector<Vec3b> colors;
   		for (int i = 0; i < number; i++)
       		{
       		//使用均匀分布的随机数确定颜色
           	Vec3b vec3 = Vec3b(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));
       		colors.push_back(vec3);
       		}

   		//以不同颜色标记出不同的连通域
   		Mat result = Mat::zeros(rice.size(), roi_img.type());
   		int w = result.cols;
   		int h = result.rows;

        for (int i = 0; i < 1; i++)
        {
            // 中心位置,
            int center_x = centroids.at<double>(i, 0)+ roi_x;
            int center_y = centroids.at<double>(i, 1)+ roi_y;
            //矩形边框
            int x = stats.at<int>(i, CC_STAT_LEFT)+ roi_x;
            int y = stats.at<int>(i, CC_STAT_TOP) + roi_y;
            int w = stats.at<int>(i, CC_STAT_WIDTH);
            int h = stats.at<int>(i, CC_STAT_HEIGHT);
            int area = stats.at<int>(i, CC_STAT_AREA);

            // 中心位置绘制
            circle(img, Point(center_x, center_y), 2, Scalar(0, 255, 0), 2, 8, 0);
            //外接圆绘制
            circle(img, Point(center_x, center_y), w/2, Scalar(0, 255, 0), 2, 8, 0);
            // 外接矩形
            Rect rect(x, y, w, h);
            rectangle(img, rect, colors[i], 1, 8, 0);
           // putText(img, format("%d", i), Point(center_x, center_y),
            putText(img, format("%s", "center"), Point(center_x, center_y),
                     FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255), 1);
            cout << "number: " << i << ",area: " << area << ",center_x: " << center_x << ",center_y: "<< center_y <<endl;
        }
       		//显示结果
          	imshow("标记后的图像", img);

    waitKey(0);
}

目标识别效果如下:
在这里插入图片描述
当然,该程序需要提前获取ROI的坐标,否者识别效果较差,该程序是一个探索结果,未必符合实际情况,再此提前说明。

参考链接:https://blog.csdn.net/qq_42722197/article/details/103826242

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值