Download as pdf or txt
Download as pdf or txt
You are on page 1of 65

Computer vision

Robotic & Vision Lab RoVis


Researches
• Visual servo
• Image Processing
• Impedance control
• Intelligent control
Le Duc Hanh, Phd • Robotics
Lecturer at Ho Chi Minh city University
of Technology

Email: ldhanh@hcmut.edu.vn
Mobile: 84909930983

Robotic & Vision Lab RoVis


REFERENCE

1. Richard Szeliski - Computer Vision_ Algorithms and Applications (2011, Springer)

2. Rafael C. Gonzalez, Richard E. Woods - Digital Image Processing (2007, Pearson)

3. Chris_Solomon,_Toby_Breckon, Fundamentals of Digital Image Processing: A


Practical Approach with Examples in Matlab (2013).

Robotic & Vision Lab RoVis


Syllabus

• buổi 1: Cài đặt phần mềm


• buổi 2: Image manipulation ( filter)
• buổi 3: Edge detection
• buổi 4: Blob detection
• buổi 5: Image Morphological
• buổi 6: Hough transform
• buổi 7: Color recognition
• buổi 8: Camera and calibration

Robotic & Vision Lab RoVis


Introduction

 Today’s robots perform complex tasks with amazing


precision and speed
 Why then have they not moved from the structure of the
factory floor into the “real” world? What is the limiting
factor?

VISION

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
What is Image processing

• Image processing
Image processing is a method to perform some operations on an image, in order to get an
enhanced image or to extract some useful information from it. It is a type of signal processing
in which input is an image and output may be image or characteristics/features associated
with that image.

• Computer Vision:
In Computer Vision, computers or machines are made to gain high-level understanding
from the input digital images or videos with the purpose of automating tasks that the
human visual system can do. It uses many techniques and Image Processing is just one of
them.

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Open and close loop control

Ex: fans, washing machine, toaster etc..

Fig2. close loop control

Ex: Refrigerator cold/hot setting, Clothes dryer with moisture sensor , Temperatue
setting for oven

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Trong công nghiệp đóng gói, người ta sử dụng hệ thống xử lý ảnh để kiểm tra xem các sản phẩm đã
được dán nhãn chưa hoặc kiểm tra nhãn hiệu bao bì có đúng với thành phần chuẩn bị được đóng gói
không.

Robotic & Vision Lab RoVis


Trong công nghiệp dược phẩm, áp dụng hệ thống xử lý ảnh để kiểm tra số lượng
viên thuốc có trong vỉ thuốc.

Robotic & Vision Lab RoVis


Trong lĩnh vực điện, điện tử xử lý ảnh dùng để phát hiện sự thiếu sót các mối
hàn sau khi hàn các chân linh kiện vào board mạch.

Robotic & Vision Lab RoVis


Trong công nghiệp chế tạo, người ta sử dụng robot để tối ưu thời gian và hỗ trợ
con người giúp tăng năng suất, giảm rủi ro.

Robotic & Vision Lab RoVis


Vision based control

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Summarize visual servoing

• Visual servoing: The process of minimizing a visually-


specified task by using visual feedback for motion
control of a robot.

• Is it difficult? Yes.
– Controlling 6D pose of the end-effector from 2D image features.
– Nonlinear projection, degenerate features, etc.
• Is it important? Of course.
– Vision is a versatile sensor.
– Many applications: industrial, health, service, space, humanoids,
etc.

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
CÁC THÔNG SỐ CẦN BIẾT CỦA CAMERA

Cảm biến hình ảnh: Một cảm biến hình ảnh (hình 1.13) bao gồm bảng mạch nhỏ
gồm các photodiode rất nhạy cảm với ánh sáng, chúng chuyển đổi từ tín hiệu
quang thành tín hiệu điện, mỗi diode khi bị ánh sáng tác động sẽ sản sinh một
điện áp tỷ lệ thuận với cường độ ánh sáng nó tác động, khi không có ánh sáng
tác động chúng sẽ không tạo ra mức điện áp nào cả. Tùy theo cường độ ánh sáng
tác động vào cảm biến, cảm biến sẽ tạo ra các tín hiệu hình ảnh. Bộ phận xử lý sẽ
xử lý những tín hiệu này thành tín hiệu video để đưa tới hiển thị trên màn hình giám
sát hoặc thiết bị ghi hình trong hệ thống.

