Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Image manipulation: detection of enclosed areas

Subject: Image manipulation: detection of enclosed areas

From: Geico Caveman

Date: 5 Apr, 2011 20:57:04

Message: 1 of 4

I have an image that consists of geometric shapes. Imagine taking a
white sheet of paper and drawing a square/circle/oval/any closed shape
on it.

I want to shade the interior of all enclosed shape with one colour and
leave the outside white.

How do I do this ?

Subject: Image manipulation: detection of enclosed areas

From: Florin Neacsu

Date: 5 Apr, 2011 21:59:05

Message: 2 of 4

Geico Caveman <spammers-go-here@spam.invalid> wrote in message <2011040515570475249-spammersgohere@spaminvalid>...
> I have an image that consists of geometric shapes. Imagine taking a
> white sheet of paper and drawing a square/circle/oval/any closed shape
> on it.
>
> I want to shade the interior of all enclosed shape with one colour and
> leave the outside white.
>
> How do I do this ?

Hi,

Have a look at bwlabel (I suppose you have the image processing toolbox).

Regards,
Florin

Subject: Image manipulation: detection of enclosed areas

From: ImageAnalyst

Date: 6 Apr, 2011 00:23:02

Message: 3 of 4

See this demo:

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by
imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;

% Generate some sample data of a rectangle with gray sides
grayImage = 255 * ones(240, 320, 'uint8');
grayImage(100, 100:200) = 50; % Top side
grayImage(170, 100:200) = 60; % Bottom side
grayImage(100:170, 100) = 70; % Left side
grayImage(100:170, 200) = 30; % Right side

% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

% Binarize image
binaryImage = grayImage > 200;
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);

% Get rid of stuff outside the rectangle
binaryImage2 = imclearborder(binaryImage);
subplot(2, 2, 3);
imshow(binaryImage2, []);
title('Just the Rectangle', 'FontSize', fontSize);

% Make it a random color.
redChannel = uint8(binaryImage2 * 255 * rand);
greenChannel = uint8(binaryImage2 * 255 * rand);
blueChannel = uint8(binaryImage2 * 255 * rand);
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
subplot(2, 2, 4);
imshow(rgbImage);
title('Now a random color', 'FontSize', fontSize);

Subject: Image manipulation: detection of enclosed areas

From: ImageAnalyst

Date: 6 Apr, 2011 00:40:06

Message: 4 of 4

Sorry - I left off the "make the outside white" part. Change the last
few lines to be this:

% Make it a random color.
redChannel = uint8(binaryImage2 * 255 * rand);
redChannel(redChannel==0) = 255; % Change zeros to white
greenChannel = uint8(binaryImage2 * 255 * rand);
greenChannel(greenChannel==0) = 255; % Change zeros to white
blueChannel = uint8(binaryImage2 * 255 * rand);
blueChannel(blueChannel==0) = 255; % Change zeros to white
rgbImage = cat(3, redChannel, greenChannel, blueChannel);

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us