필터 지우기
필터 지우기

help with regionprops in order to draw boundary

조회 수: 2 (최근 30일)
shru s
shru s 2017년 6월 4일
댓글: shru s 2017년 6월 4일
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.

채택된 답변

Image Analyst
Image Analyst 2017년 6월 4일
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
  댓글 수: 1
shru s
shru s 2017년 6월 4일
Thank you so much for your help. You are awesome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by