Robotic & Vision Lab RoVis


Về mặt kỹ thuật, cảm biến hình ảnh được chế tạo từ hai công nghệ chính: CCD và CMOS
CCD nhạy cảm hơn với ánh sáng
- Độ nhạy cảm với ánh sáng là một trong những khác biệt rất lớn giữa camera CCD và
camera CMOS.
- Cảm biến CCD tín hiệu đầu ra dạng tương tự, cảm biến CMOS tín hiệu đầu ra kỹ thuật
số.
- Cảm biến CCD và cảm biến CMOS với sự khác biệt giữa việc xử lý tín hiệu, cảm biến
CCD xử lý thông qua tín hiệu dạng tương tự (analog), với các thông số của dòng-ngang.
Cảm biến CMOS thông qua tín hiệu kỹ thuật số, trực tiếp với tham số của độ phân giải
điểm ảnh. Do vậy, khi camera giám sát sử dụng mạng IP để truyền tải thường sẽ được sử
dụng với cảm biến CMOS vì ưu thế tín hiệu kỹ thuật số, còn camera giám sát truyền
thống lại thường được sử dụng với cảm biến CCD.

Robotic & Vision Lab RoVis


Camera Lens

Robotic & Vision Lab RoVis


Độ phân giải ảnh (hình 1.16) là số điểm ảnh (pixel) có trên một đơn vị chiều dài
của hình ảnh đó. Độ phân giải ảnh được tính bằng đơn vị ppi (pixels per inch)
hoặc dpi (dots per inch).
Những pixel trên một đơn vị chiều dài của màn hình máy tính được gọi là độ
phân giải màn hình. Thường được tính bằng những dấu chấm trên một inch
(dpi).
Sensor Size =(Pixel size x required resolution)

Robotic & Vision Lab RoVis


2. Tiêu cự ống kính F – Focal length
Tiêu cự ống kính là khoảng cách từ ống kính đến cảm biến hình ảnh (tính bằng mm).

Robotic & Vision Lab RoVis


3. Góc nhìn (Field of view) (FOV)

Robotic & Vision Lab RoVis


Tiêu cự ống kính càng lớn thì tầm nhìn càng xa nhưng góc nhìn càng hẹp lại.

Robotic & Vision Lab RoVis


Calculating Camera Sensor Resolution and Lens Focal Length

I am trying to choose a camera and lens for my imaging system. How do I make sure that I have the
correct lens focal length, and image sensor resolution? I know the following about the real life situation:
1. Field of view (FOV): Area under inspection that the camera needs to acquire
2. Smallest feature: The size of the smallest feature that I want to detect in the image
3. Working distance (WD): Distance from the front of the lens to the object under inspection

Focal Length x FOV = Sensor Size x Working Distance


Sensor Size =(Pixel size x required resolution)

Robotic & Vision Lab RoVis


Ví dụ 1.1: Một camera có FOV là 200x200mm cần nhìn một vật có đặc điểm ảnh
nhỏ nhất là 1mm. Tính độ phân giải cần có của camera.

Ta có: FOV=200x200mm, smallest feature =1mm=> độ phân giải cần ít nhất là


400x400 pixel

Ví dụ 1.2: Một camera có FOV là 500x600mm cần nhìn một vật có đặc điểm ảnh nhỏ nhất
là 2mm.
a) Camera độ phân giải 640x480 có đủ không?
b) Tính độ phân giải cần có của camera.

Ta có: FOV=500x600mm, smallest feature =2mm=> độ phân giải cần ít nhất là 500x600
pixel. Camera có độ phân giải 640x480 là không đủ=> cần camera có độ phân giải cao hơn
ví dụ như (1024x768)

Robotic & Vision Lab RoVis


Ví dụ 1.3: Một camera có FOV là 508x381, sensor size: 8.47 (diagonal)
a) Nếu working distance maximum là 1129 cần chọn focus length ít nhất là bao nhiêu.
b) Nếu working distance là 889mm, focus length là bao nhiêu

