【问题标题】:How this opencv function explained in c++如何在 C++ 中解释这个 opencv 函数
【发布时间】:2016-04-17 08:05:52
【问题描述】:

我确实需要特别了解这个函数的工作原理,也许在文档中的所有函数中也是如此。这是来自 opencv 中函数的文档:这只是其中之一。

int cv::createTrackbar (const String & trackbarname, const String & winname, int * value, int count, TrackbarCallback onChange = 0,void * userdata = 0)

这是某人编写的有效代码:

createTrackbar("H_MIN", trackbarWindowName, &H_MIN, H_MAX, on_trackbar); createTrackbar("H_MAX", trackbarWindowName, &H_MAX, H_MAX, on_trackbar); createTrackbar("S_MIN", trackbarWindowName, &S_MIN, S_MAX, on_trackbar); createTrackbar("S_MAX", trackbarWindowName, &S_MAX, S_MAX, on_trackbar); createTrackbar("V_MIN", trackbarWindowName, &V_MIN, V_MAX, on_trackbar); createTrackbar("V_MAX", trackbarWindowName, &V_MAX, V_MAX, on_trackbar);

现在我的问题是:

  1. 有6个参数,为什么那家伙只写5个参数?
  2. 文档起初说 int,为什么 createTrackbar 没有 int ?
  3. &const String & trackbarname 中是什么意思,我可以理解"H_MIN" 是一个常量字符串,它是一个trackbarname,但是& 是什么意思,为什么不在那个代码中写& ?
  4. const String & winname 中也有同样的问题,即& 是什么意思?
  5. 在第三个参数int * value,值指针指向一个int,并声明在全局变量中,为什么写得这么复杂?我们不能只写 int H_MIN = 0(如果我是对的)H_MAX = 256 吗?
  6. 在第四个参数int count 上,该代码到底在哪里?
  7. 第五个参数,TrackbarCallback onChange = 0,这个我不懂
  8. 最后一个参数,void * userdata = 0 他写了一个名为on_trackbar 的函数,只有这个:void on_trackbar(int, void*){} 我也不明白这个

抱歉问的太多了,我只需要真正了解我将如何从文档中正确或自定义编写函数。

这里是所有代码:

//objectTrackingTutorial.cpp

//Written by  Kyle Hounslow 2013

//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software")
//, to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
//and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
//IN THE SOFTWARE.

#include <sstream>
#include <string>
#include <iostream>
#include <opencv\highgui.h>
#include <opencv\cv.h>
#include <opencv2\opencv.hpp>
#include <vector>


