This time, we will repeat the previous experiment, with a minor adjustment; we will add a preview
command in our code. Let's try it:
>> imaqreset >> vid = videoinput('winvideo', 1, 'dvsd_720x576'), >> test = uint8(zeros(576,720,3,100)); >> profile on
>>preview(vid);
for
loops:>> for i = 1:100 temp = getsnapshot(vid); % Acquire a frame fprintf('Processing frame number %d… ',i) % Announcement for k = 1:3 % For all 3 color channels test(:,:,k,i) = imadjust(temp(:,:,k)); % Adjust contrast end subplot(1,2,1),imshow(temp ) % Display current frame subplot(1,2,2),imshow(test(:,:,:,i)) % Display processed frame end
>> profile off % Close profiler >> profile viewer % Display profiling results
Quite different results than what we saw before.
>> stoppreview(vid)
This example showed us a general truth about MATLAB programming. The solution to our problems, especially when they are relevant to processing speed, is frequently much simpler than expected. In our case here, the solution was to open a Preview window that continuously displays what our camera sees. This way, the total time spent for 100 calls of our getsnapshot
function fell from a huge 26.824 seconds to a very low 0.353 seconds. Our code still has not reached 25 fps, since the imshow
function needs 5 seconds, hence leads to a 1/0.05 = 20 fps rate alone, but this is a smaller problem that can be handled in other ways.
Now we have reached our most crucial point. Our code is near real-time, but still not actual real-time. You should try to make adjustments in the code, which will enable it to run at least twice as fast as the one we have created so far. As different machines will produce different processing time results, you should have a goal of doubling the performance in your machine. Therefore, you should time the process of the previous example in your own machine and then try to improve the code while checking if you have accomplished your goal (at least doubling the speed). It goes without saying that we seek a performance increase without throwing out the part of the code displaying our results.
Q1. Which of the following are true?
for
loop with getsnapshot
and pause
inside it.getsnapshot
.