Converting an image to grayscale problem

조회 수: 1 (최근 30일)
Ray
Ray 2014년 11월 10일
댓글: Image Analyst 2014년 11월 10일
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.

답변 (1개)

Image Analyst
Image Analyst 2014년 11월 10일
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
Ray
Ray 2014년 11월 10일
This is for a project in class. Basically, I am only allowed to run an autograder on the code and the autograder gives me this error after testing it against an image:
Beginning convertImageToGrayscale.m easy test 1... Checking student output against reference... test aborted. Error caught and test failed. Error: Integers can only be combined with integers of the same class, or scalar doubles.
Image Analyst
Image Analyst 2014년 11월 10일
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);

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

카테고리

Help CenterFile Exchange에서 Modify Image Colors에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by