Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

Hi.I use below code for object detection:

Mat frame;

 cv::Mat frame2 = frame.clone();
 int threshval =88;

static void on_trackbar(int, void*)
{

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

    vector<Rect> rComp;
    rComp.push_back(Rect(Point((stat.at<int>(i, CC_STAT_LEFT)), (stat.at<int>(i, CC_STAT_TOP))), Size((stat.at<int>(i, CC_STAT_WIDTH)), (stat.at<int>(i, CC_STAT_HEIGHT)))));
    //  


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

    cout << "mitter" <<endl;



    }
}}

static void help()
  {
 cout << "\n This program demonstrates connected components and use of the trackbar\n"
     "Usage: \n"
     "  ./connected_components <image(../data/stuff.jpg as default)>\n"
     "The image is converted to grayscale and displayed, another image has a trackbar\n"
     "that controls thresholding and thereby the extracted contours which are drawn in color\n";};

 const char* keys = {
 "{help h||}{@image|../data/stuff.jpg|image for converting to a grayscale}" };
 int main(int argc, char* argv[]) {
 VideoCapture cap(0);
 if (!cap.isOpened())
 {
     cout << "Cannot open the video cam" << endl;
      return  0;

     }

 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(frame, frame2, threshval, threshval * 2, 3);
     morphologyEx(frame2, frame2, MORPH_CLOSE, Mat(), Point(-1, -1), 2);
     namedWindow("Image", 1);
     imshow("Image", frame2);
    // namedWindow("Connected Components", 1);
     createTrackbar("Threshold","Image" , &threshval, 255, on_trackbar);
     on_trackbar(threshval, 0);


     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

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

Hi.I use below code for object detection:

Mat frame;

 cv::Mat frame2 = frame.clone();
 int threshval =88;

static void on_trackbar(int, void*)
{

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

    vector<Rect> rComp;
    rComp.push_back(Rect(Point((stat.at<int>(i, CC_STAT_LEFT)), (stat.at<int>(i, CC_STAT_TOP))), Size((stat.at<int>(i, CC_STAT_WIDTH)), (stat.at<int>(i, CC_STAT_HEIGHT)))));
    //  


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

    cout << "mitter" <<endl;



    }
}}

static void help()
  {
 cout << "\n This program demonstrates connected components and use of the trackbar\n"
     "Usage: \n"
     "  ./connected_components <image(../data/stuff.jpg as default)>\n"
     "The image is converted to grayscale and displayed, another image has a trackbar\n"
     "that controls thresholding and thereby the extracted contours which are drawn in color\n";};

 const char* keys = {
 "{help h||}{@image|../data/stuff.jpg|image for converting to a grayscale}" };
 }
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(frame, Canny(frame2, frame2, threshval, threshval * 2, 3);
     morphologyEx(frame2, frame2, MORPH_CLOSE, Mat(), Point(-1, -1), 2);
     namedWindow("Image", 1);
 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);
    // namedWindow("Connected Components", 1);
     createTrackbar("Threshold","Image" , &threshval, 255, on_trackbar);
     on_trackbar(threshval, 0);


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

 }
 }

}
return 0; }
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