I want to surround the outline with circles and squares.

Asked 2 years ago, Updated 2 years ago, 55 views

opencv C++
I can make an outline, but I don't know how to surround it with circles or squares.

#include "stdafx.h"
    # include <vector>
    # include <strstream>

    void run()
    {
        cv::Match;

        // Main loop


        while(1){
       // Grayscale input
        cv::Matsrc=cv::imread("/*any binary image*/", cv::IMREAD_GRAYSCALE);

       // morphology treatment
                cv::Mapping;
                cv::Matkernel(3,3,CV_8U,cv::Scalar(1));
                cv::morphologyEx(thresh, opening, cv::MORPH_OPEN, kernel, cv::Point(-1,-1), 2);
                // cv::imshow("Morphology Processing", opening);

                // background region extraction
                cv::Mature_bg;
                cv::dilate(opening, secure_bg, kernel, cv::Point(-1,-1), 3);

                // foreground region extraction
                cv::Mat dist_transform;
                cv::distanceTransform(opening, dist_transform, CV_DIST_L2,5); // Thicker the distance from the contour
                cv::Mature_fg;
                double minVal, maxVal;
                cv::Point minLoc, maxLoc;
                cv::minMaxLoc(dist_transform, & minVal, & maxVal, & minLoc, & maxLoc);
                cv::threshold(dist_transform, secure_fg, 0.2*maxVal, 255, 0);
                dist_transform = dist_transform/maxVal;

                // unknown region extraction
                cv::Matunknown, sure_fg_uc1;
                sure_fg.convertTo(sure_fg_uc1, CV_8UC1);
                cv::subtract(sure_bg, secure_fg_uc1, unknown);

                // foreground labeling
                using namespace std;

                int compCount = 0;
                vector<vector<cv::Point>>contours;
                vector<cv::Vec4i>hierarchy;
                sure_fg.convertTo(sure_fg, CV_32SC1, 1.0);
                cv::findContours(sure_fg,contours,hierarchy,cv::RETR_CCOMP,cv::CHAIN_APPROX_SIMPLE);
                if(contours.empty()) return;
                cv::Mat markers=cv::Mat::zero(sure_fg.rows, secure_fg.cols, CV_32SC1);
                int idx = 0;
                for (;idx>=0;idx=hierarchy [idx][0],compCount++)
                    cv::drawContours(markers,contours,idx,cv::Scalar::all(compCount+1), -1,8, hierarchy,INT_MAX);
                markers = markers+1;

                // No unknown areas so far
                for(inti=0;i<markers.rows;i++){
                    for(int j=0;j<markers.cols;j++){
                        unsigned char&v=unknown.at<unsigned char>(i,j);
                        if(v==255){
                            markers.at<int>(i, j) = 0;
                        }

                    }

                }
                whatershed(hsv,markers);
                cv::Mat wshed(markers.size(),CV_8UC3);
                vector<cv::Vec3b>colorTab;
                for (inti=0;i<compCount;i++)
                {
                    intb=cv::theRNG().uniform(0,255);
                    intg = cv::theRNG().uniform(0,255);
                    intr=cv::theRNG().uniform(0,255);
                    colorTab.push_back(cv::Vec3b(uchar)b,(uchar)g,(uchar)r));
                }

                // paint the watershed image
                for(inti=0;i<markers.rows;i++){
                    for (int j=0; j<markers.cols;j++)
                    {
                        int index=markers.at<int>(i,j);
                        if(index==-1)
                            wshed.at<cv::Vec3b>(i,j)=cv::Vec3b(255,255,255);
                        else if (index<=0||index>compCount)
                            wshed.at<cv::Vec3b>(i,j)=cv::Vec3b(0,0,0);
                        else
                            wshed.at<cv::Vec3b>(i,j)=colorTab[index-1];
                    }
                }
                cv::MatimgG;
                cvtColor(gray, imgG, cv::COLOR_GRAY2BGR);
                wshed = wshed*0.5 + imgG*0.5;
                              }
        }

c++ opencv

2022-09-30 18:12

1 Answers

Use the boundingRect function to calculate the tangent rectangle and the minEnclosureCircle function to calculate the tangent circle.Here we go.

Please also refer to the OpenCV tutorial Creating Boundary boxes and circuits for content.


2022-09-30 18:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.