OpenCV shape detection -- C++版本

部署运行你感兴趣的模型镜像

本文主要参考OpenCV shape detection ,用C++版的OpenCV API进行重写。

源码

ShapeDetector.h

#pragma once
#ifndef SHAPEDETECTOR_H_
#define SHAPEDETECTOR_H_

#include <iostream>
#include <vector>
#include <string>

using namespace std;

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/features2d/features2d.hpp"

using namespace cv;

class ShapeDetector
{
public:
    ShapeDetector();
    ~ShapeDetector();
    void detect(const Mat &curve);
    string get_shape_type();

private:
    string m_shape;
};

#endif

ShapeDetector.cpp

#include "ShapeDetector.h"

ShapeDetector::ShapeDetector()
{
}

ShapeDetector::~ShapeDetector()
{
}

void ShapeDetector::detect(const Mat &curve)
{
    string shape = "unidentified";
    double peri = arcLength(curve, true);
    Mat approx;
    approxPolyDP(curve, approx, 0.04 * peri, true); // 0.01~0.05
    const int num_of_vertices = approx.rows;

    // if the shape is a triangle, it will have 3 vertices
    if (num_of_vertices == 3)
    {
        shape = "triangle";
    }
    else if (num_of_vertices == 4)
    {// if the shape has 4 vertices, it is either a square or a rectangle
        // Compute the bounding box of the contour and
        // use the bounding box to compute the aspect ratio
        Rect rec = boundingRect(approx);
        double ar = 1.0 * rec.width / rec.height;

        // A square will have an aspect ratio that is approximately
        // equal to one, otherwise, the shape is a rectangle
        if (ar >= 0.95 && ar <= 1.05)
        {
            shape = "square";
        }
        else
        {
            shape = "rectangle";
        }
    }
    else if (num_of_vertices == 5)
    {// if the shape is a pentagon, it will have 5 vertices
        shape = "pentagon";
    }
    else
    {// otherwise, we assume the shape is a circle
        shape = "circle";
    }
    m_shape = shape;
}

string ShapeDetector::get_shape_type()
{
    return m_shape;
}

main.cpp

#include "ShapeDetector.h"

int main(int argc, char* argv[])
{
    const string imgpath = "D:/TMP/test.png";
    Mat image = imread(imgpath);
    if (image.empty())
    {
        return -1;
    }
    Mat gray;
    if (image.channels()==3)
    {
        cvtColor(image, gray, COLOR_BGR2GRAY);
    }
    else
    {
        gray = image.clone();
    }

    Mat blurred, thresh;
    GaussianBlur(gray, blurred, Size(5, 5), 0.0);
    threshold(blurred, thresh, 60, 255, THRESH_BINARY);

    vector< vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours(thresh, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

    ShapeDetector sd;
    vector<Point> c;
    for (size_t i = 0; i < contours.size(); i++)
    {
        c = contours[i];
        Rect crect = boundingRect(c);
        // compute the center of the contour, then detect the name of the
        // shape using only the contour
        Moments M = moments(c);
        int cX = static_cast<int>(M.m10 / M.m00);
        int cY = static_cast<int>(M.m01 / M.m00);
        sd.detect(Mat(c));
        string shape = sd.get_shape_type();
        drawContours(image, contours, i, Scalar(0, 255, 0), 2);
        Point pt(cX, cY);
        putText(image, shape, pt, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 255), 2);
        imshow("Image", image);
        waitKey(0);
    }
    return 0;
}

测试

测试图像

shape_test

最终输出图像

shape_test_res

参考

OpenCV shape detection

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

