crop objects from color image and still have color objects
Show older comments
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,
Answers (2)
mariem farhat
on 12 Aug 2015
Abdullah Sarwar
on 14 Jan 2021
Edited: Abdullah Sarwar
on 14 Jan 2021
0 votes
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
Categories
Find more on Blue in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!