原始图像:


灰度图像相减:

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

源码:
-
- #include <opencv2/highgui.hpp>
- #include <opencv.hpp>
- #include <iostream>
-
- using namespace cv;
- using namespace std;
-
-
- // 滑动条名//
- const string trackbarname = "expand";
- // 窗口名//
- const string winname = "result";
- // 最大值//
- const int maxNum = 255;
-
- // 预设值//
- int value = 88;
-
- Mat frame1, frame2, gray1, gray2, dif, dif2;
- int g_nStructElementSize = 3; //结构元素(内核矩阵)的尺寸//
- // 获取自定义核//
- Mat element = getStructuringElement(MORPH_RECT,
- Size(2 * g_nStructElementSize + 1, 2 * g_nStructElementSize + 1),
- Point(g_nStructElementSize, g_nStructElementSize));
-
- void onExpand(int value, void* p) {
- // printf("value: %d\n", value);
- //dilate(img, res, element, Point(-1, -1), value);
- Mat dif = *(Mat*)p;
- dif.copyTo(dif2);
- resize(dif2, dif2, Size(478, 400));
- imshow("dif", dif2);
- //threshold(dif2, dif2, value, 255, cv::THRESH_BINARY);
- threshold(dif2, dif2, value, 255, cv::THRESH_BINARY);
- cv::Mat threshold1, threshold2, threshold3, threshold4, threshold5, threshold6, threshold7, threshold8;
- 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);
- //cv::imshow("THRESH_BINARY", threshold1);
- //cv::imshow("THRESH_BINARY_INV", threshold2);
- //cv::imshow("THRESH_TRUNC", threshold3);
- //cv::imshow("THRESH_TOZERO", threshold4);
- //cv::imshow("THRESH_TOZERO_INV", threshold5);
- //cv::imshow("THRESH_MASK", threshold6);
- //cv::imshow("THRESH_OTSU", threshold7);
- //cv::imshow("THRESH_TRIANGLE", threshold8);
- // 膨胀//
- dilate(dif2, dif2, element);
- dilate(threshold1, threshold1, element);
- dilate(threshold2, threshold2, element);
- dilate(threshold3, threshold3, element);
- dilate(threshold4, threshold4, element);
- dilate(threshold5, threshold5, element);
- dilate(threshold6, threshold6, element);
- dilate(threshold7, threshold7, element);
- dilate(threshold7, threshold7, element);
- dilate(threshold8, threshold8, element);
- // 腐蚀//
- erode(dif2, dif2, element);
- erode(threshold1, threshold1, element);
- erode(threshold2, threshold2, element);
- erode(threshold3, threshold3, element);
- erode(threshold4, threshold4, element);
- erode(threshold5, threshold5, element);
- erode(threshold6, threshold6, element);
- erode(threshold7, threshold7, element);
- erode(threshold7, threshold7, element);
- erode(threshold8, threshold8, element);
- // 腐蚀//
- resize(frame1, frame1, Size(478, 400));
- resize(frame2, frame2, Size(478, 400));
- // resize(dif2, dif2, Size(478, 400));
- imshow("frame1", frame1);
- imshow("frame2", frame2);
- imshow("result", dif2);
- cv::imshow("THRESH_BINARY", threshold1);
- cv::imshow("THRESH_BINARY_INV", threshold2);
- cv::imshow("THRESH_TRUNC", threshold3);
- cv::imshow("THRESH_TOZERO", threshold4);
- cv::imshow("THRESH_TOZERO_INV", threshold5);
- cv::imshow("THRESH_MASK", threshold6);
- cv::imshow("THRESH_OTSU", threshold7);
- cv::imshow("THRESH_TRIANGLE", threshold8);
- // imshow(winname, res);
- }
-
-
- int main(int argc, char* argv[])
- {
-
- frame1 = imread("10.bmp");
- frame2 = imread("11.bmp");
- cvtColor(frame1, gray1, COLOR_BGR2GRAY);
- cvtColor(frame2, gray2, COLOR_BGR2GRAY);
-
- Mat F1frame1, F1frame2;
- F1frame1.create(frame1.size(), CV_32FC1);
- F1frame2.create(frame2.size(), CV_32FC1);
-
- dif.create(frame1.size(), CV_32FC1);
-
- gray1.convertTo(F1frame1, CV_32FC1);
- gray2.convertTo(F1frame2, CV_32FC1);
- absdiff(F1frame1, F1frame2, dif);
- dif.convertTo(dif, CV_8UC1);
-
- namedWindow(winname);
-
- createTrackbar(trackbarname, winname, &value, maxNum, onExpand, (void*)&dif);
- onExpand(value, &dif);
- if (waitKey(0) != -1)
- {
- destroyAllWindows();
-
- }
-
- 膨胀//
- //dilate(dif, dif, element);
- 腐蚀//
- //erode(dif, dif, element);
-
- //resize(frame1, frame1, Size(478, 400));
- //resize(frame2, frame2, Size(478, 400));
- //resize(dif, dif, Size(478, 400));
- //imshow("frame1", frame1);
- //imshow("frame2", frame2);
- //imshow("dif", dif);
- //if (waitKey(0) != -1)
- //{
- // destroyAllWindows();
- //}
- // waitKey();
- return 0;
- }
参考:
OpenCV基础——threshold函数的使用_我不是校长的博客-CSDN博客_cv::threshold
opencv滑动条Trackbar使用(很全面)_红鱼鱼的博客-CSDN博客