How can I add a histogram to a live video preview?

2 views (last 30 days)
Hi I'm working on a project where I've created a custom GUI and connected the axes (axes1) to an external camera when a button is pushed and on the same button push a live histogram of the preview image should come up in a different axes. I have gotten the preview to appear and the histogram to appear, however, the histogram does not update itself automatically when the preview image is changed. I've tried everything and need a direction to go on with this. Here's my code below for just the button I'm trying to get working:
function pushbutton1_Callback(hObject, eventdata, handles)
handles = guidata(hObject);
imaqmex('feature', '-previewFullBitDepth', true);
axes(handles.axes1)
vid = videoinput('Camera_source_1', 1, 'MONO16_BIN2x2_1024x1024_SlowMode');
src = getselectedsource(vid);
vidres = vid.VideoResolution;
imWidth = vidres(1);
imHeight = vidres(2);
nBands = vid.NumberOfBands;
himage = image(zeros(imHeight,imWidth, nBands), 'Parent', handles.axes1);
figSize = get(handles.axes1, 'Position');
figWidth = figSize(3);
figHeight = figSize(4);
gca.unit = 'pixels';
gca.position = [((figWidth - imWidth)./2)...
((figHeight - imHeight)./2)...
imWidth imHeight];
vid.FramesPerTrigger = Inf;
src.ExposureTime = user_val(1);
im = preview(vid, himage);
ax = im.Parent;
im.CDataMapping = 'scaled';
colormap(ax, gray);
signalRange = [30 2000];
ax.CLim = signalRange;
axes(handles.axes2)
himage.CData = im.CData;
set(himage, 'CData', im.CData);
imhist(himage.CData, 128)
grid on
grid minor
drawnow
preview(vid, himage)
guidata(hObject, handles);

Accepted Answer

Image Analyst
Image Analyst on 21 Jul 2018
I think you're going to have to get a snapshot with getsnapshot() and then histogram that.
  3 Comments
Image Analyst
Image Analyst on 23 Jul 2018
I think you might have to not use preview and just have getsnapshot() and imhist() in a loop. So it's not "live" anymore but a series of snapshots, hopefully the histogramming won't slow it down too much and it will look "live". You might want to do the histogram only every 10 snapshots or so to speed it up.
Paul Jacobs
Paul Jacobs on 23 Jul 2018
Ok so I put in a for loop around the portion that gets the snap shot and plots it as a histogram as seen below:
axes(handles.axes2)
for I = 1:1:10000
frame_test_data = getsnapshot(vid);
himage.CData = frame_test_data;
set(himage, 'CData', frame_test_data);
imhist(himage.CData, 128)
grid on
grid minor
drawnow
end
Now the only issue I'm still having is that it interferes with another button being pushed saying: "Reference to non-existent field 'vid'" Vid being my video object that I'm trying to reference to do auto-contrasting in the preview window.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!