OpenCV使用inRange的閾值操作

2018-09-06 11:58 更新

目標(biāo)

在本教程中,您將學(xué)習(xí)如何:

  • 使用OpenCV函數(shù)cv :: inRange執(zhí)行基本閾值操作
  • 根據(jù)其具有的像素值的范圍來檢測對象

理論

  • 在上一個教程中,我們了解了如何使用cv :: threshold函數(shù)執(zhí)行閾值處理。
  • 在本教程中,我們將學(xué)習(xí)如何使用cv :: inRange函數(shù)。
  • 該概念保持不變,但現(xiàn)在我們添加了我們需要的像素值范圍。

教程代碼如下所示。您也可以從這里下載

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
#include <stdlib.h>
using namespace std;
using namespace cv;
void on_low_r_thresh_trackbar(int, void *);
void on_high_r_thresh_trackbar(int, void *);
void on_low_g_thresh_trackbar(int, void *);
void on_high_g_thresh_trackbar(int, void *);
void on_low_b_thresh_trackbar(int, void *);
void on_high_b_thresh_trackbar(int, void *);
int low_r=30, low_g=30, low_b=30;
int high_r=100, high_g=100, high_b=100;
int main()
{
    Mat frame, frame_threshold;
    VideoCapture cap(0);
    namedWindow("Video Capture", WINDOW_NORMAL);
    namedWindow("Object Detection", WINDOW_NORMAL);
    //-- Trackbars to set thresholds for RGB values
    createTrackbar("Low R","Object Detection", &low_r, 255, on_low_r_thresh_trackbar);
    createTrackbar("High R","Object Detection", &high_r, 255, on_high_r_thresh_trackbar);
    createTrackbar("Low G","Object Detection", &low_g, 255, on_low_g_thresh_trackbar);
    createTrackbar("High G","Object Detection", &high_g, 255, on_high_g_thresh_trackbar);
    createTrackbar("Low B","Object Detection", &low_b, 255, on_low_b_thresh_trackbar);
    createTrackbar("High B","Object Detection", &high_b, 255, on_high_b_thresh_trackbar);
    while((char)waitKey(1)!='q'){
        cap>>frame;
        if(frame.empty())
            break;
        //-- Detect the object based on RGB Range Values
        inRange(frame,Scalar(low_b,low_g,low_r), Scalar(high_b,high_g,high_r),frame_threshold);
        //-- Show the frames
        imshow("Video Capture",frame);
        imshow("Object Detection",frame_threshold);
    }
    return 0;
}
void on_low_r_thresh_trackbar(int, void *)
{
    low_r = min(high_r-1, low_r);
    setTrackbarPos("Low R","Object Detection", low_r);
}
void on_high_r_thresh_trackbar(int, void *)
{
    high_r = max(high_r, low_r+1);
    setTrackbarPos("High R", "Object Detection", high_r);
}
void on_low_g_thresh_trackbar(int, void *)
{
    low_g = min(high_g-1, low_g);
    setTrackbarPos("Low G","Object Detection", low_g);
}
void on_high_g_thresh_trackbar(int, void *)
{
    high_g = max(high_g, low_g+1);
    setTrackbarPos("High G", "Object Detection", high_g);
}
void on_low_b_thresh_trackbar(int, void *)
{
    low_b= min(high_b-1, low_b);
    setTrackbarPos("Low B","Object Detection", low_b);
}
void on_high_b_thresh_trackbar(int, void *)
{
    high_b = max(high_b, low_b+1);
    setTrackbarPos("High B", "Object Detection", high_b);
}

說明

1、我們來看一下程序的一般結(jié)構(gòu):

  • 創(chuàng)建兩個Matrix元素來存儲幀
    Mat frame, frame_threshold;

  • 從默認(rèn)捕獲設(shè)備捕獲視頻流。

    VideoCapture cap(0);

  • 創(chuàng)建一個窗口以顯示默認(rèn)幀和閾值幀。

    namedWindow("Video Capture", WINDOW_NORMAL);
    namedWindow("Object Detection", WINDOW_NORMAL);

  • 創(chuàng)建軌跡設(shè)置RGB值的范圍

    //-- Trackbars to set thresholds for RGB values
    createTrackbar("Low R","Object Detection", &low_r, 255, on_low_r_thresh_trackbar);
    createTrackbar("High R","Object Detection", &high_r, 255, on_high_r_thresh_trackbar);
    createTrackbar("Low G","Object Detection", &low_g, 255, on_low_g_thresh_trackbar);
    createTrackbar("High G","Object Detection", &high_g, 255, on_high_g_thresh_trackbar);
    createTrackbar("Low B","Object Detection", &low_b, 255, on_low_b_thresh_trackbar);
    createTrackbar("High B","Object Detection", &high_b, 255, on_high_b_thresh_trackbar);

  • 直到用戶想要退出程序才能執(zhí)行以下操作

        cap>>frame;
        if(frame.empty())
            break;
        //-- Detect the object based on RGB Range Values
        inRange(frame,Scalar(low_b,low_g,low_r), Scalar(high_b,high_g,high_r),frame_threshold);

  • 顯示圖像

        //-- Show the frames
        imshow("Video Capture",frame);
        imshow("Object Detection",frame_threshold);

  • 對于控制較低范圍的軌跡欄,例如紅色值:

void on_low_r_thresh_trackbar(int, void *)
{
    low_r = min(high_r-1, low_r);
    setTrackbarPos("Low R","Object Detection", low_r);
}

  • 對于控制上限的軌跡欄,例如紅色值:

void on_high_r_thresh_trackbar(int, void *)
{
    high_r = max(high_r, low_r+1);
    setTrackbarPos("High R", "Object Detection", high_r);
}
  • 有必要找到最大值和最小值,以避免諸如閾值的高值變得低于低值的差異。

結(jié)果

  1. 編譯此程序后,運(yùn)行它。該程序?qū)⒋蜷_兩個窗口
  2. 當(dāng)您從軌跡欄設(shè)置RGB范圍值時(shí),結(jié)果框架將在另一個窗口中可見。

OpenCV使用inRange的閾值操作

OpenCV使用inRange的閾值操作

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號