📌  相关文章
📜  MATLAB –使用Scratch的Robert Operator进行图像边缘检测

📅  最后修改于: 2021-04-16 09:00:20             🧑  作者: Mango

Robert运算符:此基于梯度的运算符通过离散微分计算图像中对角线相邻像素之间差异的平方和。然后进行梯度近似。它使用以下2 x 2内核或掩码–

    \[M_{x}=\left[\begin{array}{ccc}1 & 0 \\ 0 & -1\end{array}\right] \quad M_{y}=\left[\begin{array}{ccc}0 & 1 \\ -1 & 0\end{array}\right]\]

在MATLAB中的实现:

% MATLAB Code | Robert Operator from Scratch
  
% Read Input Image
input_image = imread('[name of input image file].[file format]');
  
% Displaying Input Image
input_image = uint8(input_image);
figure, imshow(input_image); title('Input Image');
  
% Convert the truecolor RGB image to the grayscale image
input_image = rgb2gray(input_image);
  
% Convert the image to double
input_image = double(input_image);
  
% Pre-allocate the filtered_image matrix with zeros
filtered_image = zeros(size(input_image));
  
% Robert Operator Mask
Mx = [1 0; 0 -1];
My = [0 1; -1 0];
  
% Edge Detection Process
% When i = 1 and j = 1, then filtered_image pixel  
% position will be filtered_image(1, 1)
% The mask is of 2x2, so we need to traverse 
% to filtered_image(size(input_image, 1) - 1
%, size(input_image, 2) - 1)
for i = 1:size(input_image, 1) - 1
    for j = 1:size(input_image, 2) - 1
  
        % Gradient approximations
        Gx = sum(sum(Mx.*input_image(i:i+1, j:j+1)));
        Gy = sum(sum(My.*input_image(i:i+1, j:j+1)));
                 
        % Calculate magnitude of vector
        filtered_image(i, j) = sqrt(Gx.^2 + Gy.^2);
         
    end
end
  
% Displaying Filtered Image
filtered_image = uint8(filtered_image);
figure, imshow(filtered_image); title('Filtered Image');
  
% Define a threshold value
thresholdValue = 100; % varies between [0 255]
output_image = max(filtered_image, thresholdValue);
output_image(output_image == round(thresholdValue)) = 0;
  
% Displaying Output Image
output_image = im2bw(output_image);
figure, imshow(output_image); title('Edge Detected Image');

输入图像–

过滤图像:

边缘检测图像:

好处:

  1. 边缘和方向的检测非常容易
  2. 保留对角方向点

局限性:

  1. 对噪音非常敏感
  2. 边缘检测不是很准确