SlideShare a Scribd company logo
Open CV Tutorial

Ubiquitous Computing Lecture
     February 15th, 2012
OpenCV installation
Setup – Windows (1)
• Download OpenCV 2.1
 http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.1/
Setup – Windows (2)
• Installation
  Add OpenCV DLLs into system path
Setup – Windows (3)
• Include the header files and static libraries
Setup – Linux
• Install OpenCV (say yes to any dependencies):
  $> sudo apt-get install libcv4 libcv-dev libhighgui4 libhighgui-
  dev libcvaux4 libcvaux-dev


• To build:
  $> gcc -lcv -lhighgui –lcvaux YourFile.cpp
Links
• Main wiki
  http://opencv.willowgarage.com/wiki/



• On-line library
  http://opencv.willowgarage.com/documentation/c/



• Tutorial
  http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html
Getting Images from the Webcam
Two Methods:
• Event Driven (or interrupt driven)
    • New Frame == interrupt
    • Adv: constant FPS, multi-threaded processing
    • Dis: overhead, must be real-time

• Polling (what we will use, the norm)
   • Grab frame when ready
   • Adv: only process what we can in real time
   • Dis: might grab same frame, variable FPS
Getting Images from the Webcam
  #include   <stdio.h>
  #include   "opencv/cv.h"
  #include   "opencv/highgui.h"
  #include   "opencv/cxcore.h"

  int main(int argc, char* argv[])
  {
       CvCapture *capture = 0; //The camera
       IplImage* frame = 0; //The images you bring out of the camera

       capture = cvCaptureFromCAM( 0 ); //Open the camera
       if (!capture ){
           printf("Could not connect to cameran" ); return 1;
       }

       cvNamedWindow( "demowin1", CV_WINDOW_AUTOSIZE ); //Create output window
        while(key != 27 /*escape key to quit*/)
       {
           //Query for the next frame
           frame = cvQueryFrame( capture );
           if( !frame ) break;
           //show the raw image in the first window
           cvShowImage( "demowin1", frame );
           //Get the last key that's been pressed for input
           key = cvWaitKey( 1 );

       }
       //Shutdown
       cvReleaseCapture( &capture ); // WARNING: DO NOT release the "frame" variable
       cvDestroyWindow( "demowin1" );
       return 0;
  }
Saving a video file
 int main(int argc, char* argv[])
{
     CvCapture *capture = 0; //The camera
     IplImage* frame = 0; //The images you bring out of the camera

     capture = cvCaptureFromCAM( 0 ); //Open the camera
     if (!capture ){
         printf("Could not connect to cameran" ); return 1;
     }

     CvVideoWriter *writer;
     int isColor = 1;
     int fps = 25; // or 30
     writer = cvCreateVideoWriter("output.avi", CV_FOURCC_DEFAULT, fps, cvSize(640, 480));


     while(key != 27 /*escape key to quit*/)
     {
         //Query for the next frame
         frame = cvQueryFrame( capture );
         if( !frame ) break;

         cvWriteFrame(writer, frame); //Just like showing an image

         //Get the last key that's been pressed for input
         key = cvWaitKey( 1 );

     }
     cvReleaseVideoWriter(&writer); //Release the video file

     cvReleaseCapture( &capture ); // WARNING: DO NOT release the "frame" variable
     return 0;
}
Accessing a pixel
{
    int i = frame->height/2;
    int j = frame->width/2;

    //slow way
    CvScalar bgr = cvGet2D(frame, i, j);
    printf("B:%d, G:%d, R:%dn", (int)bgr.val[0], (int)bgr.val[1], (int)bgr.val[2]);
    //fast way (access the array directly)
    printf("B:%d, G:%d, R:%dn",
    ((uchar *)(frame->imageData + i*frame->widthStep))[j*frame->nChannels + 0], // B
    ((uchar *)(frame->imageData + i*frame->widthStep))[j*frame->nChannels + 1], // G
    ((uchar *)(frame->imageData + i*frame->widthStep))[j*frame->nChannels + 2]);// R
    //set the value
    cvSet2D(frame, i, j, CV_RGB(0,255,0));
    cvShowImage( "demowin2", frame );
}
Transforming color images
• Grayscale
   • 0.3*R+0.59*G+0.11*B, luminance
    IplImage* grayscaleImg = cvCreateImage(cvSize(640,480), 8/*depth*/, 1/*channels*/);
    {
         cvCvtColor(frame, grayscaleImg, CV_BGR2GRAY);
         cvShowImage( "demowin2", grayscaleImg );
    }