Ta có: FOV=508x381mm, sensor size: 8.47 áp dụn công thức


Focal Length x FOV = Sensor Size x Working Distance
FOV Working Distance 381 45
  
Sensor Size Focal Length 8.47 1
=> f=25mm
Working Distance 1129
 Focal Length    25.088
45 45
Nếu working distance =889mm=> f=19.76mm=> chọn f=16mm theo tiêu
chuẩn.

Robotic & Vision Lab RoVis


Example

Robotic & Vision Lab RoVis


Ví dụ 1.4: Một camera cần nhìn vùng làm việc ít nhất 500x500mm, sensor
size: 4.4  m , feature cần quan sát có độ lớn 1mm.
a) Nếu working distance đặt ở 1500mm cần chọn focus length ít nhất là
bao nhiêu.
b) Giả sử có sẵn focus length là 16mm vậy cần điều chỉnh working
distance là bao nhiêu.

a) Ta có FOV=500x500, smallest feature: 1mm=> resolution là 1000x1000


FOV Working Distance 1500 500
  
Sensor Size Focal Length Focal Length (4.4 1000)
 Focal Length=13.2mm

b) Nếu chỉ có focus length:16 => working distance phải thay đổi là:
Focal Length x FOV = Sensor Size x Working Distance
16  500  (4.4 1000)  Working Distance
 Working Distance  1818mm

Robotic & Vision Lab RoVis


Computer Vision and Programing Tools

• MatLab
• VS2010
• Opencv
• PCL (Point Cloud Library)

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Opencv

OpenCV (Open Computer Vision) là một thư viện mã nguồn mở hàng đầu cho xử
lý về thị giác máy tính, machine learning, xử lý ảnh. OpenCV đươc viết bằng
C/C++, vì vậy có tốc độ tính toán rất nhanh, có thể sử dụng với các ứng dụng liên
quan đến thời gian thực. Opencv có các interface cho C/C++, Python Java vì vậy
hỗ trợ được cho Window, Linux, MacOs lẫn Android, iOS OpenCV có cộng đồng
hơn 47 nghìn người dùng và số lượng download vượt quá 6 triệu lần

Ứng dụng
Opencv có rất nhiều ứng dụng:
•Nhận dạng ảnh
•Xử lý hình ảnh
•Phục hồi hình ảnh/video
•Thực tế ảo
•Các ứng dụng khác

Robotic & Vision Lab RoVis


Các bước cài đặt:
 Bước 1: vào đường link: https://opencv.org/releases.html, sau đó tải phiên bản
opencv về máy, giải nén thư viện vào thư mục được định sẵn.
 Bước 2: vào thư mục: \\opencv3.2\build\x64\vc14\bin và chép toàn bộ dll của thư
viện vào thư mục SysWOW64 của window
 Bước 3: Vào linker/input như hình và add 2 thư viện: opencv_world320d.lib,
opencv_world320.lib

Robotic & Vision Lab RoVis


 Bước 4: Vào C/C++ , chọn Additional include Directories như hình và add đường dẫn
thư mục include vào: C:\Hanh\opencv3.2\build\include

Robotic & Vision Lab RoVis


Bước 5: Vào VC++ directories: chọn include directories và library directory như hình và
add đường dẫn thư mục include, và library trong thư mục build và thư mục lib

Robotic & Vision Lab RoVis


Open Image
#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat image;
image = imread("demo.jpg"); // Read the file
if (!image.data) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl;
return -1;
}
namedWindow("Display window", WINDOW_AUTOSIZE);// Create a window for display.
imshow("Display window", image); // Show our image inside it.

waitKey(0); // Wait for a keystroke in the window


return 0;
}

Robotic & Vision Lab RoVis


Open Webcam
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;

Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, COLOR_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}

Robotic & Vision Lab RoVis


