利用opencv检测出矩形1

来源:互联网 发布:黑白网络网址 编辑:程序博客网 时间:2024/05/22 13:03

利用opencv检测出矩形

//#include "stdafx.h"  #include <opencv2/opencv.hpp>#include <iostream>#include <windows.h>using namespace cv;using namespace std;int* findRectInfo(std::vector<cv::Point> rect)    {    int rectInfo[4] = {0};    int x[4]= {0},y[4]= {0};    int maxX = 0,maxY = 0,minX = 2000,minY = 2000;    //get the rect points    for(int i=0;i<4;i++)    {        x[i] = rect[i].x;        y[i] = rect[i].y;        if(maxX<x[i])            maxX = x[i];        if(maxY<y[i])            maxY = y[i];        if(minX>x[i])            minX = x[i];        if(minY>y[i])            minY = y[i];    }    rectInfo[0] = minY;    rectInfo[1] = minX;    rectInfo[2] = maxY - minY;    rectInfo[3] = maxX - minX;    cout<<"minY="<<minY<<endl;    cout<<"minX="<<minX<<endl;    cout<<"maxY - minY="<<maxY - minY<<endl;    cout<<"maxX - minX="<<maxX - minX<<endl;    return rectInfo;// 得到矩形的左上角的坐标和矩形的边长}double angle(Point pt1, Point pt2, Point pt0){    double dx1 = pt1.x - pt0.x;    double dy1 = pt1.y - pt0.y;    double dx2 = pt2.x - pt0.x;    double dy2 = pt2.y - pt0.y;    return (dx1*dx2 + dy1*dy2) / sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);}//勾股定理验证是否满足直角的关系int main(int argc, char* argv[]){    VideoCapture cap;    cap.open(0);    if (!cap.isOpened())    {        return -1;    }    double w = 320, h = 320;    cap.set(CV_CAP_PROP_FRAME_WIDTH, w);    cap.set(CV_CAP_PROP_FRAME_HEIGHT, h);    namedWindow("Video");    bool stop = false;    Mat frame1,frame2,frame3;    //IplImage* image;    CvMemStorage* storage = NULL;    while (!stop)    {        cap>> frame1;        //image = &IplImage(frame);        //IplImage *img1 = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);        cvtColor(frame1, frame2, CV_BGR2GRAY);        GaussianBlur(frame2, frame3, cv::Size(5, 5), 0, 0);        //cvShowImage("AV", img1);        // Quantize the gray scale to 30 levels        int gbins = 16;        int histSize[] = { gbins };        // gray scale varies from 0 to 256        float granges[] = { 0,256 };        const float* ranges[] = { granges };        cv::MatND hist;        // we compute the histogram from the 0-th and 1-st channels        int channels[] = { 0 };        //calculate hist        calcHist(&frame2, 1, channels, cv::Mat(), // do not use mask            hist, 1, histSize, ranges,            true, // the histogram is uniform            false);        //find the max value of hist        double maxVal = 0;        minMaxLoc(hist, 0, &maxVal, 0, 0);        int scale = 20;        cv::Mat histImg;        histImg.create(500, gbins*scale, CV_8UC3);        //show gray scale of hist image        for (int g = 0; g<gbins; g++) {            float binVal = hist.at<float>(g, 0);            int intensity = cvRound(binVal * 255);            rectangle(histImg, cv::Point(g*scale, 0),                cv::Point((g + 1)*scale - 1, binVal / maxVal * 400),                CV_RGB(0, 0, 0),                CV_FILLED);        }        imshow("histImg", histImg);        // processing        Mat hsvRe;        threshold(frame3, hsvRe, 150, 255, cv::THRESH_BINARY);//阈值设定函数  形式:void cvThreshold( const CvArr* src, CvArr* dst, double threshold, double max_value, int threshold_type );        vector<vector<Point>> contours;  //contours指所有轮廓        vector<Vec4i> hierarchy;        // find         findContours(hsvRe, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);//找出图片中的所有轮廓        // draw         //Mat result(hsvRe.size(), CV_8U, Scalar(0));        //drawContours(result, contours, -1, Scalar(255), 2);        //namedWindow("contours");        //imshow("contours", result);        imshow("Video1", frame1);        imshow("Video2", frame2);        imshow("Video3", hsvRe);//.......................识别矩形.................................................//.vector<Point> approx;vector<vector<Point>> squares;for (size_t i = 0; i < contours.size(); i++)//表示所有的轮廓{    approxPolyDP(Mat(contours[i]), approx,                  arcLength(Mat(contours[i]), true)*0.02, true);    if (approx.size() == 4 &&        fabs(contourArea(Mat(approx))) > 1000 &&        isContourConvex(Mat(approx)))    {        double maxCosine = 0;        for( int j = 2; j < 5; j++ )        {            double cosine = fabs(angle(approx[j%4],approx[j-2], approx[j-1]));            maxCosine = MAX(maxCosine, cosine);        }        if( maxCosine < 0.1 )            squares.push_back(approx);    }}//...........................get rect from image...........................................    std::vector<int> compression_params;    compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);    compression_params.push_back(9);    Mat image(3210,3210,CV_8UC3,Scalar(0)); for(int i=0;i<squares.size();i++)    {        int rect[4],*tmp;        tmp = findRectInfo(squares[i]);        cout<<i<<endl;        for(int j=0;j<4;j++)        {            rect[j] = *(tmp+j);        }         cv::Rect roi(rect[1],rect[0],rect[3],rect[2]);         cout<<"w:"<<roi.width<<endl;         cout<<"w:"<<roi.height<<endl;        //cv::Mat roi_of_image = image(roi);    }        /*char * filename = new char[100];        sprintf(filename,"F:\\Temp\\%i.png",i);        imshow("Video4", roi_of_image);        cv::imwrite(filename,roi_of_image,compression_params);*/   /* }*/    //...............................        //storage = cvCreateMemStorage(0);        //cvNamedWindow(wndname, 1);        // find and draw the squares        //cvReleaseImage(&image);        //cvClearMemStorage(storage);        if (waitKey(27) >= 0)//Esc键退出            stop = true;    }    return 0;}
 这个程序是自己在网上各种查阅和拼凑在一起能检测出矩形的程序。
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 摩拜单车怎么办月卡 出国忘了带护照怎么办 雅漾喷雾失压了怎么办 洗衣服时衣服粘上卫生纸怎么办 一晚上卫生巾都是满的怎么办 宝宝头上痱子痒怎么办 短裤里的宽松紧带拧了怎么办 肉色内衣被染黑色了怎么办 安全裤总往上缩怎么办 夏天穿裙子膝盖怕凉怎么办 夏天穿裙子膝盖冷怎么办 天凉嗓子痒咳嗽怎么办 棉服里面跑毛怎么办 棉衣里面的棉一块一块的怎么办 穿姨妈巾悟出痱子怎么办 穿裙子上衣太长了怎么办 微信封号了零钱怎么办 快递加盟商欠我工资怎么办 加盟费交了以后怎么办 加盟总部违约加盟商该怎么办 自行车锁钥匙丢了怎么办 假体隆胸肿胀痛怎么办 恶露60天不干净怎么办 剖腹产俩月恶露不干净怎么办 剖腹产恶露一个多月还没干净怎么办 小月子全身流虚汗怎么办 生完孩子严重便秘怎么办啊 产后第5天恶露少怎么办 有恶露排不出来怎么办 刚生完小孩便秘怎么办 产后15天有鲜血怎么办 恶露未净同房了怎么办 药流2月恶露不尽怎么办 生完孩子小腹痛怎么办 月子里恶露有异味怎么办 恶露干净了有异味怎么办 顺产后恶露白色异味怎么办 产妇排出的恶露有异味怎么办 恶露有股腥臭味怎么办 生完孩子下面有异味怎么办 销售货物开票税率错了怎么办