• HSV
   • What we perceive as color, rather than “sense” as color (well… sort of)
      • Hue: the color value
      • Saturation: the richness of the color relative to brightness
      • Value: the gray level intensity
   IplImage* singleChannelImg = cvCreateImage(cvSize(640,480), 8/*depth*/, 1/*channels*/);
   IplImage* hsvImg = cvCreateImage(cvSize(640,480), 8/*depth*/, 3/*channels*/);
   {
        cvCvtColor(frame, hsvImg, CV_BGR2HSV);
        cvSplit(hsvImg, singleChannelImg, NULL, NULL, NULL); // get the "Hue" component
        cvShowImage( "demowin3", singleChannelImg );
   }
Reducing image size
     For reducing image size quickly (e.g., to increase frame rate)
     • Pyramid
          • nearest neighbor (no interpolation)
          • integer multiple down-sampling (throws rows and columns)
          • Anti-aliasing built in




IplImage* smallerImg = cvCreateImage( cvSize(640/2,480/2), 8/*depth*/, 3/*channels*/);
cvPyrDown( frame, smallerImg );
Image filtering
For example low pass filtering
 cvSmooth(frame, frame, CV_GAUSSIAN, 15, 15); //size=15x15

CV_BLUR_NO_SCALE all ones (has ringing)
CV_BLUR all ones, sum to one (has ringing)
CV_GAUSSIAN a gaussian smoothing kernel (no ringing if large enough)
CV_MEDIAN choose the median of the block (non-linear)
CV_BILATERAL “perceptual closeness” (texture/color) of the pixels
Edge Detection
The Canny edge detector
 //Requires a single-channel image:
 cvCvtColor(frame, grayscaleImg, CV_BGR2GRAY);

 //Reduce the image to the edges detected:
 cvCanny(grayscaleImg, edgesImg, hyst1, hyst2, sobelSize, l2needed?);




       1. Image input



           2.                3.              4.                 5.
        Smoothing         Gradient          Local           Hysteresis
                                           Maxima          Thresholding

                            Gx,y     From magnitude      hyst1 = image
                                     and direction of    hyst2 = along lines
                                            Gx,y
Hough Circles
   //Requires a single-channel image:
   cvCvtColor(frame, grayscaleImg, CV_BGR2GRAY);
   //Want to smooth it to get less noise
   cvSmooth(grayscaleImg, grayscaleImg, CV_GAUSSIAN, 7, 9 );
   //Detect the circles in the image
   CvSeq* circles = cvHoughCircles(grayscaleImg, storage, CV_HOUGH_GRADIENT,
                      downsampling, minCenterDist, CannyThresh, AccumThresh
                      minRadius, maxRadius );



• In General, Hough transform consists of
    1. Edge detection
    2. Accumulation in parameter space

    For each detected point,
         draw shape with different parameter
    Look for local maxima

    For a circle space is (x,y,R)


                                                      R=5           R=10
Morphology Primer
• Pick a structuring element
   • Cross
   • Disk
   • Rectangle

• Pick an operation
   • Dilation (Max, “OR”)
   • Erosion (Min, “AND”)
   • Opening (E+D)
   • Closing (D+E)
Torso Tracking

Ke-Yu Chen, 2012/02/15
Project goal




       • Track the human
         torso in real time
         from a single general-
         purpose camera


                            19
