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;
    }
  • 相关阅读:
    iOS开发者程序许可协议
    IOS升级之 Objective-c新特性
    ios 性能优化之自动化UI测试
    ios 性能优化之CPU性能调优
    ios 性能优化之图形性能测试
    ios 性能优化之测试 iOS设备I/O活动
    ios 性能优化之定位应用程序的内存问题
    ios 性能优化之app运行时数据收集
    IOS8 理解应用程序扩展是如何工作的
    IOS8 创建一个应用程序扩展
  • 原文地址:https://www.cnblogs.com/mypsq/p/5337500.html
Copyright © 2011-2022 走看看