python统计图片中黑白像素个数_opencv统计二值图黑白像素个数

#include "iostream"#include"queue"#include"Windows.h"#include#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 namespacecv;using namespacestd;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 bg = createBackgroundSubtractorMOG2();

bg->setNMixtures(3);

//bg.bShadowDetection = false;

std::vector<:vector> > 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(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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值