Motivation
             • High-tech
               camera is not
               available
             • Only have 2D
               sequences




                           20
Algorithm

          1. Background
Video         Model
Source     Construction




                2.                   3.             4.
                                                            5. Silhouette
           Foreground           Upper body       Distance
                                                             extraction
         Object Extraction     Identification   Transform




                                                                            21
1. Background Model Construction
            1. Background
Video           Model
Source       Construction


         2. Foreground Object       3. Upper body     4. Distance       5. Silhouette
               Extraction            Identification   Transform          extraction




                                …                                   …




               0~50                                            51~60

                                                       Background model


                                                                                        22
2. Foreground Object Extraction
            1. Background
Video           Model
Source       Construction


         2. Foreground Object   3. Upper body             4. Distance   5. Silhouette
               Extraction        Identification           Transform      extraction




                                                  Fragmented pixels

                                      =
              -

                                                    Shadow




                                                                                        23
2-1. Fill the fragmented pixles
               1. Background
Video              Model
Source          Construction


            2. Foreground Object          3. Upper body            4. Distance   5. Silhouette
                  Extraction               Identification          Transform      extraction




                                   Closing by Reconstruction [5]




                                                                                                 24
2-2. Shadow detection [4]
                     1. Background
    Video                Model
    Source            Construction


                  2. Foreground Object   3. Upper body     4. Distance   5. Silhouette
                        Extraction        Identification   Transform      extraction




R
                  G
         Shadow pixel


                        B




                                                                                         25
3. Upper body identification
              1. Background
Video             Model
Source         Construction


           2. Foreground Object   3. Upper body     4. Distance    5. Silhouette
                 Extraction        Identification   Transform       extraction



                                                     Th = 50%



           Th * HEIGHT
                                                      Th = 60%




                                                        Th = 70%




                                                                                   26
4. Distance Transform
            1. Background
Video           Model
Source       Construction


         2. Foreground Object       3. Upper body                       4. Distance                       5. Silhouette
               Extraction            Identification                     Transform                          extraction




                    DT [1]                                    Poisson [3]



                1   1   1   1   1
                1   2   2   2   1
                1   2   3   2   1
                1   2   2   2   1                 w(x, y) = 1 +   ¼ * ( w(x+1, y) + w(x, y+1) + w(x-1, y) + w(x, y -1) )
                1   1   1   1   1


                                                                                                                           27
5. Silhouette Extraction
            1. Background
Video           Model
Source       Construction


         2. Foreground Object     3. Upper body     4. Distance   5. Silhouette
               Extraction          Identification   Transform      extraction




               Threshold = 0.55




                                                                                  28
Smooth the result
• Improve shaking results

    t-3      t-2      t-1       t


                            +         = Final Rectangular
                                        of the torso




              0.4               0.6




                                                       29
Demo



http://youtu.be/DWy3ac9HoKY




                              30
Reference
1.   Distance transform: http://en.wikipedia.org/wiki/Distance_transform

2.   Siddiqi, K.; Member, S. & Kimia, B. B. Parts of Visual Form:
     Computational Aspects IEEE Transactions on Pattern Analysis and
     Machine Intelligence, 1995, 17, 239-251

3.   Gorelick, L.; Galun, M.; Sharon, E.; Basri, R.; Society, I. C.; Society, I. C. &
     Br, A. Shape representation and classification using the poisson equation
     In In Proc. of CVPR’04, 2004, 61-67

4.   Horprasert, T.; Harwood, D. & Davis, L. S. A statistical approach for real-
     time robust background subtraction and shadow detection 1999, 1-1

5.   Closing by Reconstruction:
     http://artis.imag.fr/Members/Adrien.Bousseau/morphology/morphoma
     th.pdf


                                                                                   31
Future work
   • Dynamic threshold for
     shadow detection

   • Track multiple objects

   • Program to get ground
     truth (4 corners of the
     torso)

   • Adaptive morphological
     closing operation

                               32
