MATLAB Answers

How can I take a snapshot from USB camera and then analyze it?

조회 수: 45(최근 30일)
PamunkeBoy
PamunkeBoy 26 Jun 2018
댓글: PamunkeBoy 27 Jun 2018
On my previous work, I was trying to take an image from a file and find the centroids of the images. However, I am having trouble taking the snapshot and then having MATLAB save the image so then I can analyze it for the centroids of the dots. Is there any way this can be done?
% clear, clc
% establish video input
cam=videoinput('gentl',1,'Mono8');
%take snapshot
preview(cam)
triggerconfig(cam, 'manual');
snapshot=getsnapshot(cam);
%Display image
im=imagesc(snapshot)
im=max(im,[],3);
% Segment blobs
bw=imclose(im<20,strel('disk',5)); binaryImage = imfill(im, 'holes');
% Cenroids of individual blobs
RP=regionprops(bw,'Centroid'); N=numel(RP); C=zeros(N,2);
for i=1:N,
C(i,:)=RP(i).Centroid;
end
% Centroid of all blobs
C_net=regionprops(double(bw),'Centroid');
C_net=C_net.Centroid;
figure imshow(bw) hold on plot(C(:,1),C(:,2),'or','MarkerSize',10,'LineWidth',2) plot(C_net(:,1),C_net(:,2),'*g','MarkerSize',10,'LineWidth',2)
%Calculating the distance formula
D=sqrt(((C(4,1)) - (C(2,1))).^2 + ((C(4,2)) - (C(2,2))).^2);
formatSpec = 'Distance of top row is %4.2f pixels\n'; fprintf(formatSpec,D)
D2=sqrt(((C(3,1)) - (C(1,1))).^2 + ((C(3,2)) - (C(1,2))).^2);
formatSpec = 'Distance of bottom row is %4.2f pixels\n'; fprintf(formatSpec,D2)
closePreview(cam);
Specifically the im=max function appears to not be working. My goal is to get MATLAB to be able to take the snapshot image and determine the centroids between them.

  댓글 수: 1

로그인 to comment.

답변(2개)

Florian Morsch
Florian Morsch 27 Jun 2018
편집: Florian Morsch 27 Jun 2018
First install the support package "MATLAB Support Package for USB Webcams" ( https://de.mathworks.com/matlabcentral/fileexchange/45182-matlab-support-package-for-usb-webcams )
Then you can do something like this:
cam = webcam(); % This chooses the first connected webcam. If you have multiple use webcam(2), webcam(3) etc.
Snapshot = snapshot(cam);
image(Snapshot);
imwrite(Snapshot, 'SomeName.png'); % To save it to another location you can do something like
'C:/User/MATLABPictures/SomeName.png')
This will take a snapshot and save it as .png file to your current folder. Or you can work with the snapshot, e.g.:
cam = webcam();
cam.Resolution = '800x600';
Snapshot = snapshot(cam);
SnapshotGray = rgb2gray(Snapshot);

  댓글 수: 2

PamunkeBoy
PamunkeBoy 27 Jun 2018
I am currently using a Allied Vision Webcam, will this work with it?
Walter Roberson
Walter Roberson 27 Jun 2018
The GENTL package is perhaps more appropriate than the USB Webcams package for the Allied Vision devices.

로그인 to comment.


PamunkeBoy
PamunkeBoy 27 Jun 2018
I have actually now been able to save the image that has been captured and can upload it to MATLAB for analysis. I now want to be able to cycle this code so that each time it performs this task it saves a new image instead of just replacing the file. Can this be done? Perhaps if I modify "saveas" in my code
clear, clc
% establish video input
cam=videoinput('gentl',1,'Mono8');
%take snapshot
preview(cam)
closepreview(cam)
triggerconfig(cam, 'manual');
snapshot=getsnapshot(cam);
%display image
figure,imshow(snapshot)
saveas(gcf,'MyFigure.bmp')
% Get the image
im=imread('MyFigure.bmp');
im=max(im,[],3);
% Segment blobs
bw=imclose(im<40,strel('disk',5));
binaryImage = imfill(im, 'holes');
% Cenroids of individual blobs
RP=regionprops(bw,'Centroid');
N=numel(RP);
C=zeros(N,2);
for i=1:N, C(i,:)=RP(i).Centroid; end
%Layout of Image
figure
imshow(bw)
hold on
plot(C(:,1),C(:,2),'or','MarkerSize',10,'LineWidth',2)
%Calculating the distance formula
D=sqrt(((C(4,1)) - (C(2,1))).^2 + ((C(4,2)) - (C(2,2))).^2);
formatSpec = 'Distance of top row is %4.2f pixels\n';
fprintf(formatSpec,D)
D2=sqrt(((C(3,1)) - (C(1,1))).^2 + ((C(3,2)) - (C(1,2))).^2);
formatSpec = 'Distance of bottom row is %4.2f pixels\n';
fprintf(formatSpec,D2)

  댓글 수: 4

표시 이전 댓글 수: 1
PamunkeBoy
PamunkeBoy 27 Jun 2018
Thank you!!! I was also wondering is there a way to start the loop every time by pressing a key so it wont just cycle through?
Walter Roberson
Walter Roberson 27 Jun 2018
image_number = 0;
while true
  key = input('Return to continue, Q to quit: ', 's');
  if strcmpi(key, 'q'); break; end
  snapshot = getsnapshot(cam);
  image_number = image_number + 1;
  filename = sprintf('framein_%04d.bmp', image_number);
  imwrite(snapshot, filename);
  im = snapshot;
  ...
end
PamunkeBoy
PamunkeBoy 27 Jun 2018
Thank you for all of your help Walter!

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by