How to get the coordinate of this four points?
1 view (last 30 days)
Show older comments
How to get the coordinate of this four points?
Original Image
2 Comments
Jan
on 5 Jan 2012
Is this a bw image with perfect alignment or a scanned gray-scale image which can be slightly rotated?
Accepted Answer
David Young
on 5 Jan 2012
Here's another approach which works with the clean image you've given us. It's not as neat as Matt's approach, but it includes some techniques that you might find helpful when you're working with your more difficult camera images.
The core of this approach is a pair of morphological dilations which merge the bars into each other to form a large rectangular block. It depends on knowing the orientation of the bars, at least approximately.
imraw = imread('2ebx0t2.gif'); % dowloaded image
img = imraw < max(imraw(:)); % binarized
masklen = 21; % max bar spacing in pixels
lmask = zeros(1, masklen); % mask to smear pixels to right
lmask(ceil(end/2):end) = 1;
rmask = fliplr(lmask); % mask to smear pixels to left
block = imdilate(img, lmask) & imdilate(img,rmask); % smear pixels sideways
rprops = regionprops(block, 'BoundingBox', 'Area'); % get measurements
[~, bigblock] = max([rprops.Area]); % select largest block
bbox = rprops(bigblock).BoundingBox; % and get its bounding box
cornersx = bbox([1 1 1 1]) + [0 bbox([3 3]) 0]; % rearrange to get coords
cornersy = bbox([2 2 2 2]) + [0 0 bbox([4 4])];
imshow(imraw, []); % display image
hold on; plot(cornersx, cornersy, 'b+'); hold off; % with corner coordinates
More Answers (1)
Matt Tearle
on 5 Jan 2012
With such a clean image, you can do it pretty easily:
x = imread('barcode.gif');
y = x<15;
figure
imagesc(y), colormap('gray')
ridx1 = find(any(y,2),1,'first')
cidx1 = find(any(y,1),1,'first')
cidx2 = find(any(y,1),1,'last')
z = y(ridx1:end,cidx1:cidx2);
ridx2 = find(all(~z,2),1,'first') + ridx1 - 2
figure
imagesc(y), colormap('gray')
hold on
plot([cidx1,cidx1,cidx2,cidx2],[ridx1,ridx2,ridx1,ridx2],'r*')
The coordinates you're after being (ridx1,cidx1), (ridx1,cidx2), (ridx2,cidx1), and (ridx2,cidx2). But this won't work so well with a noisy, rotated (etc) image.
EDITED to make the last visualization clearer, a la David Young's approach.
See Also
Categories
Find more on Image Segmentation and Analysis 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!