Effacer les filtres
Effacer les filtres

Unable to display a selected image despite finding no errors while running the code

3 vues (au cours des 30 derniers jours)
I am writing the code for the part of my experiment where I am randomly displaying one of the already displayed image, along with a randomly assigned text (again, from a previous display), using PSYCHTOOLBOX on MATLAB. Though I am not getting any errors while I am running the code, only the text is getting displayed. I am unable to understand the error and would be extremely grateful if someone could help me regarding this. I have used two variations of the code, both of which faced the same issue, and I am posting both here for suggestions. Thanks in advance.
Code 1:
% Randomly select one of the stored image-text combinations
randomIndex = randi(numLocations);
% Display the randomly selected image and text
selectedImage = displayedImages{randomIndex};
selectedText = displayedTexts{randomIndex};
% Display the selected image
imageRect = CenterRectOnPointd([0 0 size(selectedImage, 2) size(selectedImage, 1)], xCenter, yCenter-(yCenter/3));
Screen('PutImage', window, selectedImage, imageRect);
% Display the selected text
DrawFormattedText(window, selectedText, 'center', yCenter+(yCenter/3), white);
% Flip the screen to display the image and text
Screen('Flip', window);
Code 2:
% Randomly select one of the stored image-text combinations
randomIndex = randi(numLocations);
% Display the randomly selected image and text
selectedImage = displayedImages{randomIndex};
selectedText = displayedTexts{randomIndex};
% Display the selected image and text
Screen('PutImage', window, selectedImage, CenterRectOnPointd([0 0 size(selectedImage, 2) size(selectedImage, 1)], xCenter, yCenter-(yCenter/3)));
DrawFormattedText(window, selectedText, 'center', yCenter+(yCenter/3), white);
% Flip the screen to display the image and text
Screen('Flip', window);

Réponse acceptée

Namnendra
Namnendra le 10 Avr 2024
Hi Adithi,
Both versions of your code seem logically sound for displaying an image and text using Psychtoolbox in MATLAB. However, the fact that only the text is being displayed, and not the image, suggests a potential issue with how the image is being handled or displayed. Here are several suggestions and considerations to troubleshoot and potentially resolve this issue:
1. Verify Image Loading and Storage
Ensure that the images stored in `displayedImages` are correctly loaded and stored as MATLAB matrices. If `displayedImages` contains file paths or is not correctly formatted, `Screen('PutImage')` might fail to display the images.
2. Use `Screen('DrawTexture')` Instead of `Screen('PutImage')`
`Screen('PutImage')` is not the recommended way to display images in Psychtoolbox due to performance reasons. Instead, use `Screen('DrawTexture')`. This requires preloading the images into offscreen windows (textures) and then drawing them. Here's how you can adapt your code:
% Assuming displayedImages now stores texture pointers
% Randomly select one of the stored image-text combinations
randomIndex = randi(numLocations);
% Display the randomly selected image and text
selectedImageTexture = displayedImages{randomIndex};
selectedText = displayedTexts{randomIndex};
% Display the selected image
imageRect = CenterRectOnPointd([0 0 size(selectedImage, 2) size(selectedImage, 1)], xCenter, yCenter-(yCenter/3));
Screen('DrawTexture', window, selectedImageTexture, [], imageRect);
% Display the selected text
DrawFormattedText(window, selectedText, 'center', yCenter+(yCenter/3), white);
% Flip the screen to display the image and text
Screen('Flip', window);
To preload images as textures, use something like:
imageTexture = Screen('MakeTexture', window, imread('imageFilePath.jpg'));
3. Check Image Transparency and Format
Ensure that the image format and transparency (alpha channel) are correctly handled. Some image formats or incorrect handling of the alpha channel can lead to images being "invisible" against the background.
4. Confirm Screen and Image Rectangles
Double-check the `imageRect` calculation and ensure it's correctly positioned and sized to display the image within the visible area of your window.
5. Debugging Steps
- Test with a Simple Image: Try displaying a simple, small image (e.g., a 100x100 pixel solid color square) to ensure that the issue is not with the image content or size.
- Check for Errors or Warnings: While you mentioned not getting errors, ensure that MATLAB's command window is not suppressing or overlooking warnings that could give clues.
- Manual Positioning: Temporarily hard-code `imageRect` to a known visible position (e.g., `[0 0 100 100]`) to ensure it's not an issue with dynamic positioning.
6. Screen Compatibility and Settings
Ensure your screen settings (resolution, color depth) are compatible with the images you're trying to display. In rare cases, display issues can stem from incompatibilities or limitations at the hardware or OS level.
By systematically going through these steps, you should be able to identify and resolve the issue with displaying images in your Psychtoolbox experiment.
I hope the above steps help resolve the issue.
Thank you.
  1 commentaire
Adithi Anil
Adithi Anil le 10 Avr 2024
Hi Namnendra,
Thank you for your kind and informative reply. It did help me resolve the issue.
Thank you very much.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by