Ask Your Question

Revision history [back]

createTrackbar should be outside while loop. trackbar is an empty function

#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;


static void on_trackbar(int, void*)
{

}


int main(int argc, char* argv[]) 
{
   Mat frame;

   Mat frame2 ;
   VideoCapture cap(0);
    if (!cap.isOpened())
    {
        cout << "Cannot open the video cam" << endl;
        return  0;

    }
    namedWindow("Image", 1);
    createTrackbar("Threshold", "Image", &threshval, 255, on_trackbar);

    while (1)
    {
        bool bSuccess = cap.read(frame); // read a new frame from video


        if (!bSuccess)
        {
            cout << "Cannot read a frame from video stream" << endl;
            break;
        }

        cvtColor(frame, frame2, COLOR_BGR2GRAY);
        GaussianBlur(frame2, frame2, Size(5, 5), 0);
        Canny(frame2, frame2, threshval, threshval * 2, 3);
        morphologyEx(frame2, frame2, MORPH_CLOSE, Mat(), Point(-1, -1), 2);
        Mat stat, centroid;
        Mat bw = threshval < 128 ? (frame2< threshval) : (frame2 > threshval);
        Mat labelImage(frame2.size(), CV_32S);
        int nLabels = connectedComponentsWithStats(bw, labelImage, stat, centroid, 8);

        std::vector<Vec3b> colors(nLabels);
        colors[0] = Vec3b(0, 0, 0);//background
        for (int label = 1; label < nLabels; ++label) 
        {
            colors[label] = Vec3b((rand() & 255), (rand() & 255), (rand() & 255));
        }
        Mat dst(frame2.size(), CV_8UC3);
        for (int r = 0; r < dst.rows; ++r) 
        {
            for (int c = 0; c < dst.cols; ++c) 
            {
                int label = labelImage.at<int>(r, c);
                Vec3b &pixel = dst.at<Vec3b>(r, c);
                pixel = colors[label];
            }
        }
        for (int i = 0; i < nLabels; i++)
        {
            if (stat.at<int>(i, CC_STAT_AREA) <80 && stat.at<int>(i, CC_STAT_AREA) >10) 
            {

                putText(frame2,"mitter",Point(10,30),FONT_HERSHEY_SIMPLEX,1,Scalar(128));
                break;

            }
        }

        imshow("Image", frame2);

        if (waitKey(1) == 27)
        {
            cout << "esc key is pressed by user" << endl;
            break;
        }

    }
    return 0;
}

createTrackbar should be outside while loop. trackbar is an empty function

#include <opencv2/opencv.hpp>
<opencv2/opencv.hpp> ddddddd

using namespace std;
using namespace cv;


static void on_trackbar(int, void*)
{

}


int main(int argc, char* argv[]) 
{
   Mat frame;

   Mat frame2 ;
   VideoCapture cap(0);
    if (!cap.isOpened())
    {
        cout << "Cannot open the video cam" << endl;
        return  0;

    }
    namedWindow("Image", 1);
    createTrackbar("Threshold", "Image", &threshval, 255, on_trackbar);

    while (1)
    {
        bool bSuccess = cap.read(frame); // read a new frame from video


        if (!bSuccess)
        {
            cout << "Cannot read a frame from video stream" << endl;
            break;
        }

        cvtColor(frame, frame2, COLOR_BGR2GRAY);
        GaussianBlur(frame2, frame2, Size(5, 5), 0);
        Canny(frame2, frame2, threshval, threshval * 2, 3);
        morphologyEx(frame2, frame2, MORPH_CLOSE, Mat(), Point(-1, -1), 2);
        Mat stat, centroid;
        Mat bw = threshval < 128 ? (frame2< threshval) : (frame2 > threshval);
        Mat labelImage(frame2.size(), CV_32S);
        int nLabels = connectedComponentsWithStats(bw, labelImage, stat, centroid, 8);

        std::vector<Vec3b> colors(nLabels);
        colors[0] = Vec3b(0, 0, 0);//background
        for (int label = 1; label < nLabels; ++label) 
        {
            colors[label] = Vec3b((rand() & 255), (rand() & 255), (rand() & 255));
        }
        Mat dst(frame2.size(), CV_8UC3);
        for (int r = 0; r < dst.rows; ++r) 
        {
            for (int c = 0; c < dst.cols; ++c) 
            {
                int label = labelImage.at<int>(r, c);
                Vec3b &pixel = dst.at<Vec3b>(r, c);
                pixel = colors[label];
            }
        }
        for (int i = 0; i < nLabels; i++)
        {
            if (stat.at<int>(i, CC_STAT_AREA) <80 && stat.at<int>(i, CC_STAT_AREA) >10) 
            {

                putText(frame2,"mitter",Point(10,30),FONT_HERSHEY_SIMPLEX,1,Scalar(128));
                break;

            }
        }

        imshow("Image", frame2);

        if (waitKey(1) == 27)
        {
            cout << "esc key is pressed by user" << endl;
            break;
        }

    }
    return 0;
}

