简体   繁体   中英

Make video capture to be grayscale and draw rectangle in it using opencv?

I want to draw a rectangle in a video capture that I converted to gray.

This is my code:

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
    VideoCapture cap(0);
    if (!cap.isOpened())
    {
        // print error msg
        return -1;
    }

    namedWindow("gray", CV_WINDOW_AUTOSIZE);
    int pointf[8][4] = { 
                            {100,100 , 150,50},
                            {100,250 , 150,200},
                            {100,430 , 150,380},
                            {295,200 , 345,150},
                            {295,400 , 345,350},
                            {490,100 , 540,50},
                            {490,250 , 540,200},
                            {490,430 , 540,380}
                            };
    int i;
    int j;
    Mat frame;
    Mat gray;
    for (;;)
    {
        cap >> frame;

        cvtColor(frame, gray, CV_BGR2GRAY);
        for (i = 0; i < 8; i++) {
            rectangle(frame, Point(pointf[i][0],pointf[i][1]), Point(pointf[i][2], pointf[i][3]), Scalar(0, 0, 255), 3);
        }
        imshow("gray", gray);
        imshow("gray",frame);   
        if (waitKey(30) >= 0)
            break;

    }

    return 0;

}

In the result, I succeed to draw eight of rectangle, but the video is not in a gray scale. If i reorder the imshow (make video gray first, then draw the rectangle), it will make a video in grayscale, but the rectangle didn't show.

So, any suggestion what I suppose to do? I'm new in using OpenCV. I use OpenCV 3.1.0 and C++ in Visual Studio 2015 Thanks.

Look at your code closely. Here's what you're doing:

    imshow("gray", gray);
    imshow("gray",frame);

What this does is that first it displays your greyscale image in a window called grey, and then immediately displays your color image frame in the same window. Which means that you don't get to see your gray image at all. That's your first error .

Your second error is in the statement:

rectangle(frame, Point(pointf[i][0],pointf[i][1]), Point(pointf[i][2], pointf[i][3]), Scalar(0, 0, 255), 3);

frame is your RGB image, not your gray image , so you're not drawing a rectangle on your grey image at all.

If you need to convert your original image to greyscale and then draw a rectangle on it, here's what your do:

for (;;)
    {
        cap >> frame;

        cvtColor(frame, gray, CV_BGR2GRAY);
        for (i = 0; i < 8; i++) {
            //draw rect on gray, not frame
            rectangle(gray, Point(pointf[i][0],pointf[i][1]), Point(pointf[i][2], pointf[i][3]), Scalar(0, 0, 255), 3);
        }
        //display the Mat objects in different windows
        imshow("gray", gray);
        imshow("original",frame);   
        if (waitKey(30) >= 0)
            break;

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM