首页 > 其他分享 >OpenCV找圆

OpenCV找圆

时间:2022-11-22 14:03:13浏览次数:46  
标签:src 0.0 iter OpenCV Points Centroid double 找圆


#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace cv;
using namespace std;
void LeastSquaresCircleFitting(vector<Point2d> Points, Point2d &Centroid, double& dRadius);

int main()

{
Mat src_gray, src_binary, src_blur, detected_edges;
Mat src = imread("月亮.bmp", 1);
cvtColor(src, src_gray, CV_BGR2GRAY);
medianBlur(src_gray, src_blur, 21);
threshold(src_blur, src_binary, 50, 255, 0);
imwrite("src_binary.jpg", src_binary);
Canny(src_binary, detected_edges, 50, 150, 3);
imwrite("筋片detected_edges.jpg", detected_edges);
//----------------------------------------------------------------//
vector<Point2d> m_Points;
//扫描取点,从左到右,逐行扫描
for (int i = 100; i <1900; ++i)
{
uchar* data = detected_edges.ptr<uchar>(i);
for (int j = 1570; j < 3000; ++j)
{
if (data[j] == 255)
{
m_Points.push_back(Point(j, i));
break;
}
}
}

//扫描取点,从右到左,逐行扫描
for (int i = 100; i < 1900; ++i)
{
uchar* data = detected_edges.ptr<uchar>(i);
for (int j = 1570; j > 50; --j)
{
if (data[j] == 255)
{
m_Points.push_back(Point(j, i));
break;
}
}
}

//扫描后的点保存在图像scan_dst中
Mat scan_dst = Mat::zeros(cv::Size(src_gray.cols, src_gray.rows), src_gray.type());
vector<Point2d>::iterator iter1;
vector<Point2d>::iterator end1 = m_Points.end();
for (iter1 = m_Points.begin(); iter1 != end1; ++iter1)
{
scan_dst.at<uchar>((*iter1).x, (*iter1).y) = 255;
}
//显示扫描后的图
//namedWindow("Result1", 0); imshow("Result1", scan_dst);
imwrite("scan_dst.jpg", scan_dst);


// //求半径
double m_dRadius_DianPian = 0;
Point2d m_Centroid_DianPian;
LeastSquaresCircleFitting(m_Points, m_Centroid_DianPian, m_dRadius_DianPian);
/最小二乘拟合圆
cout << "最小二乘拟合结果 " << endl << " " << endl << endl;
cout << "垫片直径 = " << endl << " " << m_dRadius_DianPian * 2 * 2.46 << endl << endl;
cout << "m_Centroid.x = " << endl << " " << m_Centroid_DianPian.x << endl << endl;
cout << "m_Centroid.y = " << endl << " " << m_Centroid_DianPian.y << endl << endl;

//**********在原图画拟合圆

//***垫片图上画圆
circle(src, Point2d(m_Centroid_DianPian.x, m_Centroid_DianPian.y), m_dRadius_DianPian, Scalar(255, 0, 0), 10, 8, 0);
//在原图画一个圆圈点
cv::Point2d point1;//特征点,用以画在图像中
point1.x = m_Centroid_DianPian.x;//特征点在图像中横坐标
point1.y = m_Centroid_DianPian.y;//特征点在图像中纵坐标
cv::circle(src, point1, 4, cv::Scalar(255, 0, 0), 10, 8, 0);
//*****//
//图片的缩小与放大
Mat scale_dst = Mat::zeros(1032, 1544, CV_8UC3); //转化为1544*1032大小的
resize(src, scale_dst, scale_dst.size());
namedWindow("src", 0); imshow("src", scale_dst);
waitKey(0);
return 0;
}

