edge detection operator error

조회 수: 4 (최근 30일)
dipto das
dipto das 2019년 10월 23일
댓글: DGM 2025년 7월 5일
My MATLAB keeps showing an error when I try to use any edge detection operators.
Here is my code:
a = imread('D:\videos\H67931.jpg');
figure;
imshow(a);
b = rgb2gray(a);
figure;
imshow(b);
c = edge(b,'sobel');
figure;
imshow(c);
  댓글 수: 3
Hitesh
Hitesh 2025년 4월 2일
I did not encountered any error on executing the piece of code you have shared. If you could share the error message and the image file you are using to execute the code, it would help in reproducing the issue.
DGM
DGM 2025년 7월 5일
About the only place I would expect an error here is with rgb2gray(), if the image is not RGB. It's possible to have a single-channel JPG, but I wouldn't expect one from a video source.
In modern versions after R2020b, don't use rgb2gray() unless you need your code to be backwards compatible. Use im2gray() instead. If you're using rgb2gray(), you need to wrap it in a test to make sure it is never fed a single channel image, because rgb2gray() has some ridiculous legacy behavior.
% generate a single-channel JPG
inpict = imread('cameraman.tif');
imwrite(inpict,'gray.jpg')
% read it back
inpict = imread('gray.jpg');
% im2gray() will pass a single-channel image without complaint
graypict1 = im2gray(inpict);
% rgb2gray() assumes a single channel image is a colormap.
% so unless you can ensure the image depth,
% you have to do the checks externally every time you use it.
if size(inpict,3) == 3
graypict2 = rgb2gray(inpict);
elseif size(inpict,3) == 1
graypict2 = inpict;
else
error('Expected image to have either 1 or 3 channels')
end
% ... otherwise it will either fail
graypict3 = rgb2gray(inpict);
Error using rgb2gray>parse_inputs (line 79)
Colormap must be a c-by-3 matrix. If you want to convert a 2-D grayscale image, use im2gray instead.
Error in rgb2gray (line 51)
isRGB = parse_inputs(X);
^^^^^^^^^^^^^^^^^^^^^^^^
% ... or in the case of any tiny gray image with 3 columns
% it will be treated as a colormap instead of an image.
% it will fail if the image is not specifically class 'double'
tinypict = im2uint8(rand(4,3)); % must be unit-scale double
imshow(tinypict,'InitialMagnification','fit')
tinygraypict = rgb2gray(tinypict) % throws an error
Error using rgb2gray>parse_inputs (line 79)
Colormap must be a c-by-3 matrix. If you want to convert a 2-D grayscale image, use im2gray instead.
Error in rgb2gray (line 51)
isRGB = parse_inputs(X);
^^^^^^^^^^^^^^^^^^^^^^^^
% ... or in the case that it is specifically unit-scale 'double'
% it will destroy the image because it thinks it's a colormap.
tinypict = rand(4,3); % must be unit-scale double
imshow(tinypict,'InitialMagnification','fit')
tinygraypict = rgb2gray(tinypict); % treats it as a colormap
imshow(tinygraypict,'InitialMagnification','fit')

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2025년 7월 4일
It seems to work fine:
a = imread('peppers.png');
figure;
imshow(a);
b = rgb2gray(a);
figure;
imshow(b);
c = edge(b,'sobel');
figure;
imshow(c);
If you have any more questions, then attach your image with the paperclip icon after you read this:

Community Treasure Hunt

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

Start Hunting!

Translated by