Opencv reading IP camera, Video stream, Web camera, images and

Opencv reading video files, reading video stream, Images, IP and Web cameras. I would like to cover this all in one post. Yes, video writer is also important to store your results and achievements in video. There is couple of simple trick and if you follow them, you will never have a problem with the reading and writing video, stream, files in future.
opencv web camera

Basic opencv web camera reading

There is couple think you need to take care. My favorite installation on windows platform is trough NUGET package system. It is easy in few steps. I describe this many times for example VS 2017 here. Nuget set up your project without any linking settings, library path selection, global environmental variables and you can directly start coding in few seconds. Just select and install nuget and compile code below. Nothing else.  You need to take care if you have included several thinks. highgui.hpp core.hpp, imgproc.hpp, videoio, imgcodecs. All of them are not necessary to read the web camera but for example for video stream from IP camera is possible that you really need them all.

VideoCapture web camera code

VideoCapture cap(0); is mean open the default camera web camera. Most of the time this mean web camera on your laptop or plugged in any USB camera. The video is read in 'never ending for(;;) loop' which is break when the video from camera is not available by condition if (!cap.isOpened()). Finally the Mat img;   cap >> img;  copy image from default camera devices into your MAT container. The rest is just display. 
#include "opencv2\imgproc.hpp"
#include "opencv2\objdetect\objdetect.hpp"
#include "opencv2\videoio\videoio.hpp"
#include "opencv2\imgcodecs\imgcodecs.hpp"
#include "opencv2\core\core.hpp"
#include "opencv2\highgui.hpp"
#include <vector>
#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <time.h>
using namespace cv;
using namespace std;
int main(int argcconst char** argv)
 VideoCapture cap(0);
 for (;;)
  if (!cap.isOpened()) {
   cout << "Video Capture Fail" << endl;
  else {
   Mat img;
   cap >> img;
   namedWindow("Video", WINDOW_AUTOSIZE);
   imshow("Video", img);
   int key2 = waitKey(20);
 return 0;

Opencv video file reading

Look at the example above for reading the camera. There is almost no difference. Just one, small and straightforward. As a parameter of cap put instead of default devices cap(0) the file name or path you want to open. There is almost always trouble with path.  In this example you just read the files that are located under your project. You can also read the file from different location or on one place by using the full path into some video folder as you can see in following examples. 

VideoCapture cap("movie.vmw");
VideoCapture cap("movie.mp4");
VideoCapture cap("");
VideoCapture cap("");
VideoCapture cap("C:/cm/");
VideoCapture cap("C:/cm/movie.mp4");

Opencv Image read from file and writing 

This is super easy task. Into the our Mat container  image load the image 6.jpg on this C:/adress/ path. There is something different what is great to have in case you are reading lots of images inside the folder. 

Mat image;
image = imread("C:/adress/6.jpg", CV_LOAD_IMAGE_COLOR);

CV_LOAD_IMAGE_COLOR is defined parameter to tell reader that i want MAT with 3 colors. Basically 3 MAT array container of image size. One for blue, red and green color channel. CV_LOAD_IMAGE_GRAYSCALE is defined to tall reader that i want gray scale. Basically only one Mat of the weight(cols) and high(rows) of the image.. 

To write results into file just use imwrite where the first string is just name of your result and image. Image is MAT containing what you want to save..

imwrite("image.jpg", image);

Opencv video stream verification

I am using good practice. instead of try stream directly in opencv. I prefer to verify my stream in VLC player. It is faster than modify code and compile again of passing the camera URL as parameter. Also the VLC ask for potential user name and password if its necessary. What is annoying is that all the cameras own stream URL format.  The best approach is to find your IP camera model on and apply to verify inside the VLC. After verification put this directly to VideoCapture cap("http://IP:PORT/mjpeg/video.mjpg?counter"); 

rtsp://IP:PORT/various url
Remember that VLC ask for password Opencv NOT.. Just add rtsp://username:password@IP:PORT
Important FFMPEG is needed in Linux. In case of Nuget packages depends but the stream sometimes needs special installation. 

Opencv tutorial code IP camera pseudo code

There is 3 function.. 
First of all, the main function at the end, where are established 2 threads to read the camera stream..

In Main
  • Thread call the stream function for both camera with different IP camera URL                       thread cam1(stream, "http://xxxxxxxR");
  • To run the function stream inside the thread with url as parametr use.                       cam1.join();
void stream
  • Capture video from url strCamera VideoCapture cap(strCamera) 
  • Fill the frame from cap  cap >> frame;
  • Detect people in camera detect(frame, strCamera);
void detect

Opencv C++ IP camera code, video stream

#include <iostream>
#include <thread>
#include "opencv2/opencv.hpp"
#include <vector>
using namespace std;
using namespace cv;
void detect(Mat imgString strCamera) {
  string cascadeName1 = "haar_cascade_for_people_detection.xml";
  CascadeClassifier detectorBody;
  bool loaded1 = detectorBody.load(cascadeName1);
  Mat original;
  vector human;
  cvtColor(img, img, CV_BGR2GRAY);
  equalizeHist(img, img);
  detectorBody.detectMultiScale(img, human, 1.120 | 1Size(4080), Size(400,480 ));
  if (human.size() > 0
      for (int gg = 0; gg < human.size(); gg++) 
      rectangle(original, human[gg].tl(), human[gg].br(), Scalar(00255), 280);
  imshow("Detect " + strCamera, original);
  int key6 = waitKey(40);
//End of the detect
void stream(String strCamera) {
VideoCapture cap(strCamera);
 if (cap.isOpened()) { 
      while (true) {
        Mat frame;
        cap >> frame; 
        resize(frame, frame, Size(640480));  
        detect(frame, strCamera);
int main() {
    thread cam1(stream, "http://xxxxxxxR");
    thread cam2(stream, "http://xxxxxxxR");
    return 0;

write video into file

On windows machine i usually works with simple wmv format. Works perfectly. Remember the golden rule of video writer in opencv. image Mat have to match same size as VideoWriter. The image is mat that i want to write as frame into the video.. Before I put them into the VideoWriter, I always resize to target size.. This causing the lots of trouble. You cannot see the video result only for that reason.  
Size SizeOfVideo = cv::Size(1024740);  VideoWriter video("Result.wmv"
CV_FOURCC('W''M''V''2'), CAP_PROP_FPS,SizeOfVideotrue);
resize(image, image, Size(800600)); 
 video << image; 


  1. I am using Raspberry pi cam as IP camera and it shows "std::invoke no matching overloaded function found" error.

  2. Your site is actually awe-inspiring. I've discovered numerous brand new points. The right path associated with setting up can also be intriguing. You've chosen really amazing subject. We valued this.
    USA computer vision apps

  3. This comment has been removed by a blog administrator.

  4. Now you have a video stream available and you need to capture still images from it. For that, use getsnapshot() command. Hikvision 8MP Cameras

  5. Fantastic and useful we blog thanks for publishing's useful and informative.keep up the great.
    ENER-J CCTV Security Camera

  6. PeopleLink iCam WHD 720 USB cameras are designed for boardrooms.The sleek and compact design of these cameras help in easy portability and storage.They offer easy connectivity with laptops/PC through USB ports and deliver HD quality video and the Pan and Tilt feature allows a wider angle coverage capturing a larger audience

  7. HighMark Security is a direct supplier of security cameras, video surveillance systems, and CCTV equipment. We supply analog CCTV cameras, HD security cameras, IP cameras, and complete video surveillance systems worldwide. We supply our equipment to homeowners, business owners, government agencies, and any other type of organization, any size. Most of our business comes from the Da Nang, however, we do ship our products everywhere in the world. No project is too small or too large for us to handle. We have trained sales engineers that can help design a system that will fit your requirements and budget. Mua may dinh vi, CCTV Camera, IP Camera Lap Camera Da Nang, Security Systems, Analog Camera, Smart Home Store, Omnipolis, DVR, NVR, Video Management Software, camera ip wifi da nang, read more: lap dat camera da nang. HighMark Security has earned a reputation with the best technical support and customer service in the Da Nang security camera industry, lap camera da nang, sua chua camera da nang dich vu camera da nang


  8. Thanks for sharing this kind of useful information zicom offers best advance home security.
    For More Information. click here

  9. Nice blog. Easy to understable,thanks for sharing

    PeopleLink Fisheye camera are designed for conference rooms. They offer easy connectivity with laptops/PC through USB ports and deliver HD quality video.

  10. Fantastic Post! Lot of information is helpful in some or the other way. Keep home security IP camera


Post a Comment