2015. 5. 9.

Camshift + Kalmanfilter with OpenCV & python on RaspberryPi B2 - object tracking robot for team project - 1

  최종적으로 만드려는 것은 물체를 파이카메라로 인식하여 tracking 및  요격(?) 하는 robot을 만드려는 것이다. 먼저 해야할 부분은 RaspberryPi B2(그나마 성능이 좋아서 선택)에서 Vision처리를 할 때 많이 쓰는 OpenCV 를 활용하여 물체의 색(HSV값)을 토대로 물체를 인식하는 코드를 만드는 것이다.

-Please understand through English is a bit wrong. ;) 
 I will make a robot that tracking an object with Pi Camera and Shut it down finally.
First,  I have to make a code for catch an object based on color(HSV) using RaspberryPi B2 and OpenCV.


  OpenCV를 사용하여 object tracking을 할 때 Camshift 알고리즘만으로는 안정적인 tracking이 불가능하다. 왜냐하면 Camshift만을 사용했을 경우에는 물체가 중간에 장애물 등에 의해 사라지거나 빠르게 움직일 경우 object를 놓치게 되기 때문이다. 따라서 Kalman filter(역시 독일인이 만듦)를 적용하여 노이즈를 적절히 제거하고, 움직임에 대한 경향을 토대로 Camshift의 탐색창을 재설정해줬다. 사용한 언어는 python이다.(python이 속도가 느리다고 하지만 새로 배우려는 목적이 컸으므로 python선택) 그리고 사용한 오픈소스를 손봐서 fps를 증가시켰다.

  If only use a Camshift algorithm in OpenCV, it would not be stable tracking. Because if you only use Camshift algorithm, it will be fail to track when an object is concealed by something or moving so fast that camera fps doesn't cover.
  Therefore we can apply a Kalman filter. A Kalman filter remove a noise appropriately and estimate a new object's position based on object's movement. I reset a Camshift search window by this estimated position. 
  I used a python(Although a python's speed is slow than C or C ++, I just use a python for learning). And I fixed a opensource for better fps. :)

동영상에서 보면 물체가 빠르게 움직여서 놓치게 된 경우에 예외처리를 통해 다시 물체를 재빠르게 catch하는 것을 볼 수 있다. 
또한 가려져도 다시 tracking을 한다.
만약 모니터에 화면을 출력하는 imshow를 사용하지 않는다면 fps는 더 증가하게 된다.  (전체 주기가 최대 0.3초, 평균 0.15초)
스펙이 제한적인 RPi에서 제법 쓸만한 성능이 나왔다.

In this video, you can see the exception handling for fast movement of an object. It recovered immediately. 
And it also tracks an object after concealed by something.
If it doesn't use a function 'imshow()', it will be more faster. (average cycle : 0.3sec -> 0.15sec)
The result is pretty enough although it is running on RPi.


최종적으로 만드려고 하는 tracking robot은 카메라 자체가 움직이기 때문에 위 동영상과 다르게 
카메라를 흔들었을 경우에도 tracking을 잘하는지 테스트한 결과이다.

Well, actually I will make a robot that move the camera itself. 
So as you see in this video, I conducted a shacking camera test.



  동영상 화질이 좋지않아 잘 보이지 않지만, 잘 tracking되는 것을 알 수 있다.
이제 RPi에서 추출한 target의 중심좌표를 UART통신을 통해 Arduino DUE로 보내는 부분만 추가하면 RPi에서 만들어야할 큰 틀은 완성이다. (이제 성능 쥐어짜기 및 마이너한 기능 추가에 돌입!!)

  Although video's quality is not good for recognize, It tracks very well.
Now, I will add a UART serial communication for RPi to Arduino DUE. After that, the big outline is finished.








참고문헌(reference) :

- Camshift 와 Kalmanfilter를 결합한 논문을 찾아서 어떤 알고리즘 순서로 적용하는지 참고함. (검색하면 쉽게 찾을 수 있음)
대충 설명하면 kalman으로 추정 -> 그곳을 camshift로 탐색 -> 측정한 값을 다시 kalman으로..

-OpenCV에 있는 Camshift를 이용해 python으로 짠 코드 (이 박사분 메일 답장도 칼 같아서 도움이 많이 되었음)
http://www.computervisiononline.com/blog/tutorial-using-camshift-track-objects-video

-Kalman 2D mouse tracking  python 코드 소스
https://github.com/simondlevy/OpenCV-Python-Hacks/tree/master/Kalman2D

-Kalman filter 이론 설명
http://www.matlabinuse.com/Mastering_MATLAB/43103

-OpenCV documentation 페이지
http://docs.opencv.org/

-Pi Camera documentation 페이지
http://picamera.readthedocs.org/en/release-1.10/api.html


댓글 7개:

익명 :
작성자가 댓글을 삭제했습니다.
익명 :
작성자가 댓글을 삭제했습니다.
TEWDA :

https://drive.google.com/folderview?id=0B50lLiCDqflbfnRpQWhkQmtaZEloMWlpdVZRc0JzYVA4UHBmWFpSU2dudlFGX3BPSnlyRWM&usp=sharing

여기서 다운받으시거나 안되면 이메일 주소를 보내주세요. :)

익명 :
작성자가 댓글을 삭제했습니다.
TEWDA :

공개적으로 알려드리면 홍보로 보일 수 있으니 메일을 남겨주세요 :)

Unknown :

혹시 영상을 어떻게 띄우고 클릭으로 영역을 설정하는지 알 수 있을까요? 코드만으로는 영상이 안뜨네요

TEWDA :

코드에서 키보드 입력 부분을 봐주세요!
필요에 따라 이미지를 키고 끄도록 하고 초기 영역 설정을 위한 모드를 실행할 수 있습니다.