0

I want to apply connected component analysis on a grey scale image with considering pixels whose grey level is more than a threshold. then, I want to remove those connected components whose length is less than a threshold. please help me? I wrote following code in MATLAB, is it efficient?
thank you in advance.

%im = input image;
% alpha1 = 0.0001;
% alpha2 = 0.0001;
% [row col] = size(im);
% 
% 
% thr1 = mean(mean(im))-alpha1*std(std(im));
% BW = zeros(size(im));
% 
% for rr = 1:row
%     for cc = 1:col
%         if im(rr,cc)>thr2
%             BW(rr,cc) = 1;
%         else
%             BW(rr,cc) = 0;
%         end
%     end
% end
% 
% CC = bwconncomp(BW);
% area_in_pixels = cellfun(@length,CC.PixelIdxList);
% thr2 = mean(area_in_pixels)-alpha2*std(area_in_pixels);
% idx = find(area_in_pixels <= thr3);
% for  kk = 1:length(idx)
% aaa = idx(kk);
% BW(CC.PixelIdxList{aaa})=0;
% end
bahar
  • 53
  • 7

1 Answers1

0

You can try regionprops instead to extract all objects in your image. With the code below you get the positions of all objects smaller than a threshold which you can manipulate or do what you need to do afterwards... Comparably you can go through the different objects and extract the grey level and if it is below a threshold manipulate them.

    % Threshold for the size in pixels that you want
    threshold = 100; 

    % read your image    
    rawimage = imread('yourimage.jpg');

    % create a 2D field by summing 
    im = sum(rawimage,3);

    % label all objects that have 8 neighbours    
    IMAGE_labeled = bwlabel(im,8); 

    % get the properties of all elements
    shapedata=regionprops (IMAGE_labeled,'all'); 

    % get those elements that are smaller in size (area) than the threshold
    index = find(cell2mat({shapedata(:).Area})<=threshold); 

    % make a contourplot of im
    figure
    contourf(im)
    hold on

    % creation of colormap with the size of all identified objects below the thres
    mycolormap = jet(size(index,2));

    % loop over all small objects, extraction of their position in the original file, plotting circles with different colors at the position of each small object

   imap = 1;
   mean_of_red = zeros(length(index),1);
   for i = index
      plot (shapedata(i).PixelList(:,1),shapedata(i).PixelList(:,2),'o','MarkerFaceColor',mycolormap(imap,:))
      mean_of_red(i) = mean(mean(im(shapedata(i).PixelList(:,1),shapedata(i).PixelList(:,1),1)));
      imap=imap+1; 
   end
horseshoe
  • 1,437
  • 14
  • 42
  • I think there is an error in your `index=...` it should probably be something like `index=find([shapedata.Area] < threshold)` – gregswiss Nov 04 '15 at 10:41
  • @gregswiss: right, thx, corrected it. I had an earlier version that only should return one object... – horseshoe Nov 04 '15 at 11:00
  • @horseshoe: thank you very much. it was so helpful. just, I don't understand what does do the last loop exactly? after finding the indexes, I want to remove the connected components related to these indexes in rawimage. how can I relate these indexes to their connected components in rawimage? – bahar Nov 04 '15 at 14:27
  • @bahar, I modified the code so that it becomes a bit more clear what you can do. Pixellist returns the positions of each object and index contains each index of all regions below the threshold. So if you loop over index you can adress each object and do something with it. Here I plotted circles with different colorsand extracted the mean of the red part of the image... – horseshoe Nov 04 '15 at 17:22
  • @horseshoe: I'm so grateful for your help. – bahar Nov 06 '15 at 08:27
  • @bahar: Glad I could help. – horseshoe Nov 06 '15 at 12:23