Questions?




             33

More Related Content

PDF
A basic introduction to open cv for image processing
PPTX
Getting started with open cv in raspberry pi
PPT
Open Cv 2005 Q4 Tutorial
PDF
PyCon 2012: Militarizing Your Backyard: Computer Vision and the Squirrel Hordes
PDF
Introduction to Computer Vision using OpenCV
PDF
openCV with python
PDF
OpenGL L02-Transformations
KEY
openFrameworks 007 - 3D
A basic introduction to open cv for image processing
Getting started with open cv in raspberry pi
Open Cv 2005 Q4 Tutorial
PyCon 2012: Militarizing Your Backyard: Computer Vision and the Squirrel Hordes
Introduction to Computer Vision using OpenCV
openCV with python
OpenGL L02-Transformations
openFrameworks 007 - 3D

What's hot (20)

PDF
TVM VTA (TSIM)
KEY
openFrameworks 007 - video
KEY
openFrameworks 007 - graphics
KEY
openFrameworks 007 - GL
KEY
OpenCVの基礎
PDF
C++ amp on linux
PDF
Tiramisu をちょっと、味見してみました。
PDF
Bridge TensorFlow to run on Intel nGraph backends (v0.4)
PDF
TensorFlow XLA RPC
PDF
GPU Programming on CPU - Using C++AMP
PDF
Bridge TensorFlow to run on Intel nGraph backends (v0.5)
PDF
Facebook Glow Compiler のソースコードをグダグダ語る会
PPT
Евгений Крутько, Многопоточные вычисления, современный подход.
PDF
Kirk Shoop, Reactive programming in C++
PDF
C++20 the small things - Timur Doumler
PPTX
ACM Distinguished Program: Cooperative Testing and Analysis: Human-Tool, Tool...
PDF
Антон Бикинеев, Writing good std::future&lt; C++ >
PDF
The 2016 Android Developer Toolbox [NANTES]
PDF
PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...
PPTX
PVS-Studio team experience: checking various open source projects, or mistake...
TVM VTA (TSIM)
openFrameworks 007 - video
openFrameworks 007 - graphics
openFrameworks 007 - GL
OpenCVの基礎
C++ amp on linux
Tiramisu をちょっと、味見してみました。
Bridge TensorFlow to run on Intel nGraph backends (v0.4)
TensorFlow XLA RPC
GPU Programming on CPU - Using C++AMP
Bridge TensorFlow to run on Intel nGraph backends (v0.5)
Facebook Glow Compiler のソースコードをグダグダ語る会
Евгений Крутько, Многопоточные вычисления, современный подход.
Kirk Shoop, Reactive programming in C++
C++20 the small things - Timur Doumler
ACM Distinguished Program: Cooperative Testing and Analysis: Human-Tool, Tool...
Антон Бикинеев, Writing good std::future&lt; C++ >
The 2016 Android Developer Toolbox [NANTES]
PVS-Studio and Continuous Integration: TeamCity. Analysis of the Open RollerC...
PVS-Studio team experience: checking various open source projects, or mistake...
Ad

Viewers also liked (8)

PDF
Shadow Detection and Removal in Still Images by using Hue Properties of Color...
PPTX
Low power vlsi implementation adaptive noise cancellor based on least means s...
PPTX
MEMS Approach to Low Power Wearable Gas Sensors
PDF
Introduction to Approximation Algorithms
PPT
Low power vlsi design
PPTX
Object Detection & Tracking
PDF
Low power vlsi design ppt
PPT
Low Power Techniques
Shadow Detection and Removal in Still Images by using Hue Properties of Color...
Low power vlsi implementation adaptive noise cancellor based on least means s...
MEMS Approach to Low Power Wearable Gas Sensors
Introduction to Approximation Algorithms
Low power vlsi design
Object Detection & Tracking
Low power vlsi design ppt
Low Power Techniques
Ad

