当前位置: 首页 > news >正文

c++提取矩形区域图像的梯度并拟合直线

在这里插入图片描述
c++提取旋转矩形区域的边缘最强梯度点,并拟合直线

#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>using namespace cv;
using namespace std;int main() {// 加载图像Mat img = imread("image.jpg", IMREAD_GRAYSCALE);if (img.empty()) {cout << "Could not open or find the image!" << endl;return -1;}// 定义旋转矩形 (中心点, 大小, 旋转角度)Point2f center(img.cols / 2.0f, img.rows / 2.0f);Size2f size(200, 100); // 矩形大小float angle = 45;      // 旋转角度RotatedRect rotatedRect(center, size, angle);// 提取旋转矩形区域Mat rotatedRegion;Mat rotationMatrix = getRotationMatrix2D(center, angle, 1.0);warpAffine(img, rotatedRegion, rotationMatrix, img.size(), INTER_LINEAR, BORDER_CONSTANT, Scalar(0));Rect boundingRect = rotatedRect.boundingRect();Mat croppedRegion = rotatedRegion(boundingRect);// 计算梯度Mat gradX, gradY, gradMag, gradDir;Sobel(croppedRegion, gradX, CV_32F, 1, 0); // X方向梯度Sobel(croppedRegion, gradY, CV_32F, 0, 1); // Y方向梯度magnitude(gradX, gradY, gradMag);          // 梯度幅值phase(gradX, gradY, gradDir, true);        // 梯度方向// 找到最强梯度点double minVal, maxVal;Point minLoc, maxLoc;minMaxLoc(gradMag, &minVal, &maxVal, &minLoc, &maxLoc);// 提取梯度最强的点vector<Point2f> strongGradientPoints;float threshold = 0.8 * maxVal; // 设置阈值for (int y = 0; y < gradMag.rows; y++) {for (int x = 0; x < gradMag.cols; x++) {if (gradMag.at<float>(y, x) > threshold) {strongGradientPoints.push_back(Point2f(x + boundingRect.x, y + boundingRect.y));}}}// 拟合直线Vec4f lineParams;fitLine(strongGradientPoints, lineParams, DIST_L2, 0, 0.01, 0.01);// 计算直线的两个端点Point2f linePoint(lineParams[2], lineParams[3]);Point2f lineDirection(lineParams[0], lineParams[1]);Point2f pt1 = linePoint - lineDirection * 1000; // 延长线Point2f pt2 = linePoint + lineDirection * 1000;// 在原图上绘制旋转矩形和拟合的直线Mat imgDisplay;cvtColor(img, imgDisplay, COLOR_GRAY2BGR);Point2f vertices[4];rotatedRect.points(vertices);for (int i = 0; i < 4; i++) {line(imgDisplay, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 0), 2);}line(imgDisplay, pt1, pt2, Scalar(0, 0, 255), 2);// 显示结果imshow("Rotated Region", croppedRegion);imshow("Gradient Magnitude", gradMag);imshow("Result", imgDisplay);waitKey(0);return 0;
}

效果演示
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

http://www.lryc.cn/news/531007.html

相关文章:

  • Unity Shader Graph 2D - 角色身体电流覆盖效果
  • 【LLM-agent】(task4)搜索引擎Agent
  • 携程Java开发面试题及参考答案 (200道-下)
  • GWO优化SVM回归预测matlab
  • QMK启用摇杆和鼠标按键功能
  • Unity实现按键设置功能代码
  • 基于物联网技术的实时数据流可视化研究(论文+源码)
  • list容器(详解)
  • Elasticsearch基本使用详解
  • 17.3.4 颜色矩阵
  • FPGA 时钟多路复用
  • 机器学习10
  • 【Block总结】CoT,上下文Transformer注意力|即插即用
  • linux库函数 gettimeofday() localtime的概念和使用案例
  • 编程题-电话号码的字母组合(中等)
  • EasyExcel使用详解
  • 基于“蘑菇书”的强化学习知识点(二):强化学习中基于策略(Policy-Based)和基于价值(Value-Based)方法的区别
  • 民法学学习笔记(个人向) Part.2
  • 物业管理系统源码驱动社区管理革新提升用户满意度与服务效率
  • 租房管理系统助力数字化转型提升租赁服务质量与用户体验
  • Ollama教程:轻松上手本地大语言模型部署
  • Baklib推动数字化内容管理解决方案助力企业数字化转型
  • DeepSeek-R1 论文. Reinforcement Learning 通过强化学习激励大型语言模型的推理能力
  • DOM 操作入门:HTML 元素操作与页面事件处理
  • 使用 HTTP::Server::Simple 实现轻量级 HTTP 服务器
  • C++滑动窗口技术深度解析:核心原理、高效实现与高阶应用实践
  • 基于构件的软件开发方法
  • 网站快速收录:如何设置robots.txt文件?
  • OpenGL学习笔记(六):Transformations 变换(变换矩阵、坐标系统、GLM库应用)
  • 8.攻防世界Web_php_wrong_nginx_config