domingo, 1 de junio de 2014

Object Detection - Color Filtering - OpenCV - C++

OBJECT DETECTION USING COLOR FILTERING - OPENCV2 - VISUAL STUDIO C++


Hello today I will show an easy method for detecting objects using c++ and OpenCV2:

Limitations of method:
- all object should have only one color
- background should not be composed of objects with the same shape and color as the one we are trying to detect.

Funtions to use:

- cv::VideoCapture
      -.read
- cv::Mat
- cv::namedWindow
- cv::cvtColor
- cv::inRange
- cv::GaussianBlur
- cv::HoughCircles
- cv::circle
- cv::imshow
- cv::waitKey

The ALGORITHM:
please read comments in the code. 

Variables declarations;:


Start Program loop


Showing Results






After Adding trackbars and morphological transformations you can get a better result:





Thanks to: 18F4550videos channel in Youtube

viernes, 30 de mayo de 2014

Matching Features with ORB using OpenCV (Python code)

Matching Features with ORB and Brute Force using OpenCV (Python code)

Today I will explain how to detect and match feature points using OpenCV. I will be using OpenCV 2.4.9

Funtions we will be using:

- cv2.VideoCapture()
   -.read()
- cv2.ORB()
   - .detect()
   -.compute()
- cv2.BFMatcher()
   -.match()
- cv2.imread()
- cv2.cvtColor()
- cv2.line()

The Algorithm:

1. First of all we must imports the funtions that we are going to use, for this we write:


import numpy as np
import cv2

2. we need to declare the orb detector that we will use and the Brute force matcher:


orb = cv2.ORB()
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

The parameter cv2.NORM_HAMMING specifies the distance measurement to be used, in this case, hamming distance. For ORB this is the only option we have for this parameter.

The second parameter is a boolean  if it is true, the matcher returns only those matches with value (i,j) such that i-th descriptor in set A has j-th descriptor in set B as the best match and vice-versa.

3. obtain the controll of the video capturing device (camera). 

camera = cv2.VideoCapture(0)

the parameter for the funcion VideoCapture is the id of the device we are using to capture video. if there is only one camera connected, pass 0 to the function. 

4. load Image to match. 


imgTrainColor=cv2.imread('train.png')
imgTrainGray=cv2.cvtColor(imgTrainColor,cv2.COLOR_BGR2GRAY)

the first funtion returns the image we are trying to match to our video, the parameter is the name of the image and it should be located in the same directory of the project. 

the second funtion changes the space from BGR yo GRAY. we are going to do all the processing of the image on its gray scale version. the parameters are the image we want to change to gray scale, and the secon is the type of conversion we want to achieve. 

5. we get keypoints and descriptors of our image.


kpTrain = orb.detect(imgTrainGray,None)
kpTrain, desTrain = orb.compute(imgTrainGray, kpTrain)

the first function is used to get the Key points in the image that we pass as first argument, the second argument is a mask but here we will use none. 

the second function computes the descriptors of the image using the keypoints. 

this image shows the key points obtained by ORB



6. obtaining and processing the video frame. 


 ret, imgCamColor = camera.read()
imgCamGray=cv2.cvtColor(imgCamColor,cv2.COLOR_BGR2GRAY)
kpCam = orb.detect(imgCamGray,None)
kpCam, desCam = orb.compute(imgCamGray, kpCam)

first, we get the frame by using the first function and then we get the keypoints and compute the descriptors as before. 

7. we find matches between the images and the video frame:

matches = bf.match(desCam,desTrain)
 thres_dist = (sum(dist) / len(dist)) * 0.5
matches = [m for m in matches if m.distance < thres_dist]  

we need to delete the matches that correspond to errors, we do this by setting a distance threshold and obtain the matches points whose distances are less than the threshold. Here I used as threshold half of the distance mean across all the array of matching points. 

8. plotting results. 

for i in range(len(matches)):
   pt_a = (int(kpTrain[matches[i].trainIdx].pt[0]), int(kpTrain[matches[i].trainIdx].pt[1]+hdif))
   pt_b = (int(kpCam[matches[i].queryIdx].pt[0]+w2), int(kpCam[matches[i].queryIdx].pt[1]))
   cv2.line(result, pt_a, pt_b, (255, 0, 0))

for every matching point we get the point in both images a draw a line between them. prior to this we must create the result image that is going to have both matching images. 

9. destroy windows and release camera.

cv2.destroyAllWindows()
camera.release()

THE CODE:
# -*- coding: utf-8 -*-
"""
@author: Javier Perez
@email: javier_e_perez21@hotmail.com

"""
import numpy as np
import cv2
  
ESC=27   
camera = cv2.VideoCapture(1)
orb = cv2.ORB()
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

imgTrainColor=cv2.imread('train.png')
imgTrainGray = cv2.cvtColor(imgTrainColor, cv2.COLOR_BGR2GRAY)

kpTrain = orb.detect(imgTrainGray,None)
kpTrain, desTrain = orb.compute(imgTrainGray, kpTrain)

firsttime=True

