Ask Your Question
0

Display a Text on the windows with OpenCv and c++

asked 2017-08-28 04:53:29 -0500

louis89 gravatar image

updated 2017-08-29 08:54:38 -0500

Hi.I use below code for object detection:

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) 
        {

            cout << "mitter"<<endl;

        }
    }

    imshow("Image", frame2);

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

}
return 0;

} I want to have a text on window that show "Detection off" and when the condition if (stat.at<int>(i, CC_STAT_AREA) <80 && stat.at<int>(i, CC_STAT_AREA) >10) was fulfilled then the Text in the window change to "Detection On".I'm beginner in OpenCv and c++ and I have no idea to do this.I'm so grateful to help me do this

edit retag flag offensive close merge delete

Comments

createTrackbar must be outside of while loop and ontrackbar too. I don't understand your program. You use threshold when you move trackbar only. Is it really what you want to do?

LBerger gravatar imageLBerger ( 2017-08-28 05:07:18 -0500 )edit

@LBerger thanks for your reply.This program works well for monochrome object, but it does not work well for an object with two colors (for example black and white).Do you have an idea to improve this program?

louis89 gravatar imagelouis89 ( 2017-08-28 07:44:11 -0500 )edit

If you want to draw text on color or monochrome image use : putText function

Ziri gravatar imageZiri ( 2017-08-28 11:01:21 -0500 )edit

@Ziri can I change text after a condition?

louis89 gravatar imagelouis89 ( 2017-08-28 12:05:27 -0500 )edit

PutText draws text on image so if you modifie your text you'll draw it on next frame. If you want to change text on same frame you'll have to use an overlay image.

Ziri gravatar imageZiri ( 2017-08-28 19:03:53 -0500 )edit

@Ziri but how?can you show me?I want to have a text on frame that show "Detection off" and when the condition if (stat.at<int>(i, CC_STAT_AREA) <80 && stat.at<int>(i, CC_STAT_AREA) >10) was fulfilled then the Text in the window change to "Detection On".I can use put text function and Display Detection OFF but Ican't change it after above condition

louis89 gravatar imagelouis89 ( 2017-08-29 02:33:00 -0500 )edit

Did you try ?

putText(YourImage, " Detection off ", Point2f(0,0), FONT_HERSHEY_PLAIN, 2,  Scalar(0,0,255), 2 , 8 , false);

if (stat.at<int>(i, CC_STAT_AREA) <80 && stat.at<int>(i, CC_STAT_AREA) >10) {

    putText(YourImage, " Detection On ", Point2f(0,0), FONT_HERSHEY_PLAIN, 2,  Scalar(0,0,255), 2 , 8 , false);

}

Ziri gravatar imageZiri ( 2017-08-29 03:13:37 -0500 )edit

@Ziri yes.in this way I have two Text,that "Detection Off" is Always in the picture and "Detection On" while the condition was fulfilled,displayed on the frame.I want to change "OFF" to "ON"

louis89 gravatar imagelouis89 ( 2017-08-29 03:27:27 -0500 )edit

if (stat.at<int>(i, CC_STAT_AREA) <80 && stat.at<int>(i, CC_STAT_AREA) >10) {

    putText(YourImage, " Detection On ", Point2f(100,100), FONT_HERSHEY_PLAIN, 2,  Scalar(0,0,255,255), 2 , 8 , false);

} else

{

  putText(YourImage, " Detection off ", Point2f(100,100), FONT_HERSHEY_PLAIN, 2,  Scalar(0,0,255,255), 2 , 8 , false);

}

Ziri gravatar imageZiri ( 2017-08-29 04:05:06 -0500 )edit

@Ziri no.I try this but "Detection off" still exist when the condition was fulfilled.

louis89 gravatar imagelouis89 ( 2017-08-29 05:19:11 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-08-29 07:35:00 -0500

LBerger gravatar image

updated 2017-08-29 10:51:38 -0500

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;
}
edit flag offensive delete link more

Comments

@LBerger this program only show" mitter" if stat.at<int>(i, CC_STAT_AREA) <80....but I want to change the text after the condition if stat.at<int>(i, CC_STAT_AREA) <80.....for example I have a window that display "Detection Off".I want when condition was fulfilled then "Detection off" change to "Detection On"

louis89 gravatar imagelouis89 ( 2017-08-29 08:25:25 -0500 )edit

@LBerger thanks for your help. can you help me to definition this code in the form a function?

louis89 gravatar imagelouis89 ( 2017-09-04 03:02:59 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-08-28 04:53:29 -0500

Seen: 3,600 times

Last updated: Aug 29 '17