createTrackbar should be outside while loop. trackbar is an empty function

#include <opencv2/opencv.hpp> ddddddd
 
using namespace std;
using namespace cv;


static void on_trackbar(int, void*)
{

}


int main(int argc, char* argv[]) 
{
   Mat frame;

   Mat frame2 ;
   VideoCapture cap(0);
    if (!cap.isOpened())
    {
        cout << "Cannot open the video cam" << endl;
        return  0;

    }
    namedWindow("Image", 1);
    createTrackbar("Threshold", "Image", &threshval, 255, on_trackbar);

    while (1)
    {
        bool bSuccess = cap.read(frame); // read a new frame from video


        if (!bSuccess)
        {
            cout << "Cannot read a frame from video stream" << endl;
            break;
        }

        cvtColor(frame, frame2, COLOR_BGR2GRAY);
        GaussianBlur(frame2, frame2, Size(5, 5), 0);
        Canny(frame2, frame2, threshval, threshval * 2, 3);
        morphologyEx(frame2, frame2, MORPH_CLOSE, Mat(), Point(-1, -1), 2);
        Mat stat, centroid;
        Mat bw = threshval < 128 ? (frame2< threshval) : (frame2 > threshval);
        Mat labelImage(frame2.size(), CV_32S);
        int nLabels = connectedComponentsWithStats(bw, labelImage, stat, centroid, 8);

        std::vector<Vec3b> colors(nLabels);
        colors[0] = Vec3b(0, 0, 0);//background
        for (int label = 1; label < nLabels; ++label) 
        {
            colors[label] = Vec3b((rand() & 255), (rand() & 255), (rand() & 255));
        }
        Mat dst(frame2.size(), CV_8UC3);
        for (int r = 0; r < dst.rows; ++r) 
        {
            for (int c = 0; c < dst.cols; ++c) 
            {
                int label = labelImage.at<int>(r, c);
                Vec3b &pixel = dst.at<Vec3b>(r, c);
                pixel = colors[label];
            }
        }
        for (int i = 0; i < nLabels; i++)
        {
            if (stat.at<int>(i, CC_STAT_AREA) <80 && stat.at<int>(i, CC_STAT_AREA) >10) 
            {

                putText(frame2,"mitter",Point(10,30),FONT_HERSHEY_SIMPLEX,1,Scalar(128));
                break;

            }
        }

        imshow("Image", frame2);

        if (waitKey(1) == 27)
        {
            cout << "esc key is pressed by user" << endl;
            break;
        }

    }
    return 0;
}

createTrackbar should be outside while loop. trackbar is an empty function

#include <opencv2/opencv.hpp> 

using namespace std;
using namespace cv;


static void on_trackbar(int, void*)
{

}


