在MATLAB中使用单个对象对图像进行分割

use*_*983 1 matlab image-processing image-segmentation

这是图片: 鸟

我的目标是将这只鸟放在另一个图像中.我尝试用MATLAB做的东西,rgb2gray然后imhist先得到鸟的强度,然后我做了一个面具,但我总是拿着一个面具,其中包括树和云.

buz*_*jwa 5

以下是您可以用于此任务的一些代码:

clear;
close all;
im = imread('~/Downloads/siraly_www.kepfeltoltes.hu_.jpg');
im = rgb2gray(im);

%// Manually crop the image
im = double(im(620:1619, 2150:3279));
%// Find edges
hedge = vision.EdgeDetector('Method', 'Sobel');
edges = step(hedge, im);
%// Dilate to close edges around object
edges = imdilate(edges, [0 1 0; 1 1 1; 0 1 0]);
%// Find boundary of object
bound = bwboundaries(edges);
bound = bound{1}; % Largest boundary is around object
%// Display image and object boundary
figure; imshow(im, []);
hold on;
plot(bound(:,2), bound(:,1), '.');
hold off;
%// Select all object pixels by filling the boundary
bwobject = false(size(im));
bwobject(sub2ind(size(im), bound(:,1), bound(:,2))) = true;
bwobject = imfill(bwobject, 'holes');
imobject = zeros(size(im));
imobject(bwobject) = im(bwobject);
figure; imshow(imobject, []);
Run Code Online (Sandbox Code Playgroud)

这里使用的方法是:

  1. 手动裁剪图像.
  2. 使用EdgeDetector对象进行边缘检测.
  3. 形态学操作仅检索使用对象像素imdilate,bwboundariesimfill.

以下是在代码中计算和绘制的对象边界: 带边界的对象

这是从图像中裁剪的对象: 对象被裁剪