【问题标题】:Applying an algorithm to a specific region of an image [closed]将算法应用于图像的特定区域[关闭]
【发布时间】:2013-08-05 06:18:34
【问题描述】:

我正在尝试仅将算法应用于图像的特定区域。我尝试了imfreehand,但至少对我来说无法使用此函数来做到这一点。

那么,在运行我的代码时,是否有某种方法可以将操作仅应用于 MATLAB 中图像的某些特定区域?

谢谢。

【问题讨论】:

  • 你能说得更具体点吗?您是在问如何允许用户选择一个矩形区域,就像imfreehand 一样?或者您是在问如何从图像中提取区域进行处理?区域应该是矩形吗?
  • 如果你能提供一个你遇到麻烦的例子(=代码)会更容易。
  • 我在答案中添加了一个侵蚀示例

标签: matlab region roi


【解决方案1】:

使用由任何“imroi”函数定义的掩码(包括 imfreehand 和 imellipse),您可以使用 roifilt2 使用给定的过滤器或函数仅过滤 roi。

首先,定义区域:

imshow(I); %display your image
h = imfreehand; % now pick the region
BW = createmask(h); %makes BW mask

然后,通过以下方式之一使用 roifilt2 -

定义一个过滤器并应用它:

H = fspecial('unsharp');
I2 = roifilt2(H,I,BW);`

将给定函数应用于 roi:

I2 = roifilt2(I, BW, 'histeq');

将给定函数应用于 roi,指定参数:

fh = @(I)(histeq(I,5)); %define function
I2 = roifilt2(I, BW, fh); 

最后相当于调用 I2 = hist(I,5);但仅适用于定义的 roi。

预计到达时间:

如果您想在 roi 上调用多个函数,定义自己的函数可能是最简单的方法,该函数接受图像输入(以及可选的其他参数),将适当的过滤器/函数应用于图像,并输出最终图像 - 然后您可以像上面的“histeq”一样调用“myfunc”。

【讨论】:

  • 感谢您的精彩解释。我有以下几行我想应用于ROIse=strel('disk',3); erosion=imerode(img,se); result_image=imsubtract(img,erosion); 这种情况下如何申请roifilt2
  • 看最后两行,只定义了两个函数——一个使用imerode,一个使用imsubtract,调用roifilt2两次。您可以将变量放入函数中,例如n=5; fh = @(I)(histeq(I,n));和我上面最后一个例子的效果一样。
  • 只是一个小笔记。 createmask 似乎必须是 createMask
【解决方案2】:

你可以试试roipoly

在 SO here 上有一个示例。

这是一个例子:

img = imread('peppers.jpg');        % loads the image we want to use
[BW,xi,yi] = roipoly(img);          % create a polynomial surrounding region
BW = repmat(uint8(BW),[1,1,3]);     % create mask
selIMG = img.*BW;                   % apply mask to retain roi and set all else to 0
imview(selIMG)

se=strel('disk',3); 
erosion=imerode(selIMG,se); 
result_image=imsubtract(selIMG,erosion);
imview(result_image)

编辑

侵蚀:正如 matlab 文档 explainsimerode 从周围像素中选择最小值(imdilate 则相反)。这意味着我的答案中的原始处理不适用于imerode,最好将选择之外的像素设置为色阶上的最大值,我在这里提供了一个关于如何“手动”执行此操作的示例:

img = imread('peppers.jpg');                        % loads the image we want to use
[BW,xi,yi] = roipoly(img);                          % logical mask which contains pixels of interest
nBW = uint8(~BW);                                   % inverse of logical mask to pick surrounding pixels
surroundingMaxedOut = repmat(255*nBW,[1,1,3]);      % set surrounding pixels to max value
nBW = repmat(nBW,[1,1,3]);                          % make mask with 3 channels to pick surrounding pixels
BW = repmat(uint8(BW),[1,1,3]);                     % make mask with 3 channels to handle RGB
selIMG = img.*BW;                                   % pick the image region of interest
selIMG = selIMG + surroundingMaxedOut;              % final selection with surrounding pixels maxed out
imview(selIMG)                                      % inspect the selection


se=strel('disk',3); 
erosion=imerode(selIMG,se);                         % apply erosion 
finalIMG = img.*nBW + BW.*erosion;                  % insert eroded selection into the original image
imview(finalIMG)

正如其他答案所示,matlab 具有隐式处理这些操作的例程,并且不仅在内存管理方面更高效,但是这个示例为您提供了更多控制,因此您可以看到正在发生的事情。

【讨论】:

  • 感谢您的回复。我想这会给我一个形状。我的目标是在处理图像时,只会处理特定区域without extracting该区域
  • 您能否更具体一点:显然,当您想要处理特定区域时,您必须隔离那些特定像素,例如在新数组(或索引向量)中进行处理。您可以使用 roipoly 生成掩码来选择这些像素,然后有选择地对这些像素应用操作,不是吗?
  • 感谢您的回复。当我尝试您的解决方案时,出现以下错误:Error using .* Integers can only be combined with integers of the same class, or scalar doubles.。有什么想法吗?
  • 通过以下操作解决了这个问题:BW = repmat(double(BW),[1,1,3]); BWgray=rgb2gray(BW);
  • 那么,这是否应用于该特定区域的操作?
猜你喜欢
  • 2013-10-21
  • 2020-03-12
  • 2020-10-31
  • 1970-01-01
  • 1970-01-01
  • 2020-06-28
  • 2014-01-07
  • 2019-03-03
  • 2012-08-02
相关资源
最近更新 更多