int main(int argc, char* argv[]) 
{
   Mat frame;

   Mat frame2 ;
   Mat text(100,500,CV_8UC1);
   VideoCapture cap(0);
    if (!cap.isOpened())
    {
        cout << "Cannot open the video cam" << endl;
        return  0;

    }
    namedWindow("Image", 1);
    createTrackbar("Threshold", "Image", &threshval, 255, on_trackbar);

    while (1)
    {
        bool bSuccess = cap.read(frame); // read a new frame from video


        if (!bSuccess)
        {
            cout << "Cannot read a frame from video stream" << endl;
            break;
        }

        cvtColor(frame, frame2, COLOR_BGR2GRAY);
        GaussianBlur(frame2, frame2, Size(5, 5), 0);
        Canny(frame2, frame2, threshval, threshval * 2, 3);
        morphologyEx(frame2, frame2, MORPH_CLOSE, Mat(), Point(-1, -1), 2);
        Mat stat, centroid;
        Mat bw = threshval < 128 ? (frame2< threshval) : (frame2 > threshval);
        Mat labelImage(frame2.size(), CV_32S);
        int nLabels = connectedComponentsWithStats(bw, labelImage, stat, centroid, 8);

        std::vector<Vec3b> colors(nLabels);
        colors[0] = Vec3b(0, 0, 0);//background
        for (int label = 1; label < nLabels; ++label) 
        {
            colors[label] = Vec3b((rand() & 255), (rand() & 255), (rand() & 255));
        }
        Mat dst(frame2.size(), CV_8UC3);
        for (int r = 0; r < dst.rows; ++r) 
        {
            for (int c = 0; c < dst.cols; ++c) 
            {
                int label = labelImage.at<int>(r, c);
                Vec3b &pixel = dst.at<Vec3b>(r, c);
                pixel = colors[label];
            }
        }
    bool detection=false;
    for (int i = 0; i < nLabels; i++)
        {
    {
        if (stat.at<int>(i, CC_STAT_AREA) <80 && stat.at<int>(i, CC_STAT_AREA) >10) 
            {

    {

            putText(frame2,"mitter",Point(10,30),FONT_HERSHEY_SIMPLEX,1,Scalar(128));
            detection=true;
            break;

            }
        }
}
    }
    text.setTo(0);
    if (detection)
        putText(text, "detection true", Point(10, 40), FONT_HERSHEY_SIMPLEX, 1, Scalar(128));
    else
        putText(text, "detection false", Point(10, 40), FONT_HERSHEY_SIMPLEX, 1, Scalar(128));
        imshow("text", text);

        imshow("Image", frame2);

        if (waitKey(1) == 27)
        {
            cout << "esc key is pressed by user" << endl;
            break;
        }

    }
    return 0;
}

createTrackbar should be outside while loop. trackbar is an empty function

#include <opencv2/opencv.hpp> 

using namespace std;
using namespace cv;


static void on_trackbar(int, void*)
{

}


int main(int argc, char* argv[]) 
{
   Mat frame;
    Mat frame2 ;
   Mat text(100,500,CV_8UC1);
   VideoCapture cap(0);
    if (!cap.isOpened())
    {
        cout << "Cannot open the video cam" << endl;
        return  0;

    }
    namedWindow("Image", 1);
    createTrackbar("Threshold", "Image", &threshval, 255, on_trackbar);

    while (1)
    {
        bool bSuccess = cap.read(frame); // read a new frame from video


        if (!bSuccess)
        {
            cout << "Cannot read a frame from video stream" << endl;
            break;
        }

        cvtColor(frame, frame2, COLOR_BGR2GRAY);
        GaussianBlur(frame2, frame2, Size(5, 5), 0);
        Canny(frame2, frame2, threshval, threshval * 2, 3);
        morphologyEx(frame2, frame2, MORPH_CLOSE, Mat(), Point(-1, -1), 2);
        Mat stat, centroid;
        Mat bw = threshval < 128 ? (frame2< threshval) : (frame2 > threshval);
        Mat labelImage(frame2.size(), CV_32S);
        int nLabels = connectedComponentsWithStats(bw, labelImage, stat, centroid, 8);

        std::vector<Vec3b> colors(nLabels);
        colors[0] = Vec3b(0, 0, 0);//background
        for (int label = 1; label < nLabels; ++label) 
        {
            colors[label] = Vec3b((rand() & 255), (rand() & 255), (rand() & 255));
        }
        Mat dst(frame2.size(), CV_8UC3);
        for (int r = 0; r < dst.rows; ++r) 
        {
            for (int c = 0; c < dst.cols; ++c) 
            {
                int label = labelImage.at<int>(r, c);
                Vec3b &pixel = dst.at<Vec3b>(r, c);
                pixel = colors[label];
            }
        }
    bool detection=false;
    for (int i = 0; i < nLabels; i++)
    {
        if (stat.at<int>(i, CC_STAT_AREA) <80 && stat.at<int>(i, CC_STAT_AREA) >10) 
        {

            putText(frame2,"mitter",Point(10,30),FONT_HERSHEY_SIMPLEX,1,Scalar(128));
            detection=true;
            break;

        }
    }
    text.setTo(0);
    if (detection)
        putText(text, "detection true", Point(10, 40), FONT_HERSHEY_SIMPLEX, 1, Scalar(128));
    else
        putText(text, "detection false", Point(10, 40), FONT_HERSHEY_SIMPLEX, 1, Scalar(128));
        imshow("text", text);

        imshow("Image", frame2);

        if (waitKey(1) == 27)
        {
            cout << "esc key is pressed by user" << endl;
            break;
        }

    }
    return 0;
}

