how to select region with longest length on segmented image ?
4 views (last 30 days)
Show older comments
I have segmented the image like the following: and within the boundaries I want to select the boundary with the longest length from the boundaries extracted from the image like the image below: help me please.
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
markerSize = 4;
%[filename, pathname] = uigetfile({'*.*'},'File Selector');
%if ~isequal(filename,0)
%fullname = strcat(pathname,filename);
I=imread('MH2.jpeg');
%else
% msgbox('Ban chua chon anh?')
%end
Id = size(I);
if length(Id) == 3
B0 = rgb2gray(I);
else
B0 = I;
end
B0 = imadjust(B0);
k1 = fspecial('gaussian', [5 5], 2.5);
Ic = imfilter(B0,k1);
figure(1),subplot(3,3,1), imshow(I);
title('anh goc');
figure(1), subplot(3,3,2), imshow(B0);
title('anh tang tuong phan');
figure(1), subplot(3,3,3), imshow(Ic);
title('anh loc gaussian');
%loc trung vi
I_m = medfilt2(B0,[3 3]);
figure(1),subplot(3,3,4), imshow(Ic);
title('anh loc trung vi');
%anh nhi phan
thresholdValue = 60;
bw = Ic > thresholdValue;
bw = bwareafilt(bw, 1);
figure(1),subplot(3,3,5), imshow(bw);
title('anh nhi phan');
%dong hinh thai
se = strel("disk",10);
C1 = imclose(bw, se);
figure(1),subplot(3,3,6), imshow(C1);
title('dong hinh thai');
%khai thac ranh gioi
[B,L,N] = bwboundaries(C1);
figure(2);
%subplot(3,3,7);
imshow(C1); hold on;
title('khai thac ranh gioi');
for k=1:length(B)
boundary = B{k};
if(k > N)
plot(boundary(:,2), boundary(:,1), 'g','LineWidth',1.5);
else
plot(boundary(:,2), boundary(:,1), 'r','LineWidth',1.5);
end
end
%
0 Comments
Accepted Answer
yanqi liu
on 22 Mar 2022
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
markerSize = 4;
%[filename, pathname] = uigetfile({'*.*'},'File Selector');
%if ~isequal(filename,0)
%fullname = strcat(pathname,filename);
I=imread('https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/935629/MH2.jpeg');
%else
% msgbox('Ban chua chon anh?')
%end
Id = size(I);
if length(Id) == 3
B0 = rgb2gray(I);
else
B0 = I;
end
B0 = imadjust(B0);
k1 = fspecial('gaussian', [5 5], 2.5);
Ic = imfilter(B0,k1);
figure(1),subplot(3,3,1), imshow(I);
title('anh goc');
figure(1), subplot(3,3,2), imshow(B0);
title('anh tang tuong phan');
figure(1), subplot(3,3,3), imshow(Ic);
title('anh loc gaussian');
%loc trung vi
I_m = medfilt2(B0,[3 3]);
figure(1),subplot(3,3,4), imshow(Ic);
title('anh loc trung vi');
%anh nhi phan
thresholdValue = 60;
bw = Ic > thresholdValue;
bw = bwareafilt(bw, 1);
figure(1),subplot(3,3,5), imshow(bw);
title('anh nhi phan');
%dong hinh thai
se = strel("disk",10);
C1 = imclose(bw, se);
figure(1),subplot(3,3,6), imshow(C1);
title('dong hinh thai');
%khai thac ranh gioi
[B,L,N] = bwboundaries(C1);
figure(2);
%subplot(3,3,7);
imshow(C1); hold on;
title('khai thac ranh gioi');
len = [];
for k=1:length(B)
boundary = B{k};
% comput length
len(k) = (max(boundary(:,2))-min(boundary(:,2))).^2+(max(boundary(:,1))-min(boundary(:,1))).^2;
if(k > N)
plot(boundary(:,2), boundary(:,1), 'g','LineWidth',1.5);
else
plot(boundary(:,2), boundary(:,1), 'r','LineWidth',1.5);
end
end
% find
[~,ind] = max(len);
boundary = B{ind};
plot(boundary(:,2), boundary(:,1), 'm--','LineWidth',2);
3 Comments
yanqi liu
on 22 Mar 2022
yes,sir,let us check the follow method
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
markerSize = 4;
%[filename, pathname] = uigetfile({'*.*'},'File Selector');
%if ~isequal(filename,0)
%fullname = strcat(pathname,filename);
I=imread('https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/935629/MH2.jpeg');
%else
% msgbox('Ban chua chon anh?')
%end
Id = size(I);
if length(Id) == 3
B0 = rgb2gray(I);
else
B0 = I;
end
B0 = imadjust(B0);
k1 = fspecial('gaussian', [5 5], 2.5);
Ic = imfilter(B0,k1);
figure(1),subplot(3,3,1), imshow(I);
title('anh goc');
figure(1), subplot(3,3,2), imshow(B0);
title('anh tang tuong phan');
figure(1), subplot(3,3,3), imshow(Ic);
title('anh loc gaussian');
%loc trung vi
I_m = medfilt2(B0,[3 3]);
figure(1),subplot(3,3,4), imshow(Ic);
title('anh loc trung vi');
%anh nhi phan
thresholdValue = 60;
bw = Ic > thresholdValue;
bw = bwareafilt(bw, 1);
figure(1),subplot(3,3,5), imshow(bw);
title('anh nhi phan');
%dong hinh thai
se = strel("disk",10);
C1 = imclose(bw, se);
figure(1),subplot(3,3,6), imshow(C1);
title('dong hinh thai');
%khai thac ranh gioi
b1 = bwperim(C1);
b1(:,[1:2 end-2:end]) = 0;
b1 = logical(b1);
[B,L,N] = bwboundaries(b1);
figure(2);
%subplot(3,3,7);
imshow(C1); hold on;
title('khai thac ranh gioi');
len = [];
for k=1:length(B)
boundary = B{k};
% comput length
len(k) = (max(boundary(:,2))-min(boundary(:,2))).^2+(max(boundary(:,1))-min(boundary(:,1))).^2;
if(k > N)
plot(boundary(:,2), boundary(:,1), 'g','LineWidth',1.5);
else
plot(boundary(:,2), boundary(:,1), 'r','LineWidth',1.5);
end
end
% find
[~,ind] = max(len);
boundary = B{ind};
figure;
%subplot(3,3,7);
imshow(C1); hold on;
plot(boundary(:,2), boundary(:,1), 'm--','LineWidth',2);
More Answers (0)
See Also
Categories
Find more on Filter Banks 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!