Clear Filters
Clear Filters

how to remove the background from an image ?

65 views (last 30 days)
i want to remove the background from the image so i can get the grapes only ... but i don't know how to... pls help :) .. i attached the image..... thanks in advance :)

Accepted Answer

Image Analyst
Image Analyst on 1 Nov 2021
Edited: Image Analyst on 1 Nov 2021
Very easy. Just use the Color Thresholder on the Apps tab.
  1. Load the Color Thresholderapp
  2. Load the image
  3. choose hsv color space.
  4. Adjust thresholds for green
  5. Click the Export Function button.
See attached m-file
If you want, you can use find() or regionprops() to find the bounding box of the mask and crop the image to the bounding box.

More Answers (2)

yanqi liu
yanqi liu on 1 Nov 2021
clc; clear all; close all;
im = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/785503/image.jpeg');
jm = rgb2lab(im);
vm = mat2gray(jm(:,:,3));
bw = im2bw(vm);
im2 = im;
im2(~cat(3,bw,bw,bw)) = 255;
figure; imshow(mat2gray(im2));

Chunru
Chunru on 1 Nov 2021
There are many image segmentation techniques availble in image processing toolbox. Search "image segmentation" in documentation. Below is one technique:
RGB = imread("image.jpeg");
imshow(RGB);
L = superpixels(RGB, 500);
% foreground
f = drawrectangle(gca, 'Position', [2000 1000 1500 600], 'Color', 'g');
foreground = createMask(f, RGB);
% background
b = drawrectangle(gca, 'Position', [10 10 4500 600], 'Color', 'r');
background = createMask(b, RGB);
% Segmentation
BW = lazysnapping(RGB, L, foreground, background);
RGBseg = RGB;
RGBseg(repmat(~BW, [1 1 3])) = 0;
figure
imshow(RGBseg)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!