首页 文章

OpenCV C颜色检测和打印Mac

提问于
浏览
-1

我是OpenCV的新手,正在从事视频分析项目 . 基本上,我想将我的网络摄像头分成两侧(左侧和右侧),并且已经想出如何做到这一点 . 但是,我还想分析每一面的红色和绿色,并打印出红色/绿色的像素数量 . 我必须经历过每一个可能的博客来解决这个问题,但是它仍然无效 . 下面的代码运行,但不是检测红色,因为代码可能暗示它似乎拾取白色(所有光源和白色墙壁) . 我花了几个小时梳理代码但仍无法找到解决方案 . 请帮忙!另请注意,这是通过Xcode在OSX 10.8上运行的 . 谢谢!

#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 threshold;

        //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),threshold);
        imshow("HSVLeftRed",threshold);

        //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),threshold);
        imshow("HSVLeftGreen",threshold);
    }
    return 0;
}

1 回答

  • 0

    你正在裁剪一个640x720的区域,这可能不完全适合你的内容 . 提示:使用 capture.get(CAP_PROP_FRAME_WIDTH)capture.get(CAP_PROP_FRAME_HEIGHT) 检查实际捕获分辨率 . 你可能想要考虑 Mat threshold - > Mat thresholded . 这只是一些咆哮:)

    我怀疑的是实际问题是你用于HSV的门槛 . 根据cvtolor,关于RGB到HSV转换的部分,

    输出0 <= V <= 1 .

    所以你应该使用代表你的V阈值的浮点数,即 150 -> 150/255 ~= 0.58 等 .

相关问题