Cropping a bounded area using image processing

1 view (last 30 days)
Ilker Dogan
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;
img = imread('rrr.jpg');
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');
I1 = imadjust(Igray,stretchlim(Igray),[]);
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:

Answers (1)

yanqi liu
yanqi liu on 22 Jan 2022
clc
clear all
close all
ei = 25;
st = 35;
k=ei*st;
img = imread('rrr.jpg');
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');
I1 = imadjust(Igray,stretchlim(Igray),[]);
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');

Community Treasure Hunt

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

Start Hunting!

Translated by