椭圆与圆的拟合
来源:互联网 发布:淘宝怎么更改退款金额 编辑:程序博客网 时间:2024/05/14 23:57
#include "opencv2/imgproc/imgproc.hpp"#include "opencv2/highgui/highgui.hpp"#include <stdio.h>#include <string.h>#include <iostream>using namespace cv;using namespace std;void help(){cout <<"\nThis program is demonstration for ellipse fitting. The program finds\n""contours and approximate it by ellipses.\n""Call:\n""./fitellipse [image_name -- Default stuff.jpg]\n" << endl;}char filename[100];char Src_window[100];char Rst_window[100];int sliderPos = 70;Mat image;void processImage(int, void*);int main( int argc, char** argv ){// Process 3 cases by for-loopfor(int i = 0; i <= 2; i++){sprintf(filename, "%d.jpg", i);sprintf(Src_window, "Source%d", i);sprintf(Rst_window, "Result%d", i);// Load the grayscale imageimage = imread(filename, 0); if( image.empty() ){cout << "Couldn't open image " << filename << "\nUsage: fitellipse <image_name>\n";return 0;}// Show the original imageimshow(Src_window, image); namedWindow(Rst_window, 1); // Create toolbars for binary images, and call the processing function.createTrackbar( "threshold", Rst_window, &sliderPos, 255, processImage );processImage(0, 0);// Wait for a key stroke; the same function arranges events processingchar c = waitKey();// Close windowsdestroyWindow(Src_window);destroyWindow(Rst_window);// Exit when pressing "ESC"if(c == 27)break;} return 0;}// Define trackbar callback functon. This function find contours, draw it, // fit it by ellipses and print out infomations of all the circles and ellipses.void processImage(int /*h*/, void*){ vector<vector<Point> > contours;// Convert grayscale image to binary image adjusting by trackbar. Mat bimage = image >= sliderPos; // Find all contours of the binary image. findContours(bimage, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE); Mat cimage = Mat::zeros(bimage.size(), CV_8UC3);// Draw every contour. Fit by ellipse. Print out main infomation. for(size_t i = 0; i < contours.size(); i++) {// threshold requested by cvFitEllipse_32f. size_t count = contours[i].size(); if( count < 6 ) continue; Mat pointsf;int x,y,l,s; Mat(contours[i]).convertTo(pointsf, CV_32F);// Fit the contour by ellipse including circle. RotatedRect box = fitEllipse(pointsf); // Restraint before drawing. if( MAX(box.size.width, box.size.height) > MIN(box.size.width, box.size.height)*30 ) continue;// Draw contours. drawContours(cimage, contours, (int)i, Scalar::all(255), 1, 8);// Draw ellipse including circle. ellipse(cimage, box.center, box.size*0.5f, box.angle, 0, 360, Scalar(0,255,255), 1, CV_AA);// Print out some infomation about the circle or ellipse:// center coordinate and axes length.x = cvRound(box.center.x);y = cvRound(box.center.y);s = cvRound(box.size.width*0.5);l = cvRound(box.size.height*0.5); printf("圆/椭圆的中心坐标:x=%d, y=%d, 长轴:l=%d, 短轴s=%d \n", x, y, l, s); }// Show the result image. imshow(Rst_window, cimage);}
程序效果图如下:
0 0
- 椭圆与圆的拟合
- 圆及椭圆拟合
- 椭圆拟合
- 椭圆拟合
- 基于代数距离的椭圆拟合
- 基于几何距离的椭圆拟合
- 最小二乘拟合圆及椭圆
- 最小二乘拟合&基于RANSAC的直线拟合&椭圆拟合
- 最小二乘法用于直线,多项式,圆,椭圆的拟合及程序实现
- 最小二乘法用于直线,多项式,圆,椭圆的拟合及程序实现
- 毕设之opencv椭圆批量生成与拟合
- opencv 椭圆拟合
- opencv中的椭圆拟合
- matlab 椭圆方程拟合
- 椭圆拟合fit_ellipse.m
- opencv 椭圆拟合
- 【OpenCV】椭圆拟合
- OpenCV:椭圆拟合
- java类防止sql注入
- zfs日常管理以及替换损坏磁盘
- Android 保存和恢复activity的状态数据
- Unity中内嵌网页插件UniWebView使用总结
- 排序算法(5)--快速排序
- 椭圆与圆的拟合
- 内核自带LED驱动移植
- ac_automation 模板
- numpy库
- Linux下将文件打包、压缩并分割成指定大小
- 【信贷业务】信贷五级分类的定义
- eclipse中格式化代码快捷键Ctrl+Shift+F失效的解决办法
- Linux free -m 详细说明
- Minimum Moves to Equal Array Elements问题及解法