How to resize a group of ones in an array of zeros?

8 vues (au cours des 30 derniers jours)
Mohammad Saleh
Mohammad Saleh le 23 Sep 2019
Commenté : Adam Danz le 23 Sep 2019
If i have an array of zeors (512,512) with a couple of ones in there making up a shape, how do i rezise that group of ones making that shape either smaller or larger by a inputted factor
ex
0 0 0 0 0
0 1 1 1 0
0 0 1 1 0
0 0 0 0 0
0 0 0 0 0
i though of doing a for loop changing (i,j+1), (i,j-1),(i+1,j),(i-1,j) to a value of one for (1: the number inputted to resize). However the problem with this is that this doesnt correctly resize the ones image, for an example, inputting a 2 doesnt result in a doubled image. Also this doesnt work to decrease the size of the ones image.
  3 commentaires
Mohammad Saleh
Mohammad Saleh le 23 Sep 2019
A real example would be
DT=zeros(512 ,512);
DT(256,256)=1;
DT(256,257)=1;
DT(256,258)=1;
DT(257,257)=1;
DT(257,258)=1;
So i would want to be able to increase or decrease the size which the ones make by using an input such that an input of 2 would double it, 3 would tripple it, -2 would decrease the size by half, etc. Also, i do not want to change the size of the matrix, but only to expand or decrease the shape of the ones by overtaking more zeros with ones, or replacing ones with zeros.
Adam Danz
Adam Danz le 23 Sep 2019
That's a little better but it's still not clear how you'd like to expand/decrease the shape.
Again, a few (more than 1) input & expected output pairs (with much smaller matrices) would be more grounded and would leave less of the burdon of interpretation up to us. Two volunteers have already invested time into answers that won't work because the question was unclear. It would be unwise to invest more time without a clear definition of how the expansion/contraction would work.

Connectez-vous pour commenter.

Réponses (2)

KALYAN ACHARJYA
KALYAN ACHARJYA le 23 Sep 2019
Modifié(e) : KALYAN ACHARJYA le 23 Sep 2019
If i have an array of zeors (512,512) with a couple of ones in there making up a shape, how do i rezise that group of ones making that shape either smaller or larger by a inputted factor
data=[0 0 0 0 0
0 1 1 1 0
0 0 1 1 0
0 0 0 0 0
0 0 0 0 0]
result=imresize(data,2)
%....................^ times larger, here double
Command Window:
data =
0 0 0 0 0
0 1 1 1 0
0 0 1 1 0
0 0 0 0 0
0 0 0 0 0
result =
0.0088 -0.0190 -0.0747 -0.1003 -0.0959 -0.0959 -0.1003 -0.0747 -0.0190 0.0088
-0.0212 0.0466 0.1822 0.2377 0.2132 0.2062 0.2169 0.1619 0.0413 -0.0190
-0.0813 0.1778 0.6960 0.9139 0.8315 0.8106 0.8513 0.6350 0.1619 -0.0747
-0.0813 0.1708 0.6751 0.9742 1.0681 1.1353 1.1760 0.8716 0.2222 -0.1025
-0.0212 0.0257 0.1196 0.4186 0.9229 1.1804 1.1910 0.8716 0.2222 -0.1025
0.0066 -0.0346 -0.1170 0.1009 0.6191 0.8765 0.8732 0.6350 0.1619 -0.0747
0.0022 -0.0101 -0.0346 0.0209 0.1566 0.2238 0.2227 0.1619 0.0413 -0.0190
0 0.0016 0.0049 -0.0143 -0.0560 -0.0769 -0.0769 -0.0560 -0.0143 0.0066
0 0.0005 0.0016 -0.0048 -0.0187 -0.0256 -0.0256 -0.0187 -0.0048 0.0022
0 0 0 0 0 0 0 0 0 0

Adam Danz
Adam Danz le 23 Sep 2019
Modifié(e) : Adam Danz le 23 Sep 2019
Maybe interp2() would help.
% Input data
m = [0 0 0 0 0
0 1 1 1 0
0 0 1 1 0
0 0 0 0 0
0 0 0 0 0];
% Interpolate
mInt = double(interp2(m,1) > 0);
% ^ this value (k) determines interp resolution.
% There will be 2^k-1 number of data points added
% between each sample.
% compare original vs interp'd
figure()
subplot(1,2,1)
heatmap(m)
title('Original data')
subplot(1,2,2)
heatmap(mInt)
title('Interp''d data')
190923 154738-Figure 1.png

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by