Attempting to Crop binary image

I want the white section of this image I took,
I used this code to attempt a crop,
[rows, columns] = find(ProperlyOrientedBinaryBlock);
topRow = min(rows);
bottomRow = max(rows);
leftColumn = min(columns);
rightColumn = max(columns);
croppedImage = ProperlyOrientedBinaryBlock(topRow:bottomRow, leftColumn:rightColumn);
imshow(croppedImage)
I'm not getting the result I expect. Anyone have any idea?

1 件のコメント

Satyajeet Sasmal
Satyajeet Sasmal 2015 年 11 月 16 日
Could you provide us with your workflow? What does the "ProperlyOrientedBinaryBlock" function do?

サインインしてコメントする。

回答 (2 件)

Image Analyst
Image Analyst 2015 年 11 月 16 日

1 投票

Try this:
grayImage = imread('binary.jpg');
binaryImage = grayImage > 128;
subplot(2, 1, 1);
imshow(binaryImage);
% Get convex hulls
binaryImage2 = bwconvhull(binaryImage, 'objects');
% Extract largest blob only
binaryImage2 = bwareafilt(binaryImage2, 1);
% Now you can crop:
[rows, columns] = find(binaryImage2);
topRow = min(rows);
bottomRow = max(rows);
leftColumn = min(columns);
rightColumn = max(columns);
croppedImage = binaryImage(topRow:bottomRow, leftColumn:rightColumn);
subplot(2, 1, 2);
imshow(croppedImage)
It's not super robust - if the blobs are broken up or nested, it's possible that it would need some more code to get it perfect, but it will work for rectangular Lego blocks as long as they're not too broken apart.

1 件のコメント

manimuthu Venu
manimuthu Venu 2020 年 9 月 9 日
Is it possible to get the original coordinates of the cropped image, so that it can be merged in the same position in another image? thanks a lot in advance

サインインしてコメントする。

Thorsten
Thorsten 2015 年 11 月 16 日
編集済み: Thorsten 2015 年 11 月 16 日

0 投票

The image contains some spurious white points, e.g. at (950,3). You can delete them using erosion
se = strel('disk',1)
erodedB = imerode(B,se);

カテゴリ

ヘルプ センター および File ExchangeImage Processing Toolbox についてさらに検索

質問済み:

2015 年 11 月 12 日

コメント済み:

2020 年 9 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by