zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    影响历史进程的三只苹果
    C语言实现的RSA算法程序(使用GMP)
    C++实现的大整数分解Pollard's rho算法程序
    Java实现的大整数分解Pollard's rho算法程序
    素性测试算法
    素性测试AKS算法程序
    C++实现的Miller-Rabin素性测试程序
    为什么天朝互联网的三巨头是BAT
    KMP模式匹配算法程序(Python,C++,C)
    I00014 汉若塔问题的C++程序
  • 原文地址:https://www.cnblogs.com/mypsq/p/5337500.html
Copyright © 2011-2022 走看看