当前位置:网站首页>OpenCV计算外包矩形
OpenCV计算外包矩形
2022-07-23 09:21:00 【Chaoying.】
分别计算平行于坐标轴的外接矩形和面积最小的外接矩形
#include <opencv.hpp>
#include <iostream>
using namespace std;
void CornerSift(string ImageName,cv::Rect &boundingRec,cv::RotatedRect &minAreaRec)
{
cv::Mat ImageInput;
ImageInput = cv::imread(ImageName,cv::ImreadModes::IMREAD_COLOR);
if (ImageInput.empty())
{
cout << "图像文件为空!" << std::endl;
return;
}
cv::Mat ImageRGB/*角点检测彩色*/, ImageGray;
ImageInput.copyTo(ImageRGB);
cvtColor(ImageRGB, ImageGray, cv::COLOR_BGR2GRAY);
vector<cv::KeyPoint> Keypoints;
float thContrast = 0.03;//角点对比度阈值
float thEdge = 10;//特征值之比。越小角点质量越高
cv::Ptr<cv::Feature2D> f2d = cv::SIFT::create(0, 3, thContrast, thEdge, 1.6);
f2d->detect(ImageGray, Keypoints);
int num = Keypoints.size();
//打印点坐标
vector<cv::Point2f> kPxys;
for (int i = 0; i < num; i++)
{
cv::KeyPoint kP = Keypoints[i];
//cout << kP.pt.x << "\t" << kP.pt.y << endl;
kPxys.push_back(kP.pt);
}
//绘制特征点
cv::drawKeypoints(ImageRGB, Keypoints, ImageRGB, cv::Scalar(255, 255, 0), cv::DrawMatchesFlags::DEFAULT);
//基于sift特征点计算最小外接矩形
//平行坐标轴的外接矩形
boundingRec = cv::boundingRect(kPxys);
cv::rectangle(ImageRGB, boundingRec, cv::Scalar(255, 0, 0),2);
//面积最小外接矩形
minAreaRec = cv::minAreaRect(kPxys);
cv::Point2f minAreaRecOPoints[4];
minAreaRec.points(minAreaRecOPoints);
for (int i = 0; i < 4; i++)
{
cv::Point2f startPoint = minAreaRecOPoints[i];
cv::Point2f endPoint;
if (i != 3)
{
endPoint = minAreaRecOPoints[i + 1];
}
else
{
endPoint = minAreaRecOPoints[0];
}
cv::line(ImageRGB, startPoint, endPoint, cv::Scalar(0, 255, 0), 2);
}
cv::imshow("2D Features", ImageRGB);
cv::waitKey(0);
cv::destroyAllWindows();
return;
}
int main()
{
cv::Rect boundingRec;
cv::RotatedRect minAreaRec;
CornerSift("t3.jpg",boundingRec,minAreaRec);
//获取矩形相关信息
cv::Point2f center = minAreaRec.center;
cv::Size2f recSize = minAreaRec.size;
float recAngle = minAreaRec.angle;
cout << center << endl;
cout << recSize << endl;
cout << recAngle << endl;
return 0;
}
效果
边栏推荐
- [applet automation minium] III. element positioning - use of wxss selector
- Introduction and mechanism of Aptos
- 【附下载】值得收藏的几款渗透测试常用的脚本
- JS calendar style pie chart statistics plug-in
- 2022河南萌新联赛第(二)场:河南理工大学 补题题解
- Yunna | how to manage the fixed assets of the company? How to manage the company's fixed assets better?
- C语言项目实战:24点游戏计算器(基于结构体、指针、函数、数组、循环等知识点)
- Regular verification of ID number
- Using JS to parse and execute XSS automatically
- Okrk3399 Development Board Reserved i2c4 Mounting EEPROM
猜你喜欢

koa框架的使用

利用js自动解析执行xss

Official wechat product! Applet automation framework minium sharing Preview

R语言实战应用案例:绘图篇(三)-多种组合图型绘制

基于EFR32MG24的AI 加速度姿势识别体验

Cool code rain dynamic background registration page

优化华为云服务器采用Key登陆

Optimisation du serveur Cloud Huawei avec connexion clé

Yunna | how to manage the fixed assets of the company? How to manage the company's fixed assets better?
It is suggested that Siyuan notes can be compatible with third-party sync disks
随机推荐
建议思源笔记能够兼容第三方同步盘
Can bus quick understanding
[download attached] several scripts commonly used in penetration testing that are worth collecting
Due to resource constraints, the namenode fails to start with an error unable to create new native thread
【软件测试】盘一盘工作中遇到的 MQ 异常测试
[WinForm] desktop program implementation scheme for screenshot recognition and calculation
js纹理样式饼图插件
在使用 VScode 进行代码格式化后,保存发现代码又变乱了,怎么办?vs去掉格式化
[paper notes] mobile robot navigation method based on hierarchical depth reinforcement learning
Cool code rain dynamic background registration page
js日历样式饼图统计插件
Pychart reads excel file with error: raise xlrderror (file_format_descriptions[file_format]+; not supported)
炫酷代码雨动态背景注册页面
【WinForm】关于截图识别数字并计算的桌面程序实现方案
(heavy chain dissection) Magic Tree
【C语言】猜数字小游戏+关机小程序
[array & String & Macro exercise]
Yunna - how to strengthen fixed asset management? How to strengthen the management of fixed assets?
LZ77文件压缩
基金开户网上办理是否安全?谁给解答一下