//显示结果 imshow("轮廓绘制结果", edge); vector<Point2f>point_0; vector<Point2f>point_1; vector<Point2f>point_2; vector<Point2f>point_3; vector<Point2f>point_4; vector<Point2f>point_5; vector<Point2f>point_6; vector<Point2f>point_7; vector<Point2f>point_8; vector<Point2f>point_9; vector<Point2f>point_10; vector<Point2f>point_11; vector<Point2f>point_12; vector<Point2f>point_13; vector<Point2f>point_14; vector<Point2f>point_15; vector<Point2f>point_16; vector<Point2f>point_17; vector<Point2f>point_18; vector<Point2f>point_19;
时间: 2023-06-27 12:06:06 浏览: 38
这段代码是在定义多个vector变量,每个vector变量都存储了图像中的某个轮廓的关键点坐标。这些变量的命名中包含了数字,可能是因为这些关键点所属的轮廓编号。在这段代码中,只是定义了这些vector变量,但是并没有进行赋值操作。同时,代码中还展示了一个名为"轮廓绘制结果"的窗口,将图像中的轮廓绘制出来并显示出来。
相关问题
#include <opencv2/opencv.hpp>#include <iostream>using namespace std;using namespace cv;int main(){ // 读取图像并进行预处理 Mat src = imread("test.jpg"); Mat gray, binary; cvtColor(src, gray, COLOR_BGR2GRAY); threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU); imshow("binary", binary); // 计算轮廓 vector<vector<Point> > contours; vector<Vec4i> hierarchy; findContours(binary, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point()); // 计算中心、质心、重心、外接圆圆心 Point2f center, centroid, gravity, minEnclosingCircleCenter; Moments mu = moments(contours[0], false); center = Point2f(mu.m10 / mu.m00, mu.m01 / mu.m00); minEnclosingCircle(contours[0], minEnclosingCircleCenter, radius); centroid = Point2f(mu.m10 / mu.m00, mu.m01 / mu.m00); gravity = Point2f(mu.m10 / mu.m00, mu.m01 / mu.m00 + 50); // 绘制结果 Mat drawing = Mat::zeros(binary.size(), CV_8UC3); circle(drawing, center, 5, Scalar(0, 0, 255), -1); circle(drawing, centroid, 5, Scalar(0, 255, 0), -1); circle(drawing, gravity, 5, Scalar(255, 0, 0), -1); circle(drawing, minEnclosingCircleCenter, 5, Scalar(0, 255, 255), -1); imshow("result", drawing); waitKey(0); return 0;}添加更详细的注释
好的,下面是更详细的注释:
```c++
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
// 读取图像并进行预处理
Mat src = imread("test.jpg"); // 读取图像
Mat gray, binary; // 定义灰度图像和二值图像
cvtColor(src, gray, COLOR_BGR2GRAY); // 将图像转换为灰度图像
threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU); // 对灰度图像进行二值化
imshow("binary", binary); // 显示二值图像
// 计算轮廓
vector<vector<Point> > contours; // 定义轮廓
vector<Vec4i> hierarchy; // 定义轮廓的层次结构
findContours(binary, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point()); // 提取轮廓
// 计算中心、质心、重心、外接圆圆心
Point2f center, centroid, gravity, minEnclosingCircleCenter; // 定义中心、质心、重心、外接圆圆心
Moments mu = moments(contours[0], false); // 计算轮廓的矩
center = Point2f(mu.m10 / mu.m00, mu.m01 / mu.m00); // 计算中心
float radius;
minEnclosingCircle(contours[0], minEnclosingCircleCenter, radius); // 计算外接圆
centroid = Point2f(mu.m10 / mu.m00, mu.m01 / mu.m00); // 计算质心
gravity = Point2f(mu.m10 / mu.m00, mu.m01 / mu.m00 + 50); // 计算重心
// 绘制结果
Mat drawing = Mat::zeros(binary.size(), CV_8UC3); // 创建一个空的彩色图像
circle(drawing, center, 5, Scalar(0, 0, 255), -1); // 绘制中心
circle(drawing, centroid, 5, Scalar(0, 255, 0), -1); // 绘制质心
circle(drawing, gravity, 5, Scalar(255, 0, 0), -1); // 绘制重心
circle(drawing, minEnclosingCircleCenter, 5, Scalar(0, 255, 255), -1); // 绘制外接圆圆心
imshow("result", drawing); // 显示绘制结果
waitKey(0); // 等待按键
return 0;
}
```
注释中对代码的各个部分进行了说明,使得代码更容易理解。
Mat imgthreshold; Mat imgsrc = cv::imread("C:/Users/Dell/Pictures/Camera Roll/6.png",0); imshow("imgsrc", imgsrc); threshold(imgsrc, imgthreshold,50, 110, THRESH_BINARY); imshow("imgthreshold", imgthreshold); vector<vector<Point>> contours; findContours(imgthreshold, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); cv::Mat imgdraw = imgsrc.clone(); drawContours(imgdraw, contours, -1, (0, 0, 255),0); imshow("imgdraw", imgdraw); waitKey(0);为什么这块输出的是白色边框而不是红色
在绘制边框时,你应该传入一个 Scalar 类型的参数作为边框颜色,而不是一个元组或者一个数组。修改 drawContours 函数的第四个参数,将其改为 Scalar(0, 0, 255) 即可绘制红色边框,代码如下:
```c++
Mat imgthreshold;
Mat imgsrc = cv::imread("C:/Users/Dell/Pictures/Camera Roll/6.png",0);
imshow("imgsrc", imgsrc);
threshold(imgsrc, imgthreshold,50, 110, THRESH_BINARY);
imshow("imgthreshold", imgthreshold);
vector<vector<Point>> contours;
findContours(imgthreshold, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
cv::Mat imgdraw = imgsrc.clone();
drawContours(imgdraw, contours, -1, Scalar(0, 0, 255), 1); // 修改这里
imshow("imgdraw", imgdraw);
waitKey(0);
```