当前位置:网站首页>[opencv450] image subtraction, binarization, threshold segmentation

[opencv450] image subtraction, binarization, threshold segmentation

2022-06-23 05:32:00 Ten year dream Lab

original image :

Gray image subtraction :

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

 

Source code :


#include <opencv2/highgui.hpp>
#include <opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;


//  Slider name //
const string trackbarname = "expand";
//  Window name //
const string winname = "result";
//  Maximum //
const int maxNum = 255;

//  Default //
int value = 88;

Mat  frame1, frame2, gray1, gray2, dif, dif2;
int g_nStructElementSize = 3; // Structural elements ( Kernel matrix ) The size of the //
//  Get custom core //
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);
    //  inflation //
    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);
    //  corrosion //
    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);
    //  corrosion //
    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();

    }

      inflation //
    //dilate(dif, dif, element);
      corrosion //
    //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;
}

Reference resources :

OpenCV Basics ——threshold Use of functions _ I'm not the headmaster's blog -CSDN Blog _cv::threshold

opencv Slider bar Trackbar Use ( It's comprehensive )_ Redfish blog -CSDN Blog

 

原网站

版权声明
本文为[Ten year dream Lab]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230242226498.html