当前位置:网站首页>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;
}
效果
边栏推荐
- Can bus quick understanding
- Using JS to parse and execute XSS automatically
- 固定资产管理系统哪家好?固定资产管理平台有哪些?
- Looking for peak [Abstract dichotomy exercise]
- Okrk3399 Development Board reserves i2c4 to mount EEPROM
- Using shell script to block IP with high scanning frequency
- 正则表达式常用语法解析
- [can I do your first project?] Detailed introduction and Simulation Implementation of gzip
- Optimize Huawei ECs to use key login
- 基金开户网上办理是否安全?谁给解答一下
猜你喜欢

Optimisation du serveur Cloud Huawei avec connexion clé
![[array & String & Macro exercise]](/img/44/26debc1ee958277935e73a75d894d4.png)
[array & String & Macro exercise]

Can bus quick understanding

Qt文档阅读笔记-Audio Example解析

4. Find the median of two positive arrays

Vk36n5d anti power interference / mobile phone interference 5-key 5-channel touch detection chip anti freeze function ponding in the touch area can still be operated

How can manual testing turn to automated testing? Byte 5 years of automation experience talk about

10 years of software testing engineer experience, very confused

手工测试如何转向自动化测试?字节5年自动化经验浅谈一下...

AI acceleration gesture recognition experience based on efr32mg24
随机推荐
工作小记:一次抓包
How can manual testing turn to automated testing? Byte 5 years of automation experience talk about
炫酷代码雨动态背景注册页面
C language introduction practice (11): enter a group of positive integers and sum the numbers in reverse order
【WinForm】关于截图识别数字并计算的桌面程序实现方案
104 maximum depth of binary tree and 543 diameter of binary tree and 124 maximum path sum of binary tree
运维高级作业02
可以进行2D或3D三角剖分的一些库
第2章 基础查询与排序
First acquaintance and search set
第三章 复杂一点的查询
【面试高频】cookie、session、token?看完再也不担心被问了
JS software unloading prompt expression changes with the mouse JS special effect
Argocd user management, RBAC control, script login, APP synchronization
Optimize Huawei ECs to use key login
基金开户网上办理是否安全?谁给解答一下
Chapter 2 basic query and sorting
建议思源笔记能够兼容第三方同步盘
Canvas 从入门到劝朋友放弃(图解版)
[paper notes] mobile robot navigation method based on hierarchical depth reinforcement learning