Converting an image to grayscale problem
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I'm supposed to convert a 3'd matrix which represents an image to a 2-d grayscale matrix, but am running into the following error with my code:
function [gray] = convertImageToGrayscale(orig)
% Takes in a 3-d Matrix and converts it to 2-d
gray = .299*orig(:,:,1) + .587*orig(:,:,2) + .114*orig(:,:,3);
gray = uint8(gray);
end
Error: Integers can only be combined with integers of the same class, or scalar doubles.
How do I fix this? Any help would be appreciated.
0 Kommentare
Antworten (1)
Image Analyst
am 10 Nov. 2014
gray() is a built in function so don't use that for a variable.
There should not be any problem with that code. Here's proof:
close all;
% Create a uint8 RGB image.
orig = uint8(randi(255, 100,100,3));
% Display it.
subplot(1,2,1);
imshow(uint8(orig));
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Takes in a 3-d Matrix and converts it to 2-d
grayImage = .299*orig(:,:,1) + .587*orig(:,:,2) + .114*orig(:,:,3);
grayImage = uint8(grayImage);
subplot(1,2, 2);
imshow(grayImage);
You must have gotten that error message from some other code. Please post that code.
4 Kommentare
Image Analyst
am 10 Nov. 2014
Well the autograder is wrong . You ARE combining an integer with a scalar double, which it explicitly says IS allowed. So try casting the images to double to get around the stupidity of the autograder.
orig = double(orig);
gray = .299*orig(:,:,1) + .587*orig(:,:,2) + .114*orig(:,:,3);
Siehe auch
Kategorien
Mehr zu Modify Image Colors finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!