Binary Processing
Binary Processing
depth discontinuity
illumination discontinuity
edges correspond to
Source: L. Lazebnik extrema of derivative
: 1 -1 : -1
1
Source: S. Seitz
f*h
RoVis
Robotic & Vision Lab Source: S. Seitz
Derivative of Gaussian filter
x-direction y-direction
Laplace operator f f f 2
f f 2 2
x
f 2 2 [ x y ] f f
x y y “divergence
rotationally invariant of gradient”
second derivative for 2D functions
0 0 0 0 -1 0 0 -1 0
-1 2 -1 0 2 0 -1 4 -1
0 0 0 0 -1 0 0 -1 0
Ontario
The University of
Ontario
The University of
Ontario
•Lọc cạnh (sobel filter):
Sobel Là một phép lọc giúp tìm đường biên cho ảnh. Phép tính này dựa trên sự thay đổi đột ngột cường độ sáng của
2 pixel kế nhau. Bằng cách lấy đạo hàm cường độ sáng ta có được giá trị rất lớn. giá trị này biểu hiện đây là một
điểm nằn trên cạnh như hình:
Trong OpenCV để sử dụng Sobel cho một hình ảnh, ta sử dụng ma trận kernel tương đượng lấy đạo hàm bậc nhất sau:
F
F i, j * Gx
x
F
F i, j * Gy
y
2 2
F F
M i, j
x y
F / y
i, j arctan( )
F / x
The edge detected image can be obtained from the sobel gradient by
using a threshold value
Robotic & Vision Lab RoVis
Basic Steps followed in Sobel Edge Detection:
1. Obtain the gradient of the image.
2. Find the magnitude
3. Threshold the gradient image.
A=imread('peppers.png');
B=rgb2gray(A);
C=double(B);
for i=1:size(C,1)-2
for j=1:size(C,2)-2
%Sobel mask for x-direction:
Gx=((2*C(i+2,j+1)+C(i+2,j)+C(i+2,j+2))-
(2*C(i,j+1)+C(i,j)+C(i,j+2)));
%Sobel mask for y-direction:
Gy=((2*C(i+1,j+2)+C(i,j+2)+C(i+2,j+2))-
(2*C(i+1,j)+C(i,j)+C(i+2,j)));
end
end
figure,imshow(B); title('Sobel gradient');
B=uint8(B);
figure,imshow(~B);title('Edge
detected Image');
%Input Image
A=imread('coins.png');
%Input Image
A=imread('coins.png');
%Preallocate the matrices with zeros
I=zeros(size(A));
%Filter Masks
F1=[-1 0 1;-2 0 2; -1 0 1];
F2=[-1 -2 -1;0 0 0; 1 2 1];
A=double(A);
for i=1:size(A,1)-2
for j=1:size(A,2)-2
%Gradient operations
Gx=sum(sum(F1.*A(i:i+2,j:j+2)));
Gy=sum(sum(F2.*A(i:i+2,j:j+2)));
%Magnitude of vector
I(i+1,j+1)=sqrt(Gx.^2+Gy.^2);
end
end
I=uint8(I);
B=im2bw(B);
figure,imshow(B);title('Edge detected Image');
Với
o src: Là ảnh gốc.
o dst: Là ảnh sau khi thực hiện phép lọc số ảnh.
o ksize: Là kích thước của ma trận lọc. Giá trị mặc định là 3.
o ddepth: Làđộ sâu của ảnh sau phép lọc: VD: CV_32F, CV_64F,...
o dx: Là đạo hàm theo hướng x.
o dy: Là đạo hàm theo hướng y.
o Để đạo hàm theo hướng nào thì ta đặt giá trị đó lên 1.
o scale và delta: Là 2 thông số tùy chọn cho việc tính giá trị đạo hàm lưu giá
trị vi sai vào ảnh sau phép lọc. Mặc định là 1 và 0.
o borderType: Là phương pháp để ước lượng và căn chỉnh các điểm ảnh
nếu phép lọc chúng vượt ra khỏi giới hạn của ảnh. giá trị mặc định là 4.
S
S i, j * Gx
x
S
S i, j * Gy
y
2 2
S S
M i, j
x y
S / y
i, j arctan( )
S / x
Robotic & Vision Lab RoVis
Non-Maximum Suppression
We wish to mark points along the curve where the magnitude is biggest.
We can do this by looking for a maximum along a slice normal to the curve
(non-maximum suppression). These points should form a curve. There are
then two algorithmic issues: at which point is the maximum, and where is the
next one?
Robotic & Vision Lab RoVis
Non-Maximum Suppression
if S x, y S x , y
S x , y
M x, y & S x, y S x , y
x, y 0 otherwise
x, y
x,y and x,y are the neighbors of x,y in S
x, y along the direction normal to an edge
• Once the edge direction is known, the next step is to relate the
edge direction to a direction that can be traced in an image. So if
the pixels of a 5x5 image are aligned as follows:
• x x x x x
x x x x x
x x a x x
x x x x x
x x x x x
• Then, it can be seen by looking at pixel "a", there are only four
possible directions when describing the surrounding pixels
• 0 degrees (in the horizontal direction),
• 45 degrees (along the positive diagonal),
• 90 degrees (in the vertical direction), or
• 135 degrees (along the negative diagonal).
Robotic & Vision Lab RoVis
Non-Maximum Suppression
Sy 2
tan θ
Sx 3 1
Tìm góc:
Áp dụng Ngưỡng Hysteresis
Áp dụng ngưỡng là 40
edge.convertTo(draw, CV_8U);
namedWindow("image", CV_WINDOW_AUTOSIZE);
imshow("image", draw);
waitKey(0);
return 0; Robotic & Vision Lab RoVis
Ví dụ 5.2: tìm cạnh biên của hình sau sử dụng thư viện opencv [61]
imshow("laplacian", laplacian);
for (int y = 1; y < result.rows - 1; ++y)
{
for (int x = 1; x < result.cols - 1; ++x)
{
result.at<uchar>(y, x) = 0;
if (laplacian.at<double>(y - 1, x)*laplacian.at<double>(y + 1, x) < 0)
{
result.at<uchar>(y, x) = 255;
}
if (laplacian.at<double>(y, x - 1)*laplacian.at<double>(y, x + 1) < 0)
{
result.at<uchar>(y, x) = 255;
}
if (laplacian.at<double>(y + 1, x - 1)*laplacian.at<double>(y - 1, x + 1) < 0)
{
result.at<uchar>(y, x) = 255;
}
if (laplacian.at<double>(y - 1, x - 1)*laplacian.at<double>(y + 1, x + 1) < 0)
{
result.at<uchar>(y, x) = 255;
}
}
}
threshold(laplacian, result, 1, 255, CV_THRESH_BINARY);
Trường hợp 2: Không có gì thay đổi khi ta di chuyển khung hình dọc theo đường thẳng=> ta đang di chuyển trên đường biên hay cạnh.
Trường hợp 3: Thay đổi lớn khi ta di chuyển khung hình theo mọi hướng=> ta đang di chuyển ở góc
E (u , v) w( x, y )[ I ( x u , y v) I ( x, y )]2
x, y
Trong đó, E(u, v) Tổng số bình phương giá trị độ lệch, w(x, y) là cửa sổ tại (x,
y), I(x, y) và I(x+ u, y + v) là giá trị cường độ sáng của pixel tại các vị trí (x,
y) và (u + x, v + y).
gọi λ1 và λ2 là các trị riêng của M. Khi đó, biểu thức biểu hiện đáp ứng góc
sẽ quyết định xem cửa sổ w có chứa góc hay là không.
R det M k (trace M )2
Với
det M 12
trace M 1 2
I x2 I x I y
Bước 2: xây dựng ma trận M w( x, y ) 2
x, y x y y
I I I
Nếu giá trị |R| nhỏ thì vùng pixel đó không chứa thông tin có giá trị,
còn gọi là “flat” region;
Nếu R 0 , tức là khi 1 2 hoặc 2 1 , thì vùng pixel đó chỉ
chứa cạnh “edge”;
Nếu giá trị R lớn, tức là khi 1 1, 2 1 và 1 2 , thì vùng
pixel này chứa góc “corner”.
cvZero(evec);
cvZero(eval);
cvReleaseMat(&evec);
cvReleaseMat(&eval);
Tính Gradient của vùng pixel theo phương x, I x có tổng cộng 14 ô, và tổng Tính Gradient của vùng pixel theo phương y, I y có tổng cộng 14 ô, và tổng
là 14 => I x =14
là: 14 => I y =14
I x I y =2
all i , j in N
I (i, j )
2
I ( x, y ) I ( x, y )
all i , j in N
x
x y
i, j i, j
M ( x, y ) all i , j in N 2
I ( x, y ) I ( x, y ) I (i, j )
all i , j in N
i , j x y
i, j y
14 2
M ( x, y ) => 1 12, 2 14
2 14
Cả 2 gái trị eigen đều lớn=> đây là góc
Eigen là: 0 và 28