apt-cache search opencv cl-opencv-apps - opencv_apps Robot OS package - LISP bindings gstreamer1.0-opencv - GStreamer OpenCV plugins gyoto-bin - General relativistic ray-tracing command-line interface libcv-bridge-dev - cv_bridge Robot OS package - development libcv-bridge2d - cv_bridge Robot OS package libgstreamer-opencv1.0-0 - GStreamer OpenCV libraries libimage-geometry-dev - image_geometry Robot OS package - development libimage-geometry1d - image_geometry Robot OS package - runtime libmrgingham-dev - Chessboard finder for visual calibration routines libmrgingham2 - Chessboard finder for visual calibration routines libmrpt-img-dev - Mobile Robot Programming Toolkit - img development package libmrpt-img2.4 - Mobile Robot Programming Toolkit - img library libopencv-apps-dev - Opencv_apps Robot OS package - development files libopencv-apps2d - opencv_apps Robot OS package - runtime files libopencv-calib3d-dev - development files for libopencv-calib3d4.5d libopencv-calib3d4.5d - computer vision Camera Calibration library libopencv-contrib-dev - development files for libopencv-contrib4.5d libopencv-contrib4.5d - computer vision contrlib library libopencv-core-dev - development files for libopencv-core4.5d libopencv-core4.5d - computer vision core library libopencv-dev - development files for opencv libopencv-dnn-dev - development files for libopencv-dnn4.5d libopencv-dnn4.5d - computer vision Deep neural network module libopencv-features2d-dev - development files for libopencv-features2d4.5d libopencv-features2d4.5d - computer vision Feature Detection and Descriptor Extraction library libopencv-flann-dev - development files for libopencv-flann4.5d libopencv-flann4.5d - computer vision Clustering and Search in Multi-Dimensional spaces library libopencv-highgui-dev - development files for libopencv-highgui4.5d libopencv-highgui4.5d - computer vision High-level GUI and Media I/O library libopencv-imgcodecs-dev - development files for libopencv-imgcodecs4.5d libopencv-imgcodecs4.5d - computer vision Image Codecs library libopencv-imgproc-dev - development files for libopencv-imgproc4.5d libopencv-imgproc4.5d - computer vision Image Processing library libopencv-ml-dev - development files for libopencv-ml4.5d libopencv-ml4.5d - computer vision Machine Learning library libopencv-objdetect-dev - development files for libopencv-objdetect4.5d libopencv-objdetect4.5d - computer vision Object Detection library libopencv-photo-dev - development files for libopencv-photo4.5d libopencv-photo4.5d - computer vision computational photography library libopencv-shape-dev - development files for libopencv-shape4.5d libopencv-shape4.5d - computer vision shape descriptors and matchers library libopencv-stitching-dev - development files for libopencv-stitching4.5d libopencv-stitching4.5d - computer vision image stitching library libopencv-superres-dev - development files for libopencv-superres4.5d libopencv-superres4.5d - computer vision Super Resolution library libopencv-video-dev - development files for libopencv-video4.5d libopencv-video4.5d - computer vision Video analysis library libopencv-videoio-dev - development files for libopencv-videoio4.5d libopencv-videoio4.5d - computer vision Video I/O library libopencv-videostab-dev - development files for libopencv-videostab4.5d libopencv-videostab4.5d - computer vision video stabilization library libopencv-viz-dev - development files for libopencv-viz4.5d libopencv-viz4.5d - computer vision 3D data visualization library libopencv4.5-java - Java bindings for the computer vision library libopencv4.5d-jni - Java jni library for the computer vision library mrgingham - Chessboard finder for visual calibration routines node-opencv - OpenCV Bindings for node.js octave-video - video file reader/writer for Octave opencv-data - development data for opencv opencv-doc - OpenCV documentation and examples php-facedetect - Detect faces with PHP python-willow-doc - Python image library (documentation) python3-cv-bridge - cv_bridge ROS package - Python 3 bindings python3-gyoto - General relativistic geodesic integration for the Python 3 language python3-image-geometry - image_geometry Robot OS package - Python 3 bindings python3-mrgingham - Chessboard finder for visual calibration routines python3-opencv - Python 3 bindings for the computer vision library python3-opencv-apps - opencv_apps Robot OS package - Python 3 bindings python3-pylibdmtx - Read Data Matrix barcodes (Python3 version) python3-pyzbar - Python bindings for libzbar python3-willow - Python image library combining Pillow, Wand and OpenCV (Python 3) ros-opencv-apps - opencv_apps Robot OS package - apps sightcalibrator - Camera calibration software ros-humble-aruco-opencv - ArUco marker detection using aruco module from OpenCV libraries. ros-humble-aruco-opencv-dbgsym - debug symbols for ros-humble-aruco-opencv ros-humble-aruco-opencv-msgs - Message definitions for aruco_opencv package. ros-humble-aruco-opencv-msgs-dbgsym - debug symbols for ros-humble-aruco-opencv-msgs ros-humble-cv-bridge - This contains CvBridge, which converts between ROS2 Image messages and OpenCV images. ros-humble-grid-map-cv - Conversions between grid maps and OpenCV images. ros-humble-image-geometry - `image_geometry` contains C++ and Python libraries for interpreting images geometrically. ros-humble-swri-opencv-util - A package with commonly used OpenCV functionality. ros-humble-swri-opencv-util-dbgsym - debug symbols for ros-humble-swri-opencv-util ros-humble-vision-opencv - Packages for interfacing ROS2 with OpenCV, a library of programming functions for real time computer vision. ros-iron-aruco-opencv - ArUco marker detection using aruco module from OpenCV libraries. ros-iron-aruco-opencv-dbgsym - debug symbols for ros-iron-aruco-opencv ros-iron-aruco-opencv-msgs - Message definitions for aruco_opencv package. ros-iron-aruco-opencv-msgs-dbgsym - debug symbols for ros-iron-aruco-opencv-msgs ros-iron-cv-bridge - This contains CvBridge, which converts between ROS2 Image messages and OpenCV images. ros-iron-grid-map-cv - Conversions between grid maps and OpenCV images. ros-iron-image-geometry - `image_geometry` contains C++ and Python libraries for interpreting images geometrically. ros-iron-swri-opencv-util - A package with commonly used OpenCV functionality. ros-iron-swri-opencv-util-dbgsym - debug symbols for ros-iron-swri-opencv-util ros-iron-vision-opencv - Packages for interfacing ROS2 with OpenCV, a library of programming functions for real time computer vision. ros-rolling-aruco-opencv - ArUco marker detection using aruco module from OpenCV libraries. ros-rolling-aruco-opencv-dbgsym - debug symbols for ros-rolling-aruco-opencv ros-rolling-aruco-opencv-msgs - Message definitions for aruco_opencv package. ros-rolling-aruco-opencv-msgs-dbgsym - debug symbols for ros-rolling-aruco-opencv-msgs ros-rolling-cv-bridge - This contains CvBridge, which converts between ROS2 Image messages and OpenCV images. ros-rolling-image-geometry - `image_geometry` contains C++ and Python libraries for interpreting images geometrically. ros-rolling-swri-opencv-util - swri_opencv_util ros-rolling-swri-opencv-util-dbgsym - debug symbols for ros-rolling-swri-opencv-util ros-rolling-vision-opencv - Packages for interfacing ROS2 with OpenCV, a library of programming functions for real time computer vision. robotyfw@yfw:~$
最新发布
12-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Digital2Slave

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值