# Cropping a bounded area using image processing

1 view (last 30 days)
Ilker Dogan on 21 Jan 2022
Answered: yanqi liu on 22 Jan 2022
Hello everyone. I want to crop the traced area with image processing. What is the steps to do it?
My Code:
clc
clear all
close all
ei = 25;
st = 35;
k=ei*st;
h = ones(ei,st)/k;
I = imfilter(img,h,'symmetric');
figure
subplot(3,3,1), imshow(img), title('Orginal');
subplot(3,3,2), imshow(I), title('Filtered Image');
%Converting to BW
Igray = rgb2gray(I);
subplot(3,3,3), imshow(Igray), title('Grey');
level = graythresh(I1);
BWj = imbinarize(I1,level);
dim = size(BWj);
IN = ones(dim(1),dim(2));
BW = xor(BWj,IN); %Inverting
subplot(3,3,4), imshow(BW), title('Black and White');
%Finding of an initial point
row = round(dim(1)/2);
col = min(find(BW(row,:)));
%Tracing
boundary = bwtraceboundary(BW, [row, col], 'W');
subplot(3,3,5), imshow(img), title('Traced');
hold on;
%Display traced boundary
plot(boundary(:,2),boundary(:,1),'g','LineWidth',2);
hold off
%
The Output: yanqi liu on 22 Jan 2022
clc
clear all
close all
ei = 25;
st = 35;
k=ei*st;
h = ones(ei,st)/k;
I = imfilter(img,h,'symmetric');
figure
subplot(3,3,1), imshow(img), title('Orginal');
subplot(3,3,2), imshow(I), title('Filtered Image');
%Converting to BW
Igray = rgb2gray(I);
subplot(3,3,3), imshow(Igray), title('Grey');
level = graythresh(I1);
BWj = imbinarize(I1,level);
dim = size(BWj);
IN = ones(dim(1),dim(2));
BW = xor(BWj,IN); %Inverting
subplot(3,3,4), imshow(BW), title('Black and White');
%Finding of an initial point
row = round(dim(1)/2);
col = min(find(BW(row,:)));
%Tracing
boundary = bwtraceboundary(BW, [row, col], 'W');
subplot(3,3,5), imshow(img), title('Traced');
hold on;
%Display traced boundary
plot(boundary(:,2),boundary(:,1),'g','LineWidth',2);
hold off
%
% crop it
[r,c] = find(BW);
rect = [min(c) min(r) max(c)-min(c) max(r)-min(r)];
img_rect = imcrop(img, rect);
subplot(3,3,6), imshow(img_rect), title('Croped');

R2021b

### Community Treasure Hunt

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

Start Hunting!