while True:
   
    ret, imgCamColor = camera.read()
    imgCamGray = cv2.cvtColor(imgCamColor, cv2.COLOR_BGR2GRAY)
    kpCam = orb.detect(imgCamGray,None)
    kpCam, desCam = orb.compute(imgCamGray, kpCam)
    matches = bf.match(desCam,desTrain)
    dist = [m.distance for m in matches]
    thres_dist = (sum(dist) / len(dist)) * 0.5
    matches = [m for m in matches if m.distance < thres_dist]   

    if firsttime==True:
        h1, w1 = imgCamColor.shape[:2]
        h2, w2 = imgTrainColor.shape[:2]
        nWidth = w1+w2
        nHeight = max(h1, h2)
        hdif = (h1-h2)/2
        firsttime=False
       
    result = np.zeros((nHeight, nWidth, 3), np.uint8)
    result[hdif:hdif+h2, :w2] = imgTrainColor
    result[:h1, w2:w1+w2] = imgCamColor

    for i in range(len(matches)):
        pt_a=(int(kpTrain[matches[i].trainIdx].pt[0]), int(kpTrain[matches[i].trainIdx].pt[1]+hdif))
pt_b=(int(kpCam[matches[i].queryIdx].pt[0]+w2), int(kpCam[matches[i].queryIdx].pt[1]))
        cv2.line(result, pt_a, pt_b, (255, 0, 0))

    cv2.imshow('Camara', result)
  
    key = cv2.waitKey(20)                                 
    if key == ESC:
        break

cv2.destroyAllWindows()
camera.release()

RESULTS:






DOCUMENTATION:

Capturing video:
http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html

ORB implementation:
http://docs.opencv.org/trunk/doc/py_tutorials/py_feature2d/py_orb/py_orb.html

Open CV Prebuild Configuration in Visual Studio 2012

OpenCV Prebuild Configuration - Visual Studio 2012
Prebuild configuration is the easiest way to start coding in OpenCV with Visual Studio. Today I will teach you how to generate a configuration file (.props) for you OpenCV projects in Visual Studio 2012 using prebuild configuration. You will use this file for all you projects and once created you just have to copy and paste it into them. 

What you will need:

1) Visual Studio installed in you computer

2) OpenCV extracted in you computer. 
link: http://opencv.org/downloads.html
Note: Current version when writing this blogg was: 2.4.9


STEPS:

Creating a project in visual studio:
0) configure enviroment variable

if you are in windows 8:
right click in windows icon at the left-down corner of you screen, then click "System" and then go to "Adavanced system settings"

here, click in "Enviroment Variables..." button

In the next window find the system variable Path and click in "Edit..." to edit this variable. 

go to the end of the Variable value text box add a ";" and the directory: 

opencv->build->include-> x86 -> vc10 (if you are using Visual Studio 2010) or vc11 (if you are using Visual Studio 2012) ->bin



1)Create Visual Studio Project 
go to file -> New Project...

select "Win32 Console Application" from the options in the Installed->Templates->Visual C++ tab. 

Write a Name for the project and then press Ok

Press Next in the first Windows that appears

Select "Console Application" in the application type,
Uncheck "Precompiled Header"
uncheck "Security Development Lifecycle (SDL) checks"
check "Empty project"


click finish

2)Property Manager
 make sure you can see the tab window "Property Manager ", if you cant, then go to View->Other Windows -> Property Manager


3)Creating and configuring configuration file
in the property manager right click the folder "Debug| Win32" and click "Add New Project Property sheet"

in the Add New Item window select Property Sheet (.props) and write a name for this file, for example: "myConfiguration.props" then click "Add" Button. you will see that now, the file appears inside the folder "Debug| Win32

right click on the file you just create and click in "properties"

3.1 navigate to the "VC++ Directories" int "Common Properties"and configure:

3.1.1 the "include directories" by clicking on the arrow and then clicking "<Edit...>"  in the windows that appears click on the folder and a line will be added to the space below, then click on the "'..." and navigate in you system to the place where your  OpenCV folder is, i.e: where you extracted when installing it; then go to:
opencv->build->include
 then click in "Select Folder" and then click in "OK" button in the "include directories" Window

3.1.2 the "library directories" by doing the same process of the last step but selecting the folders:

opencv->build->include-> x86 -> vc10 (if you are using Visual Studio 2010) or vc11 (if you are using Visual Studio 2012) ->bin 

add also here. 

opencv->build->include-> x86 -> vc10 (if you are using Visual Studio 2010) or vc11 (if you are using Visual Studio 2012) ->lib

click in "Select Folder" and then click in "OK" button in the "include directories" Window


3.2 navigate to the Linker->input in "Common Properties"and configure:

3.2.1  the "additional Dependencies". here you will have to write the name of the files that end with a "d.lib" and that are in the directory:

opencv->build->include-> x86 -> vc10 (if you are using Visual Studio 2010) or vc11 (if you are using Visual Studio 2012) ->bin 

this depends on the version you are using of OpenCV; I was using 2.4.9 and my list was:

opencv_core249d.lib
opencv_imgproc249d.lib
opencv_highgui249d.lib
opencv_ml249d.lib
opencv_video249d.lib
opencv_features2d249d.lib
opencv_calib3d249d.lib
opencv_contrib249d.lib
opencv_flann249d.lib
opencv_gpu249d.lib
opencv_legacy249d.lib
opencv_nonfree249d.lib
opencv_objdetect249d.lib
opencv_ocl249d.lib
opencv_photo249d.lib
opencv_stitching249d.lib
opencv_superres249d.lib
opencv_videostab249d.lib


then click ok to end configuring the "Additional Dependencies"

3.3 navigate to the Linker->General in "Common Properties"and configure:

set "Use Library Dependency Inputs" to yes. 




FINISH!!!
Now you can use your OpenCv library in this project. 

Important: this configuration file (.props) is located in the folder of the project you just created, you need to save this file somewhere you can remember because whenever you create an OpenCV project you need to import this file to the "Debug|Win32" tab in the "Property Manager "