Similar to Open cv tutorial (20)

PDF
Fcv core szeliski_zisserman
PDF
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
PPTX
Defect detection in circlips using image processing in ni lab view
PDF
Computer Vision Workshop
PDF
06 robot vision
PDF
Recognition and tracking moving objects using moving camera in complex scenes
PDF
Augmented Reality Video Playlist - Computer Vision Project
PDF
Implementation of Computer Vision Applications using OpenCV in C++
PPTX
project_final_seminar
PPTX
Computer vision techniques for interactive art
PDF
IRJET- 3D Vision System using Calibrated Stereo Camera
PDF
The International Journal of Engineering and Science (The IJES)
PDF
Gesture Recognition Based Video Game Controller
PPTX
Computer Vision-UNIT1-2025-PART abcB.pptx
PDF
Portfolio for CS 6475 Computational Photography
PDF
26.motion and feature based person tracking
PDF
Resolution Independent 2D Cartoon Video Conversion
PPT
Introduction to Machine Vision
PDF
Manuscript document digitalization and recognition: a first approach
PDF
Gv2512441247
Fcv core szeliski_zisserman
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
Defect detection in circlips using image processing in ni lab view
Computer Vision Workshop
06 robot vision
Recognition and tracking moving objects using moving camera in complex scenes
Augmented Reality Video Playlist - Computer Vision Project
Implementation of Computer Vision Applications using OpenCV in C++
project_final_seminar
Computer vision techniques for interactive art
IRJET- 3D Vision System using Calibrated Stereo Camera
The International Journal of Engineering and Science (The IJES)
Gesture Recognition Based Video Game Controller
Computer Vision-UNIT1-2025-PART abcB.pptx
Portfolio for CS 6475 Computational Photography
26.motion and feature based person tracking
Resolution Independent 2D Cartoon Video Conversion
Introduction to Machine Vision
Manuscript document digitalization and recognition: a first approach
Gv2512441247

More from Eric Larson (20)

PDF
PupilWare Petra 2015
PDF
Mobile healthforthemasses.2015
PDF
Flipping the clinic: in home health monitoring using mobile phones
PDF
First world problems: education, options, and impact
PDF
Recognizing mHealth through phone-as-a-sensor technology
PDF
Consumer Centered Calibration End Use Water Monitoring
PDF
Big Data, Small Data
PDF
Phone As A Sensor Technology: mHealth and Chronic Disease
PDF
Commercialization and Broader Impact: mirroring research through commercial d...
PDF
Creating the Dots: Computer Science and Engineering for Good
PDF
Mobilizing mHealth: interdisciplinary computer science and engineering
PDF
Applications and Derivation of Linear Predictive Coding
PDF
BreatheSuite
PDF
Job Talk
PDF
Larson.defense
PDF
Sensing for Sustainability: Disaggregated Sensing of Electricity, Gas, and Water
PDF
Ubicomp2012 spiro smartpresentation
PDF
Machine Learning Lecture
PPTX
ACEEE 2012
PDF
Accurate and Privacy Preserving Cough Sensing from a Low Cost Microphone
PupilWare Petra 2015
Mobile healthforthemasses.2015
Flipping the clinic: in home health monitoring using mobile phones
First world problems: education, options, and impact
Recognizing mHealth through phone-as-a-sensor technology
Consumer Centered Calibration End Use Water Monitoring
Big Data, Small Data
Phone As A Sensor Technology: mHealth and Chronic Disease
Commercialization and Broader Impact: mirroring research through commercial d...
Creating the Dots: Computer Science and Engineering for Good
Mobilizing mHealth: interdisciplinary computer science and engineering
Applications and Derivation of Linear Predictive Coding
BreatheSuite
Job Talk
Larson.defense
Sensing for Sustainability: Disaggregated Sensing of Electricity, Gas, and Water
Ubicomp2012 spiro smartpresentation
Machine Learning Lecture
ACEEE 2012
Accurate and Privacy Preserving Cough Sensing from a Low Cost Microphone

