//显示结果 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; int m, n; for (m = 0; m < contours.size(); m++) { for (n = 0; n < contours[m].size(); n++) { switch (m) { case 0: point_0.push_back(contours[m][n]); break; case 1: point_1.push_back(contours[m][n]); break; case 2: point_2.push_back(contours[m][n]); break; case 3: point_3.push_back(contours[m][n]); break; case 4: point_4.push_back(contours[m][n]); break; case 5: point_5.push_back(contours[m][n]); break; case 6: point_6.push_back(contours[m][n]); break; case 7: point_7.push_back(contours[m][n]); break; case 8: point_8.push_back(contours[m][n]); break; case 9: point_9.push_back(contours[m][n]); break; case 10: point_10.push_back(contours[m][n]); break; case 11: point_11.push_back(contours[m][n]); break; case 12: point_12.push_back(contours[m][n]); break; case 13: point_13.push_back(contours[m][n]); break; case 14: point_14.push_back(contours[m][n]); break; case 15: point_15.push_back(contours[m][n]); break; case 16: point_16.push_back(contours[m][n]); break; case 17: point_17.push_back(contours[m][n]); break; case 18: point_18.push_back(contours[m][n]); break; case 19: point_19.push_back(contours[m][n]); break; default: cout << "input error" << endl; } } } Mat img(321, 432, CV_8UC1, cv::Scalar(0));

时间: 2023-06-27 19:06:03 浏览: 48
这段代码的作用是将找到的轮廓分别存储到不同的 vector<Point2f> 中,并创建一个大小为321x432的空白图像。其中,vector<Point2f> 是一个存储二维点坐标的容器,contours[m][n] 表示第 m 个轮廓的第 n 个点的坐标。根据轮廓的数量,将每个轮廓中的点分别存储到对应的 vector<Point2f> 中。最后,创建一个大小为321x432的空白图像,类型为 CV_8UC1,即8位无符号单通道图像,像素值初始化为0。
相关问题

//显示结果 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;

