MATLAB: How to get the coordinate of this four points

image analysisimage processingMATLAB

How to get the coordinate of this four points?
Original Image

Best Answer

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