OpenCV检测斑马线

转载地址:http://blog.csdn.net/dawn1227/article/details/64125542


简单思路:

1.提取感兴趣区域后

2.将图片二值化,通过白色像素点所占比判断前方是否有斑马线


//
//  main.cpp
//  test0
//
//  Created by 吴卓 on 16/10/6.
//  Copyright © 2016年 吴卓. All rights reserved.
//

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

#define cvQueryHistValue_1D( hist, idx0 ) \
((float)cvGetReal1D( (hist)->bins, (idx0)))

int bSums(Mat src)
{
    
    int counter = 0;
    //迭代器访问像素点
    Mat_<uchar>::iterator it = src.begin<uchar>();
    Mat_<uchar>::iterator itend = src.end<uchar>();
    for (; it!=itend; ++it)
    {
        if((*it)>0) counter+=1;//二值化后,像素点是0或者255
    }
    return counter;
}

int main()
{
    
    Mat image=imread("/Users/wuzhuo/Desktop/大创/斑马线1.jpg", CV_LOAD_IMAGE_COLOR);
    Mat I;
    cvtColor(image,I,CV_BGR2GRAY);
    
    Mat roi1(I, Rect(0,260,1024,100));
    threshold(roi1, roi1, 180, 255, CV_THRESH_BINARY);   //灰度变二值
    
    namedWindow("1", CV_WINDOW_AUTOSIZE);
    imshow("1", roi1);
    waitKey();
    
    int a = bSums(roi1);//调用函数bSums
    imshow("A",roi1);
    imwrite("/Users/wuzhuo/Desktop/斑马线1.jpg", roi1);
    
    if (a > 5000) {
        cout << "注意:前方有斑马线" << endl;
    }
    //cout << "A:" << a << endl;
    waitKey();
    
    return 0;
}


猜你喜欢

转载自blog.csdn.net/alisa_xf/article/details/78134335
今日推荐