• 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;
    }
  • 相关阅读:
    读写文件时遇到编码问题解决方法
    记录vim经常使用的几个命令
    linux 安装nginx+php+mysql
    php 几种排序模式
    rsync同步的艺术
    关于nuxt项目部署和更新
    springboot使用mybatis时报错Invalid bound statement (not found)
    Springboot 接受前端数据报错 "status": 415,"error": "Unsupported Media Type"
    youtube网页版 评论显示不出来
    [SPDK/NVMe存储技术分析]015
  • 原文地址:https://www.cnblogs.com/mypsq/p/5337500.html
Copyright © 2020-2023  润新知