How to get the coordinate of this four points?
1 次查看(过去 30 天)
显示 更早的评论
How to get the coordinate of this four points?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/146431/image.jpeg)
Original Image
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/146432/image.gif)
2 个评论
Jan
2012-1-5
Is this a bw image with perfect alignment or a scanned gray-scale image which can be slightly rotated?
采纳的回答
David Young
2012-1-5
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
更多回答(1 个)
Matt Tearle
2012-1-5
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!