How to display an image in axes of GUI in the function I added

2 visualizzazioni (ultimi 30 giorni)
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 BytesAvailable_Callback(obj,event) , when i call : axes(handles.axes1) imshow('image1.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?
My code:
%-----------------HAM HIEN VIDEO-------------------------
global vidobj; global hand; % create the video object with the desired resolution vidobj =videoinput('winvideo',1,'YUY2_640x480');%, info.DeviceInfo(1,1).SupportedFormats{1}); set(vidobj, 'FramesPerTrigger', 1); set(vidobj, 'ReturnedColorSpace', 'rgb');
% create a handle to the axes where the image will be displayed vidRes = get(vidobj, 'VideoResolution'); nBands = get(vidobj, 'NumberOfBands'); hImage = image( zeros(vidRes(2), vidRes(1), nBands), 'parent', handles.video);
% identify which function is associated with the UpdatePreviewWindowFcn % video_preview_fcn (see below) is called automatically each time the camera has an image available - you don't need to call this function setappdata(hImage, 'UpdatePreviewWindowFcn', @video_preview_fcn);
% call preview to start the live image feed preview(vidobj,hImage);
%srcObj1 = get(vidobj, 'Source'); % Tuy chinh mau cho video %set(srcObj1(1), 'Brightness', 10);
%-------------------- HIEN ANH LEN GIAO DIEN------------------------
s=serial('COM7');
s.BytesAvailableFcn = @BytesAvailable_Callback
fopen(s);
% fclose(s);
%ind = fscanf(s);
%i=isempty(ind)
*%This function I added to read data from RS232 port, then processing this data and make it as a conditon to display image but can not *
%---------------------- Serial_Callback
function BytesAvailable_Callback(obj,event)
global hand;
global vidobj;
ind = fscanf(obj)
%--------Moi them vao - Trung ----
fid = fopen('mavach.txt', 'at');
fprintf(fid,'%s\n\n',ind);%Write 'word' in text file (upper)
% set(handles.mavach,'String',ind);
fclose(fid);
%---------------------------------
% Edit code here - Ngo Hai Bac
set(hand.mavach,'String',ind);
% if(~isempty(ind))
% Neu co ma vach thi chup anh-----------
testpic = getsnapshot(vidobj);
imwrite(testpic,'image1.jpg');
_axes(hand.image)
imshow('image1.jpg');_
% I can not display this image in axes because it always diplay in new window
%--------Xuat ma vach ra file txt ----
fid = fopen('mavach.txt', 'at');
% fprintf(fid,'%s\n',ind); %Write 'word' in text file (upper)
fclose(fid);
%---------------------------------
%------------------------------------------------------------------
%------------NHAN DANG BIEN SO XE------------------
% Nhi phan anh
BW = imread('image1.jpg');
I1 = rgb2gray(BW);
I1 = medfilt2(I1,[3 3]); % Loc trung vi
level=graythresh(I1);
BW1=im2bw(I1,level);
BW1 = bwareaopen(BW1,350); % Loai bo nhung doi tuong be hon 350 pixel
%-----------TIM VUNG BIEN SO XE---------------------
object = regionprops(BW1);
for index=1:length(object)
%BoundingBox co 2 thanh phan tra ve la [ul_corner width]
% ul_corner: toa do [x y]chi goc phia tren cung ben trai cua box
% width: [x_width y_width] chieu rong va chieu cao cua box
if object(index).Area > 50000 && object(index).BoundingBox(3)/object(index).BoundingBox(4) > 0.5
x = ceil(object(index).BoundingBox(1)+4) % Lam tron len
y= ceil(object(index).BoundingBox(2)+4) %Toa do
widthX = floor(object(index).BoundingBox(3)-15) % Lam tron xuong
widthY = floor(object(index).BoundingBox(4)-9) % Chieu cao va chieu dai
subimage(index) = {BW(y:y+widthY,x:x+widthX,:)};
subimage{index}=~subimage{index};
% figure,imshow(subimage{index});
BW1= subimage{index};
end
end
imwrite(BW1,'image2.jpg')
axes(hand.plateimage)
imshow('image2.jpg')
%This image is too...

Risposta accettata

Image Analyst
Image Analyst il 8 Apr 2013
I had this weird thing once with live video. Sean De Wolski was trying to explain it to me over the phone. He had some reason for it and I think his "solution" was to write the image directly to the "cdata" property of the axes instead of using imshow(). I never did that, and I dropped the project for a few weeks and when I came back to it, I didn't have that problem anymore and didn't have to use Sean's workaround. I don't know what happened to "fix" it. Perhaps Sean will chime in here with an explanation.
  2 Commenti
leo
leo il 9 Apr 2013
How to write image directly to the "cdata" property?
h = image(X); colormap(map) Y = get(h,'CData'); I find this in help but don't know how?

Accedi per commentare.

Più risposte (1)

renuka
renuka il 21 Mag 2021
I wanted to diaplay all images in a folder one by one on the same axes using Matlab GUI. My below code is displaying only first image in the folder? kndly give the solution
handles.output = hObject;
filelist=dir(fullfile('E:\desktop\MATLAB\img-seg\CNN\ias1\*.jpg'));
handles.filelist = filelist;
handles.frameindex = 1;
% Update handles structure
guidata(hObject, handles);
axes(handles.axes1);
filelist = handles.filelist;
frameindex = handles.frameindex;
myfolder='E:\desktop\MATLAB\img-seg\CNN\ias1';
currentframefile = filelist(frameindex).name;
handles.frameindex = frameindex+1;
ff=fullfile(myfolder,currentframefile);
I=imread(ff);
imshow(I);
guidata(hObject, handles);

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by