create mask that separates objects from white background

8 ビュー (過去 30 日間)
Finn Hartley
Finn Hartley 2021 年 2 月 11 日
コメント済み: Finn Hartley 2021 年 2 月 28 日
Hi,
I need to create a mask for fruits placed inside a white box so i can analsyse the properties of the fruit. A green apple is shown in the attached image for example but the fruits could be a range of colours (excluding black/whitish colours), so i require code to segment these different coloured fruits into a mask that seperates them from the white backgoreund, but I am struggling on how to approach this. Any help would be appreciated.
Thankyou

採用された回答

BC
BC 2021 年 2 月 27 日
編集済み: BC 2021 年 2 月 28 日
Hi, maybe the code below can help start you off! It's not a perfect segmentation, and I'm fairly new to MATLAB so someone else might have a better method. This link may also help you too, have a look under "Functions", then "Segmentation Techniques".
apple = imread("apple.jpg"); % read in image name
grayapple = rgb2gray(apple); % convert image to grayscale
level = graythresh(grayapple); % get threshold value
BWapple = imbinarize(grayapple,level); % binarize image using the threshold value
BWopen = bwpropfilt(~BWapple,"area",1,"largest"); % filter only the 1 largest blob. The ~ inverts the mask
BWfilled = imfill(BWopen,"holes"); % fill any missing holes
finalimage = imoverlay(apple,~BWfilled,"k"); % burn the mask onto the original image, with black (k) background
imshow(finalimage) % show the final image
  1 件のコメント
Finn Hartley
Finn Hartley 2021 年 2 月 28 日
Hi BC,
Thanks for your help! thats exactly what im looking for.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImages についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by