这段代码是在定义多个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; } ``` 注释中对代码的各个部分进行了说明,使得代码更容易理解。

相关推荐

将下列代码转换成python代码 #include <opencv2/opencv.hpp> #include <vector> #include <time.h> using namespace cv; using namespace std; // 8邻域 const Point neighbors[8] = { { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, { 0, -1 }, { -1, -1 }, { -1, 0 }, {-1, 1} }; int main() { // 生成随机数 RNG rng(time(0)); Mat src = imread("1.jpg"); Mat gray; cvtColor(src, gray, CV_BGR2GRAY); Mat edges; Canny(gray, edges, 30, 100); vector seeds; vector contour; vector<vector> contours; int i, j, k; for (i = 0; i < edges.rows; i++) for (j = 0; j < edges.cols; j++) { Point c_pt = Point(i, j); //如果当前点为轮廓点 if (edges.at<uchar>(c_pt.x, c_pt.y) == 255) { contour.clear(); // 当前点清零 edges.at<uchar>(c_pt.x, c_pt.y) = 0; // 存入种子点及轮廓 seeds.push_back(c_pt); contour.push_back(c_pt); // 区域生长 while (seeds.size() > 0) { // 遍历8邻域 for (k = 0; k < 8; k++) { // 更新当前点坐标 c_pt.x = seeds[0].x + neighbors[k].x; c_pt.y = seeds[0].y + neighbors[k].y; // 边界界定 if ((c_pt.x >= 0) && (c_pt.x <= edges.rows - 1) && (c_pt.y >= 0) && (c_pt.y <= edges.cols - 1)) { if (edges.at<uchar>(c_pt.x, c_pt.y) == 255) { // 当前点清零 edges.at<uchar>(c_pt.x, c_pt.y) = 0; // 存入种子点及轮廓 seeds.push_back(c_pt); contour.push_back(c_pt); }// end if } } // end for // 删除第一个元素 seeds.erase(seeds.begin()); }// end while contours.push_back(contour); }// end if } // 显示一下 Mat trace_edge = Mat::zeros(edges.rows, edges.cols, CV_8UC1); Mat trace_edge_color; cvtColor(trace_edge, trace_edge_color, CV_GRAY2BGR); for (i = 0; i < contours.size(); i++) { Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); //cout << edges[i].size() << endl; // 过滤掉较小的边缘 if (contours[i].size() > 5) { for (j = 0; j < contours[i].size(); j++) { trace_edge_color.at<Vec3b>(contours[i][j].x, contours[i][j].y)[0] = color[0]; trace_edge_color.at<Vec3b>(contours[i][j].x, contours[i][j].y)[1] = color[1]; trace_edge_color.at<Vec3b>(contours[i][j].x, contours[i][j].y)[2] = color[2]; } } } imshow("edge", trace_edge_color); waitKey(); return 0; }

class SR_net { public: SR_net(string path, vector<int> input_size, bool fp32, bool cuda = true); private: vector<int64_t> Gdims; int Gfp32; Env env = Env(ORT_LOGGING_LEVEL_ERROR, "RRDB"); SessionOptions session_options = SessionOptions(); Session* Gsession = nullptr; vector<const char*> Ginput_names; vector<const char*> Goutput_names; vector<int> Ginput_size = {}; }; SR_net::SR_net(string path, vector<int> input_size, bool fp32, bool cuda) { this->Ginput_size = input_size; this->Gfp32 = fp32; clock_t startTime_, endTime_; startTime_ = clock(); session_options.SetIntraOpNumThreads(6); if (cuda) { OrtCUDAProviderOptions cuda_option; cuda_option.device_id = 0; cuda_option.arena_extend_strategy = 0; cuda_option.cudnn_conv_algo_search = OrtCudnnConvAlgoSearchExhaustive; cuda_option.gpu_mem_limit = SIZE_MAX; cuda_option.do_copy_in_default_stream = 1; session_options.AppendExecutionProvider_CUDA(cuda_option); } wstring widestr = wstring(path.begin(), path.end()); this->Gsession = new Session(env, widestr.c_str(), this->session_options); this->session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_ALL); AllocatorWithDefaultOptions allocator; this->Ginput_names = { "input" }; this->Goutput_names = { "output" }; endTime_ = clock(); cout << " The model loading time is:" << (double)(endTime_ - startTime_) / CLOCKS_PER_SEC << "s" << endl; } int main() { vector<int> input_shape = {}; SR_net net("E:/prj/SR_C/onnx_file/rrdb_full.onnx", input_shape, true, true); vector<String> files; glob("E:/prj/超分样本/1", files, true); size_t num = files.size(); bool Moos = true; cout << "共读取了" << num << "张图片" << endl; cout << "--------------------------------" << endl; for (int i = 0; i < num; i++) { Mat srcimg = imread(files[i]); Mat SR_image = net.Detect(srcimg, Moos); imshow("input", srcimg); imshow("result", SR_image); imwrite("./output/" + to_string(i + 1) + ".png", SR_image); waitKey(0); } },在这段代码中,我如何把SR_net net("E:/prj/SR_C/onnx_file/rrdb_full.onnx", input_shape, true, true);这一行写到主函数的外面?

最新推荐

recommend-type

解决python中显示图片的plt.imshow plt.show()内存泄漏问题

主要介绍了解决python中显示图片的plt.imshow plt.show()内存泄漏问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a

pre_o_1csdn63m9a1bs0e1rr51niuu33e.a
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这
recommend-type

导入numpy库,创建两个包含9个随机数的3*3的矩阵,将两个矩阵分别打印出来,计算两个数组的点积并打印出来。(random.randn()、dot()函数)

可以的,以下是代码实现: ```python import numpy as np # 创建两个包含9个随机数的3*3的矩阵 matrix1 = np.random.randn(3, 3) matrix2 = np.random.randn(3, 3) # 打印两个矩阵 print("Matrix 1:\n", matrix1) print("Matrix 2:\n", matrix2) # 计算两个数组的点积并打印出来 dot_product = np.dot(matrix1, matrix2) print("Dot product:\n", dot_product) ``` 希望