#include "opencv2/opencv.hpp"
#include <iostream> cap >> frame;
// If the frame is empty, break immediately
using namespace std; if (frame.empty())
using namespace cv; break;
int main(){ // Write the frame into the file 'outcpp.avi'
// Create a VideoCapture object and use camera to capture the videovideo.write(frame);
VideoCapture cap(0); // Display the resulting frame
// Check if camera opened successfully imshow( "Frame", frame );
if(!cap.isOpened()) // Press ESC on keyboard to exit
{ char c = (char)waitKey(1);
cout << "Error opening video stream" << endl; if( c == 27 )
return -1; break;
} }
// When
// Default resolution of the frame is obtained.The default resolution everything
is system done, release the video
dependent.
int frame_width = cap.get(CV_CAP_PROP_FRAME_WIDTH); capture and write object
int frame_height = cap.get(CV_CAP_PROP_FRAME_HEIGHT); cap.release();
// Define the codec and create VideoWriter object.The outputvideo.release();
is stored in 'outcpp.avi' file.
// Closes all the windows
VideoWriter video("outcpp.avi",CV_FOURCC('M','J','P','G'),10, Size(frame_width,frame_height));
while(1) destroyAllWindows();
{ return 0;
Mat frame; }
// Capture frame-by-frame

Robotic & Vision Lab RoVis


UI CLR Form visual studio

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
Robotic & Vision Lab RoVis
UI CLR form

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e) {

VideoCapture cap(1); // open the default camera


Mat edges;
namedWindow("edges", 1);
for (;;)
{
Mat frame;

cap >> frame; // get a new frame from camera


cvtColor(frame, edges, COLOR_BGR2GRAY);
GaussianBlur(edges, edges, cv::Size(7, 7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
DrawCVImage(dispic1, edges);
waitKey(30);

Robotic & Vision Lab RoVis


Mat image to picture box
private: void DrawCVImage(System::Windows::Forms::Control^ control, cv::Mat&
colorImage)
{
System::Drawing::Graphics^ graphics = control->CreateGraphics();
System::IntPtr ptr(colorImage.ptr());
System::Drawing::Bitmap^ b;
switch (colorImage.type())
{
case CV_8UC3: // non-grayscale images are correctly displayed here
b = gcnew System::Drawing::Bitmap(colorImage.cols, colorImage.rows,
colorImage.step,
System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);
break;
case CV_8UC1: // grayscale images are incorrectly displayed here
b = gcnew System::Drawing::Bitmap(colorImage.cols, colorImage.rows,
colorImage.step,
System::Drawing::Imaging::PixelFormat::Format8bppIndexed, ptr);
break;
default:
break;
}
System::Drawing::RectangleF rect(0, 0, (float)control->Width, (float)control-
>Height);
graphics->DrawImage(b, rect);
} Robotic & Vision Lab RoVis
Install using cmake
Compiling OpenCV with OpenNI for Microsoft Kinect Sensor

http://seevisionc.blogspot.com/2012/07/compiling-opencv-with-openni-for.html

Robotic & Vision Lab RoVis


Download and extract opencv-3.3.1 and opencv_contrib-3.3.1
Go to https://github.com/opencv/opencv/releases and download opencv-3.3.1 source code zip
Go to https://github.com/opencv/opencv_contrib/releases and download opencv_contrib-3.3.1
source code zip

Robotic & Vision Lab RoVis


Now click configure.

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Check “INSTALL_C_EXAMPLES”
In flag “OPENCV_EXTRA_MODULES_PATH”, give path of modules directory within
opencv_contrib-3.3.1
Check option WITH_OPENNI and click configure again

Robotic & Vision Lab RoVis


• Make sure that the folders to OpenNI and Kinect drivers are correct and click configure
again.
• If everything is fine then there will be no entries marked in red. To make sure that the build
is configured properly with OpenNI, see the OPENNI field in the output window, it should say
YES and should be configured with SensorKinect drivers.

Robotic & Vision Lab RoVis


Now finally click Generate and go to install folder to find the file named OpenCV.sln. Open
this project file and in the solution explorer towards the left hand side, select ALL_BUILD
and build solution. This will take a while.

Robotic & Vision Lab RoVis


Robotic & Vision Lab RoVis
Exercise

1. Open a movie in computer


2. Open webcam and save frame when click button s

Robotic & Vision Lab RoVis

You might also like