Recently uploaded (20)

PDF
Zenith AI: Advanced Artificial Intelligence
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
Hybrid model detection and classification of lung cancer
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Enhancing emotion recognition model for a student engagement use case through...
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
TLE Review Electricity (Electricity).pptx
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
Approach and Philosophy of On baking technology
PDF
Getting Started with Data Integration: FME Form 101
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Zenith AI: Advanced Artificial Intelligence
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Hybrid model detection and classification of lung cancer
Building Integrated photovoltaic BIPV_UPV.pdf
Enhancing emotion recognition model for a student engagement use case through...
SOPHOS-XG Firewall Administrator PPT.pptx
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
gpt5_lecture_notes_comprehensive_20250812015547.pdf
TLE Review Electricity (Electricity).pptx
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Accuracy of neural networks in brain wave diagnosis of schizophrenia
WOOl fibre morphology and structure.pdf for textiles
Approach and Philosophy of On baking technology
Getting Started with Data Integration: FME Form 101
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Programs and apps: productivity, graphics, security and other tools
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Unlocking AI with Model Context Protocol (MCP)
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx

Open cv tutorial

  • 1. Open CV Tutorial Ubiquitous Computing Lecture February 15th, 2012
  • 3. Setup – Windows (1) • Download OpenCV 2.1 http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.1/
  • 4. Setup – Windows (2) • Installation Add OpenCV DLLs into system path
  • 5. Setup – Windows (3) • Include the header files and static libraries
  • 6. Setup – Linux • Install OpenCV (say yes to any dependencies): $> sudo apt-get install libcv4 libcv-dev libhighgui4 libhighgui- dev libcvaux4 libcvaux-dev • To build: $> gcc -lcv -lhighgui –lcvaux YourFile.cpp
  • 7. Links • Main wiki http://opencv.willowgarage.com/wiki/ • On-line library http://opencv.willowgarage.com/documentation/c/ • Tutorial http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html
  • 8. Getting Images from the Webcam Two Methods: • Event Driven (or interrupt driven) • New Frame == interrupt • Adv: constant FPS, multi-threaded processing • Dis: overhead, must be real-time • Polling (what we will use, the norm) • Grab frame when ready • Adv: only process what we can in real time • Dis: might grab same frame, variable FPS
  • 9. Getting Images from the Webcam #include <stdio.h> #include "opencv/cv.h" #include "opencv/highgui.h" #include "opencv/cxcore.h" int main(int argc, char* argv[]) { CvCapture *capture = 0; //The camera IplImage* frame = 0; //The images you bring out of the camera capture = cvCaptureFromCAM( 0 ); //Open the camera if (!capture ){ printf("Could not connect to cameran" ); return 1; } cvNamedWindow( "demowin1", CV_WINDOW_AUTOSIZE ); //Create output window while(key != 27 /*escape key to quit*/) { //Query for the next frame frame = cvQueryFrame( capture ); if( !frame ) break; //show the raw image in the first window cvShowImage( "demowin1", frame ); //Get the last key that's been pressed for input key = cvWaitKey( 1 ); } //Shutdown cvReleaseCapture( &capture ); // WARNING: DO NOT release the "frame" variable cvDestroyWindow( "demowin1" ); return 0; }
  • 10. Saving a video file int main(int argc, char* argv[]) { CvCapture *capture = 0; //The camera IplImage* frame = 0; //The images you bring out of the camera capture = cvCaptureFromCAM( 0 ); //Open the camera if (!capture ){ printf("Could not connect to cameran" ); return 1; } CvVideoWriter *writer; int isColor = 1; int fps = 25; // or 30 writer = cvCreateVideoWriter("output.avi", CV_FOURCC_DEFAULT, fps, cvSize(640, 480)); while(key != 27 /*escape key to quit*/) { //Query for the next frame frame = cvQueryFrame( capture ); if( !frame ) break; cvWriteFrame(writer, frame); //Just like showing an image //Get the last key that's been pressed for input key = cvWaitKey( 1 ); } cvReleaseVideoWriter(&writer); //Release the video file cvReleaseCapture( &capture ); // WARNING: DO NOT release the "frame" variable return 0; }
  • 11. Accessing a pixel { int i = frame->height/2; int j = frame->width/2; //slow way CvScalar bgr = cvGet2D(frame, i, j); printf("B:%d, G:%d, R:%dn", (int)bgr.val[0], (int)bgr.val[1], (int)bgr.val[2]); //fast way (access the array directly) printf("B:%d, G:%d, R:%dn", ((uchar *)(frame->imageData + i*frame->widthStep))[j*frame->nChannels + 0], // B ((uchar *)(frame->imageData + i*frame->widthStep))[j*frame->nChannels + 1], // G ((uchar *)(frame->imageData + i*frame->widthStep))[j*frame->nChannels + 2]);// R //set the value cvSet2D(frame, i, j, CV_RGB(0,255,0)); cvShowImage( "demowin2", frame ); }
  • 12. Transforming color images • Grayscale • 0.3*R+0.59*G+0.11*B, luminance IplImage* grayscaleImg = cvCreateImage(cvSize(640,480), 8/*depth*/, 1/*channels*/); { cvCvtColor(frame, grayscaleImg, CV_BGR2GRAY); cvShowImage( "demowin2", grayscaleImg ); } • HSV • What we perceive as color, rather than “sense” as color (well… sort of) • Hue: the color value • Saturation: the richness of the color relative to brightness • Value: the gray level intensity IplImage* singleChannelImg = cvCreateImage(cvSize(640,480), 8/*depth*/, 1/*channels*/); IplImage* hsvImg = cvCreateImage(cvSize(640,480), 8/*depth*/, 3/*channels*/); { cvCvtColor(frame, hsvImg, CV_BGR2HSV); cvSplit(hsvImg, singleChannelImg, NULL, NULL, NULL); // get the "Hue" component cvShowImage( "demowin3", singleChannelImg ); }
  • 13. Reducing image size For reducing image size quickly (e.g., to increase frame rate) • Pyramid • nearest neighbor (no interpolation) • integer multiple down-sampling (throws rows and columns) • Anti-aliasing built in IplImage* smallerImg = cvCreateImage( cvSize(640/2,480/2), 8/*depth*/, 3/*channels*/); cvPyrDown( frame, smallerImg );
  • 14. Image filtering For example low pass filtering cvSmooth(frame, frame, CV_GAUSSIAN, 15, 15); //size=15x15 CV_BLUR_NO_SCALE all ones (has ringing) CV_BLUR all ones, sum to one (has ringing) CV_GAUSSIAN a gaussian smoothing kernel (no ringing if large enough) CV_MEDIAN choose the median of the block (non-linear) CV_BILATERAL “perceptual closeness” (texture/color) of the pixels
  • 15. Edge Detection The Canny edge detector //Requires a single-channel image: cvCvtColor(frame, grayscaleImg, CV_BGR2GRAY); //Reduce the image to the edges detected: cvCanny(grayscaleImg, edgesImg, hyst1, hyst2, sobelSize, l2needed?); 1. Image input 2. 3. 4. 5. Smoothing Gradient Local Hysteresis Maxima Thresholding Gx,y From magnitude hyst1 = image and direction of hyst2 = along lines Gx,y
  • 16. Hough Circles //Requires a single-channel image: cvCvtColor(frame, grayscaleImg, CV_BGR2GRAY); //Want to smooth it to get less noise cvSmooth(grayscaleImg, grayscaleImg, CV_GAUSSIAN, 7, 9 ); //Detect the circles in the image CvSeq* circles = cvHoughCircles(grayscaleImg, storage, CV_HOUGH_GRADIENT, downsampling, minCenterDist, CannyThresh, AccumThresh minRadius, maxRadius ); • In General, Hough transform consists of 1. Edge detection 2. Accumulation in parameter space For each detected point, draw shape with different parameter Look for local maxima For a circle space is (x,y,R) R=5 R=10
  • 17. Morphology Primer • Pick a structuring element • Cross • Disk • Rectangle • Pick an operation • Dilation (Max, “OR”) • Erosion (Min, “AND”) • Opening (E+D) • Closing (D+E)
  • 19. Project goal • Track the human torso in real time from a single general- purpose camera 19
  • 20. Motivation • High-tech camera is not available • Only have 2D sequences 20
  • 21. Algorithm 1. Background Video Model Source Construction 2. 3. 4. 5. Silhouette Foreground Upper body Distance extraction Object Extraction Identification Transform 21
  • 22. 1. Background Model Construction 1. Background Video Model Source Construction 2. Foreground Object 3. Upper body 4. Distance 5. Silhouette Extraction Identification Transform extraction … … 0~50 51~60 Background model 22
  • 23. 2. Foreground Object Extraction 1. Background Video Model Source Construction 2. Foreground Object 3. Upper body 4. Distance 5. Silhouette Extraction Identification Transform extraction Fragmented pixels = - Shadow 23
  • 24. 2-1. Fill the fragmented pixles 1. Background Video Model Source Construction 2. Foreground Object 3. Upper body 4. Distance 5. Silhouette Extraction Identification Transform extraction Closing by Reconstruction [5] 24
  • 25. 2-2. Shadow detection [4] 1. Background Video Model Source Construction 2. Foreground Object 3. Upper body 4. Distance 5. Silhouette Extraction Identification Transform extraction R G Shadow pixel B 25
  • 26. 3. Upper body identification 1. Background Video Model Source Construction 2. Foreground Object 3. Upper body 4. Distance 5. Silhouette Extraction Identification Transform extraction Th = 50% Th * HEIGHT Th = 60% Th = 70% 26
  • 27. 4. Distance Transform 1. Background Video Model Source Construction 2. Foreground Object 3. Upper body 4. Distance 5. Silhouette Extraction Identification Transform extraction DT [1] Poisson [3] 1 1 1 1 1 1 2 2 2 1 1 2 3 2 1 1 2 2 2 1 w(x, y) = 1 + ¼ * ( w(x+1, y) + w(x, y+1) + w(x-1, y) + w(x, y -1) ) 1 1 1 1 1 27
  • 28. 5. Silhouette Extraction 1. Background Video Model Source Construction 2. Foreground Object 3. Upper body 4. Distance 5. Silhouette Extraction Identification Transform extraction Threshold = 0.55 28
  • 29. Smooth the result • Improve shaking results t-3 t-2 t-1 t + = Final Rectangular of the torso 0.4 0.6 29
  • 31. Reference 1. Distance transform: http://en.wikipedia.org/wiki/Distance_transform 2. Siddiqi, K.; Member, S. & Kimia, B. B. Parts of Visual Form: Computational Aspects IEEE Transactions on Pattern Analysis and Machine Intelligence, 1995, 17, 239-251 3. Gorelick, L.; Galun, M.; Sharon, E.; Basri, R.; Society, I. C.; Society, I. C. & Br, A. Shape representation and classification using the poisson equation In In Proc. of CVPR’04, 2004, 61-67 4. Horprasert, T.; Harwood, D. & Davis, L. S. A statistical approach for real- time robust background subtraction and shadow detection 1999, 1-1 5. Closing by Reconstruction: http://artis.imag.fr/Members/Adrien.Bousseau/morphology/morphoma th.pdf 31
  • 32. Future work • Dynamic threshold for shadow detection • Track multiple objects • Program to get ground truth (4 corners of the torso) • Adaptive morphological closing operation 32