How to crop out the barcode??
3 次查看(过去 30 天)
显示 更早的评论
I have found out the way to determine the barcode and currently working on cropping out the barcode. How can I write the code so that it can crop out the barcode?? I have paste my original image and my coding below.
Original Image
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/146704/image.png)
Processed Image
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/146705/image.gif)
% Read Image
rgb = imread('barcode.jpg');
% Resize Image
rgb = imresize(rgb,0.33);
%figure(),imshow(rgb);
% Convert from RGB to Gray
Igray = rgb2gray(rgb);
BW2 = edge(Igray,'canny');
%figure(),imshow(BW2);
% Perform the Hough transform
[H, theta, rho] = hough(BW2);
% Find the peak pt in the Hough transform
peak = houghpeaks(H);
% Find the angle of the bars
barAngle = theta(peak(2));
J = imrotate(rgb,barAngle,'bilinear','crop');
%figure(),imshow(J);
Jgray = double(rgb2gray(J));
% Calculate the Gradients
[dIx, dIy] = gradient(Jgray);
%if min(dIx(:))<= -100 && max(dIx(:))>=100 || min(dIy(:))<=-100 && max(dIy(:))>=100
if barAngle <= 65 && barAngle >=-65 && min(dIx(:))<= -100
B = abs(dIx) - abs(dIy);
else
B = abs(dIy) - abs(dIx);
end
% Low-Pass Filtering
H = fspecial('gaussian', 20, 10);
C = imfilter(B, H);
C = imclearborder(C);
%figure(),imshow(C);
figure(),imagesc(C);colorbar;
0 个评论
采纳的回答
Chandra Kurniawan
2012-2-12
D = C > 20;
Ilabel = bwlabel(D);
stat = regionprops(Ilabel,'boundingbox');
for x = 1 : length(stat)
Icrop{x} = imcrop(J,stat(x).BoundingBox);
end
1 个评论
Daniel Purba
2020-7-27
Mas Chandra, saya menggunakan Code seperti diatas untuk cropping, hanya saja terkendala harus memilih mana cropping image yang mengandung barcode. Apakah ada code khusus untuk langsung mendapatkan cropped image yang mengandung barcode? Terimakasih.
Chandra, I tried the code you gave, the obstacle is we still have to choose a region that contains a barcode. Is there a way that directly displays a cropped image that contains a barcode? Thanks.
更多回答(2 个)
Image Analyst
2012-2-11
Looks like thresholding at about 20 should do it. Then label and use regionprops to do size filtering and get the bounding box. Then use imcrop to do the cropping.
2 个评论
Image Analyst
2012-2-12
Looks like Chandra provided that to you. If you want just the biggest blob though, you'd have to do it slightly differently.
Andrea Carron
2012-2-16
I tried to use the code posted by Kim with the image above, but I obtained this figure:
How is it possible?
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!