I need to divide my matlab output figure into pixels or grid of desired dimension.
Show older comments
How should it be done. should i save this output figure in another variable?
can "the game of life" coding approach be taken to divide the output figure into pixels.
(also thereafter change colour of the few selected pixels or grid and group them into different categories based on colour and make them move as pointer) i presently dont have image processing toolbox installed Suggest a solution
Answers (2)
KSSV
on 21 Nov 2016
clc; clear all ;
% make random data
data = rand(1000,2) ;
% divide into grid
M = 5 ; N = 5 ;
x = linspace(0,1,M) ;
y = linspace(0,1,N) ;
[X,Y] = meshgrid(x,y) ;
Z = zeros(size(X)) ;
figure
plot(data(:,1),data(:,2),'.r') ;
hold on
plot(X,Y,'k') ; plot(Y,X,'k')
%%Get points inside for each box
P = cell(M,N) ;
for i = 1:N-1
for j = 1:M-1
A = [X(i,j) Y(i,j)] ;
B = [X(i+1,j+1) Y(i+1,j+1)] ;
idx = find(data(:,1) >= A(1) & data(:,1) <B(1)) ;
idy = find(data(:,2) >= A(2) & data(:,2) <B(2)) ;
id = intersect(idx,idy) ;
P{i,j} = [data(id,1) data(id,2)] ;
% plot points inside first box
plot(P{i,j}(:,1),P{i,j}(:,2),'O','color',rand(1,3))
end
end
Walter Roberson
on 21 Nov 2016
1 vote
Use mat2cell() to divide your image up into pieces.
It is not obvious to me that cellular automata has anything to do with your question.
If you want to move an image on the display, then image() or imshow() it and record the handle returned, and then to move it, change the handle XData and YData properties.
6 Comments
Walter Roberson
on 21 Nov 2016
Edited: Walter Roberson
on 21 Nov 2016
Setting the XData and YData would work if you were just moving the sections up/down/left/right, but in that simulation at http://kmarkow.github.io/roundabouts/ the sections need to curve as well.
To handle that, the easiest way might be to use one hgtransform for each section, and set the transform matrices as needed. You would need one hgtransform per section.
Another way of handling it would be to use one patch object for each section with the image displayed on the patch by using texture mapping, and instead of keeping an hgtransform for each section you would modify the patch vertex locations as you went. This would involve transform matrix calculations but not the overhead of an hgtransform for each object (but you would need to store the center coordinates and the orientation for each object.)
In theory you could also manage it all as one single patch object using texture mapping, with a whole bunch of faces that do not happen to connect together, with you modifying the vertex locations as you went. This would involve transform matrix calculations but not the overhead of an hgtransform for each object (but you would need to store the center coordinates and the orientation for each object.)
h b
on 21 Nov 2016
Walter Roberson
on 21 Nov 2016
You have to imshow() or image() each of the cell entries, passing in XData and YData so that it displays in the proper location. Or, if you are intending to rotate like in the demonstration, you need to follow one of the other approaches I indicated (e.g. patch or hgtransform)
h b
on 22 Nov 2016
Walter Roberson
on 22 Nov 2016
Example without rotation
cam = imread('cameraman.tif');
subimages = mat2cell(cam, 16 * ones(1, 16), 16 * ones(1,16));
number_of_subimages = numel(subimages);
X = zeros(1, number_of_subimages);
Y = zeros(1, number_of_subimages);
for subidx = 1 : number_of_subimages
imh(subidx) = image( subimages{subidx}, 'XData', X(subidx), 'YData', Y(subidx));
hold on
end
hold off
axis([-100 300 -100 300])
%now move them
for movenum = 1 : 50
for subidx = 1 : number_of_subimages
X(subidx) = X(subidx) + randi([-32,32]);
Y(subidx) = Y(subidx) + randi([-32,32]);
set( imh(subidx), 'XData', X(subidx), 'YData', Y(subidx));
end
pause(1/4);
end
Categories
Find more on Lighting, Transparency, and Shading 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!