Smoothing the edges of my contour

5 ビュー (過去 30 日間)
NIMA FARHANG
NIMA FARHANG 2019 年 3 月 22 日
コメント済み: NIMA FARHANG 2019 年 3 月 25 日
Hello :)
Would you mind helping me to reduce the zigzags in my contour boundaries?
My code and contour image is attached.
Thanks a lot in advance!
My whole code for an image processing:
close all;
clear all;
clc;
tic;
%{
n is the number of images processed for the desirable result which depends
on the experiment. In other words, each experiment has its own n.
%}
n = 601;
% Here is the preallocation section.
%==========================================================================
Peaks = zeros(1,360);
Degrees = (1:360);
x = cell(n,1);
fname = zeros(1,19);
fname1 = zeros(1,13);
angle0 = zeros(360,n);
angle1 = zeros(360,n);
st0 = zeros(360,n);
st1 = zeros(360,n);
x0 = zeros(1,n);
x1 = zeros(1,n);
y0 = zeros(1,n);
y1 = zeros(1,n);
%==========================================================================
for ii=1:n
%Here the pictures will be read from their path.(line 27:40)
fname = int2str(ii);
if ii<10
fname1 = '10mm_exp01_000';
end
if ii>=10 && ii<100
fname1 = '10mm_exp01_00';
end
if ii>=100 && ii<1000
fname1 = '10mm_exp01_0';
end
%{
Here the name of the file is created in order to be read from the path
and a proper size will be defined,some filters will be applied and at
the end the images are turned into binary. The boundary function will
find the boundaries of the image. (line 48:
%}
fname = strcat(fname1,fname,'.tif');
im = imread(fname);
im = im(20:770,70:820); % Defining a proper frame size
h = fspecial('gaussian',51,15); % Filtering the image
bim = imfilter(im,h,'replicate'); % Filtering the image
bim = im2bw(bim); % Turning the images into binary
bim = imfill(bim,'holes'); % Empty the center of the image
B0 = boundaries(bim); % Finding the boundaries of the image
d = cellfun('length',B0);
[max_d,k] = max(d);
b0 = B0{k};
[M,N] = size(bim); % M=length and N=width of the image
a1 = ones(1,5)/5; % 10 point averaging filter, Noncausal
b0 = filtfilt(a1,1,b0);
%{
The x and y coordinates for the center of the image is manually added.
x0 = 381 and y0 = 365
%}
[st0(:,ii),angle0(:,ii),x0(ii),y0(ii)] = signature(b0,381,365);
for k=1:max(size(b0))
b0(k,1) = b0(k,1) - x0(ii);
b0(k,2) = b0(k,2) - y0(ii);
end
[st1(:,ii),angle1(:,ii),x1(ii),y1(ii)] = signature(b0); %st=radius
end
%{
For each degree degrees we apply fft function.
%}
for k=1:360
x{k} = st1(k,:);
Fs = 65; % Camera frequency which is added manually
L = length(x);
T_inc = 1/Fs; % Time increment for fft plot
signal_FFT = fft(x{k})/L; % MATLAB fft output
amplitude = 2*abs(signal_FFT(2:L/2+2));
frequency = Fs/2*linspace(0,1,L/2+1);
Tf = T_inc*L; % Final time
time=0:T_inc:Tf-T_inc; % Time vector
Peaks(k)= max(x{k});
figure(k);
fft_figure = figure(k);
set(fft_figure, 'Visible', 'off');
plot (frequency,amplitude,'red');
title ('Signal-Sided Amplitude Spectrum')
xlabel ('Frequency (Hz) ')
ylabel ('Magnitude of Signal (units of signal)')
% folder = 'C:\Users\umroot\Desktop\Debugging the MATLAB codes\8th Draft\FFT';
% figFileName = sprintf('FFT for %d deg.tif',k);
% fullFileName = fullfile(folder, figFileName);
% saveas(gcf,fullFileName);
figure(k); % Plotting the signals
radius_figure = figure(k);
set(radius_figure, 'Visible', 'off');
plot(time(:),x{k,:});
xlabel('Time (s)');
ylabel('Radius');
title('Simulated Signal of Radiuses');
% folder = 'C:\Users\umroot\Desktop\Debugging the MATLAB codes\8th Draft\Radii';
% figFileName = sprintf('Radii for image no %d .tif',k);
% fullFileName = fullfile(folder, figFileName);
% saveas(gcf,fullFileName);
end
X_Axis = linspace (-st1(180,1),st1(1,1),M);
Y_Axis = linspace (-st1(90,1),st1(270,1),N);
figure;
cont = contour (X_Axis,Y_Axis,flipud(bim),1,'red');
axis square;
toc;
The Original, edited pics and contour is below:
Original.bmp untitled.bmp contour.bmp
And with the the filters I have used in my code, the edges for the contour is like this:
Contour_zoomed.bmp
Far being said, the reason to smooth the contour edges is to reduce the errors in fft function which I want to apply on my contour too.
Thanks again :)

回答 (1 件)

Image Analyst
Image Analyst 2019 年 3 月 22 日
If the sort-of circular mask thing is a binary image, you can smooth the boundary by blurring it with conv2() or imfilter() and then thresholding.
  7 件のコメント
Image Analyst
Image Analyst 2019 年 3 月 24 日
NIMA:
As you can clearly see from the attached demo, as the window size gets larger, the image gets more blurry and thus the shape will be smoother. It it weren't so round already, you'd see it more clearly.
0001 Screenshot.png
NIMA FARHANG
NIMA FARHANG 2019 年 3 月 25 日
Cheifly thanks a lot for taking your time to answer my questions. I do really appreciate it! Unfortunately I get this error when running the attached code:
No appropriate method, property, or field Neighborhood for class strel.
(at this line: kernel = se.Neighborhood / sum(se.Neighborhood(:)); )
Far being said I am using 2012b version.
Again, thank you so much!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

製品


リリース

R2012b

Community Treasure Hunt

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

Start Hunting!

Translated by