필터 지우기
필터 지우기

truecolor error in changing RGB of pixels

조회 수: 1 (최근 30일)
David Phung
David Phung 2014년 10월 19일
댓글: Image Analyst 2014년 10월 19일
Hi, Ive been trying to change the pixels of an image but I get an error everytime I try to change something other than red. For example, red=20, blue=0, green=0 works. However, red=10 blue=10 green=50 doesnt work. The error I get is
Error using image
TrueColor CData contains element out of range 0.0 <= value <= 1.0
Error in changeRGB (line 19)
image(pic) %show image
This is the code
name= input('Enter name of file: \n', 's'); %file name
format= input ('Enter format of file: \n', 's');%file format
redchange = input('Change in percent of red: \n');%red change
bluechange = input ('Change in percent of blue: \n');%blue change
greenchange = input ('Change in percent of green: \n');%green change
pic= imread(name, format); %reading image
pic= double(pic); %converting to doubles
pic=pic/255; %scaling everything by dividing by 255.
pic(:,:,1)= redchange*pic(:,:,1); %multiply the values by percentage
pic(pic(:,:,1)>1)=1; %changing all values over 1 equal to one
pic(:,:,2)= greenchange*pic(:,:,2); %multiply the values by percentage
pic(pic(:,:,2)>1)=1; %changing all values over 1 equal to one
pic(:,:,3)= bluechange*pic(:,:,3); %multiply the values by percentage
pic(pic(:,:,3)>1)=1; %changing all values over 1 equal to one
image(pic) %show image
axis off %no axis

채택된 답변

Mohammad Abouali
Mohammad Abouali 2014년 10월 19일
편집: Mohammad Abouali 2014년 10월 19일
pic(pic(:,:,2)>1)=1
and
pic(pic(:,:,3)>1)=1
These are not doing what you think they should do.
The best is to change the last part of your code like this
pic(:,:,1)= redchange*pic(:,:,1); %multiply the values by percentage
pic(:,:,2)= greenchange*pic(:,:,2); %multiply the values by percentage
pic(:,:,3)= bluechange*pic(:,:,3); %multiply the values by percentage
pic(pic>1)=1;
pic(pic<0)=0;
so remove all three pic(pic(:,:,1)>1)=1, pic(pic(:,:,2)>1)=1, and pic(pic(:,:,3)>1)=1 and just before image(pic) replace them with pic(pic>1)=1; and pic(pic<0)=0;
  댓글 수: 2
David Phung
David Phung 2014년 10월 19일
Oh my god. Thank you so much! I was thinking about doing something like that, but i used if else statements instead and that didnt work out too well for me. Would you mind explaining the error in my method? I get how yours work.
Image Analyst
Image Analyst 2014년 10월 19일
Please mark the answer as accepted since you said it works. It's because you used logical indexing wrong. You got a logical index based on just one color channel. Then you used that it index into a 3D array. Since your logical index will have only rows*columns elements, it will change ONLY those. Since it does not have rows*columns*numberOfColorChannels, it will clip only a third of the pixels, not all of them. To get all of the logical indexes that cover all three color channels, do what Mohammad showed you.
Alternatively do it a 2D image at a time
greenChannel = pic(:, :, 2);
greenChannel(greenChannel>1)=1;
In the above case both the logical index vector greenChannel>1, and the greenChannel itself have the same number of elements so all elements will get checked instead of just some of them.

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

추가 답변 (0개)

카테고리

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