1. createTrackbar是Opencv中的API,其可在显示图像的窗口中快速创建一个滑动控件,用于手动调节阈值,具有非常直观的效果。具体定义如下:

    CV_EXPORTS int createTrackbar(const string& trackbarname, const string& winname,
                                  int* value, int count,
                                  TrackbarCallback onChange = 0,
                                  void* userdata = 0);

形式参数一、trackbarname:滑动空间的名称;

形式参数二、winname:滑动空间用于依附的图像窗口的名称;

形式参数三、value:初始化阈值;

形式参数四、count:滑动控件的刻度范围;

形式参数五、TrackbarCallback是回调函数,其定义如下:

typedef void (CV_CDECL *TrackbarCallback)(int pos, void* userdata);

参考: https://blog.csdn.net/mysee1989/article/details/41379817 https://docs.opencv.org/3.4.1/dc/dfa/Morphology_1_8cpp-example.html#a20

2. Rect 是矩形类,成员变量x、y、width、height,分别为左上角点的坐标和矩形的宽和高。常用的成员函数有Size()返回值为一个Size,area()返回矩形的面积,contains(Point)用来判断点是否在矩形内,inside(Rect)函数判断矩形是否在该矩形内,tl()返回左上角点坐标,br()返回右下角点坐标。

Rect类的使用

    rect = rect ± point (shifting a rectangle by a certain offset)
    rect = rect ± size (expanding or shrinking a rectangle by a certain amount)
    rect += point, rect -= point, rect += size, rect -= size (augmenting operations)
    rect = rect1 & rect2 (rectangle intersection)
    rect = rect1 | rect2 (minimum area rectangle containing rect1 and rect2 )
    rect &= rect1, rect |= rect1 (and the corresponding augmenting operations)
    rect == rect1, rect != rect1 (rectangle comparison)

参考: https://www.cnblogs.com/happyamyhope/p/9844629.html

3. copyTo 是深拷贝,但是要根据大小信息,决定是否重新申请空间,clone 不管大小信息,全部重新申请空间进行深拷贝。

参考: https://blog.csdn.net/u013806541/article/details/70154719

4. opencv 提供的字符串格式化如下:

string formated_str = format("I have made %d dollars on this product.", 500);

参考: https://blog.csdn.net/yiyeshuanglinzui/article/details/108388683

5. 获取矩阵的行,列,位数。

Mat(int rows, int cols, int type), 直接使用属性就行。

m.rows
m.cols

参考: https://blog.csdn.net/renweiyi1487/article/details/101616758

6. 添加文字, putText

void cv::putText    (   InputOutputArray    img,
        const String &      text,
        Point   org,
        int     fontFace,
        double      fontScale,
        Scalar      color,
        int     thickness = 1,
        int     lineType = LINE_8,
        bool    bottomLeftOrigin = false 
    )   
    cv::putText(image, text, origin, font_face, font_scale, cv::Scalar(0, 255, 255), thickness, 8, 0);

参考:https://docs.opencv.org/3.4/d6/d6e/group__imgproc__draw.html#ga5126f47f883d730f633d74f07456c576 https://blog.csdn.net/guduruyu/article/details/68491211

7. imutils 有 c++ 的部分实现,https://github.com/minooei/imutils

8. 获得轮廓最小矩形,使用 boundingRect

Rect boundRect = boundingRect( contours_poly[i] );

参考: https://docs.opencv.org/3.4/da/d0c/tutorial_bounding_rects_circles.html

9. 矩形 rectangle()

void cv::rectangle  (   InputOutputArray    img,
        Point   pt1,
        Point   pt2,
        const Scalar &      color,
        int     thickness = 1,
        int     lineType = LINE_8,
        int     shift = 0 
    )   
void cv::rectangle  (   Mat &   img,
        Rect    rec,
        const Scalar &      color,
        int     thickness = 1,
        int     lineType = LINE_8,
        int     shift = 0 
    )   
rectangle(src, boundRect[i], Scalar(0, 255, 0));
rectangle(flipFrame, Point(roi_ws, roi_hs), Point(roi_we, roi_he), Scalar(0, 255, 0), 0)

参考: https://docs.opencv.org/3.4/d6/d6e/group__imgproc__draw.html#ga07d2f74cadcf8e305e810ce8eed13bc9 https://kknews.cc/code/66yekj3.html

