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

7 views (last 30 days)
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
GEEVARGHESE TITUS
GEEVARGHESE TITUS on 21 Feb 2017

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

Walter Roberson
Walter Roberson on 21 Feb 2017
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
Walter Roberson
Walter Roberson on 24 Apr 2017
The algorithm says that the hs computation should be done only on pixels outside of the lesion. "Extract region outside the lesion border..."

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!