识别matlab中的相邻像素 [英] identify the adjacent pixels in matlab

查看:360
本文介绍了识别matlab中的相邻像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们假设A是,

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

我需要识别与特定强度值相邻的所有数字。例如。强度1,3和4与强度值2相邻。
在Matlab中执行此操作的有效方法是什么?

I need to identify all the numbers which are adjacent to a particular intensity value. E.g. the intensities 1, 3, and 4 are adjacent to the intensity value 2. What is the effective way to do it in Matlab?

我可以使用以下,

   glcm = graycomatrix(A)

但是如果A有更多的强度值,例如10000 graycomatrix将不是一种有效的方法。

But if A have a larger number of intensity values e.g. 10000 graycomatrix will not be an efficient method.

推荐答案

您可以使用2D卷积构建一个掩码,根据该值选择值掩码,然后将它们减少到唯一值:

You can build a mask with a 2D convolution, select the values according to that mask, and then reduce them to unique values:

% // Data:
A = [ 1 1 1 1 1 1
      1 2 2 3 3 3
      4 4 2 2 3 3
      4 4 2 2 2 3
      4 4 4 4 3 3
      5 5 5 5 5 5 ];
value = 2;
adj = [0 1 0; 1 0 1; 0 1 0]; %// define adjacency. [1 1 1;1 0 1;1 1 1] to include diagonals

%// Let's go
mask = conv2(double(A==value), adj, 'same')>0; %// pixels adjacent to those equal to `value`
result = unique(A(mask));

在示例中,这会产生

result =
     1
     2
     3
     4

请注意,结果包括 2 ,因为某些值 2 的像素相邻具有该值的像素。

Note that the result includes 2 because some pixels with value 2 have adjacent pixels with that value.

这篇关于识别matlab中的相邻像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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