A beginners question to clarify something I am trying to do it MATLAB regarding an image

조회 수: 1 (최근 30일)
I am trying to learn how to use MATLAB so therefore am a complete beginner, I am sorry if this question is very basic or I do not use the correct terms/explain myself properly as I have no programming experience and I am finding it quite difficult to understand the help guide and textbook I have.
So I have created three variables for the colour bands by typing
Image_red=Image(:,:,1); Image_green=Image(:,:,2); Image_blue=Image(:,:,3);
and I can display one (eg red) by typing
figure, imagesc(Image_red), colormap(‘gray’)
The textbook I am working through then starts with the following instructions on using a variable to control the band selection.
Type >>band=1;
Then type >>Image_new=Image(:,:,band);
We will still get the red band in the new image. If we set band=2 then we would get the green band..
Write a new function called ‘draw1band.m’ which uses two input variables: an image name and another variable called ‘band’ to draw a grayscale image of either of the 3 bands. Once written, you should be able to type:
>>draw1band(‘srgmatlab’, 3)
And get a figure of the blue band.
I can't for the life of me figure out how to do this! Could anyone offer any help please? Thank you in advance. Sam
  댓글 수: 1
Jan
Jan 2011년 8월 3일
Please post what you have done so far and ask a specific question.
Reading the Getting Started sections of the documentation will help to learn the basics.

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

채택된 답변

Florin Neacsu
Florin Neacsu 2011년 8월 3일
Hi,
Have a look at "function". type
doc function
In this case you need something like:
function [] = draw1band(inputImage,band)
%maybe sanity checks for band and inputImage
oneCh=inputImage(:,:,band);
figure
imagesc(oneCh);
colormap gray;
end
You need to save this as an .m file. You can do this in many ways. Maybe try :
edit draw1band
Feel free to have a look at the FAQ page http://matlab.wikia.com/wiki/FAQ
Regards, Florin

추가 답변 (2개)

Paulo Silva
Paulo Silva 2011년 8월 3일
Simple code to show one image and each rgb components on different axes
name='ngc6543a.jpg';
subplot(411)
rgb = imread(name);
imagesc(rgb); title(['RGB image ' name])
for band=1:3
subplot(4,1,band+1)
im=rgb(:,:,band);
imagesc(im); title(['Image using band=' num2str(band)])
colormap('gray')
end
Now one possible function
function draw1band(Iname,band)
%example use from MATLAB documentation
%Iname='ngc6543a.jpg';
%band=1;
subplot(211)
rgb = imread(Iname);
imagesc(rgb); title(['RGB image ' Iname])
subplot(212)
im=rgb(:,:,band);
imagesc(im); title(['Image using band=' num2str(band)])
colormap('gray')
end

Sam
Sam 2011년 8월 5일
Thanks, both were good answers - and sorry next time I will be more specific. Thank you.

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by