필터 지우기
필터 지우기

Label/color each region in the image

조회 수: 6 (최근 30일)
missC
missC 2014년 4월 19일
댓글: Image Analyst 2014년 5월 6일
Hi , How can I label each region in the attached image? As you can see, it has three (3) connected regions/faces. It is better to be labeled with different color.
Thanks in advance

채택된 답변

Image Analyst
Image Analyst 2014년 4월 19일
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
% Read in a demo image.
folder = 'C:\Users\missc\Documents\Temporary';
baseFileName = 'a.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(grayImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(grayImage);
title('Original Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Threshold so we can get a binary image to use as a mask.
binaryImage = grayImage < 100;
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage);
axis on;
title('Binary Image', 'FontSize', fontSize);
% Get rid of white touching the border.
binaryImage = imclearborder(binaryImage, 4);
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage);
axis on;
title('Faces Image', 'FontSize', fontSize);
[labeledImage, numberOfRegions] = bwlabel(binaryImage, 4);
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
% Display the image.
subplot(2, 2, 4);
imshow(coloredLabels);
axis on;
title('Colored Label Image', 'FontSize', fontSize);
  댓글 수: 7
missC
missC 2014년 5월 6일
편집: missC 2014년 5월 6일
yes sir, I am so sorry because mislead to your understanding. That is what I meant, each face just consist of one boundary. Now I have 3 faces and I should have 3 boundaries. How can I do that sir? Or how to label only the edge not the face.
Really need your help. Thanks again
Image Analyst
Image Analyst 2014년 5월 6일
boundaries = bwboundaries(binaryImage);
Then you can use the kink finding code I referred you to if you want to divide each boundary up into 4 straight segments. Or you can run along fitting a line and getting the histogram of the slopes and looking for 4 slopes that are found a lot (meaning when you're fitting pixels along one side only and not spanning across a corner).

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by