如何在MATLAB中编写if else条件语句? [英] how to write this if else condition statement in MATLAB?

查看:1488
本文介绍了如何在MATLAB中编写if else条件语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for each pixel, do
      if R>90 & R>G & R>B 
         classify the pixel as **Healthy**
      else 
         classify the pixel as non-healthy 

我正在尝试实现一种读取皮肤病变图像的算法,并在提取R,G和B值后,将病变分类为健康皮肤或非健康皮肤。 如果条件

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

但是,当我尝试实现它时,只有非健康的皮肤数组才会在for循环内更新健康的皮肤阵列保持为零。我不知道如何克服这个故障。请帮忙。

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


推荐答案

作为替代,矢量化方法:

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

这篇关于如何在MATLAB中编写if else条件语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