Ask Your Question

Revision history [back]

try something like this

struct ParamThresh {
    int threshMin;
    String winName;
    Mat img;
};

void AjouteGlissiere(String nomGlissiere, String nomFenetre, int minGlissiere, int maxGlissiere, int valeurDefaut, int *valGlissiere, void(*f)(int, void *), void *r)
{
    createTrackbar(nomGlissiere, nomFenetre, valGlissiere, 1, f, r);
    setTrackbarMin(nomGlissiere, nomFenetre, minGlissiere);
    setTrackbarMax(nomGlissiere, nomFenetre, maxGlissiere);
    setTrackbarPos(nomGlissiere, nomFenetre, valeurDefaut);
}

void ChooseThreshold(int x, void *r)
{
    ParamThresh *pgc = (ParamThresh*)r;
    if (!pgc->img.empty() )
    {
        Mat imgThresh;
        threshold(pgc->img, imgThresh, pgc->threshMin, 255, THRESH_BINARY);
        imshow("thresh", imgThresh);
        imshow("original", pgc->img);
        Mat imgColor, imgThreshColor;
        vector<Mat> p;
        p.push_back(Mat::zeros(pgc->img.size(), CV_8UC1));
        p.push_back(pgc->img);
        p.push_back(Mat::zeros(pgc->img.size(),CV_8UC1));
        merge(p, imgColor);
        cvtColor(pgc->img&imgThresh, imgThreshColor, COLOR_GRAY2BGR);
        Mat dst;
        addWeighted(imgColor, 0.15, imgThreshColor, 0.85, 0, dst);
        imshow(pgc->winName, dst);
        waitKey(50);
    }
}

int main(int argc, char *argv[])
{
    ParamThresh pgc;
    pgc.winName = "result";
    pgc.img = imread("C:/Users/Laurent.PC-LAURENT-VISI/Desktop/15138717637485638.jpg", IMREAD_GRAYSCALE);
    namedWindow(pgc.winName, WINDOW_GUI_EXPANDED);
    AjouteGlissiere("thresh", pgc.winName, 0, 255, 128, &pgc.threshMin, ChooseThreshold,(void*)&pgc);
    char code = 0;
    while(code!=27)
        code=waitKey(10);
    return 0;
}