【问题标题】:OpenCV C++ Mat to IntegerOpenCV C++ Mat 转整数
【发布时间】:2014-12-18 07:11:56
【问题描述】:

我对 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;
}

提前致谢!

【问题讨论】:

    标签: c++ xcode macos opencv video-processing


    【解决方案1】:

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

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

    这是怎么用的,

    int cal = countNonZero(image);
    

    根据您的代码更改image

    【讨论】:

      猜你喜欢
      • 2014-01-28
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      • 2014-10-21
      • 1970-01-01
      相关资源
      最近更新 更多