轮廓周围绘制矩形框和圆形框

1 、API介绍

轮廓周围绘制矩形 ,基于RDP算法实现,目的是减少多边形轮廓点数


image.png
approxPolyDP(InputArray  curve, OutputArray approxCurve,  double  epsilon,  bool  closed)

cv::boundingRect(InputArray points)得到轮廓周围最小矩形左上交点坐标和右下角点坐标,绘制一个矩形

cv::minAreaRect(InputArray  points)得到一个旋转的矩形,返回旋转矩形

轮廓周围绘制圆和椭圆

v::minEnclosingCircle(InputArray points, //得到最小区域圆形
    Point2f& center, // 圆心位置
    float& radius)// 圆的半径
cv::fitEllipse(InputArray  points)得到最小椭圆

2 、流程

首先将图像变为二值图像
发现轮廓,找到图像轮廓
通过相关API在轮廓点上找到最小包含矩形和圆,旋转矩形与椭圆。
绘制它们。

3 、整体代码测试

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

using namespace std;
using namespace cv;
Mat src, gray_src, drawImg;
int threshold_v = 170;
int threshold_max = 255;
const char* output_win = "rectangle-demo";
RNG rng(12345);
void Contours_Callback(int, void*);
int main(int argc, char** argv) {
    src = imread("D:\\pic/tubao.png");
    if (!src.data) {
        printf("could not load image...\n");
        return -1;
    }
    cvtColor(src, gray_src, CV_BGR2GRAY);
    blur(gray_src, gray_src, Size(3, 3), Point(-1, -1));

    const char* source_win = "input image";
    namedWindow(source_win, CV_WINDOW_AUTOSIZE);
    namedWindow(output_win, CV_WINDOW_AUTOSIZE);
    imshow(source_win, src);

    createTrackbar("Threshold Value:", output_win, &threshold_v, threshold_max, Contours_Callback);
    Contours_Callback(0, 0);

    waitKey(0);
    return 0;
}

void Contours_Callback(int, void*) {
    Mat binary_output;
    vector<vector<Point>> contours;
    vector<Vec4i> hierachy;
    threshold(gray_src, binary_output, threshold_v, threshold_max, THRESH_BINARY);
    imshow("binary image", binary_output);
    findContours(binary_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(-1, -1));

    vector<vector<Point>> contours_ploy(contours.size());
    vector<Rect> ploy_rects(contours.size());
    vector<Point2f> ccs(contours.size());
    vector<float> radius(contours.size());

    vector<RotatedRect> minRects(contours.size());
    vector<RotatedRect> myellipse(contours.size());

    for (size_t i = 0; i < contours.size(); i++) {
        approxPolyDP(Mat(contours[i]), contours_ploy[i], 3, true);
        ploy_rects[i] = boundingRect(contours_ploy[i]);
        minEnclosingCircle(contours_ploy[i], ccs[i], radius[i]);
        if (contours_ploy[i].size() > 5) {
            myellipse[i] = fitEllipse(contours_ploy[i]);
            minRects[i] = minAreaRect(contours_ploy[i]);
        }
    }

    // draw it
    drawImg = Mat::zeros(src.size(), src.type());
    Point2f pts[4];
    for (size_t t = 0; t < contours.size(); t++) {
        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        //rectangle(drawImg, ploy_rects[t], color, 2, 8);
        //circle(drawImg, ccs[t], radius[t], color, 2, 8);
        if (contours_ploy[t].size() > 5) {
            ellipse(drawImg, myellipse[t], color, 1, 8);
            minRects[t].points(pts);
            for (int r = 0; r < 4; r++) {
                line(drawImg, pts[r], pts[(r + 1) % 4], color, 1, 8);
            }
        }
    }

    imshow(output_win, drawImg);
    return;
}
image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本章包括以下内容: 用Canny 算子检测图像轮廓; 用霍夫变换检测直线; 点集的直线拟合; 提取连续区域; 计算...
    sumpig阅读 3,831评论 1 3
  • canvas元素的基础知识 在页面上放置一个canvas元素,就相当于在页面上放置了一块画布,可以在其中进行图形的...
    oWSQo阅读 10,357评论 0 19
  • 图像处理(ImageMagick) 介绍安装/配置要求安装运行时配置资源类型预定义常数例子基本用法Imagick ...
    daos阅读 2,466评论 0 1
  • 前述:Python程序设计可以利用多种方法实现对图像和图像的呈现和处理,在这是利用Python3.x自带的tkin...
    IIronMan阅读 9,192评论 0 8
  • 傷情生前是個謎,死後仍是謎。所以不知道她家在何處的夜聖雅只得把她髒在了傍花隨柳谷:這裡有她最忠實的丫鬟彩燕,最...
    独孤空杯阅读 79评论 0 1