Effacer les filtres
Effacer les filtres

Code Gui read value in opc data access explorer

3 vues (au cours des 30 derniers jours)
Luong Tu
Luong Tu le 22 Avr 2016
Commenté : Luong Tu le 23 Avr 2016
my code is below but i can not read data from opc to capture image, please help me!!thank you very much!! It show error
Undefined operator '==' for input arguments of type 'struct'.
Error in camera_camera_OutputFcn (line 101)
if r == 1
Error in gui_mainfcn (line 264)
feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in camera (line 42)
gui_mainfcn(gui_State, varargin{:});
function varargout = camera_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
vid = videoinput('winvideo',1, 'MJPG_1280x720');
% only capture one frame per trigger, we are not recording a video
vid.FramesPerTrigger = 1;
% output would image in RGB color space
vid.ReturnedColorspace = 'rgb';
% tell matlab to start the webcam on user request, not automatically
triggerconfig(vid, 'manual');
% we need this to know the image height and width
vidRes = get(vid, 'VideoResolution');
% image width
imWidth = vidRes(1);
% image height
imHeight = vidRes(2);
% number of bands of our image (should be 3 because it's RGB)
nBands = get(vid, 'NumberOfBands');
% create an empty image container and show it on axPreview
hImage = image(zeros(imHeight, imWidth, nBands), 'parent', handles.axes1);
% begin the webcam preview
preview(vid, hImage);
handles.vid=vid;
da = opcda('localhost', 'KEPware.KEPServerEx.V4');
connect(da);
grp = addgroup(da);
itm1 = additem(grp, 'Channel1.Device1.Group1.capture');
r = read(itm1);
if r == 1
axes(handles.axes2)
if ~isfield(handles, 'vid')
warndlg('Please do the oncamera first!');
return;
end
vid=handles.vid;
pause(3);
data=getsnapshot(vid);
imshow(data);
savename = strcat('Desktop' ,'hinh', '.jpg');
imwrite(data,savename);
axes(handles.axes3);
RGB = imread('Desktophinh.jpg');
imshow(RGB);
I = rgb2gray(RGB);
threshold = graythresh(I);
bw = im2bw(I,threshold);
imshow(bw)
bw = bwareaopen(bw,100);
se = strel('disk',2);
bw = imclose(bw,se);
imshow(bw)
[B,L] = bwboundaries(bw,'noholes');
imshow(label2rgb(L, @jet, [.5 .5 .5]))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
stats = regionprops(L,'Area','Centroid');
threshold = 0.94;
stats = regionprops(L,'Area','Centroid');
threshold = 0.80;
% loop over the boundaries
for k = 1:length(B)
% obtain (X,Y) boundary coordinates corresponding to label 'k'
boundary = B{k};
% compute a simple estimate of the object's perimeter
delta_sq = diff(boundary).^2;
perimeter = sum(sqrt(sum(delta_sq,2)));
% obtain the area calculation corresponding to label 'k'
area = stats(k).Area;
% compute the roundness metric
metric = 4*pi*area/perimeter^2;
% display the results
metric_string = sprintf('%2.2f',metric);
% mark objects above the threshold with a black circle
if metric > threshold
centroid = stats(k).Centroid;
plot(centroid(1),centroid(2),'ko');
text(centroid(1),centroid(2),'hinh tron');
end
if metric < threshold
centroid = stats(k).Centroid;
plot(centroid(1),centroid(2),'ko');
text(centroid(1),centroid(2),'khong la hinh tron');
end
text(boundary(1,2)-35,boundary(1,1)+13,metric_string,'Color','y',...
'FontSize',14,'FontWeight','bold');
end
end

Réponse acceptée

Walter Roberson
Walter Roberson le 22 Avr 2016
It is clear in the documentation that read() is going to return a structure. http://www.mathworks.com/help/opc/ug/read.html
You might want to access the Value field, r.Value
  3 commentaires
Walter Roberson
Walter Roberson le 22 Avr 2016
Sorry, I do not do interactive work as a volunteer. No SMS, no Facebook Messenger, no Skype, no teamviewer, no Snapchat, no Google Hangouts, no phone calls.
Luong Tu
Luong Tu le 23 Avr 2016
I have a code to capture image, but one time, how to capture image one time I write value 1 to opc data access Explorer, thank you very much my code is function varargout = webcam_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure varargout{1} = handles.output; vid = videoinput('winvideo',1, 'MJPG_1280x720');
% only capture one frame per trigger, we are not recording a video vid.FramesPerTrigger = 1; % output would image in RGB color space vid.ReturnedColorspace = 'rgb'; % tell matlab to start the webcam on user request, not automatically triggerconfig(vid, 'manual'); % we need this to know the image height and width vidRes = get(vid, 'VideoResolution'); % image width imWidth = vidRes(1); % image height imHeight = vidRes(2); % number of bands of our image (should be 3 because it's RGB) nBands = get(vid, 'NumberOfBands'); % create an empty image container and show it on axPreview hImage = image(zeros(imHeight, imWidth, nBands), 'parent', handles.axes1); % begin the webcam preview preview(vid, hImage); handles.vid=vid; guidata(hObject, handles); da = opcda('localhost', 'KEPware.KEPServerEx.V4'); connect(da); grp = addgroup(da); grp.UpdateRate = 40; itm1 = additem(grp, 'Channel1.Device1.Group1.capture'); data = read(grp); opcdata= data.Value; if (opcdata==1) opcdata=0; disconnect(da); delete(da); axes(handles.axes2); vid=handles.vid; pause(3); data=getsnapshot(vid); imshow(data); savename = strcat('Desktop' ,'hinh', '.jpg'); imwrite(data,savename); end

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by