Barcode detection by webcam.
15 views (last 30 days)
Show older comments
Duc Do Minh
on 18 Dec 2015
Commented: Walter Roberson
on 19 Dec 2015
I'm using 2013a version and i have a project which uses webcam to read barcode. My functions are based on a demo of Matlab vision, how could i read and decode through webcam directly, not using "getsnapshot"? I'm sorry for my bad grammar. Here is my code:
obj = videoinput('winvideo', 2,'YUY2_640x480');
set(obj,'ReturnedColorSpace','rgb');
src_obj = getselectedsource(obj);
get(src_obj);
vidRes = get(obj, 'VideoResolution');
nBands = get(obj, 'NumberOfBands');
hImage = image( zeros(vidRes(2), vidRes(1), nBands) );
FinalCode1=0;
while (1)
ip_im = getsnapshot(obj);
imshow(ip_im);
%filter
fi_im=imsharpen(ip_im);
% Convert from RGB to Gray
gray_im= rgb2gray(fi_im);
%1
%Threshold Image to black and white
J = im2bw(gray_im, graythresh(gray_im));
%Resize and process
J=imresize(J,[240 320]);
Row = [80 120 160]';
[R,F] = Feature(J, Row);
[Center, Width, Num] = Bar(F);
[Sequence, Num] = Detection(Width, Num, 2);
[LGCode, LCode, GCode, RCode, LCodeRev, GCodeRev, RCodeRev] = Codebook;
[Code, Conf] = Recognition(Center, Width, Sequence, Num, LGCode, LCode, GCode, RCode, LCodeRev, GCodeRev, RCodeRev);
%Validation
OddSum= Code(1:1)+Code(3:3)+Code(5:5)+Code(7:7)+Code(9:9)+Code(11:11)+Code(13:13);
EvenSum = Code(2:2)+Code(4:4)+Code(6:6)+Code(8:8)+Code(10:10)+Code(12:12);
Checksums = OddSum + 3*EvenSum;
Valid = and((not(mod(Checksums,10))),(Conf >= 0.7));
FinalCode=uint64(0);
for i = 1 :13
FinalCode=FinalCode*10 + uint64(Code(i:i));
end
if and(Valid ,(FinalCode ~= FinalCode1))
Code'
sprintf('%013i',FinalCode)
FinalCode1 = FinalCode';
end
end
0 Comments
Accepted Answer
Walter Roberson
on 18 Dec 2015
If you were using R2014a or later, instead of using getsnapshot() from the Image Acquisition Toolbox, you could use snapshot() from the USB Webcams support package.
The general purpose of getsnapshot() and snapshot() are the same: they obtain a single frame from the video feed so that the frame can be analyzed.
2 Comments
Walter Roberson
on 19 Dec 2015
snapshot() does not trigger or delay video output. If you are using the Computer Vision toolbox you can create whatever output image you want and then step() the array with a videoplayer object to have the image displayed.
More Answers (0)
See Also
Categories
Find more on Tracking and Motion Estimation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!