MATLAB: Help with regionprops in order to draw boundary

image processingImage Processing Toolboximage segmentation

i have the following image
and I am using this code to draw a bounding box around the image
img=imread('li.jpg');
% figure(1)
imshow(img);
img=rgb2gray(img);
threshold = graythresh(img);
img =~im2bw(img,threshold);
img = bwareaopen(img,30);
pause(1)
imshow(~img);
[L Ne]=bwlabel(img);
regProp=regionprops(L,'BoundingBox');
hold on
for n=1:size(regProp,1)
rectangle('Position',regProp(n).BoundingBox,'EdgeColor','r','LineWidth',2)
end
hold off
pause (1)
figure
for n=1:Ne
[r,c] = find(L==n);
n1=img(min(r):max(r),min(c):max(c));
figure;
imshow(~n1);
saveas(figure(n),fullfile('C:\SegmentedCharacters',['figure' num2str(n) '.jpg']));
end
but i am unable to do so. Could anyone help me out? Thank you.

Best Answer

  • There were lots of fixes that needed to be made. Here is the fixed code:
    % Initialization steps.
    clc; % Clear the command window.
    close all; % Close all figures (except those of imtool.)
    clear; % Erase all existing variables. Or clearvars if you want.
    workspace; % Make sure the workspace panel is showing.
    format long g;
    format compact;
    fontSize = 20;
    grayImage = imread('li.jpg');
    % Display the image.
    subplot(3, 3, 1);
    imshow(grayImage);
    axis on;
    % Get the dimensions of the image. Convert to gray scale if it's more than 1 color channel.
    % numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
    [rows, columns, numberOfColorChannels] = size(grayImage);
    if numberOfColorChannels > 1
    % It's not really gray scale like we expected - it's color.
    % Use weighted sum of ALL channels to create a gray scale image.
    grayImage = rgb2gray(grayImage);
    end
    % Crop off white surround
    grayImage = grayImage(321:472, 97:1104);
    subplot(3, 3, 2);
    imshow(grayImage);
    axis on;
    % Set up figure properties:
    % Enlarge figure to full screen.
    set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
    % Get rid of tool bar and pulldown menus that are along top of figure.
    set(gcf, 'Toolbar', 'none', 'Menu', 'none');
    % Give a name to the title bar.
    set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
    % Let's compute and display the histogram.
    [pixelCount, grayLevels] = imhist(grayImage);
    subplot(3, 3, 3);
    bar(grayLevels, pixelCount); % Plot it as a bar chart.
    grid on;
    title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');
    xlabel('Gray Level', 'FontSize', fontSize);
    ylabel('Pixel Count', 'FontSize', fontSize);
    xlim([0 grayLevels(end)]); % Scale x axis manually.
    % threshold = graythresh(grayImage); % Bad for a JPG image
    threshold = 200;
    mask = grayImage > threshold;
    % Get rid of small blobs
    mask = bwareaopen(mask, 50);
    subplot(3, 3, 3);
    imshow(mask);
    axis on;
    [labeledImage, numberOfRegions] = bwlabel(mask);
    props = regionprops(labeledImage,'BoundingBox');
    hold on
    for n = 1 : numberOfRegions
    rectangle('Position',props(n).BoundingBox, 'EdgeColor','r', 'LineWidth',2)
    end
    hold off
    % Crop regions and save them.
    folder = 'C:\SegmentedCharacters';
    for n = 1 : numberOfRegions
    croppedImage = imcrop(mask, props(n).BoundingBox);
    subplot(3, 3, n+3);
    imshow(croppedImage);
    axis on;
    % Save to disk
    baseFileName = sprintf('figure %d.png', n);
    fullFileName = fullfile(folder, baseFileName)
    imwrite(croppedImage, fullFileName);
    end