createTrackbar should be outside while loop. trackbar is an empty function

#include <opencv2/opencv.hpp> 

using namespace std;
using namespace cv;


static void on_trackbar(int, void*)
{

}


int main(int argc, char* argv[]) 
{
   Mat frame;
   Mat frame2 ;
   Mat text(100,500,CV_8UC1);
    VideoCapture cap(0);
    if (!cap.isOpened())
    {
        cout << "Cannot open the video cam" << endl;
        return  0;

    }
    namedWindow("Image", 1);
    createTrackbar("Threshold", "Image", &threshval, 255, on_trackbar);

    while (1)
    {
        bool bSuccess = cap.read(frame); // read a new frame from video


        if (!bSuccess)
        {
            cout << "Cannot read a frame from video stream" << endl;
            break;
        }

        cvtColor(frame, frame2, COLOR_BGR2GRAY);
        GaussianBlur(frame2, frame2, Size(5, 5), 0);
        Canny(frame2, frame2, threshval, threshval * 2, 3);
        morphologyEx(frame2, frame2, MORPH_CLOSE, Mat(), Point(-1, -1), 2);
        Mat stat, centroid;
        Mat bw = threshval < 128 ? (frame2< threshval) : (frame2 > threshval);
        Mat labelImage(frame2.size(), CV_32S);
        int nLabels = connectedComponentsWithStats(bw, labelImage, stat, centroid, 8);

        std::vector<Vec3b> colors(nLabels);
        colors[0] = Vec3b(0, 0, 0);//background
        for (int label = 1; label < nLabels; ++label) 
        {
            colors[label] = Vec3b((rand() & 255), (rand() & 255), (rand() & 255));
        }
        Mat dst(frame2.size(), CV_8UC3);
        for (int r = 0; r < dst.rows; ++r) 
        {
            for (int c = 0; c < dst.cols; ++c) 
            {
                int label = labelImage.at<int>(r, c);
                Vec3b &pixel = dst.at<Vec3b>(r, c);
                pixel = colors[label];
            }
        }
    bool detection=false;
    for (int i = 0; i < nLabels; i++)
    {
        if (stat.at<int>(i, CC_STAT_AREA) <80 && stat.at<int>(i, CC_STAT_AREA) >10) 
        {

            putText(frame2,"mitter",Point(10,30),FONT_HERSHEY_SIMPLEX,1,Scalar(128));
            detection=true;
            break;

        }
    }
    text.setTo(0);
    if (detection)
        putText(text, "detection true", Point(10, 40), FONT_HERSHEY_SIMPLEX, 1, Scalar(128));
    else
        putText(text, "detection false", Point(10, 40), FONT_HERSHEY_SIMPLEX, 1, Scalar(128));
        imshow("text", text);

        imshow("Image", frame2);

        if (waitKey(1) == 27)
        {
            cout << "esc key is pressed by user" << endl;
            break;
        }

    }
    return 0;
}