椭圆与圆的拟合

来源:互联网 发布:淘宝怎么更改退款金额 编辑:程序博客网 时间: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
原创粉丝点击