毕设之Opencv批量圆拟合
程序思路:编写read_csv()函数读取图片目录下txt文档,获取各BMP文件绝对路径以及对应圆的圆心坐标、半径参数。读取各BMP图像,转为灰度图,二值化后检测轮廓,然后检测最小圆;并将检测结果和实际结果存入txt文档。
不需调参;对于完美圆的检测优于霍夫圆变换。
功能小结:
1、read_csv()函数
void read_csv(string&csvPath, vector<String>&CirclePath, vector<int>&Circle_x, vector<int>&Circle_y, vector<int>&Circle_r)
csvPath:txt路径
CirclePath:从txt中读取的bmp路径
Circle_x:bmp图像中圆的圆心x坐标
Circle_y:bmp图像中圆的圆心y坐标
Circle_r:bmp图像中圆的半径r
2、创建txt文本,并写入数据
#include <iostream>
#include <sstream>
#include <fstream>
ofstream file("filepath",ios::out);
if (file.is_open())
{
file <<;
}
file.close();
3、转化为灰度图
cvtColor(srcImage,midImage, COLOR_BGR2GRAY);//转化边缘检测后的图为灰度图
@paramsrc input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC... ), orsingle-precision
floating-point.
@paramdst output image of the same size and depth as src.
@paramcode color space conversion code (see cv::ColorConversionCodes).
@paramdstCn number of channels in the destination image; if the parameter is 0, thenumber of the
channelsis derived automatically from src and code.
4、图像二值化Threshold()
函数 Threshold对单通道数组应用固定阈值操作。该函数的典型应用是对灰度图像进行阈值操作得到二值图像。(cvCmpS也可以达到此目的)或者是去掉噪声,例如过滤很小或很大像素值的图像点。本函数支持的对图像取阈值的方法由 threshold_type确定。
CV_EXPORTS_W double threshold( InputArray src,OutputArray dst,
double thresh, double maxval, int type );
/**@brief Applies a fixed-level threshold to each array element.
Thefunction applies fixed-level thresholding to a single-channel array. Thefunction is typically
usedto get a bi-level (binary) image out of a grayscale image ( cv::compare couldbe also used for
thispurpose) or for removing a noise, that is, filtering out pixels with too smallor too large
values.There are several types of thresholding supported by the function. They aredetermined by
typeparameter.
Also,the special values cv::THRESH_OTSU or cv::THRESH_TRIANGLE may be combined withone of the
abovevalues. In these cases, the function determines the optimal threshold valueusing the Otsu's
orTriangle algorithm and uses it instead of the specified thresh . The functionreturns the
computedthreshold value. Currently, the Otsu's and Triangle methods are implementedonly for 8-bit
images.
@paramsrc input array (single-channel, 8-bit or 32-bit floating point).
@paramdst output array of the same size and type as src.
@paramthresh threshold value.
@parammaxval maximum value to use with the THRESH_BINARY and THRESH_BINARY_INVthresholding
types.
@paramtype thresholding type (see the cv::ThresholdTypes).
@sa adaptiveThreshold, findContours, compare,min, max
*/
5、检索轮廓FindContours()
函数FindContours从二值图像中检索轮廓,并返回检测到的轮廓的个数。first_con