Error using Sobel edge filter with a color image

18 views (last 30 days)
Hello!
I get the following error message:
Error using edge Expected input number 1, I, to be two-dimensional.
Error in edge>parse_inputs (line 476)
validateattributes(I,{'numeric','logical'},{'real','nonsparse','2d'},mfilename,'I',1);
Error in edge (line 208)
[a,method,thresh,sigma,thinning,H,kx,ky] = parse_inputs(varargin{:});
Error in V01_06042017 (line 3)
BW1 = edge(I,'sobel');
Code:
I = imread('Bild.tif');
BW1 = edge(I,'sobel');
imshow (BW1);
line 476: validateattributes(I,{'numeric','logical'},{'real','nonsparse','2d'},mfilename,'I',1);
line208: [a,method,thresh,sigma,thinning,H,kx,ky] = parse_inputs(varargin{:});
Can anyone help me? Thank you very much.

Answers (2)

Saurabh Gupta
Saurabh Gupta on 12 Apr 2017
As the error mentions, the variable 'I' is expected to be 2-D . Verify if that is the case. I'm guessing that your image is a color image and imread returns 3-D data.
If your image uses CMYK, you may find this post helpful for converting your data to RGB.
RGB image data can be converted to grayscale or indexed using rgb2gray or rgb2ind respectively, which can then be used in the edge function.

Image Analyst
Image Analyst on 14 Jul 2022
You either have to convert the color image to gray scale,
rgbImage = imread('peppers.png');
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Color Image')
[rows, columns, numberOfColorChannels] = size(rgbImage);
if numberOfColorChannels == 3
rgbImage = rgb2gray(rgbImage);
subplot(2, 2, 2);
imshow(rgbImage);
title('Gray Scale Image')
end
BW1 = edge(rgbImage,'sobel');
subplot(2, 2, 3:4);
imshow(BW1);
title('Sobel Edge Image of Grayscale Image')
% or do it on each color channel one at a time.
figure;
subplot(2, 3, 1);
imshow(rgbImage);
title('Original Color Image')
rgbImage = imread('peppers.png');
[rows, columns, numberOfColorChannels] = size(rgbImage);
if numberOfColorChannels == 3
BW1 = zeros(rows, columns, numberOfColorChannels, 'uint8');
for k = 1 : numberOfColorChannels
thisColorChannel = rgbImage(:, :, k);
BW1(:, :, k) = uint8( 255 * edge(thisColorChannel,'sobel'));
subplot(2, 3, k+1);
imshow(BW1(:,:,k), []);
caption = sprintf('Edge image of Color Channel %d', k);
title(caption)
end
subplot(2, 3, 5:6);
imshow(BW1);
title('Color Sobel Edge Image')
end

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!