how to remove the background from an image ?

65 次查看(过去 30 天)
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 :)

采纳的回答

Image Analyst
Image Analyst 2021-11-1
编辑:Image Analyst 2021-11-1
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.

更多回答(2 个)

yanqi liu
yanqi liu 2021-11-1
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 2021-11-1
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)

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by