您好,登錄后才能下訂單哦!
場景
嘗試使用直方圖對圖像進(jìn)行增強(qiáng),然后使用二值化函數(shù),分割出車輛的輪廓,顯然這個(gè)在應(yīng)對道路和車輛顏色相近的情況下,即使不是相近,依舊沒有達(dá)到任何的效果
代碼
#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("直方圖均衡化圖像增強(qiáng)效果", 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;
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。