How to display text on the windows (webcam windows) openCV?

asked 2014-05-21 01:36:46 -0600

norzanbzura gravatar image

updated 2014-05-21 02:18:00 -0600

berak gravatar image

Actually I want to display a text on the popup windows but I have no idea how to code it. There's anyone can help me? Thanks for your consideration. Here is my code.


#include "opencv/highgui.h"
#include "opencv/cv.h"
#include <opencv/cxcore.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

int main(int, char**)
{
    Mat src;
    Mat src_gray;
    int thresh = 100;
    int max_thresh = 255;
    RNG rng (12345);
cv::Mat frame;
VideoCapture capture;
capture.open(0);

while (true)
{
    capture.read(frame);
    Mat threshold_output;
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;

    cvtColor(frame, src_gray, CV_BGR2GRAY); //produce out2, a one-channel image (CV_BUC1)
    blur(src_gray, src_gray, Size(5,5));

    threshold(src_gray, threshold_output, thresh, max_thresh, CV_THRESH_BINARY_INV);

    findContours(threshold_output, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0,0));

    //Find the rotated rectangles
    vector<RotatedRect> minRect(contours.size());

    for(int i=0; i<contours.size(); i++)
    {
        minRect[i] = minAreaRect(Mat(contours[i]));
    }

    //Draw contours + rotated rectangles + ellipses
    Mat drawing = Mat::zeros(threshold_output.size(), CV_8UC3);

    for(int i=0; i<contours.size(); i++)
    {
        Scalar color = Scalar(rng.uniform(0,0), rng.uniform(0,0), rng.uniform(250,250));

        //countours
        drawContours(drawing, contours, i, color, 1,8, vector<Vec4i>(), 0, Point());

        //Rotated rectangle
        Point2f rect_points[4]; minRect[i].points(rect_points);

        for(int j=0; j<4; j++)
            line(frame, rect_points[j], rect_points[(j+1)%4], color, 1, 8);
    }

    //Show in a window
    namedWindow("Contours", CV_WINDOW_AUTOSIZE);
    imshow("Contours",frame);

    int c = cvWaitKey(10);
    if ((char) c==72)

        break;
}

return 0;

}

edit retag flag offensive close merge delete

Comments

1

read a bit

btw, you probably want to use the c++ headers in your code, not the old c-ones: "opencv2/highgui/highgui.hpp" instead of "opencv/highgui.h" etc.

berak gravatar imageberak ( 2014-05-21 01:46:38 -0600 )edit

Thank you for your attention. By the way, I do not know how to code to put the text on the webcam video popup.

norzanbzura gravatar imagenorzanbzura ( 2014-05-21 04:32:52 -0600 )edit

putText( frame,... );

berak gravatar imageberak ( 2014-05-21 06:07:43 -0600 )edit

It's like this?

    Mat frame = Mat::zeros(imgH,imgW,CV_WINDOW_AUTOSIZE);
    int fontFace = FONT_HERSHEY_COMPLEX_SMALL;
    double fontScale = 1.5;
    int thickness = 2;
    Point textOrg(imgW/5, imgH/1.2);
    string someText = "Dimension: ";
    putText(frame, someText, textOrg, fontFace, fontScale, Scalar::all(255), thickness,8);

    for(;;)
    {
        imshow("Object Measurement", frame);
        if (waitKey(30) &gt; 0)
        break;
    }

I'm using this code, but it doesn't appear on the webcam video popup.

norzanbzura gravatar imagenorzanbzura ( 2014-05-21 08:47:00 -0600 )edit

Mat::zeros(imgH,imgW,CV_WINDOW_AUTOSIZE) -> Mat::zeros(imgH,imgW,CV_8UC1)

Daniil Osokin gravatar imageDaniil Osokin ( 2014-05-21 09:56:43 -0600 )edit

Hi Daniil, thank you for your attention. Actually it's work! But video (webcam) doesn't appear. Thank you.

norzanbzura gravatar imagenorzanbzura ( 2014-05-21 17:23:49 -0600 )edit