How to write an if-else statement for this condition in MATLAB?

for each pixel, do
if R>90 & R>G & R>B
classify the pixel as **Healthy**
else
classify the pixel as non-healthy
I am trying to implement an algorithm which reads a skin lesion image and after extracting the R, G, and B values, it classifies the lesion into healthy skin or non-healthy skin based on the if condition
However, when I try to implement it, only the non-healthy skin array is getting updated inside the for loop and the healthy skin array remains zero. I do not know how to overcome this glitch. Please help.
hs=zeros(m,n); %initialising heaklthy skin array
nhs=zeros(m,n); %initialising non-healthy skin array
R=colorSkin(:, :, 1);
G=colorSkin(:, :, 2);
B=colorSkin(:, :, 3);
for i = 1:m
for j = 1:n
if R>90&R>B&R>G
hs(i, j)= colorSkin(i, j);
else
nhs(i,j)=colorSkin(i,j);
end
end
end

1 Comment

Basically the indexing of the R, G, B plane was missing in the if loop. Just added a few more lines and changed the type of the output variables,to verify whether the code is working.

      sk=imread('skincancer1.jpg');
      [m n dim]=size(sk);
      hs=zeros(m,n); %initialising heaklthy skin array
      nhs=zeros(m,n); %initialising non-healthy skin array
      R=sk(:, :, 1);
      G=sk(:, :, 2);
      B=sk(:, :, 3);
      for i = 1:m
          for j = 1:n     
              if R(i,j)>150&R(i,j)>180&R(i,j)>B(i,j)&R(i,j)>G(i,j)
                  hs(i, j)= sk(i, j);
              else
                  nhs(i,j)=sk(i,j);
              end
          end
      end
      imshow(uint8(nhs))
      title('normal');
      figure;imshow(uint8(hs))
      title('cancer');

Sign in to comment.

 Accepted Answer

Your line
if R>90&R>B&R>G
is testing all of R and all of B and all of G at the same time. R>90&R>B&R>G is calculating a 2D array of logical results. Then "if" is asked to make a decision based upon the 2D array of results. The behavior is well-defined in MATLAB: MATLAB considers the condition to be true if and only if all of the values are non-zero . Which would require that the entire matrix consisted of healthy skin.
You probably wanted
if R(i,j) > 90 & R(i,j) > B(i,j) & R(i,j) > G(i,j)
However, your code can be replaced with
hs = zeros(m, n, class(colorSkin) ); %initialising heaklthy skin array
nhs = zeros(m, n, class(colorSkin) ); %initialising non-healthy skin array
R = colorSkin(:, :, 1);
G = colorSkin(:, :, 2);
B = colorSkin(:, :, 3);
ROI = R > 90 & R > B & R > G;
hs(ROI) = R(ROI);
nhs(~ROI) = R(~ROI);
It is not clear to me why you wanted to copy the red channel into hs and nhs: I suspect you overlooked that colorSkin(i, j) is a 2D reference to a 3D array.
Are you sure that you do not just want to create an ROI? An array of true and false values representing where the skin is healthy or not?
Or perhaps you want to transfer full color:
hs = colorSkin; %initialising heaklthy skin array
nhs = colorSkin; %initialising non-healthy skin array
R = colorSkin(:, :, 1);
G = colorSkin(:, :, 2);
B = colorSkin(:, :, 3);
ROI = R > 90 & R > B & R > G;
ROI3 = ROI(:,:,[1 1 1]);
hs(~ROI3) = 0;
nhs(ROI3) = 0;

15 Comments

Actually, this is the algorithm I was following.
I got stuck at the healthy skin part and wasn't able to proceed further. Your answer really helped! Thank you.
How do I proceed with the second part of the algorithm?
The first bit simplifies to
R = colorSkin(:, :, 1);
G = colorSkin(:, :, 2);
B = colorSkin(:, :, 3);
hs = R > 90 & R > B & R > G;
After that you can do things like
B(hs) ./ R(hs) + G(hs)
I tried that. But I am getting an error that says "Subscript indices must either be real positive integers or logicals."
nB=B(nhs)./(R(nhs)+B(nhs)+G(nhs));
Rs=mean(R(hs));
R = colorSkin(:, :, 1);
G = colorSkin(:, :, 2);
B = colorSkin(:, :, 3);
hs = R > 90 & R > B & R > G;
Rs = mean(R(hs));
Once you have determined the mean of the red of the healthy skin, you do not need to know anymore whether a pixel was healthy or not. You do not need the nhs array for any purpose, and you are finished with the hs array now.
nB = B./R + G + B;
rR = R./Rs;
veil = nB >= 0.3 & (-194 <= rR & rR < =51);
Error: File: blah3.m Line: 17 Column: 41
The expression to the left of the equals sign is not a valid target for an assignment.
This is the error I get when I run your last line of code:
veil = nB >= 0.3 & (-194 <= rR & rR < =51);
How to overcome it?
I am getting a completely black image. I tried using 'double' but it still doesn't work.
Please attach your code, and a copy of the image you are loading.
clc;
clear all;
close all;
sk=imread('veilfused.jpg');
figure();
subplot(3,1,1)
imshow(sk);
title('original Image');
R = sk(:, :, 1);
G = sk(:, :, 2);
B = sk(:, :, 3);
hs = R > 90 & R > B & R > G;
subplot(3,1,2)
imshow(hs);
title('healthy skin');
Rs = mean(R(hs));
nB =double( B./R + G + B);
rR = double(R./Rs);
veil =(nB >= 0.3 & (194 <= rR & rR <= 51));
subplot(3,1,3);
imshow((veil));
title('veil');
You lost a "-". You have
veil =(nB >= 0.3 & (194 <= rR & rR <= 51));
instead of
veil =(nB >= 0.3 & (-194 <= rR & rR <= 51));
Okay, I am getting the image now, but it is still not detecting the blue-white veil part in the original image. Instead I am getting the whole image in the veil part. In the algorithm which I have attached they haved used -194 and -51, I tried that but that gives me a black image again. Where am I going wrong?
Any guidance on my query above?
"Any guidance on my query above?"
I had to sleep.
The algorithm says that the hs computation should be done only on pixels outside of the lesion. "Extract region outside the lesion border..."
Yep. Got it now. Thank you so much!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!