场景
尝试使用直方图对图像进行增强,然后使用二值化函数,分割出车辆的轮廓,显然这个在应对道路和车辆颜色相近的情况下,即使不是相近,依旧没有达到任何的效果
代码
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
Mat srcImg;
void on_trackbar(int pos)
{
Mat contourImg ;
srcImg.copyTo(contourImg);
Mat graysrcImg = Mat::zeros(srcImg.rows, srcImg.cols, CV_8UC3);
threshold(srcImg, graysrcImg, pos, 255, 3);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Mat dst = Mat::zeros(srcImg.rows, srcImg.cols, CV_8UC3);
findContours(graysrcImg, contours,hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
if( !contours.empty() && !hierarchy.empty() )
{
int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] )
{
if (contourArea(contours[idx]) < 500000) continue;
Scalar color( (rand()&255), (rand()&255), (rand()&255) );
drawContours( contourImg, contours, idx, Scalar(255, 0, 0), CV_FILLED, 8, hierarchy );
}
}
imshow( "FindContour", contourImg );
}
int main()
{
const char* srcImgFile = "D:/20170601092226.png";
srcImg = imread(srcImgFile);
if (srcImg.empty()) return -1;
Mat p_w_picpathRGB[3];
split(srcImg, p_w_picpathRGB);
for (int i = 0; i < 3; i++)
{
equalizeHist(p_w_picpathRGB[i], p_w_picpathRGB[i]);
}
merge(p_w_picpathRGB, 3, srcImg);
imshow("直方图均衡化图像增强效果", srcImg);
cvtColor( srcImg, srcImg, CV_RGB2GRAY );
namedWindow("srcImg", 1);
imshow("srcImg", srcImg);
int nThreshold = 0;
namedWindow("FindContour", 1);
cvCreateTrackbar("bar", "FindContour", &nThreshold, 254, on_trackbar);
on_trackbar(1);
waitKey(0);
return 0;
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。