using namespace cv;
using namespace std;
//initial min and max HSV filter values.
//these will be changed using trackbars
int H_MIN = 0;
int H_MAX = 256;
int S_MIN = 0;
int S_MAX = 256;
int V_MIN = 0;
int V_MAX = 256;
//default capture width and height
const int FRAME_WIDTH = 640;
const int FRAME_HEIGHT = 480;
//max number of objects to be detected in frame
const int MAX_NUM_OBJECTS = 50;
//minimum and maximum object area
const int MIN_OBJECT_AREA = 20 * 20;
const int MAX_OBJECT_AREA = FRAME_HEIGHT*FRAME_WIDTH / 1.5;
//names that will appear at the top of each window
const string windowName = "Original Image";
const string windowName1 = "HSV Image";
const string windowName2 = "Thresholded Image";
const string windowName3 = "After Morphological Operations";
const string trackbarWindowName = "Trackbars";
void on_trackbar(int, void*)
{//This function gets called whenever a
    // trackbar position is changed





}
string intToString(int number){


    std::stringstream ss;
    ss << number;
    return ss.str();
}
void createTrackbars(){
    //create window for trackbars


    namedWindow(trackbarWindowName, 0);
    //create memory to store trackbar name on window
    char TrackbarName[50];
    sprintf_s(TrackbarName, "H_MIN", H_MIN);
    sprintf_s(TrackbarName, "H_MAX", H_MAX);
    sprintf_s(TrackbarName, "S_MIN", S_MIN);
    sprintf_s(TrackbarName, "S_MAX", S_MAX);
    sprintf_s(TrackbarName, "V_MIN", V_MIN);
    sprintf_s(TrackbarName, "V_MAX", V_MAX);
    //create trackbars and insert them into window
    //3 parameters are: the address of the variable that is changing when the trackbar is moved(eg.H_LOW),
    //the max value the trackbar can move (eg. H_HIGH), 
    //and the function that is called whenever the trackbar is moved(eg. on_trackbar)
    //                                  ---->    ---->     ---->      
    createTrackbar("H_MIN", trackbarWindowName, &H_MIN, H_MAX, on_trackbar);
    createTrackbar("H_MAX", trackbarWindowName, &H_MAX, H_MAX, on_trackbar);
    createTrackbar("S_MIN", trackbarWindowName, &S_MIN, S_MAX, on_trackbar);
    createTrackbar("S_MAX", trackbarWindowName, &S_MAX, S_MAX, on_trackbar);
    createTrackbar("V_MIN", trackbarWindowName, &V_MIN, V_MAX, on_trackbar);
    createTrackbar("V_MAX", trackbarWindowName, &V_MAX, V_MAX, on_trackbar);

}
void drawObject(int x, int y, Mat &frame){

    //use some of the openCV drawing functions to draw crosshairs
    //on your tracked image!

    //UPDATE:JUNE 18TH, 2013
    //added 'if' and 'else' statements to prevent
    //memory errors from writing off the screen (ie. (-25,-25) is not within the window!)

    circle(frame, Point(x, y), 20, Scalar(0, 255, 0), 2);
    if (y - 25>0)
        line(frame, Point(x, y), Point(x, y - 25), Scalar(0, 255, 0), 2);
    else line(frame, Point(x, y), Point(x, 0), Scalar(0, 255, 0), 2);
    if (y + 25<FRAME_HEIGHT)
        line(frame, Point(x, y), Point(x, y + 25), Scalar(0, 255, 0), 2);
    else line(frame, Point(x, y), Point(x, FRAME_HEIGHT), Scalar(0, 255, 0), 2);
    if (x - 25>0)
        line(frame, Point(x, y), Point(x - 25, y), Scalar(0, 255, 0), 2);
    else line(frame, Point(x, y), Point(0, y), Scalar(0, 255, 0), 2);
    if (x + 25<FRAME_WIDTH)
        line(frame, Point(x, y), Point(x + 25, y), Scalar(0, 255, 0), 2);
    else line(frame, Point(x, y), Point(FRAME_WIDTH, y), Scalar(0, 255, 0), 2);

    putText(frame, intToString(x) + "," + intToString(y), Point(x, y + 30), 1, 1, Scalar(0, 255, 0), 2);

}
void morphOps(Mat &thresh){

    //create structuring element that will be used to "dilate" and "erode" image.
    //the element chosen here is a 3px by 3px rectangle

    Mat erodeElement = getStructuringElement(MORPH_RECT, Size(3, 3));
    //dilate with larger element so make sure object is nicely visible
    Mat dilateElement = getStructuringElement(MORPH_RECT, Size(8, 8));

    erode(thresh, thresh, erodeElement);
    erode(thresh, thresh, erodeElement);


    dilate(thresh, thresh, dilateElement);
    dilate(thresh, thresh, dilateElement);



}
void trackFilteredObject(int &x, int &y, Mat threshold, Mat &cameraFeed){

    Mat temp;
    threshold.copyTo(temp);
    //these two vectors needed for output of findContours
    vector< vector<Point> > contours;
    vector<Vec4i> hierarchy;
    //find contours of filtered image using openCV findContours function
    findContours(temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
    //use moments method to find our filtered object
    double refArea = 0;
    bool objectFound = false;
    if (hierarchy.size() > 0) {
        int numObjects = hierarchy.size();
        //if number of objects greater than MAX_NUM_OBJECTS we have a noisy filter
        if (numObjects<10){
            for (int index = 0; index >= 0; index = hierarchy[index][0]) {

                Moments moment = moments((cv::Mat)contours[index]);
                double area = moment.m00;

                //if the area is less than 20 px by 20px then it is probably just noise
                //if the area is the same as the 3/2 of the image size, probably just a bad filter
                //we only want the object with the largest area so we safe a reference area each
                //iteration and compare it to the area in the next iteration.
                if (area>400 && area<FRAME_HEIGHT*FRAME_WIDTH/1.5 && area>refArea){
                    printf("%f \n", area);
                    x = moment.m10 / area;
                    y = moment.m01 / area;
                    objectFound = true;
                    refArea = area;
                }
                else objectFound = false;


            }
            //let user know you found an object
            if (objectFound == true){
                putText(cameraFeed, "Tracking Object", Point(0, 50), 2, 1, Scalar(0, 255, 0), 2);
                //draw object location on screen
                drawObject(x, y, cameraFeed);
            }

        }
        else putText(cameraFeed, "TOO MUCH NOISE! ADJUST FILTER", Point(0, 50), 1, 2, Scalar(0, 0, 255), 2);
    }
}
int main(int argc, char* argv[])
{
    //some boolean variables for different functionality within this
    //program
    bool trackObjects = true;
    bool useMorphOps = true;
    //Matrix to store each frame of the webcam feed
    Mat cameraFeed;
    //matrix storage for HSV image
    Mat HSV;
    //matrix storage for binary threshold image
    Mat threshold;
    //x and y values for the location of the object
    int x = 0, y = 0;
    //create slider bars for HSV filtering
    createTrackbars();
    //video capture object to acquire webcam feed
    VideoCapture capture;
    //open capture object at location zero (default location for webcam)
    capture.open(0);
    //set height and width of capture frame
    capture.set(CV_CAP_PROP_FRAME_WIDTH, FRAME_WIDTH);
    capture.set(CV_CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT);
    //start an infinite loop where webcam feed is copied to cameraFeed matrix
    //all of our operations will be performed within this loop
    while (1){
        //store image to matrix
        capture.read(cameraFeed);
        //convert frame from BGR to HSV colorspace
        cvtColor(cameraFeed, HSV, COLOR_BGR2HSV);
        //filter HSV image between values and store filtered image to
        //threshold matrix
        inRange(HSV, Scalar(H_MIN, S_MIN, V_MIN), Scalar(H_MAX, S_MAX, V_MAX), threshold);
        //perform morphological operations on thresholded image to eliminate noise
        //and emphasize the filtered object(s)
        if (useMorphOps)
            morphOps(threshold);
        //pass in thresholded frame to our object tracking function
        //this function will return the x and y coordinates of the
        //filtered object
        if (trackObjects)
            trackFilteredObject(x, y, threshold, cameraFeed);

        //show frames 
        imshow(windowName2, threshold);
        imshow(windowName, cameraFeed);
        imshow(windowName1, HSV);


        //delay 30ms so that screen can refresh.
        //image will not appear without this waitKey() command
        waitKey(30);
    }

    return 0;
}

【问题讨论】:

  • 有 6 个参数,为什么那家伙只写了 5 个参数 -- 函数默认了最后两个参数。这是 C++ 基础知识。您的其他一些问题也涉及 C++ 的基础知识——也许您应该花一些时间阅读有关 C++ 的书籍和/或教程?
  • “& 是什么意思”& 表示通过引用传递变量而不是复制它。这样就可以在函数中更改调用变量。如果通过引用传递 const 变量,这通常是出于性能原因,因为复制值需要一些时间。

标签: c++ function opencv


【解决方案1】:

按顺序:

1) 最后一个参数 userdata 是默认的,这可以通过函数签名中的 =0 看到。这意味着如果您不为参数指定值,它将被设置为默认值 0。

