opencv之clahe限制对比对自适应均衡之后检测车辆

代码展示

#include 
#include 
#include 
#include 
using namespace std;
using namespace cv;
void Entrance(Mat car,string camName,CascadeClassifier& face_cascade);
int main()
{
    CascadeClassifier face_cascade;
    string cascade_name = "1220_加入960Neg_桃树坪_2700.xml";
    //string cascade_name = "Pos3100_Neg7800_30stage.xml";
    //string cascade_name = "Pos3200_Neg7800_30stage.xml";
    //string cascade_name = "Pos3000_Neg7800_30stage.xml";
    if (!face_cascade.load(cascade_name))
    {
        printf("级联分类器错误,可能未找到文件,拷贝该文件到工程目录下!\n");
        //exit(1);
        return -1;
    }
    string str_array[] = {"QQ截图20170114154353.png","QQ截图20170114154433.png","QQ截图20170114154518.png","QQ截图20170114154614.png",
        "QQ截图20170114154628.png","QQ截图20170114154654.png","QQ截图20170114154716.png","QQ截图20170114154739.png","QQ截图20170114154831.png",
    "QQ截图20170114154847.png","QQ截图20170114154922.png","QQ截图20170114154942.png","QQ截图20170114155006.png"};
    vector<string>str_vec(str_array,str_array+13);
    for(int i = 0; i < str_vec.size();i++)
    {
        Mat img = imread(str_vec[i]);
        resize(img,img,Size(432,240));
        Entrance(img,(str_vec[i]),face_cascade);
        //imshow(str_vec[i],img);
        //waitKey(10);
    }
    system("pause");
    return 0;
}

void Entrance(Mat car,string camName,CascadeClassifier& face_cascade)
{
    std::vector cars;
    vector cars_filtered;
    Mat car_gray, small_img;
    double scale = 1.3;
    Point pt1, pt2;
    cvtColor(car, car_gray, CV_BGR2GRAY);  //rgb类型转换为灰度类型  
    resize(car_gray, small_img, Size(cvRound(car_gray.cols / scale), cvRound(car_gray.rows / scale)), 0, 0, CV_INTER_LINEAR);
    //equalizeHist(small_img, small_img);   //直方图均衡化  

    /////使用clahe自适应均衡
    Ptr clahe = createCLAHE();
    clahe->setClipLimit(4);
    clahe->setTilesGridSize(Size(4,4));
    clahe->apply(small_img, small_img);


    face_cascade.detectMultiScale(small_img,
        cars,
        1.1,
        5,
        CV_HAAR_DO_CANNY_PRUNING,
        Size(20, 20));

    ///////第一次滤除:滤除此存不合适的车辆
    for(vector::iterator it = cars.begin();it != cars.end();it++)
    {
        //if((cars[i].x + cars[i].width)*scale - cars[i].x*scale > 150)
        if(((*it).x+(*it).width)*scale  - (*it).x * scale > 120 
            || ((*it).x+(*it).width)*scale  - (*it).x * scale < 20)
        {
            cars.erase(it);
            it--;
        }
    }
    /////////第二次滤除:滤除嵌套的,留下最内层的
    int m,n;
    for (m = 0; mfor(n = 0; nif (n != m && (r & cars[n]) == r)
                break;
        if (n == cars.size())
            cars_filtered.push_back(r);
    }

    for (int i = 0; i < cars_filtered.size(); i++)
    {
        pt1.x = cars_filtered[i].x*scale;
        pt2.x = (cars_filtered[i].x + cars_filtered[i].width)*scale;
        pt1.y = cars_filtered[i].y*scale;
        pt2.y = (cars_filtered[i].y + cars_filtered[i].height)*scale;
        rectangle(car, pt1, pt2, CV_RGB(255, 255, 0), 2, 8, 0);

    }
    //string str_path = "C:\\Users\\Administrator\\Desktop\\检测效果不好_让大师姐测试\\自己检测之后_加入自适应均衡_3000\\";
    //string str_path = "C:\\Users\\Administrator\\Desktop\\检测效果不好_让大师姐测试\\自己检测之后_加入自适应均衡_3200\\";
    //string str_path = "C:\\Users\\Administrator\\Desktop\\检测效果不好_让大师姐测试\\自己检测之后_加入自适应均衡_3100\\";
    string str_path = "C:\\Users\\Administrator\\Desktop\\检测效果不好_让大师姐测试\\自己检测之后_不加自适应均衡_上次的分类器_加直方图均衡\\";

    imwrite(str_path+camName,car);
    //imshow(camName,car);
    //waitKey(10);
}

你可能感兴趣的:(opencv,图像处理)