Please help me to solve this problem!. I have to design four images flickering at different frequencies and spacing between images are 2.5cm.
4 views (last 30 days)
Show older comments
S = imread('rose.jpeg'); % read the image from files
A = imresize(S,[183,275]); % resize image
subplot(221); % to make 2 by 2 matrix of pictures
imshow(A); % display the image
B = imread('lotus.jpeg');
subplot(222);
imshow(B);
T = imread('lily.jpeg') ;
C = imresize(T,[183,275]);
subplot(223);
imshow(C);
D = imread('orchid.jpeg');
subplot(224);
imshow(D);
% imfinfo("Lily.jpeg")
% imfinfo("rose.jpeg")
% imfinfo("lotus.jpeg")
% imfinfo("orchid.jpeg")
2 Comments
Answers (1)
Suraj Kumar
on 3 Oct 2024
Hi Shweta,
To display images flickering at different frequencies in MATLAB, you can implement the flickering logic in your code as follows:
Use a loop to update the display at intervals determined by the flicker frequencies. The ‘mod’ function helps toggle the visibility of each image.
% Start the flickering loop
tic;
while toc < duration
for i = 1:4
% Determine which image to display
currentTime = toc;
if mod(floor(currentTime / flickerIntervals(i)), 2) == 0
% Display image
subplot(2, 2, i);
switch i
case 1
imshow(A);
case 2
imshow(B);
case 3
imshow(C);
case 4
imshow(D);
end
else
% Clear image
subplot(2, 2, i);
imshow([]);
end
end
pause(0.1); % Small pause to control update speed
end
You can adjust the image sizes, frequencies, or duration according to your requirements.
To learn more about the ‘tic’ or ‘pause’ functions in MATLAB, you can refer to the following documentation:
- https://www.mathworks.com/help/matlab/ref/tic.html
- https://www.mathworks.com/help/matlab/ref/pause.html
Hope this solves your query.
1 Comment
DGM
on 3 Oct 2024
Avoid destroying or creating objects in a loop. That's just slow. Create them and then change their properties as needed. In this case, the CData doesn't even need to be changed. Just toggle the visiblity.
% some dummy images
A = imread('cameraman.tif');
B = fliplr(A);
C = flipud(A);
D = flipud(B);
% stuff everything into a cell array
% i'm just doing this so that i can be flexible with addressing
% which makes testing easier
images = {A; B; C; D};
% parameters
duration = 100; % total run time for the animation (seconds)
flickerIntervals = [1 2 4 8]; % period for each frame (seconds)
tiling = [2 2]; % [y x]
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set up the figure completely beforehand
nframes = numel(images);
him = gobjects(nframes,1);
for k = 1:nframes
subplot(tiling(1),tiling(2),k)
him(k) = imshow(images{k});
end
framestate = true(1,nframes);
% Start the flickering loop
tic;
while toc < duration
currentTime = toc;
T(t) = currentTime; t = t+1;
% calculate all states at a single point in time
% compensate for the mod2 operation, otherwise you're halving the frequency
priorframestate = framestate;
framestate = mod(floor(currentTime ./ (flickerIntervals/2)), 2) == 0;
% only operate on state changes
for f = find(framestate ~= priorframestate)
if framestate(f)
him(f).Visible = 'on';
else
him(f).Visible = 'off';
end
end
drawnow
% this only controls the best-case timing resolution
% the achievable refresh rate is limited by loop time
% for me, loop time is already frequently above 50ms,
% so we're just wasting extra time with a pause
% pause(0.01);
end
The peak loop time does seem to depend on the number of subplots in the figure. Note however that the peak loop time (for me) is the same regardless of whether we're updating a single subplot or multiple subplots.
Compared to the original, this has less than half the peak loop time of the original (with the pause disabled). That's just on my system though. It's still not able to deal cleanly with frame durations <<0.5s. Then again, I get terrible tearing on this LCD monitor anyway, so maybe I just can't tell.
Note also that none of this arranges the images with a specified physical spacing. I didn't bother, because the idea of using MATLAB to flicker images as an advertisement just seems ridiculous. Maybe OP is selling seizure medications.
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!