need help to change this code to live web cam streaming

asked 2016-09-03 11:25:35 -0500

Tharindu J gravatar image

Hello I followed this video(https://www.youtube.com/watch?v=Y3ac5rFMNZ0) and it was perfectly worked.. but when i change code for web cam capturing it only gets static image.. then i commented lines 54 - 58, lines 177 - 183,(as mentioning in video comment) but still not working... Anyone knows to solve this.I need to apply this to my conveyor design undergraduate project to count objects.. Below shows my edited code(only main cpp)....

// main.cpp

include<opencv2 core="" core.hpp="">

include<opencv2 highgui="" highgui.hpp="">

include<opencv2 imgproc="" imgproc.hpp="">

include<iostream>

include<conio.h> // it may be necessary to change or remove this line if not using Windows

include "Blob.h"

define SHOW_STEPS // un-comment or comment this line to show steps or not

// global variables /////////////////////////////////////////////////////////////////////////////// const cv::Scalar SCALAR_BLACK = cv::Scalar(0.0, 0.0, 0.0); const cv::Scalar SCALAR_WHITE = cv::Scalar(255.0, 255.0, 255.0); const cv::Scalar SCALAR_YELLOW = cv::Scalar(0.0, 255.0, 255.0); const cv::Scalar SCALAR_GREEN = cv::Scalar(0.0, 200.0, 0.0); const cv::Scalar SCALAR_RED = cv::Scalar(0.0, 0.0, 255.0);

// function prototypes //////////////////////////////////////////////////////////////////////////// void matchCurrentFrameBlobsToExistingBlobs(std::vector<blob> &existingBlobs, std::vector<blob> &currentFrameBlobs); void addBlobToExistingBlobs(Blob &currentFrameBlob, std::vector<blob> &existingBlobs, int &intIndex); void addNewBlob(Blob &currentFrameBlob, std::vector<blob> &existingBlobs); double distanceBetweenPoints(cv::Point point1, cv::Point point2); void drawAndShowContours(cv::Size imageSize, std::vector<std::vector<cv::point> > contours, std::string strImageName); void drawAndShowContours(cv::Size imageSize, std::vector<blob> blobs, std::string strImageName); bool checkIfBlobsCrossedTheLine(std::vector<blob> &blobs, int &intHorizontalLinePosition, int &carCount); void drawBlobInfoOnImage(std::vector<blob> &blobs, cv::Mat &imgFrame2Copy); void drawCarCountOnImage(int &carCount, cv::Mat &imgFrame2Copy);

/////////////////////////////////////////////////////////////////////////////////////////////////// int main(void) {

cv::VideoCapture capVideo;

cv::Mat imgFrame1;
cv::Mat imgFrame2;

std::vector<Blob> blobs;

cv::Point crossingLine[2];

int carCount = 0;

capVideo.open(0);

if (!capVideo.isOpened()) {                                                 // if unable to open video file
    std::cout << "error reading video file" << std::endl << std::endl;      // show error message
    _getch();                   // it may be necessary to change or remove this line if not using Windows
    return(0);                                                              // and exit program
}

/*if (capVideo.get(CV_CAP_PROP_FRAME_COUNT) < 2) {
    std::cout << "error: video file must have at least two frames";
    _getch();                   // it may be necessary to change or remove this line if not using Windows
    return(0);
}*/

capVideo.read(imgFrame1);
capVideo.read(imgFrame2);

int intHorizontalLinePosition = (int)std::round((double)imgFrame1.rows * 0.35);

crossingLine[0].x = 0;
crossingLine[0].y = intHorizontalLinePosition;

crossingLine[1].x = imgFrame1.cols - 1;
crossingLine[1].y = intHorizontalLinePosition;

char chCheckForEscKey = 0;

bool blnFirstFrame = true;

int frameCount = 2;

while (capVideo.isOpened() && chCheckForEscKey != 27) {

    std::vector<Blob> currentFrameBlobs;

    cv::Mat imgFrame1Copy = imgFrame1.clone();
    cv::Mat imgFrame2Copy = imgFrame2.clone();

    cv::Mat imgDifference;
    cv::Mat imgThresh;

    cv::cvtColor(imgFrame1Copy, imgFrame1Copy, CV_BGR2GRAY);
    cv::cvtColor(imgFrame2Copy, imgFrame2Copy, CV_BGR2GRAY);

    cv::GaussianBlur(imgFrame1Copy, imgFrame1Copy, cv::Size(5, 5), 0);
    cv::GaussianBlur(imgFrame2Copy, imgFrame2Copy, cv::Size(5, 5), 0);

    cv::absdiff(imgFrame1Copy, imgFrame2Copy, imgDifference);

    cv::threshold(imgDifference, imgThresh, 30, 255.0, CV_THRESH_BINARY);

    cv::imshow("imgThresh", imgThresh);

    cv::Mat structuringElement3x3 = cv::getStructuringElement(cv ...
(more)
edit retag flag offensive close merge delete