This example shows how to set up and display a live histogram.
The Image Acquisition Toolbox™ together with the Image Processing Toolbox™ can be used to display a video feed with a live histogram. This can be useful when calibrating camera settings such as aperture using manual controls. This example shows how to use the PREVIEW function, its associated custom update function and the IMHIST function to place a video preview window adjacent to a live histogram. The techniques here can be used to display other live information too. For example, a live video feed can be placed next to a filtered version of the video.
This example uses a callback function, found in the
directory, that displays the live histogram in a figure window.
Watch a clip of the video feed and histogram. (8 seconds)
% Access an image acquisition device. vidobj = videoinput('winvideo'); % Convert the input images to grayscale. vidobj.ReturnedColorSpace = 'grayscale';
An image object of the same size as the video is used to store and display incoming frames.
% Retrieve the video resolution. vidRes = vidobj.VideoResolution; % Create a figure and an image object. f = figure('Visible', 'off'); % The Video Resolution property returns values as width by height, but % MATLAB images are height by width, so flip the values. imageRes = fliplr(vidRes); subplot(1,2,1); hImage = imshow(zeros(imageRes)); % Set the axis of the displayed image to maintain the aspect ratio of the % incoming frame. axis image;
Specify the UpdatePreviewWindowFcn callback function that is called each time a new frame is available. The callback function is responsible for displaying new frames and updating the histogram. It can also be used to apply custom processing to the frames. More details on how to use this callback can be found in the documentation for the PREVIEW function. This callback function itself is defined in the file update_livehistogram_display.m
% Here are the contents of update_livehistogram_display.m which contains % the callback function. dbtype('update_livehistogram_display.m')
1 function update_livehistogram_display(obj,event,hImage) 2 % This callback function updates the displayed frame and the histogram. 3 4 % Copyright 2007-2017 The MathWorks, Inc. 5 % 6 7 % Display the current image frame. 8 set(hImage, 'CData', event.Data); 9 10 % Select the second subplot on the figure for the histogram. 11 subplot(1,2,2); 12 13 % Plot the histogram. Choose 128 bins for faster update of the display. 14 imhist(event.Data, 128); 15 16 % Refresh the display. 17 drawnow
% The PREVIEW function starts the camera and display. The image on which to % display the video feed is also specified. preview(vidobj, hImage); % View the histogram for 30 seconds. pause(30);
Above is a sample image of the histogram and video feed.
% Stop the preview image and delete the figure. stoppreview(vidobj); delete(f);
Once the video input object is no longer needed, delete and clear the associated variable.
delete(vidobj) clear vidobj