Rotate one image and plot on top of another

7 views (last 30 days)
Hello,
I am trying to plot two images on top of one another. The code included allows me to rotate the human torso by a defined angle, and then plot it on top of the other image (lower legs and protractor). My problem is that when I do so, the bottom image becomes much lighter than the top image, and when the top image crosses over the bottom image, the parts of the bottom image can still be seen. How do I solve these two problems?
% reading in the two images
Protractor = importdata('STS_Protractor2.jpg');
Torso = importdata('STS_Torso2.jpg'); % top of midline is at (349,304) (349.5,316)
RotationPoint = [510,745]; % This is the point the Torso image will be rotated around (center of hip)
Protractor_0 = [510,135]; % location of the 0 value on the protractor
LineLength = 500; % Length of the line centered on the torso and extending past the head
Angle = 25;
timeperiod = 'Pre00';
% rotate trunk image by max trunk angle from model output data, then format image so it can be placed on top of protractor image
TrunkAngle_Rotation = rotateAround(Torso,RotationPoint(2),RotationPoint(1),-Angle, 'crop'); % (image,y-coord,x-cord,angle,'variable') - Allows for rotation of torso image
Clear_Background = ~rotateAround(true(size(Torso)),RotationPoint(2),RotationPoint(1),-Angle); % When image is rotated, this changes the background to white rather than leaving black space.
TrunkAngle_Rotation(Clear_Background&~imclearborder(Clear_Background)) = 255; % Redefines the variable to include the new white background
% Plotting the two figures ontop of each other
figure;
image([0 1084], [0 1084],Protractor);% First plotting the protractor background (maintaining the 1084 x 1084 image size)
hold on
image([0 1084], [0 1084],TrunkAngle_Rotation) % Plotting trunk angle image
alpha(.90)
hold on
axis off
% Set the size for the figure
set(gcf, 'Units', 'Normalized', 'OuterPosition', [.5, .5, 0.19, 0.311]);
Thanks in advance.

Accepted Answer

DGM
DGM on 25 Sep 2023
Edited: DGM on 25 Sep 2023
Since the images are the same size, this can be done with a simple blend, without the need for any compositing.
% reading in the two images
Protractor = importdata('STS_Protractor2.jpg');
Torso = importdata('STS_Torso2.jpg'); % top of midline is at (349,304) (349.5,316)
RotationPoint = [510,745]; % This is the point the Torso image will be rotated around (center of hip)
Protractor_0 = [510,135]; % location of the 0 value on the protractor
LineLength = 500; % Length of the line centered on the torso and extending past the head
Angle = 25;
timeperiod = 'Pre00';
% rotate trunk image by max trunk angle from model output data, then format image so it can be placed on top of protractor image
TrunkAngle_Rotation = rotateAround(Torso,RotationPoint(2),RotationPoint(1),-Angle, 'crop'); % (image,y-coord,x-cord,angle,'variable') - Allows for rotation of torso image
Clear_Background = ~rotateAround(true(size(Torso)),RotationPoint(2),RotationPoint(1),-Angle); % When image is rotated, this changes the background to white rather than leaving black space.
TrunkAngle_Rotation(Clear_Background) = 255; % Redefines the variable to include the new white background
% blend the two images using a "darken" blend
outpict = min(Protractor,TrunkAngle_Rotation);
% Plotting the output image
imshow(outpict,'border','tight');
Note that there's nothing preventing the hand and torso from obscuring the thigh. That can be addressed.
% reading in the two images
Protractor = importdata('STS_Protractor2.jpg');
Torso = importdata('STS_Torso2.jpg'); % top of midline is at (349,304) (349.5,316)
RotationPoint = [510,745]; % This is the point the Torso image will be rotated around (center of hip)
Protractor_0 = [510,135]; % location of the 0 value on the protractor
LineLength = 500; % Length of the line centered on the torso and extending past the head
Angle = 25;
timeperiod = 'Pre00';
% create a mask
mask = imfill(im2gray(Torso)<250,'holes');
mask = bwareafilt(mask,1);
imshow(mask,'border','tight');
% rotate trunk image by max trunk angle from model output data, then format image so it can be placed on top of protractor image
TrunkAngle_Rotation = rotateAround(Torso,RotationPoint(2),RotationPoint(1),-Angle, 'crop'); % (image,y-coord,x-cord,angle,'variable') - Allows for rotation of torso image
Clear_Background = ~rotateAround(true(size(Torso)),RotationPoint(2),RotationPoint(1),-Angle); % When image is rotated, this changes the background to white rather than leaving black space.
TrunkAngle_Rotation(Clear_Background) = 255; % Redefines the variable to include the new white background
mask = rotateAround(mask,RotationPoint(2),RotationPoint(1),-Angle, 'crop'); % rotate the mask
% blank the background where the FG overlaps
Protractor(repmat(mask,[1,1,size(Protractor,3)])) = 255;
% blend the two images using a "darken" blend
outpict = min(Protractor,TrunkAngle_Rotation);
% Plotting the two figures ontop of each other
imshow(outpict,'border','tight');
... although I suppose that's still kind of wrong. I guess the arm and torso could be masked separately, but I don't know how far you want to go with that. The concept of carrying the mask through the transformation should hold.
Using an RGBA workflow would simplify this a bit, but not with standard IPT/MATLAB tools.
  3 Comments
Image Analyst
Image Analyst on 26 Sep 2023
@DGM Just wondering, how did you get his rotateAround.m? Doesn't look like he ever attached it.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 25 Sep 2023
Try creating a new image by replacing only the parts of the rotated one where it's not white. Like
mask = TrunkAngle_Rotation < 200; % A 3-D RGB image since TrunkAngle_Rotation is color.
combinedImage = Protractor; % Initialize
combinedImage(mask) = 0; % Blacken where the mask is.
imshow(combinedImage);
If the blue line is not blue and you want it blue, then you may have to adapt it a bit.
  4 Comments
Josh Tome
Josh Tome on 25 Sep 2023
Eventhough neither of the image files have any transparency, I was hoping to eliminate what appears to be the transparency of the top image so that you can't see any of the bottom image through it.
Image Analyst
Image Analyst on 25 Sep 2023
I don't have the code for rotateAround. I'll check back later for it.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!