Background remove from Image
Version 1.0.0 (1.2 KB) by
MUHAMMAD BILAL WARIS
remove the background from an image in MATLAB without using any built-in function.
This MATLAB code is used to remove the background from an image without using any built-in functions. The code performs the following steps:
- Reads an image file and loads it into the variable img.
- Converts the image to grayscale using the rgb2gray function. This is because the thresholding operation is performed on a grayscale image, as it represents the intensity values of each pixel.
- Uses global thresholding to convert the grayscale image to a binary image. The graythresh function is used to determine the optimal threshold value for the image. The im2bw function is then used to convert the grayscale image to a binary image.
- Invert the binary image
- Multiply the binary image with the original image using repmat function to remove the background. This step sets the pixels corresponding to the background in the original image to 0.
- Display the image using the imshow function.
This is a basic method to remove background, it may not work well with images that have complex backgrounds or have variations in intensity. You can use other methods like color-based or contour-based methods to improve the accuracy of removing background.
Cite As
MUHAMMAD BILAL WARIS (2026). Background remove from Image (https://www.mathworks.com/matlabcentral/fileexchange/123950-background-remove-from-image), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Created with
R2022b
Compatible with any release
Platform Compatibility
Windows macOS LinuxTags
Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
| Version | Published | Release Notes | |
|---|---|---|---|
| 1.0.0 |