# how to assign imagesc to figure 1

4 views (last 30 days)
Eiman Rana on 19 Sep 2021
Commented: Walter Roberson on 20 Sep 2021
In this part you will write a program to perform a simple analysis of a random image. First, generate a random array of size 40x40 with each entry being either 0 or 1 and display it in Figure 1. Next, write a program segment that searches for single entries (value = 1) that have no neighbors (all 8 neighbors have values of 0) and records them in another array of the same size. Display the results visually in Figure 2. An example run is shown below: content of random matrix (left) and entries with no neighbors (right).
Please help me with underlined part
n = 40;
m = 40;
A1 = rand(n,m) < 0.5;
imagesc(A1);
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Walter Roberson on 19 Sep 2021
fig1 = figure(1);
ax1 = axes('Parent', fig1);
imagesc(A1, 'Parent', ax1);
If there is an existing axes you want to draw into, then use its handle in the 'Parent' option of imagesc
##### 1 CommentShowHide None
Walter Roberson on 20 Sep 2021
I suggest you read about conv2() with regards to finding isolated pixels.

Sign in to comment.

### More Answers (1)

Image Analyst on 20 Sep 2021
Following Walter's code call figure(2) to create a figure numbered "2", then use bwmorph() with the 'clean' option to remove single pixels. Use xor() with the original binary image to get an image with only the single, isolated pixels.
fig1 =
A1 =
imshow(A1)
output = bwmorph(
output = xor(
fig2 = figure(2);
imshow(output)
Complete this since it seems to be your homework and they probably don't want you to turn in our solution as your own. It should be trivial now that we've done most of it for you.
##### 5 CommentsShowHide 4 older comments
Walter Roberson on 20 Sep 2021
output = conv2(A1, [1 1 1;1 -1 1; 1 1 1], 'same') == -1;

Sign in to comment.

### Categories

Find more on Morphological Operations in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!