Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 如何从图像中分离相同颜色的连接对象?_Matlab_Image Processing_Image Segmentation - Fatal编程技术网

Matlab 如何从图像中分离相同颜色的连接对象?

Matlab 如何从图像中分离相同颜色的连接对象?,matlab,image-processing,image-segmentation,Matlab,Image Processing,Image Segmentation,我的图片如下所示: RGB=imread('testImage.jpg'); RGB = im2double(RGB); cform = makecform('srgb2lab', 'AdaptedWhitePoint', whitepoint('D65')); I = applycform(RGB,cform); channel1Min = 12.099; channel1Max = 36.044; channel2Min = -9.048; channel2Max = 48.547; cha

我的图片如下所示:

RGB=imread('testImage.jpg');
RGB = im2double(RGB);
cform = makecform('srgb2lab', 'AdaptedWhitePoint', whitepoint('D65'));
I = applycform(RGB,cform);
channel1Min = 12.099;
channel1Max = 36.044;
channel2Min = -9.048;
channel2Max = 48.547;
channel3Min = -53.996;
channel3Max = 15.471;
BW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
    (I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
    (I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
maskedRGBImage = RGB;
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
figure
imshow(maskedRGBImage)

我正在应用一些类似于代码中的阈值。我可以像下面这样分离蓝色对象:

但是,现在我在分离这些蓝色对象时遇到了一个问题。我应用了分水岭(我不知道我是对是错),但没有成功,所以我需要帮助来分离这些连接的对象

我尝试使用的代码如下所示:

RGB=imread('testImage.jpg');
RGB = im2double(RGB);
cform = makecform('srgb2lab', 'AdaptedWhitePoint', whitepoint('D65'));
I = applycform(RGB,cform);
channel1Min = 12.099;
channel1Max = 36.044;
channel2Min = -9.048;
channel2Max = 48.547;
channel3Min = -53.996;
channel3Max = 15.471;
BW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
    (I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
    (I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
maskedRGBImage = RGB;
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
figure
imshow(maskedRGBImage)
RGB=imread('testImage.jpg');
RGB=im2double(RGB);
cform=makecform('srgb2lab','AdaptedWhitePoint',whitepoint('D65'));
I=ApplyForm(RGB,cform);
通道1min=12.099;
channel1Max=36.044;
通道2min=-9.048;
信道2max=48.547;
通道3min=-53.996;
信道3max=15.471;

BW=(I(:,:,1)>=channel1Min)和(I(:,:,1)=channel2Min)和(I(:,:,2)=channel3Min)和(I(:,,:,3)一般来说,这种类型的分割是一个严重的研究问题。在您的案例中,您可以很好地结合使用这些方法。这些方法在显微镜图像处理中得到了广泛的应用

首先,通过清除小斑点和填充孔来稍微清理
BW

BWopen = imopen(BW, strel('disk', 6));
BWclose = imclose(BWopen, strel('disk', 6));
(您可能需要稍微调整一下结构元素,“6”只是一个半径,似乎对您的测试图像有效。)

然后你可以用侵蚀性侵蚀来产生一些种子

seeds = imerode(BWclose, strel('disk', 35));
您可以将其用于分水岭,或仅将
BW
中的每个点指定给其最近的种子

labels = bwlabel(seeds);

[D, i] = bwdist(seeds);
closestLabels = labels(i);
originalLabels = BWopen .* closestLabels;

imshow(originalLabels, []);

我将尝试以下步骤:

  • 将图像转换为灰色,然后转换为二值遮罩

  • 应用形态开口()来清洁小噪音对象

  • 使用应用连接组件分析(CCA)。每个连接组件至少包含一个对象

  • 这些蓝色对象看起来确实像拉伸/扭曲的圆,所以我会尝试Hough变换来检测每个标记组件内的圆环。根据您的Matlab版本和可用的工具箱,有一个内置函数()或在线可用的代码()

  • 然后,您需要对每个组件内的对象数量N(N>=1)做出一些决定。我事先不知道最佳标准应该是什么,但您也可以应用以下简单规则: [i] 对象需要具有最小大小。 [ii]重叠圆对应于同一对象(或不对应,取决于重叠量)

  • 然后,圆的质心可以作为种子来完成最终的对象分割。当然,如果每个组件中只有一个圆,您可以直接将其作为对象保留

  • 我没有在Matlab中检查所有步骤的有效性,但我很快检查了1、2和4,它们似乎很有希望。我在图像的中心显示了最困难组件的圆检测结果:

    我用于创建此图像的代码是:

    close all;clear all;clc;
    
    addpath 'circle_hough'; % add path to code of [Hough transform for circles] link above
    
    im = imread('im.jpg');
    img = rgb2gray(im);
    
    mask = img>30; mask = 255*mask; % create a binary mask
    figure;imshow(mask)
    
    % filter the image so that only the central part of 6 blue objects remains (for demo purposes only)
    o = zeros(size(mask)); o(170:370, 220:320) = 1;
    mask = mask.*o; 
    figure;imshow(mask);
    
    se = strel('disk',3); 
    mask = imopen(mask,se); % apply morphological opening
    figure;imshow(mask);
    
    % check for circles using Hough transform (see also circle_houghdemo.m in [Hough transform for circles] link above)
    radii = 15:5:40; % allowed circle radii 
    h = circle_hough(mask, radii, 'same', 'normalise');
    % choose the 10 biggest circles
    peaks = circle_houghpeaks(h, radii, 'nhoodxy', 15, 'nhoodr', 21, 'npeaks', 10);
    
    % show result
    figure;imshow(im);
    for peak = peaks
        [x, y] = circlepoints(peak(3));
        hold on;plot(x+peak(1), y+peak(2), 'g-');
    end
    

    一些自发的想法。我假设最终的目标是计算蓝色小体。如果是这样,我会寻找一种方法来确定这些物体的总面积和小体的平均面积。作为转换为二进制(黑白)的第一步:


    自形态操作(打开/关闭)不会保留区域,我会使用bwlabel查找连接的组件。看图片,我看到12个孤立的小体,两个连接的组,一些边缘截断的小体和一些小的噪音碎片。因此,首先移除小对象和那些接触边缘的。然后确定总面积(BWLea)以及作为一个微粒平均面积的代理的对象的中值大小。

    您用于复制结果的实际代码将很好地完成您的回答。感谢您的建议@rayryeng,我添加了代码。非常好。谢谢。+1。