2) createTrackbar 的返回类型为 int,但是,您通过不将 createTrackbar 设置为变量来忽略此数据,这在您的情况下很好。

3) & 表示通过引用传递。这意味着您传递给函数的变量可以在函数执行期间进行修改。请参阅 here 了解更多信息。

4)同上

5) 这不一定是全局的。这里发生的是您将整数的内存地址传递给函数。这将允许函数取消引用该内存地址处的内容。如果您不熟悉 c 和 c++ 如何处理指针/引用/内存,您应该考虑对其进行一些研究,因为它是 c 编程中一个非常完整的概念。一个好的起点是here。就这个变量的作用而言,本质上,它是您可以引用的变量的地址,它会随着滑块的位置而更新,因此如果您只抓取滑块,则无需每次都调用滑块函数滑块的值。

6) 计数是滑块上的最大位置。这在文档中很清楚。

7) 这个 onchange 方法是一个函数的回调,只要轨迹栏位置发生变化,就可以执行该函数。默认为0,意思是如果你没有指定在trackbar移动时调用的函数,那么就不会调用任何函数。如果您只是想获取轨迹栏的值,请参阅我为第 5 点编写的内容,并改用 value 参数。

8) 这不是 userdata 参数。编译器能够看到 on_trackbar 实际上是 onchange 的参数,因为它的类型。因此,他实际上是将用户数据默认为0,并且在轨迹栏移动时具有功能。有关这方面的更多信息,您应该阅读 c 中的编译器时多态性。很棒的帖子here

看起来您还没有完全掌握 C 和 C++ 基础知识。我建议您阅读一本关于 C 或 C++ 的书,因为您可以使用您谈到的一些概念来做一些非常强大的东西。祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多