OpenCV:在一个窗口中记录镜头并在第二个窗口中显示相同的视频但仅具有轮廓 [英] OpenCV: record footage in one window and Display the same video in 2nd window but with contours only

查看:48
本文介绍了OpenCV:在一个窗口中记录镜头并在第二个窗口中显示相同的视频但仅具有轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕获视频并将其显示在一个窗口上,并在第二个窗口中同时显示轮廓.我正在努力解决如何在第二个窗口中显示处理过的视频.请分析我的代码并提出解决方案或指出我哪里出错了,也许给我一些在线教程或资源的指导.谢谢.

I want to capture a video and display it on one window and have second window in which contours are displayed simultaneous. I am struggling with how to have the processed video displayed in the second window. Please analyze my code and suggest a solution or indicate where am going wrong maybe give me some directions to an online tutorial or sources. Thanks.

     #include "iostream"
    #include<opencv\cv.h>
    #include<opencv\highgui.h>
    #include<opencv\ml.h>
    #include<opencv\cxcore.h>
    #include <iostream> 
    #include <vector>
    #include <string> 
    #include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat)
    #include <opencv2/highgui/highgui.hpp> // Video write

    using namespace cv;
    using namespace std;


    Mat image; Mat image_gray; Mat image_gray2; Mat threshold_output;
    Mat frame;
    int thresh=100, max_thresh=255;


    int main(int argc, char** argv) {

     //Capture Video
     VideoCapture capCam(1);
     if (!capCam.isOpened()){
        cout<<"ERROR: Failed to Initialize Camera"<<endl;
        return 1;
     }
     else{
        cout<<"Camera Initialized"<<endl;
     }

 //Create Window
char* ImputFootage = "Source";
namedWindow(ImputFootage, CV_WINDOW_AUTOSIZE);
imshow(ImputFootage, frame);

char* OutputFootage = "Processed";
namedWindow(OutputFootage, CV_WINDOW_AUTOSIZE);
imshow(OutputFootage, frame);



 while(1){
    capCam>> frame;
    imshow("Source", frame);
    return(1);

    if(capCam.read(ImputFootage)){

        //Convert Image to gray & blur it
cvtColor( image, 
    image_gray, 
    CV_BGR2GRAY );

blur( image_gray, 
    image_gray2,
    Size(3,3) );
//Threshold Gray&Blur Image
threshold(image_gray2, 
    threshold_output, 
    thresh, 
    max_thresh, 
    THRESH_BINARY);

//2D Container
vector<vector<Point>> contours;

//Fnd Countours Points, (Imput Image, Storage, Mode1, Mode2, Offset??)
findContours(threshold_output,
    contours, // a vector of contours
    CV_RETR_EXTERNAL,// retrieve the external contours
    CV_CHAIN_APPROX_NONE,
    Point(0, 0)); // all pixels of each contours    

// Draw black contours on a white image
Mat result(threshold_output.size(),CV_8U,Scalar(255));
drawContours(result,contours,
    -1, // draw all contours
    Scalar(0), // in black
    2); // with a thickness of 2


    }
 }
char CheckForEscKey = waitKey(10);
return 1;
}

推荐答案

您甚至在使用相机捕获帧之前就试图显示它们.编译器没有给你错误,因为 Mat 被声明了,但它们没有值(空),而且你试图显示 Mat 图像,但你从相机捕获的是 Mat 帧.此外,您缺少退出(esc 序列,并且您的等待键已退出相机循环.

You were trying to show frames even before they were captured with camera. Compiler was not giving you error because Mat were declared ,but they were without value (null), Moreover you were trying to display Mat image, but what you capture from camera is Mat frame. Also, you lack exit (esc sequence, and your wait key was OUT of camera loop.

无论如何,这是你的代码(重写),我希望这是你想要的.

Anyway, here is your code (rewritten), I hope this is what you wanted.

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;
using namespace cv;

Mat image;
Mat image_gray;
Mat image_gray2;
Mat threshold_output;
Mat frame;
int thresh = 100, max_thresh = 255;

int main(int argc, char** argv)
{

    //Capture Video
    VideoCapture capCam(0);
    if (!capCam.isOpened())
    {
        cout << "ERROR: Failed to Initialize Camera" << endl;
        return 1;
    }
    else
    {
        cout << "Camera Initialized" << endl;
    }

    //Create Window
    char* ImputFootage = "Source";
    namedWindow(ImputFootage, CV_WINDOW_AUTOSIZE);

    char* OutputFootage = "Processed";
    namedWindow(OutputFootage, CV_WINDOW_AUTOSIZE);

    while (1)
    {
        capCam >> frame;
        imshow(ImputFootage, frame);

        if (capCam.read(frame))
        {

            //Convert Image to gray & blur it
            cvtColor(frame, image_gray, CV_BGR2GRAY);

            blur(image_gray, image_gray2, Size(3, 3));
            //Threshold Gray&Blur Image
            threshold(image_gray2, threshold_output, thresh, max_thresh, THRESH_BINARY);

            //2D Container
            vector<vector<Point> > contours;

            //Fnd Countours Points, (Imput Image, Storage, Mode1, Mode2, Offset??)
            findContours(threshold_output, contours, // a vector of contours
                    CV_RETR_EXTERNAL, // retrieve the external contours
                    CV_CHAIN_APPROX_NONE, Point(0, 0)); // all pixels of each contours

            // Draw black contours on a white image
            Mat result(threshold_output.size(), CV_8U, Scalar(255));
            drawContours(result, contours, -1, // draw all contours
                    Scalar(0), // in black
                    2); // with a thickness of 2

            imshow(OutputFootage, result);

            char CheckForEscKey = waitKey(10);

            //If the key pressed by user is Esc(ASCII is 27) then break out of the loop
            if (CheckForEscKey == 27)
            {
                break;
            }

        }
    }

    return 0;
}

这篇关于OpenCV:在一个窗口中记录镜头并在第二个窗口中显示相同的视频但仅具有轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