Edge detection error message

1 view (last 30 days)
Why isn't the following code working?
>> str=sprintf('Scratch1.jpg');
>> img_ORG = imread(str);
>> fsc = edge(img_ORG, 'Canny');
Error using edge
Expected input number 1, I, to be two-dimensional.
Error in edge>parse_inputs (line 449)
validateattributes(I,{'numeric','logical'},{'real','nonsparse','2d'},mfilename,'I',1);
Error in edge (line 189)
[a,method,thresh,sigma,thinning,H,kx,ky] = parse_inputs(varargin{:});

Accepted Answer

Image Analyst
Image Analyst on 12 Feb 2017
It's color, not gray scale like you had expected. Use this code to convert to gray scale:
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
  3 Comments
Image Analyst
Image Analyst on 12 Feb 2017
The problem is that your code throws an error if img_ORG is already a gray scale image. My code only calls rgb2gray() if img_ORG is an RGB image. See how I check numberOfColorChannels and only call it if numberOfColorChannels is 3? Therefore no error will ever be thrown with my code, whereas it could with your code if you ever read in a gray scale image. So my code is more robust than your code. Writing code for almost 40 years. mostly for one of the world's largest companies, has taught me that it's better to be safe and robust, even if you think you don't need to be.
Secondly your use of cryptic, non-descriptive variable names like img_ORG and fsc, and the lack of comments, make the code you've posted unmaintainable should someone ever need to inherit your code - this is intolerable at a large company with mission critical software especially if people's lives or safety are at risk.
Walter Roberson
Walter Roberson on 16 Aug 2017
mohd abdul-raouf comments to Image Analyst:
I agree , Robustness is must requirement for a Good Programmer

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!