基于边缘检测“剪切”图像

时间:2015-04-23 14:54:31

标签: matlab image-processing

这是我的原始图片:Weld Nut

我已要求用户裁剪它,将其转换为灰度并在其上运行以下代码:

edgeImg = edge(grayImg,'canny',0.23);

结果如下:

Edges

我想基本上“切掉”中间圆圈和外边缘的所有东西。我很难搞清楚如何做到这一点,说实话,我很茫然。

我考虑过填写我想要保留在二进制图像中的区域,然后我可以将它用作图章,但我无法想出一种没有填充中间圆圈的方法同样。

有什么想法吗?

感谢。

编辑:这个白色区域是我想要保留的:

Highlighted

2 个答案:

答案 0 :(得分:2)

我建议不要首先进行边缘检测,否则会丢失与颜色相关的有价值信息。您可以尝试一些聚类算法,例如K-Means (including source code)或其他任何算法。

完成群集后,您可以将与群集相关的像素保留在对象中。可以基于图像中的对象位置(包括图像的裁剪)及其颜色来选择期望的聚类。

2个群集的K-Means聚类的代码示例如下:

he = imread('D:\1.jpg');
imshow(he);

cform = makecform('srgb2lab');
lab_he = applycform(he,cform);

ab = double(lab_he(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);

%One cluster for your object and one for background
nColors = 2;
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
                                      'Replicates',2);


pixel_labels = reshape(cluster_idx,nrows,ncols);

segmented_images = cell(1,3);
rgb_label = repmat(pixel_labels,[1 1 3]);

for k = 1:nColors
    color = he;
    color(rgb_label ~= k) = 0;
    segmented_images{k} = color;
end

%Show both clusters: object and non-object
imshow(segmented_images{1});
figure;
imshow(segmented_images{2});

结果细分非常好:

enter image description here enter image description here

答案 1 :(得分:1)

您可以简单地使用color thresholder,而不是使用K-Means,因为您有很多颜色信息。然后你可以调用自动生成的名为createMask的掩码函数,并在那里进一步处理你的图像。代码如下。此方法的最佳部分是createMask可以重复使用任何图片,而不仅仅是您自己的图片!

% Read Image
I = imread('r8ATB.jpg');
figure; imshow( I );

% Crop Image
C = I(75:490,40:460,:);
figure; imshow( C );

% Plot Noisy Mask
[BW,MK] = createMask( C );
figure; imshow( BW );
figure; imshow( BW );

% Fix Holes
imopen( ... );

这是原始图片。

Original Image

裁剪图片

enter image description here

启动阈值窗口。

enter image description here

阈值参数

enter image description here

创建面具

enter image description here

最终影像

enter image description here

使用my参数自动生成的createMask.m函数如下所示。

function [BW,maskedRGBImage] = createMask(RGB)
%createMask  Threshold RGB image using auto-generated code from colorThresholder app.
%  [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
%  auto-generated code from the colorThresholder App. The colorspace and
%  minimum/maximum values for each channel of the colorspace were set in the
%  App and result in a binary mask BW and a composite image maskedRGBImage,
%  which shows the original RGB image values under the mask BW.

% Auto-generated by colorThresholder app on 23-Apr-2015
%------------------------------------------------------


% Convert RGB image to chosen color space
I = rgb2hsv(RGB);

% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.983;
channel1Max = 0.167;

% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.205;
channel2Max = 1.000;

% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.341;
channel3Max = 1.000;

% Create mask based on chosen histogram thresholds
BW = ( (I(:,:,1) >= channel1Min) | (I(:,:,1) <= channel1Max) ) & ...
    (I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
    (I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);

% Invert mask
BW = ~BW;

% Initialize output masked image based on input image.
maskedRGBImage = RGB;

% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;

然后,您可以继续使用imopenimclose来清理面具。然后将其应用于图像。我的方法需要调整以使其按照任何方法完美,但它会为您提供一致的结果。

要获得图像的补充,您需要做的就是反转遮罩并应用它。