0

I have a noisy image with multiple separated circular regions that are blurred out. An example of such image with six Region of Interests (ROI) is :

enter image description here image source

Segmenting this image with global threshold is easy in Matlab using bwconncomp and a given threshold. But I want to set a fix threshold (e.g. 54%) with respect to the maximum pixel value of every ROI (instead of the whole image) to segment each ROI.

I have a collection of images with different ROI sizes and positions, and I need to segment them all based on the regional-based thresholding, therefore I cannot use Matlab interactive tool to select them either.

Thanks

m.s.
  • 16,063
  • 7
  • 53
  • 88
Ash3323
  • 74
  • 1
  • 11

1 Answers1

0

After you've done connected component analysis try using bwlabel. It will label each of your 6 regions a number 1-6. Then you use each of the regions/labels as a mask. You look at only the values in the region and find your max

%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

edit

If you want to use the results of your bwconncomp directly do something more like this

%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

another alternative is to crop the images and then to simply run your calculations on the cropped image. I jsut came across this SO 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 if you have another shape that isn't a rectangle.

Community
  • 1
  • 1
andrew
  • 2,451
  • 1
  • 15
  • 22
  • Thanks Andrew. The issue is how to run another segmentation with the new `my_thresh` values for each region separately. I cannot just use the regions in `lbls` because they were segmented with a different threshold at the beginning with `bwconncompt`, while new threshold value may contain pixels that aren't included in the first segmentation. Then the issue would be how to segment the specific ROI with its corresponding `my_thresh` value and not to run it over the whole image. – Ash3323 May 11 '15 at 17:08
  • 1
    I think I end up using `roipoly` to select the regions in the second part. I was hoping to do it without a need to manually draw the regions, but because regions are relatively close together and they're not neccessarily rectangles, I'm just going to draw the mask myself, then use thresholds from the first step to segment the appropriate ROI. Thanks again Andrew. – Ash3323 May 11 '15 at 18:52