opencv统计二值图黑白像素个数

#include "iostream"
#include "queue"
#include "Windows.h"

#include <opencv2/ml/ml.hpp>
#include "opencv2/opencv.hpp"
#include "Windows.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/calib3d/calib3d.hpp" 
#include "opencv2/ml.hpp"
#include "opencv/cv.h"
#include "opencv/ml.h"
#include "opencv/highgui.h"
#include "opencv/cvaux.h"
#include "opencv/cvwimage.h"
#include "opencv/cxcore.h"
#include "opencv/cxmisc.h"
#include "opencv2/cvconfig.h"
#define MAX 30

using namespace cv;
using namespace std;
int color[10000];


int main(int argc, char *argv[]) {
    cv::Mat frame;
    cv::Mat back;
    cv::Mat fore;
    //cv::VideoCapture cap("C:\\C_C++ EX8 code\\Video\\MyVideo.wmv");
    cv::VideoCapture cap(0);
    cv::Ptr<BackgroundSubtractorMOG2> bg = createBackgroundSubtractorMOG2();

    bg->setNMixtures(3);
    //bg.bShadowDetection = false;
    std::vector<std::vector<cv::Point> > contours;

    cv::namedWindow("Frame");
    cv::namedWindow("Background");

    while(1)
    {
        cap >> frame;
        //bg.operator()(frame, fore);
        bg->apply(frame, fore,0.01);
        
        cv::erode(fore, fore, cv::Mat());
        cv::dilate(fore, fore, cv::Mat());
        bg->getBackgroundImage(back);
        //cv::findContours(fore, contours, CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
        //cv::drawContours(frame, contours, -1, cv::Scalar(0, 0, 255), 2);
        threshold(fore, fore, 20, 250, CV_THRESH_BINARY_INV);
        //---------------------------------------------------------------
        int nRow = fore.rows;
        int nCol = fore.cols;
        //imshow("img", binaryimg);
        memset(color, 0, sizeof(color));
        for (int i = 0; i < nRow; i++)
        {
            uchar *data = fore.ptr<uchar>(i);
            for (int j = 0; j < nCol; j++)
            {
                if (*data == 0)
                    color[j]++;
                *data++;
            }
        }

        int high = 300;
        Mat histimg(high, nCol, CV_8UC3);
        for (int j = 0; j < nCol; j += 2)
        {
            line(histimg, Point(j, high - color[j]), Point(j, high), Scalar(0, 0, 250), 1);
        }
        imshow("Hist", histimg);
        //---------------------------------------------------------------
            
        //---------------------------------------------------------------
        cv::imshow("Foreground", fore);
        cv::imshow("Frame", frame);
        cv::imshow("Background", back);
        if (cv::waitKey(40) >= 0)
            break;
    }
    return 0;
}

 

posted @ 2016-03-30 15:50  一样菜  阅读(9643)  评论(0编辑  收藏  举报