C++ OpenCV C++;Mat到整数

C++ OpenCV C++;Mat到整数,c++,xcode,macos,opencv,video-processing,C++,Xcode,Macos,Opencv,Video Processing,我是OpenCV的新手,所以请容忍我。我正在用OSX 10.8运行一台Mac Mini。我有一个程序,可以识别颜色,并以二进制图片(黑白)显示它们。但是,我想将白色像素的数量存储为整数(或浮点等),以便与其他像素数量进行比较。我该怎么做?这是我目前的代码- #include <iostream> #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2

我是OpenCV的新手,所以请容忍我。我正在用OSX 10.8运行一台Mac Mini。我有一个程序,可以识别颜色,并以二进制图片(黑白)显示它们。但是,我想将白色像素的数量存储为整数(或浮点等),以便与其他像素数量进行比较。我该怎么做?这是我目前的代码-

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    VideoCapture cap(0); //capture the video from webcam

    if ( !cap.isOpened() )  // if not success, exit program
    {
        cout << "Cannot open the web cam" << endl;
        return -1;
    }

    namedWindow("HSVLeftRed", CV_WINDOW_AUTOSIZE);
    namedWindow("HSVLeftGreen", CV_WINDOW_AUTOSIZE);


    while (true) {

        Mat image;
        cap.read(image);
        Mat HSV;
        Mat leftgreen;
        Mat leftred;

        //Left Cropping
        Mat leftimg = image(Rect(0, 0, 640, 720));       

        //Left Red Detection
        cvtColor(leftimg,HSV,CV_BGR2HSV);
        inRange(HSV,Scalar(0,0,150),Scalar(0,0,255), leftgreen);
        //imshow("HSVLeftRed", leftgreen);
        //print pixel type    

        //Left Green Detection
        cvtColor(leftimg,HSV,CV_BGR2HSV);
        inRange(HSV,Scalar(still need to find proper min values),Scalar(still need to find proper max values), leftgreen);
        //imshow("HSVLeftGreen", leftgreen);
        //compare pixel types
    }
    return 0;
}
#包括
#包括“opencv2/highgui/highgui.hpp”
#包括“opencv2/imgproc/imgproc.hpp”
#包括“opencv2/core/core.hpp”
使用名称空间cv;
使用名称空间std;
int main(int argc,字符**argv)
{
VideoCapture cap(0);//从网络摄像头捕获视频
如果(!cap.isopend())//如果不成功,退出程序
{

cout要计算非零像素,OpenCV有这个函数
cv::countNonZero
。它需要输入图像,其非零像素的数量,我们要计算并输出非零像素的数量(
int
)。这是文档

在您的情况下,由于所有像素都是黑色或白色,因此所有非零像素都将是白色像素

这就是如何使用它

int cal = countNonZero(image);
根据您的代码更改图像