10. 获得轮廓最小面积

◆ contourArea()
double cv::contourArea  (   InputArray      contour,
        bool    oriented = false 
    )   
fabs(contourArea(Mat(c)));

参考: https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#ga2c759ed9f497d4a618048a2f56dc97f1 https://docs.opencv.org/3.4.1/dd/d9d/segment_objects_8cpp-example.html#a1

11. 膨胀

◆ dilate()
void cv::dilate     (   InputArray      src,
        OutputArray     dst,
        InputArray      kernel,
        Point   anchor = Point(-1,-1),
        int     iterations = 1,
        int     borderType = BORDER_CONSTANT,
        const Scalar &      borderValue = morphologyDefaultBorderValue() 
    )   
dilate(mid_filer,gray_dilate1,element);

参考: https://docs.opencv.org/3.4/d4/d86/group__imgproc__filter.html#ga4ff0f3318642c4f469d0e11f242f3b6c https://www.itread01.com/articles/1478557515.html https://docs.opencv.org/3.4.1/d8/dc0/morphology2_8cpp-example.html#a10 https://zhuanlan.zhihu.com/p/40326127 https://www.jianshu.com/p/ee72f5215e07 https://www.cnblogs.com/ssyfj/p/9276999.html

12. 查找轮廓

    findContours(image,contours,hierarchy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point());

查找轮廓的参数导致的结果,参考: https://blog.csdn.net/dcrmg/article/details/51987348

13. mat 的创建,复制和释放,构造函数等等

参考: https://blog.csdn.net/wanggao_1990/article/details/53150926 https://blog.csdn.net/guyuealian/article/details/70159660

14. createTrackbar 用于创建滑动控件,方便调试效果。

参考: https://blog.csdn.net/u013270326/article/details/72821149

15. 求差 absdiff

absdiff(frameNow,framePre,frameDet);

参考: https://blog.csdn.net/dcrmg/article/details/52234929

16. 翻转图像

flip()
void cv::flip   (   InputArray      src,
        OutputArray     dst,
        int     flipCode 
    )   

参考: https://docs.opencv.org/3.4/d2/de8/group__core__array.html#gaca7be533e3dac7feb70fc60635adf441

17. 截取部分矩阵

◆ Mat() [15/29]
cv::Mat::Mat    (   const Mat &     m,
        const Range &   rowRange,
        const Range &   colRange = Range::all() 
    )   
roiFrame = Mat(flipFrame, Range(roi_hs, roi_he), Range(roi_ws, roi_we));

参考: https://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#a92a3e9e5911a2eb0cf0950a0a9670c76

18. 转换颜色

◆ cvtColor()
void cv::cvtColor   (   InputArray      src,
        OutputArray     dst,
        int     code,
        int     dstCn = 0 
    )   
cvtColor(roiFrame, hsvFrame, COLOR_BGR2HSV)

参考: https://docs.opencv.org/3.4/d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab

19. 通过上下限阈值从图像中提取作为前景,其他作为后景,完成图像的二值化。

inRange()
void cv::inRange    (   InputArray      src,
        InputArray      lowerb,
        InputArray      upperb,
        OutputArray     dst 
    )   
inRange(hsvFrame, Scalar(0, 20, 70), Scalar(20, 255, 255), maskFrame);

参考: https://docs.opencv.org/master/d2/de8/group__core__array.html#ga48af0ab51e36436c5d04340e036ce981 https://docs.opencv.org/master/d6/d7f/samples_2cpp_2camshiftdemo_8cpp-example.html#a33

20. 寻找轮廓 findContours 获得的轮廓类型是: std::vector<std::vector >, 获取轮廓面积的时候输入的轮廓参数是 std::vector

参考: https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#gadf1ad6a0b82947fa1fe3c3d497f260e0 https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#ga2c759ed9f497d4a618048a2f56dc97f1

21. 画轮廓线

◆ drawContours()
void cv::drawContours   (   InputOutputArray    image,
        InputArrayOfArrays      contours,
        int     contourIdx,
        const Scalar &      color,
        int     thickness = 1,
        int     lineType = LINE_8,
        InputArray      hierarchy = noArray(),
        int     maxLevel = INT_MAX,
        Point   offset = Point() 
    )   

