How to get the coordinate of this four points?

2 views (last 30 days)
How to get the coordinate of this four points?
Original Image
  2 Comments
Jan
Jan on 5 Jan 2012
Is this a bw image with perfect alignment or a scanned gray-scale image which can be slightly rotated?
Kim
Kim on 5 Jan 2012
This image is from the web, so it is perfect alignment.
However I'm dealing with scanned gray-scale image which was taken from the camera.

Sign in to comment.

Accepted Answer

David Young
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
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.

Community Treasure Hunt

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

Start Hunting!