MATLAB Answers

Rotating Subparts of Image

13 views (last 30 days)
Jerry Wang
Jerry Wang on 23 Jun 2017
Commented: Jerry Wang on 27 Jun 2017
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
Jerry Wang
Jerry Wang on 27 Jun 2017
This function would be helpful, if I can extract the pacmen (the coordinates of the centers and radii are known), rotate them pivoting at the center, and then overlaying them on top of the image. Do you know how that can be done?

Sign in to comment.

Answers (2)

Peter Cook
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;
  1 Comment
Jerry Wang
Jerry Wang on 23 Jun 2017
What I am basically doing in the function is, taking in the radius of the pacman, taking in the number of sides of the imaginary polygon being created, taking in the distance between the center of the imaginary polygon, and the vertices. Using these data, I am determining the positions of the centers of the pacmen (stored in arrays x, and y), and then calling the function 'circles' to plot circles at those centers, with the input radius. Then, I am overlaying the generated image with a solid, filled, polygon, with the circle centers as its vertices, to create the 'pacman' effect. So, I basically just have the x and y coordinates of the centers stored, and the 'pacman' radii.

Sign in to comment.


Image Analyst
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?
  1 Comment
Jerry Wang
Jerry Wang on 27 Jun 2017
The 'theta' variable represents the angle of the (r, theta) polar coordinates of the centers of the pacmen. I draw circles at those centers, and then overlay a filled polygon on top of it, with its vertices at the centers of the circles, to create the effect of a pacman. So, changing the theta would not change the opening of the pacmen mouth, it'll just change the position of the circles.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by