Skip to content

Instantly share code, notes, and snippets.

@lordloh
Created October 30, 2017 23:04
Show Gist options
  • Save lordloh/fbede42a6ea23a0c1f121b1308870b07 to your computer and use it in GitHub Desktop.
Save lordloh/fbede42a6ea23a0c1f121b1308870b07 to your computer and use it in GitHub Desktop.
A MATLAB function to generate a mask for playing cards
function mask=card_mask(img)
BW_a=imbinarize(img,'adaptive');
BW_g=imbinarize(img,'global');
cc_a=bwconncomp(BW_a,4);
cc_g=bwconncomp(BW_g,4);
obj_sizes_a=cellfun('length',cc_a.PixelIdxList);
obj_sizes_g=cellfun('length',cc_g.PixelIdxList);
[px_count_a,idx_a]=max(obj_sizes_a);
[px_count_g,idx_g]=max(obj_sizes_g);
mask=false(size(img));
mask_idx=intersect(cc_a.PixelIdxList{idx_a},cc_g.PixelIdxList{idx_g});
mask(mask_idx)=true;
mask=imfill(mask,'holes');
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment