manual pixel selection for grow cut segmentation then autosegment based on pixel intensity values within 100 in matlab

Here's an example code to manually select pixels using impixel() function and perform grow cut segmentation based on pixel intensity values within 100.

main.m
% Read the input image
img = imread('input_image.jpg');
imshow(img);

% Select the pixels to be segmented using impixel()
pixel_selection = impixel(img);
[row, col, ~] = size(img);
mask = zeros(row, col);

% Set the selected pixels in the mask
for i = 1:size(pixel_selection, 1)
    x = pixel_selection(i,1);
    y = pixel_selection(i,2);
    mask(y,x) = 1;
end

% Perform the segmentation using grow cut
bw = im2bw(img, graythresh(img));
mask = imdilate(mask,strel('disk',1)); % Dilate the mask for better performance
bw_cut = growcut(img,mask);

% Segment pixels based on intensity values within 100
for i = 1:row
    for j = 1:col
        if img(i,j) <= 100
            bw_cut(i,j) = 0;
        end
    end
end

% Display the segmented image
imshow(bw_cut);
761 chars
33 lines

Note: growcut() function is not a built-in function in MATLAB, it is provided as a part of the example files in the MATLAB documentation for image processing. You will need to download the example files from MATLAB website and add it to your MATLAB path.

gistlibby LogSnag