how to assign imagesc to figure 1

18 views (last 30 days)
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);

Accepted Answer

Walter Roberson
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 Comment
Walter Roberson
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
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 Comments
Image Analyst
Image Analyst on 20 Sep 2021
I'm confused. My guess is that @Eiman Rana deleted some comments between Walter's.
Also, there are multiple ways to achieve the same result, some of which depend on the data type of the image. I gave one way but there are others. Looks like bwareafilt() could do it in one line of code instead of 2 with bwmorph() and xor().
Walter Roberson
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 Image Processing and Computer Vision 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!