How to remove image background in Matlab

How would I remove the gray background in the grayscale image so i could put a white backgound instead? Please help.. I'm a newbie in Matlab. i'm out of touch on these.. it just an assignment sir.. can you lend me a hand .. please. :((
(original image)
(desired output)

 Accepted Answer

Here is one simplistic example.
% an image
A = imread('coins.png');
% basic thresholding to select objects
mk = A>100;
% fill any holes in objects
mk = imfill(mk,'holes');
% add colored overlay on background region
bgcolor = [0.7 0.3 1]; % white or any color
outpict = imoverlay(A,~mk,bgcolor);
imshow(outpict)
Bear in mind that if the goal is to change colors in an image, simple binary masking isn't always a good solution for all needs. It will always leave ragged edges on the output. If this is technical image processing where visual appearances aren't important, then logical is probably appropriate.
Similarly, color adjustment by constant replacement destroys all content in the altered regions. Sometimes that's intended, sometimes not.

1 Comment

ow. okay sir. thank you for the help. i will keep these in mind. ^^

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2017b

Asked:

on 4 Nov 2022

Commented:

on 4 Nov 2022

Community Treasure Hunt

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

Start Hunting!