show image in axes in serial callback function

i need your help
I'm trying to display an image in GUI but a problems is that I can not display image in axes in the function I added:
function Serial_Callback(obj,event,handles) , when i call : axes(handles.axes1) imshow('image.jpg') then it do not display in axes but display in new window figure1. If i call axes(handles.axes1); imshow('image1.jpg') in any button function, it correctly display in that axes. Why?
this is my code:
s = serial('COM1','Baudrate',9600,'DataBits',8,'Parity','none','StopBits',1);
s.ReadAsyncMode = 'continuous'; %read direction
s.InputBufferSize=2;
s.OutputBufferSize=1;
s.Terminator = 'CR';
s.BytesAvailableFcnCount = 1;
s.Timeout = 1;
s.BytesAvailableFcnMode = 'byte';
s.BytesAvailableFcn = @Serial_Callback;
try
fopen(s);
catch err;
if (strcmp(err.identifier,'MATLAB:serial:fopen:opfailed'))
msgbox('Open COMport fail!!! Please try again!','Error','error');
end
end
function Serial_Callback(hObject,eventdata,handles)
global video hinh I J K a P S s;
video = videoinput('winvideo',2);
hinh = getsnapshot(video);
imwrite(hinh,'anh.jpg');
a = imread('anh.jpg');
axes(handles.axes1);
imshow(a);
J = rgb2gray(a);
J = imadjust(J);
K = im2bw(J,0.7);
K = imfill(K,'hole');
K = bwareaopen(K,2500);
axes(handles.axes2);
imshow(K);

 Accepted Answer

Your code does not show how the handle structure gets created. Also, I don't see how what you've shown could even work since you've got
s.BytesAvailableFcn = @Serial_Callback;
which will result in your callback function Serial_Callback only receiving two arguments, not three, so I would expect you to get an error undefined variable handles in that callback.
Anyway, rather than
axes(someaxes);
imshow(someimage);
which always run the risk that some other callback/timer/user interaction changes the active axes between the two lines
imshow(someimage, 'Parent', someaxes);
is guaranteed to get the image on someaxes. I doubt it will solve your problem though.
Of course, if the axes you pass it is the wrong one, it'll still display somewhere else.

3 Comments

i'd try your code " imshow(a,'Parent',handles.axes1);" but still not working
Guillaume
Guillaume on 24 Mar 2017
Edited: Guillaume on 24 Mar 2017
Well, I did say that it's unlikely to change anything.
As I said, your code does not show how handles get passed to the callback. Clearly, whatever is in that handles structure when the callback gets called is not what you expect. Without you showing how it's passed, it's hard to tell what's wrong.
You can fix my code... how to handles get passed to callback?

Sign in to comment.

More Answers (0)

Asked:

on 24 Mar 2017

Commented:

on 25 Mar 2017

Community Treasure Hunt

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

Start Hunting!