参考: https://docs.opencv.org/master/d6/d6e/group__imgproc__draw.html#ga746c0625f1781f1ffc9056259103edbc

22. 计算曲线长度或者轮廓周长。

◆ arcLength()
double cv::arcLength    (   InputArray      curve,
        bool    closed 
    )   

参考: https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#ga8d26483c636be6b35c3ec6335798a47c

23. 计算轮廓近似多边形

◆ approxPolyDP()
void cv::approxPolyDP   (   InputArray      curve,
        OutputArray     approxCurve,
        double      epsilon,
        bool    closed 
    )   

参考: https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#ga0012a5fdaea70b8a9970165d98722b4c

24. 计算凸包

◆ convexHull()
void cv::convexHull     (   InputArray      points,
        OutputArray     hull,
        bool    clockwise = false,
        bool    returnPoints = true 
    )   

参考: https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#ga014b28e56cb8854c0de4a211cb2be656

25. 计算凸性缺陷

◆ convexityDefects()
void cv::convexityDefects   (   InputArray      contour,
        InputArray      convexhull,
        OutputArray     convexityDefects 
    )   

注意: contour 是 vector, convexhull 是 vector,convexityDefects 是 vector

如果有报错: OpenCV Error: Assertion failed (hpoints > 0) in convexityDefects, file /home/neha/opencv-3.4.0/modules/imgproc/src/convhull.cpp, line 284 terminate called after throwing an instance of 'cv::Exception' what(): /home/neha/opencv-3.4.0/modules/imgproc/src/convhull.cpp:284: error: (-215) hpoints > 0 in function convexityDefects 那么需要检查 convexhull 是不是 vector

参考: https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#gada4437098113fd8683c932e0567f47ba https://github.com/wonderseen/Sparse-Points-Gen-Convex/issues/1

26. Vec4i 是 4个 int 组成的 vector 向量,即 vector

参考: https://www.coder.work/article/826469

27. 画圆圈。

◆ circle()
void cv::circle     (   InputOutputArray    img,
        Point   center,
        int     radius,
        const Scalar &      color,
        int     thickness = 1,
        int     lineType = LINE_8,
        int     shift = 0 
    )   

参考: https://docs.opencv.org/master/d6/d6e/group__imgproc__draw.html#gaf10604b069374903dbd0f0488cb43670
https://blog.csdn.net/caomin1hao/article/details/81876836

28. 画线

◆ line()
void cv::line   (   InputOutputArray    img,
        Point   pt1,
        Point   pt2,
        const Scalar &      color,
        int     thickness = 1,
        int     lineType = LINE_8,
        int     shift = 0 
    )   

参考: https://docs.opencv.org/master/d6/d6e/group__imgproc__draw.html#ga7078a9fae8c7e7d13d24dac2520ae4a2

29. 高斯模糊

◆ GaussianBlur()
void cv::GaussianBlur   (   InputArray      src,
        OutputArray     dst,
        Size    ksize,
        double      sigmaX,
        double      sigmaY = 0,
        int     borderType = BORDER_DEFAULT 
    )

模糊参数的影响效果可以参考: https://www.cnblogs.com/sdu20112013/p/11600436.html

参考: https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#gaabe8c836e97159a9193fb0b11ac52cf1

30. 手势识别,用到凸包和缺陷。

具体可参考: http://www.zfhblog.com/index.php/archives/22/ https://www.cnblogs.com/Anita9002/p/5332122.html https://docs.opencv.org/master/d7/d1d/tutorial_hull.html https://blog.csdn.net/lichengyu/article/details/38392473

31. resize

    cv::Mat dst(300, 300, image.type());
    cv::resize(image, dst, dst.size(), 0, 0, cv::INTER_LINEAR);

参考: https://blog.csdn.net/i_chaoren/article/details/54564663 https://blog.csdn.net/u012005313/article/details/51943442 https://www.jianshu.com/p/11879a49d1a0

32. 复制所有的图像数据

    cv::Mat image;
    image = cv::imread(imgPath);
    memcpy(data, image.data, image.dataend - image.datastart);

参考: https://www.jianshu.com/p/cfc0c1f87bf8

33. 清除图像数据

Mat mat3 = Mat::zeros(1, 4, CV_32F);
mat3.release();

参考: https://blog.csdn.net/wanggao_1990/article/details/53150926

标签: C++, opencv

添加新评论