Qt-OpenCV学习笔记--计算周长--arcLength()
生活随笔
收集整理的這篇文章主要介紹了
Qt-OpenCV学习笔记--计算周长--arcLength()
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
概述
這個(gè)函數(shù)用來(lái)計(jì)算 輪廓的周長(zhǎng) 或者 曲線的長(zhǎng)度。
函數(shù)
double cv::arcLength (InputArray curve,bool closed )| curve | 計(jì)算對(duì)象(輪廓 或者 圖形的頂點(diǎn)) |
| closed | 標(biāo)識(shí)符(曲線是否閉合,一般為true) |
測(cè)試代碼
#include "widget.h" #include "ui_widget.h"#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp>#include <vector> #include <QDebug>using namespace cv; using namespace std;Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this);//生成畫板Mat mat = Mat::zeros(500,500,CV_8UC1);//構(gòu)造點(diǎn)Point2f a(100,100);Point2f b(400,100);Point2f c(400,400);Point2f d(100,400);//生成輪廓vector<Point> contour_rec;contour_rec.push_back(a);contour_rec.push_back(b);contour_rec.push_back(c);contour_rec.push_back(d);//計(jì)算矩形的周長(zhǎng)double len_rec = cv::arcLength(contour_rec,true);//打印qDebug()<<"矩形的周長(zhǎng):"<<len_rec;//繪制圓Point center(mat.size().width/2,mat.size().height/2);circle(mat,center,150,255,-1);//獲取輪廓vector<vector<Point>> contours_cir;findContours(mat,contours_cir,RETR_EXTERNAL,CHAIN_APPROX_TC89_KCOS );//計(jì)算圓的周長(zhǎng)double len_cir = cv::arcLength(contours_cir[0],true);//打印qDebug()<<"圓的周長(zhǎng):"<<len_cir;qDebug()<<"數(shù)量:"<<contours_cir.size();//顯示imshow("mat",mat);}Widget::~Widget() {delete ui; }測(cè)試結(jié)果
測(cè)試過(guò)程中發(fā)現(xiàn),用函數(shù)?findContours() 生成?contours ,周長(zhǎng)的計(jì)算結(jié)果,與理論計(jì)算值相比較,存在偏差。
通過(guò)修改參數(shù)?method 的值,反復(fù)測(cè)試和比較,發(fā)現(xiàn)值為?CHAIN_APPROX_TC89_KCOS 時(shí),偏差最小。
參考
OpenCV-計(jì)算輪廓周長(zhǎng)cv::arcLength
總結(jié)
以上是生活随笔為你收集整理的Qt-OpenCV学习笔记--计算周长--arcLength()的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 想用MP4格式,怎么把视频格式改成mp4
- 下一篇: razer鼠标测试软件,比HERO更强么