how to write this if else condition statement in MATLAB?

夙愿已清 提交于 2019-12-25 18:12:29

问题


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 healthy skin array
nhs=0;         %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:


As an alternative, vectorized approach:

R=colorSkin(:, :, 1);
G=colorSkin(:, :, 2);
B=colorSkin(:, :, 3);
skin=repmat(R>90 & R>B & R>G,1,1,3);

hs=colorSkin;
hs(~skin)=0;
nhs(skin)=0;

This code should be considerably faster than looping




回答2:


When you running the loop, you are just checking the same matrix every time.

Here R, G, B all are a 2-D matrix.

I think you want to do the checking for each pixel's R, G, and B values.

So, when you did

if R > 90 & R > B & R > G

What it is doing is checking that all the elements of the R matrix are > 90, which may not be true for most of the time.

So the correct implementation is:

if R(i, j) > 90 & R(i, j) > G(i, j) & R(i, j) > B(i, j)

Change this code, and it should work fine. Please do comment if you want any further clarification.



来源:https://stackoverflow.com/questions/42358746/how-to-write-this-if-else-condition-statement-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!