在 Matlab 中使用基于区域的阈值进行图像分割
Image segmentation with region-based thresholds in Matlab
我有一张嘈杂的图像,其中包含多个模糊的分离圆形区域。具有六个兴趣区域 (ROI) 的此类图像的示例是:
使用 bwconncomp
和给定的阈值在 Matlab 中使用全局阈值分割此图像很容易。但是我想设置一个固定阈值(例如54%)相对于每个ROI的最大像素值(而不是整个图像)来分割每个 ROI。
我有一组具有不同 ROI 大小和位置的图像,我需要根据基于区域的阈值对它们进行分割,因此我也无法使用 Matlab 交互式工具来 select 它们。
谢谢
完成连通分量分析后,尝试使用 bwlabel。它将为您的 6 个区域中的每一个标记一个数字 1-6。然后你使用每个 regions/labels 作为掩码。您只查看该区域的值并找到您的最大值
%after bwconncomp
lbls = bwlabel(my_bw_conn_output);
num_lbls = max(lbls(:));
percentage_of_max = 54;
my_thresh = zeros(1,num_lbls);
for ii=1:num_lbls
%isolates only one ROI
temp_mask = (lbls == ii);
%we do two maxes, one for rows one for columns
max_val = max(max(uint8(temp_mask) .* image_with_values_to_threshold));
%this could be for color pictures too
my_thresh(ii) = percentage_of_max * max_val
end
编辑
如果您想直接使用 bwconncomp 的结果,请执行类似这样的操作
%my code is written assuming you started with a grayscale image, though you
%could adapt it for a color image
cc = bwconncomp(grayscale_image);
percentage_of_max = 54;
for ii=1:cc.NumObjects
mask = zeros(cc.ImageSize);
%converts the indexes back to x,y coordinates
[x_coords,y_coords] = ind2sub(cc.ImageSize,cc.PixelIdxList{ii}(:));
mask(x_coords,y_coords) = 1;
%masks the image
roi_im = bsxfun(@times, uint8(mask), grayscale_image);
%% do processing for this region here using roi_im
curr_thresh = max(roi_im (:)) * percentage_of_max;
thesh_im = grayscale_image > curr_thresh;
end
另一种方法是裁剪图像,然后简单地运行 对裁剪后的图像进行计算。我只是遇到了这个所以 post
How to crop face section from an image with given corner points. MATLAB that helps with cropping perfect squares. You can also use roipoly 如果您有其他不是矩形的形状。
我有一张嘈杂的图像,其中包含多个模糊的分离圆形区域。具有六个兴趣区域 (ROI) 的此类图像的示例是:
使用 bwconncomp
和给定的阈值在 Matlab 中使用全局阈值分割此图像很容易。但是我想设置一个固定阈值(例如54%)相对于每个ROI的最大像素值(而不是整个图像)来分割每个 ROI。
我有一组具有不同 ROI 大小和位置的图像,我需要根据基于区域的阈值对它们进行分割,因此我也无法使用 Matlab 交互式工具来 select 它们。
谢谢
完成连通分量分析后,尝试使用 bwlabel。它将为您的 6 个区域中的每一个标记一个数字 1-6。然后你使用每个 regions/labels 作为掩码。您只查看该区域的值并找到您的最大值
%after bwconncomp
lbls = bwlabel(my_bw_conn_output);
num_lbls = max(lbls(:));
percentage_of_max = 54;
my_thresh = zeros(1,num_lbls);
for ii=1:num_lbls
%isolates only one ROI
temp_mask = (lbls == ii);
%we do two maxes, one for rows one for columns
max_val = max(max(uint8(temp_mask) .* image_with_values_to_threshold));
%this could be for color pictures too
my_thresh(ii) = percentage_of_max * max_val
end
编辑
如果您想直接使用 bwconncomp 的结果,请执行类似这样的操作
%my code is written assuming you started with a grayscale image, though you
%could adapt it for a color image
cc = bwconncomp(grayscale_image);
percentage_of_max = 54;
for ii=1:cc.NumObjects
mask = zeros(cc.ImageSize);
%converts the indexes back to x,y coordinates
[x_coords,y_coords] = ind2sub(cc.ImageSize,cc.PixelIdxList{ii}(:));
mask(x_coords,y_coords) = 1;
%masks the image
roi_im = bsxfun(@times, uint8(mask), grayscale_image);
%% do processing for this region here using roi_im
curr_thresh = max(roi_im (:)) * percentage_of_max;
thesh_im = grayscale_image > curr_thresh;
end
另一种方法是裁剪图像,然后简单地运行 对裁剪后的图像进行计算。我只是遇到了这个所以 post How to crop face section from an image with given corner points. MATLAB that helps with cropping perfect squares. You can also use roipoly 如果您有其他不是矩形的形状。