OpenCV inRange()函数

时间:2016-06-28 16:50:23

标签: c++ opencv color-detection

我尝试运行以下代码:

#include<opencv\cv.h>
#include<opencv\highgui.h>

using namespace cv;


int main() {

    VideoCapture cap;
    cap.open(0);


    while (1) {

Mat src;
Mat threshold;

cap.read(src);

inRange(src, Scalar(0, 0, 0), Scalar(255, 0, 0), threshold);
imshow("thr", threshold);
imshow("hsv", src);

        waitKey(33);
    }
    return 0;
}

但似乎它没有过滤,因为我运行代码时只出现一个空白窗口。

如何让代码检测红色?

1 个答案:

答案 0 :(得分:2)

你必须像这样修改inRange函数:

inRange(src, Scalar(0, 0, 0), Scalar(255, 255, 255), threshold);

如果您尝试仅限制第一个频道(蓝色频道),则必须将其他频道设为空闲,因此请将其设置为0及其lawerb中的dtype255

通常为np.uint8

E.g。

inRange(src, Scalar(0, 50, 0), Scalar(255, 100, 255), threshold);

此行将比较第二个频道(绿色频道)并忽略其他频道。