• 【opencv450】 图像相减、二值化、阈值分割


    原始图像:

    灰度图像相减:

    cv::threshold(dif2, threshold1, value, 255, THRESH_BINARY):

     cv::threshold(dif2, threshold2, value, 255, THRESH_BINARY_INV):

    cv::threshold(dif2, threshold3, value, 255, THRESH_TRUNC):

    cv::threshold(dif2, threshold4, value, 255, THRESH_TOZERO):

    cv::threshold(dif2, threshold5, value, 255, THRESH_TOZERO_INV):

    cv::threshold(dif2, threshold6, value, 255, THRESH_MASK):

    cv::threshold(dif2, threshold7, value, 255, THRESH_OTSU):

     

     cv::threshold(dif2, threshold8, value, 255, THRESH_TRIANGLE):

     

     value=75

     value = 119

     

    源码:

    1. #include <opencv2/highgui.hpp>
    2. #include <opencv.hpp>
    3. #include <iostream>
    4. using namespace cv;
    5. using namespace std;
    6. // 滑动条名//
    7. const string trackbarname = "expand";
    8. // 窗口名//
    9. const string winname = "result";
    10. // 最大值//
    11. const int maxNum = 255;
    12. // 预设值//
    13. int value = 88;
    14. Mat frame1, frame2, gray1, gray2, dif, dif2;
    15. int g_nStructElementSize = 3; //结构元素(内核矩阵)的尺寸//
    16. // 获取自定义核//
    17. Mat element = getStructuringElement(MORPH_RECT,
    18. Size(2 * g_nStructElementSize + 1, 2 * g_nStructElementSize + 1),
    19. Point(g_nStructElementSize, g_nStructElementSize));
    20. void onExpand(int value, void* p) {
    21. // printf("value: %d\n", value);
    22. //dilate(img, res, element, Point(-1, -1), value);
    23. Mat dif = *(Mat*)p;
    24. dif.copyTo(dif2);
    25. resize(dif2, dif2, Size(478, 400));
    26. imshow("dif", dif2);
    27. //threshold(dif2, dif2, value, 255, cv::THRESH_BINARY);
    28. threshold(dif2, dif2, value, 255, cv::THRESH_BINARY);
    29. cv::Mat threshold1, threshold2, threshold3, threshold4, threshold5, threshold6, threshold7, threshold8;
    30. cv::threshold(dif2, threshold1, value, 255, THRESH_BINARY);
    31. cv::threshold(dif2, threshold2, value, 255, THRESH_BINARY_INV);
    32. cv::threshold(dif2, threshold3, value, 255, THRESH_TRUNC);
    33. cv::threshold(dif2, threshold4, value, 255, THRESH_TOZERO);
    34. cv::threshold(dif2, threshold5, value, 255, THRESH_TOZERO_INV);
    35. cv::threshold(dif2, threshold6, value, 255, THRESH_MASK);
    36. cv::threshold(dif2, threshold7, value, 255, THRESH_OTSU);
    37. cv::threshold(dif2, threshold8, value, 255, THRESH_TRIANGLE);
    38. //cv::imshow("THRESH_BINARY", threshold1);
    39. //cv::imshow("THRESH_BINARY_INV", threshold2);
    40. //cv::imshow("THRESH_TRUNC", threshold3);
    41. //cv::imshow("THRESH_TOZERO", threshold4);
    42. //cv::imshow("THRESH_TOZERO_INV", threshold5);
    43. //cv::imshow("THRESH_MASK", threshold6);
    44. //cv::imshow("THRESH_OTSU", threshold7);
    45. //cv::imshow("THRESH_TRIANGLE", threshold8);
    46. // 膨胀//
    47. dilate(dif2, dif2, element);
    48. dilate(threshold1, threshold1, element);
    49. dilate(threshold2, threshold2, element);
    50. dilate(threshold3, threshold3, element);
    51. dilate(threshold4, threshold4, element);
    52. dilate(threshold5, threshold5, element);
    53. dilate(threshold6, threshold6, element);
    54. dilate(threshold7, threshold7, element);
    55. dilate(threshold7, threshold7, element);
    56. dilate(threshold8, threshold8, element);
    57. // 腐蚀//
    58. erode(dif2, dif2, element);
    59. erode(threshold1, threshold1, element);
    60. erode(threshold2, threshold2, element);
    61. erode(threshold3, threshold3, element);
    62. erode(threshold4, threshold4, element);
    63. erode(threshold5, threshold5, element);
    64. erode(threshold6, threshold6, element);
    65. erode(threshold7, threshold7, element);
    66. erode(threshold7, threshold7, element);
    67. erode(threshold8, threshold8, element);
    68. // 腐蚀//
    69. resize(frame1, frame1, Size(478, 400));
    70. resize(frame2, frame2, Size(478, 400));
    71. // resize(dif2, dif2, Size(478, 400));
    72. imshow("frame1", frame1);
    73. imshow("frame2", frame2);
    74. imshow("result", dif2);
    75. cv::imshow("THRESH_BINARY", threshold1);
    76. cv::imshow("THRESH_BINARY_INV", threshold2);
    77. cv::imshow("THRESH_TRUNC", threshold3);
    78. cv::imshow("THRESH_TOZERO", threshold4);
    79. cv::imshow("THRESH_TOZERO_INV", threshold5);
    80. cv::imshow("THRESH_MASK", threshold6);
    81. cv::imshow("THRESH_OTSU", threshold7);
    82. cv::imshow("THRESH_TRIANGLE", threshold8);
    83. // imshow(winname, res);
    84. }
    85. int main(int argc, char* argv[])
    86. {
    87. frame1 = imread("10.bmp");
    88. frame2 = imread("11.bmp");
    89. cvtColor(frame1, gray1, COLOR_BGR2GRAY);
    90. cvtColor(frame2, gray2, COLOR_BGR2GRAY);
    91. Mat F1frame1, F1frame2;
    92. F1frame1.create(frame1.size(), CV_32FC1);
    93. F1frame2.create(frame2.size(), CV_32FC1);
    94. dif.create(frame1.size(), CV_32FC1);
    95. gray1.convertTo(F1frame1, CV_32FC1);
    96. gray2.convertTo(F1frame2, CV_32FC1);
    97. absdiff(F1frame1, F1frame2, dif);
    98. dif.convertTo(dif, CV_8UC1);
    99. namedWindow(winname);
    100. createTrackbar(trackbarname, winname, &value, maxNum, onExpand, (void*)&dif);
    101. onExpand(value, &dif);
    102. if (waitKey(0) != -1)
    103. {
    104. destroyAllWindows();
    105. }
    106. 膨胀//
    107. //dilate(dif, dif, element);
    108. 腐蚀//
    109. //erode(dif, dif, element);
    110. //resize(frame1, frame1, Size(478, 400));
    111. //resize(frame2, frame2, Size(478, 400));
    112. //resize(dif, dif, Size(478, 400));
    113. //imshow("frame1", frame1);
    114. //imshow("frame2", frame2);
    115. //imshow("dif", dif);
    116. //if (waitKey(0) != -1)
    117. //{
    118. // destroyAllWindows();
    119. //}
    120. // waitKey();
    121. return 0;
    122. }

    参考:

    OpenCV基础——threshold函数的使用_我不是校长的博客-CSDN博客_cv::threshold

    opencv滑动条Trackbar使用(很全面)_红鱼鱼的博客-CSDN博客

     

  • 相关阅读:
    【ARFoundation学习笔记】射线检测
    python中def一个方法,就一定对应一个return吗
    小程序初始创建
    InternImage
    基于划分的聚类分析——K-means(机器学习)
    安徽省专业技术类职业资格与职称对应表
    什么是 React JS 以及为什么要使用它?
    截胡高通,三星Exynos 2400率先登场:旗舰芯片定位。
    看图说话:对脏读、不可重复度、幻读进行总结
    【MySQL 第一天安装教程】
  • 原文地址:https://blog.csdn.net/cxyhjl/article/details/125412544