Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenCV:如何解释inRange的结果?_C++_Opencv_Feature Detection_Color Detection - Fatal编程技术网

C++ OpenCV:如何解释inRange的结果?

C++ OpenCV:如何解释inRange的结果?,c++,opencv,feature-detection,color-detection,C++,Opencv,Feature Detection,Color Detection,我正在处理视频图像,我想检测视频是否包含特定范围的红色像素。这可能吗 以下是我根据教程改编的代码: #ifdef __cplusplus - (void)processImage:(Mat&)image; { cv::Mat orig_image = image.clone(); cv::medianBlur(image, image, 3); cv::Mat hsv_image; cv::cvtColor(image, hsv_image, cv::CO

我正在处理视频图像,我想检测视频是否包含特定范围的红色像素。这可能吗

以下是我根据教程改编的代码:

#ifdef __cplusplus
- (void)processImage:(Mat&)image;
{
    cv::Mat orig_image = image.clone();
    cv::medianBlur(image, image, 3);
    cv::Mat hsv_image;
    cv::cvtColor(image, hsv_image, cv::COLOR_BGR2HSV);
    cv::Mat lower_red_hue_range;
    cv::Mat upper_red_hue_range;
    cv::inRange(hsv_image, cv::Scalar(0, 100, 100), cv::Scalar(10, 255, 255), lower_red_hue_range);
    cv::inRange(hsv_image, cv::Scalar(160, 100, 100), cv::Scalar(179, 255, 255), upper_red_hue_range);
    // Interpret values here
}
解读价值观


我想检测inRange操作的结果是否为零。换句话说,我想了解在原始图像中是否有任何匹配的像素,其颜色在给定的上下红色刻度范围内。如何解释结果?

首先,您需要了解或使用上下遮罩:

Mat mask = lower_red_hue_range | upper_red_hue_range;
然后您可以
countNonZero
查看是否存在非零像素(即,您发现了一些东西)

最好先应用形态侵蚀或开口去除小(可能有噪音)斑点:

或者查找连接的组件(
findContours
connectedComponentsWithStats
),并根据某些条件删除/搜索:

vector<vector<Point>> contours
findContours(mask.clone(), contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

double threshold_on_area = 100.0;
for(int i=0; i<contours.size(); ++i)
{
    double area = countourArea(contours[i]);
    if(area < threshold_on_area)
    {
        // don't consider this contour
        continue;
    } 
    else
    {
        // do something (e.g. drawing a bounding box around the contour)
        Rect box = boundingRect(contours[i]);
        rectangle(hsv_image, box, Scalar(0, 255, 255));
    }
}
矢量轮廓
findContours(mask.clone()、轮廓、外部重建、链近似简单);
双阈值_面积=100.0;

对于(int i=0;i首先需要或上下遮罩。然后您可以
countNonZero
查看是否存在非零像素(即,您发现了一些东西)。最好先应用形态学腐蚀或打开来移除小的(可能有噪声的)斑点,或查找连接的组件(
findContours
connectedComponentsWithStats
)并根据一些标准进行删减/搜索谢谢。我知道这很蹩脚,但请你能添加一些示例代码吗?这是我的第一个OpenCV测试项目,我正在努力在这么短的时间内熟悉所有这些函数和概念。我发布了一个答案。这应该可以让你开始。如果你有更精确的要求请把它们添加到你的问题中,我会尽量做出更准确的回答。
Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
morphologyEx(mask, mask, MORPH_OPEN, kernel); // or MORPH_ERODE
vector<vector<Point>> contours
findContours(mask.clone(), contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

double threshold_on_area = 100.0;
for(int i=0; i<contours.size(); ++i)
{
    double area = countourArea(contours[i]);
    if(area < threshold_on_area)
    {
        // don't consider this contour
        continue;
    } 
    else
    {
        // do something (e.g. drawing a bounding box around the contour)
        Rect box = boundingRect(contours[i]);
        rectangle(hsv_image, box, Scalar(0, 255, 255));
    }
}