Rotating Subparts of Image
2 views (last 30 days)
Show older comments
I want to rotate subparts of images, like the following specific angles.
or,
Into something like this.
or,
The code used to generate the first figure generates the figure with any given number of 'pacman'-s, and the radii and centers of the 'pacmen' are known. I want to rotate each of them by some specified angle, and the coordinates of the 'pacman' centers and their radii are known. Also, I want 'export_fig' to export the figure leaving some boundary, not cutting it exactly at the edges. The function that generates the figures (the not rotated ones) is given below (it uses a function called circles.m ). Any help in this regard will be deeply appreciated.
function kanizsa(rad_pacman, rad_figure, number, nature, background)
%%Prabaha, 21st June, 2017
%%The function generates a Kanizsa polygon, which is regular
% rad_pacman: radius individual pacmans
% rad_figure: distance between center of polygon to its vertices
% number: number of edges
% background: background color in ColorSpec; default value [0.5 0.5 0.5]
% nature: takes value 1,2,3 where 1 ir illusory, 2 is real, 3 is amodal
r1 = rad_pacman;
r2 = rad_figure;
ax = r2 + 1.5*r1;
n = number;
if ~exist('background', 'var')
background = [0.5 0.5 0.5];
end
bg = background;
theta=0+2*pi/(2*n):2*pi/n:2*pi-2*pi/(2*n);
normal = figure('Color', bg);
axis([-ax ax -ax ax]);
axis square;
axis off;
for i=1:length(theta)
x(i) = r2*cos(theta(i));
y(i) = r2*sin(theta(i));
circles(x(i), y(i), r1, 'color', 'black');
end
set(gca,'Color',bg);
hold on;
occ = fill(x,y,bg);
if nature==1
set(occ, 'EdgeColor', 'None');
elseif nature==3
set(occ, 'EdgeColor', 'None');
for i=1:length(theta)
circles(x(i), y(i), r1, 'facecolor', 'none');
end
end
camroll(-90);
export_fig('kanizsa.png');
end
2 Comments
John BG
on 24 Jun 2017
Hi
the solution in
may be of help.
Now you want just to rotate the 'pacmans' but with imrotatex.m you can choose the pivot point to be different than the current centre of each 'pacman'.
Answers (2)
Peter Cook
on 23 Jun 2017
This might work for you: 1. Mask a box around the pacman & extract the pixel values 2. Make the background grey where you pulled pacman out 3. Zero-center the pixels 4. Rotate the black pixels 5. Put the rotated black pixels back on the image
Here I assume your pacman attributes are in a struct and your greyscale pixel values are in a matrix called IMG.
%pull pacman from background (assuming greyscale image)
pacmanBox = IMG( (pacman(k).center(1) - pacman(k).radius):...
(pacman(k).center(1) + pacman(k).radius),...
(pacman(k).center(2) - pacman(k).radius):...
(pacman(k).center(2) + pacman(k).radius) );
%fill background back to grey (assuming greyscale image)
IMG( (pacman(k).center(1) - pacman(k).radius):...
(pacman(k).center(1) + pacman(k).radius),...
(pacman(k).center(2) - pacman(k).radius):...
(pacman(k).center(2) + pacman(k).radius) ) = 0.5;
%find pacman coordinates
[I,J] = find(pacmanBox==0);
pacman(k).xPixel = I - pacman(k).radius;
pacman(k).yPixel = J - pacman(k).radius;
%make transformation matrix
rotationMatrix = [cosd(theta),sind(theta),0;-sind(theta),cosd(theta),0;0,0,1];
%rotate pacman
X = [pacMan(k).xPixel,pacMan(k).yPixel,ones(length(xPixel),1)]';
rotatedPacman = rotationMatrix*X;
%update pixel coordinates in pacman struct
pacman(k).xPixel = round(rotatedPacMan(1,:)');
pacman(k).yPixel = round(rotatedPacMan(2,:)');
%update image (assuming greyscale image)
IMG(pacman(k).center(1)+pacman(k).xPixel,...
pacman(k).center(2)+pacman(k).yPixel) = 0;
Image Analyst
on 24 Jun 2017
I believe it's as simple as changing this one line in your program:
theta=0+2*pi/(2*n):2*pi/n:2*pi-2*pi/(2*n);
simply change the formula to start and stop at the angles you desire. Why would it need to be any more complicated than that?
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!