crop objects from color image and still have color objects

1 次查看(过去 30 天)
Hello,
I know how to crop an object from a binary image. But how to do it from a color image to get a color final image around the object.
Thnak you,

回答(2 个)

mariem farhat
mariem farhat 2015-8-12
Ok, I find it!
RGB=imread(I);
%%Convert to gray scale
Gray=rgb2gray(RGB);
%%Convert to binary image
threshold = graythresh(Gray);
imagen =~im2bw(Gray,threshold);
%%Remove all object containing fewer than 30 pixels
imagen = bwareaopen(imagen,30);
%%Label connected components
[L Ne]=bwlabel(imagen);
%%Measure properties of image regions
propied=regionprops(L,'BoundingBox','Area');
% %%Objects extraction
for n=1:Ne
if(propied(n).Area>1000)
[r,c] = find(L==n);
switch (type)
case 'RGB'
%%get the image of the object
s_I=size(RGB);
s = size(r);
n1(:,:,1) = zeros(s_I(1),s_I(2));
n1(:,:,2) = zeros(s_I(1),s_I(2));
n1(:,:,3) = zeros(s_I(1),s_I(2));
n1 (:,:,1) =255;
n1 (:,:,2) = 255;
n1 (:,:,3) = 255;
for i=1:s
n1(r(i),c(i),1)=RGB(r(i),c(i),1);
n1(r(i),c(i),2)=RGB(r(i),c(i),2);
n1(r(i),c(i),3)=RGB(r(i),c(i),3);
end;
n1 = uint8(n1);
% get the croped object from the image
R = n1(min(r):max(r),min(c):max(c),1);
G = n1(min(r):max(r),min(c):max(c),2);
B = n1(min(r):max(r),min(c):max(c),3);
n2 = cat(3,R,G,B);
n2 = uint8(n2);
that is it! Hope it will help someone.

Abdullah Sarwar
Abdullah Sarwar 2021-1-14
编辑:Abdullah Sarwar 2021-1-14
clear all;
clc;
rgbImage = imread('04738.bmp');
% Extract color channels.
redChannel = rgbImage(:,:,1); % Red channel
greenChannel = rgbImage(:,:,2); % Green channel
blueChannel = rgbImage(:,:,3); % Blue channel
size_img=size(rgbImage);%size of img
minval=min(size_img(1:2));%min value of size matrix
if minval<128% crop dimentions
i=64
elseif minval<256
i=128
elseif minval<512
i=256
elseif minval<1024
i=512
elseif minval<2048
i=1024
end
x=redChannel(1:i,1:i);%crop red
y=greenChannel(1:i,1:i);%crop green
z=blueChannel(1:i,1:i);%crop blue
recombinedRGBImage = cat(3, x, y, z);%recombine all planes
% Display them all.
subplot(1,2,1);
imshow(rgbImage);
subplot(1,2,2);
imshow(recombinedRGBImage);
title('Recombined to Form Original RGB Image Again')
%% please make changes according to your need

类别

Help CenterFile Exchange 中查找有关 Get Started with Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by