您好,登录后才能下订单哦!
这篇文章主要为大家展示了“c#如何实现车辆的轮廓识别”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“c#如何实现车辆的轮廓识别”这篇文章吧。
场景
实现了车辆的轮廓识别,并且已经提取轮廓的最小矩形范围,现在需要知道车尾离矩形最近的两个点,可能有点大材小用
代码
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <vector>
using namespace cv;
using namespace std;
void Dilate( InputArray src, OutputArray dst)
{
int dilation_type = MORPH_RECT;
int dilation_size = 10;
Mat dielem = getStructuringElement( dilation_type,
Size( 2*dilation_size + 1, 2*dilation_size+1 ),
Point( dilation_size, dilation_size ) );
///膨胀操作
dilate( src, dst, dielem );
}
/*
该函数主要是捕获图片中完整出现轮廓的车辆,判断条件为
车辆的轮廓的Y坐标不能大于图片的长度,其次过滤掉面积过小
的轮廓,很可能是车镜或者帧间差分将车辆拆分成两段的误差
*/
void CaptureCompleteVehicle(Mat &srcMat, Mat &grayMat)
{
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(grayMat, contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point());
Mat dstMat=Mat::zeros(grayMat.size(),CV_8UC1);
Mat contourMat;
srcMat.copyTo(contourMat);
int picHeight = grayMat.size().height;
bool bTouchBotton = false;
bool bTouchTop = false;
vector<Moments> mu(contours.size());
for (int i=0; i<contours.size(); i++)
{
mu[i] = moments(contours[i], false);
}
vector<Point2f> mc(contours.size());
for (int i=0; i<contours.size(); i++)
{
mc[i] = Point2d(mu[i].m10 / mu[i].m00, mu[i].m01 / mu[i].m00);
}
for(int i=0; i<contours.size(); i++)
{
if (contourArea(contours[i]) < 10000) continue;
bTouchBotton = false;
bTouchTop = false;
for (int k=0; k<contours[i].size(); k++)
{
Point2f pos = contours[i][k];
if ((pos.y +10) > picHeight)
{
bTouchBotton = true;
break;
}
if (pos.y == 0)
{
bTouchTop = true;
break;
}
}
if (bTouchBotton || bTouchTop) continue;
drawContours(dstMat, contours, i, Scalar(255, 0, 0), 1, 8, hierarchy);
RotatedRect rect=minAreaRect(contours[i]);
Point2f P[4];
rect.points(P);
int leftBottonIndex = 0;
for(int j=0; j<=3; j++)
{
line(contourMat, P[j], P[(j+1)%4], Scalar(255, 0, 0), 2);
if ((P[j].x < mc[i].x) && (P[j].y > mc[i].y))
{
leftBottonIndex = j;
}
}
cv::Rect re(P[leftBottonIndex].x - 20, P[leftBottonIndex].y - 20 , 40, 40);
rectangle(contourMat, re, Scalar(0, 255, 0), 4);
circle(contourMat, mc[i], 5, Scalar(0, 0, 255), -1, 8, 0);
}
imshow("NewAreaRect", contourMat);
}
int main(int argc,char *argv[])
{
VideoCapture videoCap("E:/smoky-cars/positive/大庆东路与水机路交叉口(东北)_冀BU0157_02_141502_01_3_50.wh364");
if(!videoCap.isOpened()) return -1;
double videoFPS=videoCap.get(CV_CAP_PROP_FPS); //获取帧率
double videoPause=1000/videoFPS;
Mat framePrePre; //上上一帧
Mat framePre; //上一帧
Mat frameNow; //当前帧
Mat frameDet; //运动物体
videoCap>>framePrePre;
videoCap>>framePre;
cvtColor(framePrePre,framePrePre,CV_RGB2GRAY);
cvtColor(framePre,framePre,CV_RGB2GRAY);
int save=0;
while(true)
{
videoCap>>frameNow;
if(frameNow.empty()||waitKey(videoPause)==27) break;
cvtColor(frameNow,frameNow,CV_RGB2GRAY);
Mat Det1;
Mat Det2;
absdiff(framePrePre,framePre,Det1); //帧差1
absdiff(framePre,frameNow,Det2); //帧差2
threshold(Det1,Det1,0,255,CV_THRESH_OTSU); //自适应阈值化
threshold(Det2,Det2,0,255,CV_THRESH_OTSU);
Mat element=getStructuringElement(0,Size(3,3)); //膨胀核
dilate(Det1,Det1,element); //膨胀
dilate(Det2,Det2,element);
bitwise_and(Det1,Det2,frameDet);
framePrePre=framePre;
framePre=frameNow;
Dilate(frameDet, frameDet);
CaptureCompleteVehicle(frameNow, frameDet);
waitKey(1000);
}
return 0;
}
以上是“c#如何实现车辆的轮廓识别”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。