Images are rotated counter clockwise without any reason,
Hi,
So, I’m working on a code where I have a folder with 33 images and they are of different orientation (mix of landscape and portrait sizes). I have the script that reads the images from the folder and overlay gaze data on top of these images. The script is running fine except a small bug that im facing is that for final plot of 33 images, some images that are actually portrait size (elongated top to bottom) are plotted with counter clock-wise rotation to show it as a landscape.
The two images below are for reference. where Img x025 is correct plotted with right data but Img x033 is incorrect and its rotated and therefore the gaze data is correct but due to rotation, its not overlaid correctly.
I want the x033 image to look like this below and there are about 12-14 images that are displayed or showed in this way with wrong orientation:
The code is as follows, which is part of a larger script:
imageDir = ‘C:Usersmqs6680Downloadssaliency-masterimages1’;
% Load the data
filename = ‘DD2.tsv’;
opts = detectImportOptions(filename, ‘FileType’, ‘text’);
data = readtable(filename, opts);
% Get unique participants
participants = unique(data.(‘ParticipantName’));
% Initialize a structure for participants
participantData = struct();
% Get a list of all JPG files in the folder
imageFiles = dir(fullfile(imageDir, ‘*.jpg’));
% Read resolutions and store in a map for quick lookup
resolutions = containers.Map(‘KeyType’, ‘char’, ‘ValueType’, ‘any’);
for i = 1:length(imageFiles)
imagePath = fullfile(imageDir, imageFiles(i).name);
img = imread(imagePath);
resolution = [size(img, 1), size(img, 2)]; % [height, width]
[~, imageID, ~] = fileparts(imageFiles(i).name);
resolutions(imageID) = resolution;
end
% Now you can access each participant’s data and each image’s data using:
% participantData.participantName_var.imageName_var
%% Individual Fixation map
% Assuming participantData is already filled from previous code
% Get all participant names from the structure
participantNames = fieldnames(participantData);
% Prompt user to enter a participant name or choose from a list
disp(‘Available participants:’);
disp(participantNames);
selectedParticipant = input(‘Enter the name of the participant to plot (case-sensitive): ‘, ‘s’);
% Check if the entered participant name is valid
if isfield(participantData, selectedParticipant)
% Get all images for the selected participant
imageNames = fieldnames(participantData.(selectedParticipant));
for j = 1:length(imageNames)
imageName = imageNames{j};
imageData = participantData.(selectedParticipant).(imageName);
% Ensure data exists for plotting
if ~isempty(imageData)
actualFixationX = imageData.ActualFixationX;
actualFixationY = imageData.ActualFixationY;
% Read the image from the file
imagePath = fullfile(‘C:Usersmqs6680Downloadssaliency-masterimages’, strcat(imageName, ‘.jpg’)); % Assumes image extension is jpg
img = imread(imagePath);
% Check if the image needs to be rotated (portrait orientation)
if size(img, 1) > size(img, 2)
img = imrotate(img, -90); % Rotate the image clockwise by 90 degrees
% Correctly swap and calculate new fixation points
tempX = actualFixationX; % Temporary variable for swapping
actualFixationX = actualFixationY; % New X is the old Y
actualFixationY = size(img, 1) – tempX + 1; % New Y is adjusted based on the new image height
end
% Create a scatter plot on top of the image
figure; % Create a new figure for each image
imshow(img, ‘InitialMagnification’, ‘fit’); % Display the image and fit it to the window
hold on; % Hold on to overlay scatter plot on the image
scatter(actualFixationX, actualFixationY, ‘filled’, ‘r’); % Red points
hold off; % Release the hold to allow further plots to be displayed separately
title(sprintf(‘Fixation Points on Image for Participant: %s, Image: %s’, selectedParticipant, imageName));
xlabel(‘Actual Fixation X (pixels)’);
ylabel(‘Actual Fixation Y (pixels)’);
% Explicitly set the axis limits to match the image dimensions
axis on; % Turn on axis lines, optional
xlim([0 size(img, 2)]); % Set x-axis limits based on image width
ylim([0 size(img, 1)]); % Set y-axis limits based on image height
end
end
else
fprintf(‘No data available for participant: %sn’, selectedParticipant);
end
Also, I have tried following different debugging methods:
Method 1:
% Check if the image needs to be rotated (portrait orientation)
if size(img, 1) > size(img, 2)
img = imrotate(img, -90); % Rotate the image clockwise by 90 degrees
% Swap X and Y fixation points for the rotated image
newFixationX = size(img, 1) – actualFixationY + 1; % Recalibrate to the new dimensions
newFixationY = actualFixationX;
actualFixationX = newFixationX;
actualFixationY = newFixationY;
end
Method 2:
% Check if the image needs to be rotated (portrait orientation)
if size(img, 1) > size(img, 2)
img = imrotate(img, -90); % Rotate the image clockwise by 90 degrees
% Swap X and Y fixation points for the rotated image
[actualFixationX, actualFixationY] = deal(actualFixationY, size(img, 2) – actualFixationX + 1);
end
I have cut portion of the code where it was handling the gaze data related stuff. Anyhelp will be appreciated. Thank you so much.Hi,
So, I’m working on a code where I have a folder with 33 images and they are of different orientation (mix of landscape and portrait sizes). I have the script that reads the images from the folder and overlay gaze data on top of these images. The script is running fine except a small bug that im facing is that for final plot of 33 images, some images that are actually portrait size (elongated top to bottom) are plotted with counter clock-wise rotation to show it as a landscape.
The two images below are for reference. where Img x025 is correct plotted with right data but Img x033 is incorrect and its rotated and therefore the gaze data is correct but due to rotation, its not overlaid correctly.
I want the x033 image to look like this below and there are about 12-14 images that are displayed or showed in this way with wrong orientation:
The code is as follows, which is part of a larger script:
imageDir = ‘C:Usersmqs6680Downloadssaliency-masterimages1’;
% Load the data
filename = ‘DD2.tsv’;
opts = detectImportOptions(filename, ‘FileType’, ‘text’);
data = readtable(filename, opts);
% Get unique participants
participants = unique(data.(‘ParticipantName’));
% Initialize a structure for participants
participantData = struct();
% Get a list of all JPG files in the folder
imageFiles = dir(fullfile(imageDir, ‘*.jpg’));
% Read resolutions and store in a map for quick lookup
resolutions = containers.Map(‘KeyType’, ‘char’, ‘ValueType’, ‘any’);
for i = 1:length(imageFiles)
imagePath = fullfile(imageDir, imageFiles(i).name);
img = imread(imagePath);
resolution = [size(img, 1), size(img, 2)]; % [height, width]
[~, imageID, ~] = fileparts(imageFiles(i).name);
resolutions(imageID) = resolution;
end
% Now you can access each participant’s data and each image’s data using:
% participantData.participantName_var.imageName_var
%% Individual Fixation map
% Assuming participantData is already filled from previous code
% Get all participant names from the structure
participantNames = fieldnames(participantData);
% Prompt user to enter a participant name or choose from a list
disp(‘Available participants:’);
disp(participantNames);
selectedParticipant = input(‘Enter the name of the participant to plot (case-sensitive): ‘, ‘s’);
% Check if the entered participant name is valid
if isfield(participantData, selectedParticipant)
% Get all images for the selected participant
imageNames = fieldnames(participantData.(selectedParticipant));
for j = 1:length(imageNames)
imageName = imageNames{j};
imageData = participantData.(selectedParticipant).(imageName);
% Ensure data exists for plotting
if ~isempty(imageData)
actualFixationX = imageData.ActualFixationX;
actualFixationY = imageData.ActualFixationY;
% Read the image from the file
imagePath = fullfile(‘C:Usersmqs6680Downloadssaliency-masterimages’, strcat(imageName, ‘.jpg’)); % Assumes image extension is jpg
img = imread(imagePath);
% Check if the image needs to be rotated (portrait orientation)
if size(img, 1) > size(img, 2)
img = imrotate(img, -90); % Rotate the image clockwise by 90 degrees
% Correctly swap and calculate new fixation points
tempX = actualFixationX; % Temporary variable for swapping
actualFixationX = actualFixationY; % New X is the old Y
actualFixationY = size(img, 1) – tempX + 1; % New Y is adjusted based on the new image height
end
% Create a scatter plot on top of the image
figure; % Create a new figure for each image
imshow(img, ‘InitialMagnification’, ‘fit’); % Display the image and fit it to the window
hold on; % Hold on to overlay scatter plot on the image
scatter(actualFixationX, actualFixationY, ‘filled’, ‘r’); % Red points
hold off; % Release the hold to allow further plots to be displayed separately
title(sprintf(‘Fixation Points on Image for Participant: %s, Image: %s’, selectedParticipant, imageName));
xlabel(‘Actual Fixation X (pixels)’);
ylabel(‘Actual Fixation Y (pixels)’);
% Explicitly set the axis limits to match the image dimensions
axis on; % Turn on axis lines, optional
xlim([0 size(img, 2)]); % Set x-axis limits based on image width
ylim([0 size(img, 1)]); % Set y-axis limits based on image height
end
end
else
fprintf(‘No data available for participant: %sn’, selectedParticipant);
end
Also, I have tried following different debugging methods:
Method 1:
% Check if the image needs to be rotated (portrait orientation)
if size(img, 1) > size(img, 2)
img = imrotate(img, -90); % Rotate the image clockwise by 90 degrees
% Swap X and Y fixation points for the rotated image
newFixationX = size(img, 1) – actualFixationY + 1; % Recalibrate to the new dimensions
newFixationY = actualFixationX;
actualFixationX = newFixationX;
actualFixationY = newFixationY;
end
Method 2:
% Check if the image needs to be rotated (portrait orientation)
if size(img, 1) > size(img, 2)
img = imrotate(img, -90); % Rotate the image clockwise by 90 degrees
% Swap X and Y fixation points for the rotated image
[actualFixationX, actualFixationY] = deal(actualFixationY, size(img, 2) – actualFixationX + 1);
end
I have cut portion of the code where it was handling the gaze data related stuff. Anyhelp will be appreciated. Thank you so much. Hi,
So, I’m working on a code where I have a folder with 33 images and they are of different orientation (mix of landscape and portrait sizes). I have the script that reads the images from the folder and overlay gaze data on top of these images. The script is running fine except a small bug that im facing is that for final plot of 33 images, some images that are actually portrait size (elongated top to bottom) are plotted with counter clock-wise rotation to show it as a landscape.
The two images below are for reference. where Img x025 is correct plotted with right data but Img x033 is incorrect and its rotated and therefore the gaze data is correct but due to rotation, its not overlaid correctly.
I want the x033 image to look like this below and there are about 12-14 images that are displayed or showed in this way with wrong orientation:
The code is as follows, which is part of a larger script:
imageDir = ‘C:Usersmqs6680Downloadssaliency-masterimages1’;
% Load the data
filename = ‘DD2.tsv’;
opts = detectImportOptions(filename, ‘FileType’, ‘text’);
data = readtable(filename, opts);
% Get unique participants
participants = unique(data.(‘ParticipantName’));
% Initialize a structure for participants
participantData = struct();
% Get a list of all JPG files in the folder
imageFiles = dir(fullfile(imageDir, ‘*.jpg’));
% Read resolutions and store in a map for quick lookup
resolutions = containers.Map(‘KeyType’, ‘char’, ‘ValueType’, ‘any’);
for i = 1:length(imageFiles)
imagePath = fullfile(imageDir, imageFiles(i).name);
img = imread(imagePath);
resolution = [size(img, 1), size(img, 2)]; % [height, width]
[~, imageID, ~] = fileparts(imageFiles(i).name);
resolutions(imageID) = resolution;
end
% Now you can access each participant’s data and each image’s data using:
% participantData.participantName_var.imageName_var
%% Individual Fixation map
% Assuming participantData is already filled from previous code
% Get all participant names from the structure
participantNames = fieldnames(participantData);
% Prompt user to enter a participant name or choose from a list
disp(‘Available participants:’);
disp(participantNames);
selectedParticipant = input(‘Enter the name of the participant to plot (case-sensitive): ‘, ‘s’);
% Check if the entered participant name is valid
if isfield(participantData, selectedParticipant)
% Get all images for the selected participant
imageNames = fieldnames(participantData.(selectedParticipant));
for j = 1:length(imageNames)
imageName = imageNames{j};
imageData = participantData.(selectedParticipant).(imageName);
% Ensure data exists for plotting
if ~isempty(imageData)
actualFixationX = imageData.ActualFixationX;
actualFixationY = imageData.ActualFixationY;
% Read the image from the file
imagePath = fullfile(‘C:Usersmqs6680Downloadssaliency-masterimages’, strcat(imageName, ‘.jpg’)); % Assumes image extension is jpg
img = imread(imagePath);
% Check if the image needs to be rotated (portrait orientation)
if size(img, 1) > size(img, 2)
img = imrotate(img, -90); % Rotate the image clockwise by 90 degrees
% Correctly swap and calculate new fixation points
tempX = actualFixationX; % Temporary variable for swapping
actualFixationX = actualFixationY; % New X is the old Y
actualFixationY = size(img, 1) – tempX + 1; % New Y is adjusted based on the new image height
end
% Create a scatter plot on top of the image
figure; % Create a new figure for each image
imshow(img, ‘InitialMagnification’, ‘fit’); % Display the image and fit it to the window
hold on; % Hold on to overlay scatter plot on the image
scatter(actualFixationX, actualFixationY, ‘filled’, ‘r’); % Red points
hold off; % Release the hold to allow further plots to be displayed separately
title(sprintf(‘Fixation Points on Image for Participant: %s, Image: %s’, selectedParticipant, imageName));
xlabel(‘Actual Fixation X (pixels)’);
ylabel(‘Actual Fixation Y (pixels)’);
% Explicitly set the axis limits to match the image dimensions
axis on; % Turn on axis lines, optional
xlim([0 size(img, 2)]); % Set x-axis limits based on image width
ylim([0 size(img, 1)]); % Set y-axis limits based on image height
end
end
else
fprintf(‘No data available for participant: %sn’, selectedParticipant);
end
Also, I have tried following different debugging methods:
Method 1:
% Check if the image needs to be rotated (portrait orientation)
if size(img, 1) > size(img, 2)
img = imrotate(img, -90); % Rotate the image clockwise by 90 degrees
% Swap X and Y fixation points for the rotated image
newFixationX = size(img, 1) – actualFixationY + 1; % Recalibrate to the new dimensions
newFixationY = actualFixationX;
actualFixationX = newFixationX;
actualFixationY = newFixationY;
end
Method 2:
% Check if the image needs to be rotated (portrait orientation)
if size(img, 1) > size(img, 2)
img = imrotate(img, -90); % Rotate the image clockwise by 90 degrees
% Swap X and Y fixation points for the rotated image
[actualFixationX, actualFixationY] = deal(actualFixationY, size(img, 2) – actualFixationX + 1);
end
I have cut portion of the code where it was handling the gaze data related stuff. Anyhelp will be appreciated. Thank you so much. data import, plotting, image analysis MATLAB Answers — New Questions