基于OpenCV的图像处理功能实现:识别圆形、刻度线和距离测量
基于OpenCV的图像处理功能实现:识别圆形、刻度线和距离测量
本文介绍了利用C++语言和OpenCV库实现图像处理功能的示例代码,包括识别黑色圆形、标注圆形和圆心,识别刻度线并进行长度测量,以及计算面积最大圆心到其他圆心的距离。
1. 识别黑色圆形并标注
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
int main()
{
// 读取图片
Mat img = imread('test.jpg', IMREAD_COLOR);
// 转为灰度图
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
// 高斯滤波
GaussianBlur(gray, gray, Size(5, 5), 0, 0);
// 二值化
Mat binary;
threshold(gray, binary, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
// 查找轮廓
vector<vector<Point>> contours;
findContours(binary, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
// 遍历轮廓
for (size_t i = 0; i < contours.size(); i++)
{
// 计算轮廓面积
double area = contourArea(contours[i]);
// 如果面积小于100,则认为是噪点,忽略
if (area < 100)
continue;
// 拟合圆形
RotatedRect rect = fitEllipse(contours[i]);
// 判断是否为黑色圆形
if (rect.size.width / rect.size.height > 0.9 && rect.size.width / rect.size.height < 1.1)
{
// 标出圆形和圆心
Point2f center = rect.center;
circle(img, center, rect.size.width / 2, Scalar(0, 0, 255), 2);
circle(img, center, 2, Scalar(0, 255, 0), 2);
}
}
// 显示结果
imshow('Result', img);
waitKey(0);
return 0;
}
该代码实现了识别一张图片中的黑色圆,并将黑色的圆用红圈标出,用绿色的点标出圆的圆心。
2. 识别刻度线并进行长度测量
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
int main()
{
// 读取图片
Mat img = imread('test.jpg', IMREAD_COLOR);
// 转为灰度图
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
// 高斯滤波
GaussianBlur(gray, gray, Size(5, 5), 0, 0);
// 二值化
Mat binary;
threshold(gray, binary, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
// 查找轮廓
vector<vector<Point>> contours;
findContours(binary, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
// 遍历轮廓
for (size_t i = 0; i < contours.size(); i++)
{
// 计算轮廓面积
double area = contourArea(contours[i]);
// 如果面积小于100,则认为是噪点,忽略
if (area < 100)
continue;
// 拟合直线
Vec4f line;
fitLine(contours[i], line, DIST_L2, 0, 0.01, 0.01);
// 计算直线长度
float length = sqrt(pow(line[2] - line[0], 2) + pow(line[3] - line[1], 2));
// 如果长度为5cm,则认为是刻度线
if (length > 120 && length < 130)
{
// 标出刻度线
line(img, Point(line[0], line[1]), Point(line[2], line[3]), Scalar(0, 0, 255), 2);
}
}
// 显示结果
imshow('Result', img);
waitKey(0);
return 0;
}
该代码实现了识别图片中的刻度线,刻度线总长是5cm,并以这个长度为基准度量图片中的其他长度。
3. 计算面积最大圆心到其他圆心的距离
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
int main()
{
// 读取图片
Mat img = imread('test.jpg', IMREAD_COLOR);
// 转为灰度图
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
// 高斯滤波
GaussianBlur(gray, gray, Size(5, 5), 0, 0);
// 二值化
Mat binary;
threshold(gray, binary, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
// 查找轮廓
vector<vector<Point>> contours;
findContours(binary, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
// 计算面积最大的圆
double maxArea = 0;
Point2f maxCenter;
float maxRadius;
for (size_t i = 0; i < contours.size(); i++)
{
double area = contourArea(contours[i]);
if (area > maxArea)
{
maxArea = area;
minEnclosingCircle(contours[i], maxCenter, maxRadius);
}
}
// 遍历其他圆
for (size_t i = 0; i < contours.size(); i++)
{
// 计算圆心距离
Point2f center;
float radius;
minEnclosingCircle(contours[i], center, radius);
float distance = sqrt(pow(center.x - maxCenter.x, 2) + pow(center.y - maxCenter.y, 2));
// 标出圆
circle(img, center, radius, Scalar(0, 0, 255), 2);
circle(img, center, 2, Scalar(0, 255, 0), 2);
// 标出距离
putText(img, to_string(distance), (center + maxCenter) / 2, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 255), 2);
}
// 显示结果
imshow('Result', img);
waitKey(0);
return 0;
}
该代码实现了测量面积最大的圆的心到其他每一个圆的圆心的距离,并在图上标出圆和距离。
通过以上代码示例,我们可以利用OpenCV库实现各种图像处理功能,例如识别形状、测量尺寸、进行图像分析等。当然,实际应用中可能需要根据具体需求对代码进行调整和优化。
原文地址: https://www.cveoy.top/t/topic/jsES 著作权归作者所有。请勿转载和采集!