plot with reversed y-axis in a normal y-axis

I have a video plotted (or played frame by fame) and I want to plot a shape representing the corresponding eye position.
The video has its y-axis reversed (can't control it) (0 up). The eye position is with coordinates in which the y-axis is normal (0 at bottom).
I need to plot this eye position somehow with reversed y-axis on the video.
I'm using insertShape but I can't change the position of the shape to be plotted in a reversed y-axis on the video. I tried flip, axis handel, and many things, nothing is working.
Any suggestion please?!

 Réponse acceptée

Jack
Jack le 30 Mar 2023
You can use the image function in MATLAB to display the video frames and then use insertShape to plot the eye position on top of the frame. To reverse the y-axis, you can set the YDir property of the axes object to 'reverse'. Here's some sample code to get you started:
% Load the video file
v = VideoReader('my_video.mp4');
% Create a figure and axes to display the frames
fig = figure;
ax = axes('Parent', fig);
% Set the y-axis direction to 'reverse'
set(ax, 'YDir', 'reverse');
% Loop through the frames and display them
while hasFrame(v)
frame = readFrame(v);
image(ax, frame);
% Get the eye position for the current frame
eye_pos = [x, y, width, height]; % Replace with your own code
% Plot the eye position on top of the frame
insertShape(ax, 'Rectangle', eye_pos, 'Color', 'red');
% Update the figure
drawnow;
end
In this code, x, y, width, and height are the coordinates of the eye position in a normal y-axis coordinate system. You may need to adjust these values to match the dimensions of your video frames. The insertShape function plots a red rectangle at the specified eye position, and the drawnow function updates the figure to display the current frame with the eye position.

4 commentaires

Suzuki
Suzuki le 31 Mar 2023
Thanks.
I have tried that, but it still plot the eye pos using the video y-axis.
I tried yyaxis left and right but it slows down the video when I plot it togrther with the eye pos.
the eye pos are adjusted I belive, because if I plot the video and the eye pos seperately, the position is correct, it is only wrong when they are together because the video y-axis is reversed. I can only reverse it if I flip the image but then the image is not in the right shape.
If the yyaxis command is slowing down the video playback when you plot it together with the eye position, you can try a different approach to adjust the y-axis limits of the video plot to match the reversed y-axis of the eye position plot. Here's an example code snippet that might help:
% create a sample video and eye position data
video = rand(100, 100, 50);
eye_pos = rand(50, 2);
% plot the video and eye position separately to adjust the y-axis limits
subplot(2, 1, 1);
imshow(video(:,:,1));
xlim([1 size(video, 2)]);
ylim([1 size(video, 1)]);
subplot(2, 1, 2);
plot(eye_pos(:,1), size(video, 1) - eye_pos(:,2) + 1, 'b'); % adjust the y-coordinates
xlim([1 size(video, 2)]);
ylim([1 size(video, 1)]);
% plot the video and eye position together
figure;
imshow(video(:,:,1));
hold on;
plot(eye_pos(:,1), size(video, 1) - eye_pos(:,2) + 1, 'b'); % adjust the y-coordinates
hold off;
xlim([1 size(video, 2)]);
ylim([1 size(video, 1)]);
In this code, we plot the video and eye position separately to adjust the y-axis limits of both plots to match the reversed y-axis of the eye position data. Then we plot them together using imshow for the video and plot for the eye position, with the hold on and hold off commands to combine the plots. We also adjust the y-coordinates of the eye position data by subtracting them from the height of the video and adding 1 to shift them to the correct y-axis position.
I hope this helps! Let me know if you have any further questions.
Suzuki
Suzuki le 3 Avr 2023
Modifié(e) : Suzuki le 3 Avr 2023
Thanks.
When I tried that it plots frame 1 then the eye trace on what seems to be a different figure (because the background is white, not the video frame 1 (for ex)), but if i remove the hold off it' ok. However, the eye traces are not in the correct position. Furthermore,it plots the video images in black and white, don't know why.
And I also tried your example, but the eye traces are also plotted in the lower corner of the video, I guess this is not the right position.
Suzuki
Suzuki le 5 Avr 2023
Modifié(e) : Suzuki le 5 Avr 2023
Hey Jack,
I was doing something incorrect, but I've corrected it.
Your codes worked ,it flipped the eye pos, but no need for the +1.
thank alot.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by