如何在Matlab中重新分类图像? [英] How to reclassify images in Matlab?

查看:184
本文介绍了如何在Matlab中重新分类图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Matlab将连续数据重新分类为分类数据。以下脚本采用4波段(红色,绿色,蓝色,近红外)航拍图像并计算归一化差异植被指数(即显示健康绿色植被的植被指数)。然后,脚本将值从(-1到1)重新调整为(0 - 255)。这是我试图在脚本的第三部分重新分类的矩阵 %%重新分类Imag1矩阵。我试图使用条件语句来执行重分类,尽管这可能是错误的方法。脚本中的重新分类步骤没有任何明显的效果。

I am attempting to reclassify continuous data to categorical data using Matlab. The following script takes a 4-band (Red, Green, Blue, nIR) aerial image and calculates the normalized difference vegetation index (i.e. a vegetation index showing healthy green vegetation). The script then rescales the values from (-1 to 1) to (0 - 255). This is the matrix I am trying to reclassify in the third section of the script %% Reclassify Imag1 matrix. I am attempting to use conditional statements to perform the reclassification, although this may be the wrong approach. The reclassification step in the script does not have any apparent effect.

如何将连续值(0 - 255)重新分类为分类值(1,2, 3,4)以逐个单元为基础?

file = 'F:\path\to\naip\image\4112107_ne.tif';
[Z R] = geotiffread(file);
outputdir = 'F:\temp\';

%% Make NDVI calculations
NIR = im2single(Z(:,:,4));
red = im2single(Z(:,:,3));
ndvi = (NIR - red) ./ (NIR + red);
ndvi = double(ndvi);

%% Stretch NDVI to 0-255 and convert to 8-bit unsigned integer
ndvi = floor((ndvi + 1) * 128); % [-1 1] -> [0 256]
ndvi(ndvi < 0) = 0;             % not really necessary, just in case & for symmetry
ndvi(ndvi > 255) = 255;         % in case the original value was exactly 1
Imag1 = uint8(ndvi);

%% Reclassify Imag1 matrix
if (150 <= Imag1)
    Imag1 = 1;
elseif (150 > Imag1) & (140 < Imag1)
    Imag1 = 2;
elseif (140 > Imag1) & (130 < Imag1)
    Imag1 = 3;
elseif (130 >= Imag1)
    Imag1 = 4;

end

%% Write the results to disk
tiffdata = geotiffinfo(file);
outfilename = [outputdir 'reclass_ndvi' '.tif'];  
geotiffwrite(outfilename, Imag1, R, 'GeoKeyDirectoryTag', tiffdata.GeoTIFFTags.GeoKeyDirectoryTag) 

disp('Processing complete')


推荐答案

试试这个:

Imag1 = [ 62  41 169 118 210;
         133 158  96 149 110;
         211 200  84 194  29;
         209  16  15 146  28;
          95 144  13 249 170];

Imag1(find(Imag1 <= 130)) = 4;
Imag1(find(Imag1 >= 150)) = 1;
Imag1(find(Imag1 >  140)) = 2;
Imag1(find(Imag1 >  130)) = 3;

结果:

Imag1 =

    62    41   169   118   210
   133   158    96   149   110
   211   200    84   194    29
   209    16    15   146    28
    95   144    13   249   170

Imag1 =

   4   4   1   4   1
   3   1   4   2   4
   1   1   4   1   4
   1   4   4   2   4
   4   2   4   1   1

我可以进入逻辑如果您愿意,请详细说明,但我想确认这会首先给出您的预期结果。

I can go into the logic in detail if you like, but I wanted to confirm that this gives your expected results first.

基于评论的一些更新在后续问题上消除不必要的查找并使代码更加健壮且独立于执行顺序。

Some updates based on comments on the follow-up question to eliminate the unnecessary find and make the code more robust and independent of execution order.

Imag2 = zeros(size(Imag1));
Imag2(Imag1 >= 150) = 1;
Imag2((Imag1 >  140) & (Imag1 < 150)) = 2;
Imag2((Imag1 >  130) & (Imag1 < 141)) = 3;
Imag2(Imag1 <= 130) = 4;

请注意,结果现在在 Imag2 而不是覆盖 Imag1

Note that the results are now in Imag2 instead of overwriting Imag1.

这篇关于如何在Matlab中重新分类图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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