void LeastSquaresCircleFitting(vector<Point2d> Points, Point2d &Centroid, double& dRadius)
{
int iNum = (int)Points.size();
double X1 = 0.0;
double Y1 = 0.0;
double X2 = 0.0;
double Y2 = 0.0;
double X3 = 0.0;
double Y3 = 0.0;
double X1Y1 = 0.0;
double X1Y2 = 0.0;
double X2Y1 = 0.0;
vector<Point2d>::iterator iter;
vector<Point2d>::iterator end = Points.end();
for (iter = Points.begin(); iter != end; ++iter)
{
X1 = X1 + (*iter).x;
Y1 = Y1 + (*iter).y;
X2 = X2 + (*iter).x * (*iter).x;
Y2 = Y2 + (*iter).y * (*iter).y;
X3 = X3 + (*iter).x * (*iter).x * (*iter).x;
Y3 = Y3 + (*iter).y * (*iter).y * (*iter).y;
X1Y1 = X1Y1 + (*iter).x * (*iter).y;
X1Y2 = X1Y2 + (*iter).x * (*iter).y * (*iter).y;
X2Y1 = X2Y1 + (*iter).x * (*iter).x * (*iter).y;
}
double C = 0.0;
double D = 0.0;
double E = 0.0;
double G = 0.0;
double H = 0.0;
double a = 0.0;
double b = 0.0;
double c = 0.0;
C = iNum * X2 - X1 * X1;
D = iNum * X1Y1 - X1 * Y1;
E = iNum * X3 + iNum * X1Y2 - (X2 + Y2) * X1;
G = iNum * Y2 - Y1 * Y1;
H = iNum * X2Y1 + iNum * Y3 - (X2 + Y2) * Y1;
a = (H * D - E * G) / (C * G - D * D);
b = (H * C - E * D) / (D * D - G * C);
c = -(a * X1 + b * Y1 + X2 + Y2) / iNum;
double A = 0.0;
double B = 0.0;
double R = 0.0;
A = a / (-2);
B = b / (-2);
R = double(sqrt(a * a + b * b - 4 * c) / 2);
Centroid.x = A;
Centroid.y = B;
dRadius = R;
}

OpenCV找圆_2d


标签:src,0.0,iter,OpenCV,Points,Centroid,double,找圆
From: https://blog.51cto.com/u_13875041/5877903

相关文章

  • opencv实现人脸识别和眼部识别
    代码importcv2ascvimg=cv.imread("./lena.jpg")gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)face_cascade=cv.CascadeClassifier('/usr/local/share/opencv4/haarcas......
  • 利用opencv拼接图像视频摄像头进行录像
    将图像拼接成视频格式今天想将5000张图片转换成视频格式,操作如下:importosimportcv2importnumpyasnppath='/home/violet/PycharmProjects/deepSort/images/img1/'fil......
  • python-opencv抓取RTMP
    opencv安装sudoapt-getinstallpython3-opencv源码安装https://blog.csdn.net/u011922698/article/details/123268143pip3installopencv-python#安装opencvpip3......
  • Python OpenCV给证件照换底色
    ✅作者简介:热爱科研的算法开发者,Python、Matlab项目可交流、沟通、学习。......
  • OpenCV基础 | 1.像素运算
    ✅作者简介:热爱科研的算法开发者,Python、Matlab项目可交流、沟通、学习。......
  • OpenCV实现艺术字
    本文参考自《计算机视觉40例从入门到深度学习(OpenCV-Python)》原理介绍通过简单的或运算实现。lenacolor.pngwatermark.bmp(二值图像)lenacolor作为艺术字的背景图像......
  • OpenCV的图像加法
    本文参考自《计算机视觉40例从入门到深度学习(OpenCV-Python)》5.5.2前言日常生活中,我们对于加法的结果有如下两种处理方式取模处理,又称作“循环取余”,例如对时间的处......
  • OpenCV实现LSB算法(数字水印)
    本文参考自《计算机视觉40例从入门到深度学习(OpenCV-Python)》LSB算法的原理就不在过多的介绍了,直接上代码。lenacolor.pngwatermark.bmp#LSB算法importnumpyasn......
  • OpenCV提取图像的位平面
    提取位平面函数(仅仅支持灰度图像)defextractBitPlace(img,layer):h,w=img.shapemat=np.ones((h,w),np.uint8)mat=mat*(2**(layer-1))returncv2.bitw......
  • OpenCV常用函数
    1.读取图像cv2.imread(filename,[,flags])2.色彩空间转换dst=cv2.cvtColor(src,code,[,dstCn])code是色彩空间转换码dstCn是目标图像的通道数。如果参数为默认值......