1

我想捕捉视频并将其显示在一个窗口上,并有第二个窗口同时显示轮廓。我正在努力解决如何在第二个窗口中显示处理后的视频。请分析我的代码并提出解决方案或指出哪里出了问题,可能会给我一些在线教程或资源的指示。谢谢。

     #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;
}
4

2 回答 2

0

你应该打电话imshow("Processed", result);后打电话drawContours

于 2013-01-27T05:11:20.817 回答
0

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

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

#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;
}
于 2013-08-05T17:09:40.270 回答