MATLAB Answers

0

Is poly2mask taking too much of the time?

Asked by Zara Khan on 16 Sep 2018
Latest activity Commented on by Image Analyst
on 17 Sep 2018
I am working on a dataset which consists of 1000 binary images. Following things are needed to be done for every single images: 1. Make all images to vertically align(if any of them are already vertically align then leave as it is) 2. Draw 8 circles with different radius values 3. Store each circle perimeter pixel to a profile. I have tried doing this. For storing perimeter pixel values I have used poly2mask and have considered its boundary. But this is taking too much of the time. Program is going extremely extremely slow. Unable to get outputs. Only using few data I am able to check. For 1000 data I waited for a whole day still not getting my desired output. Please anyone help me in doing this.
clc;
close all;
clear;
workspace;
folder = 'D:\data_set';
filepattern = fullfile(folder, 'P*.png');
directoryInfo = dir(filepattern);
numImages = length(directoryInfo);
for k = 1 : numImages
baseFileName = directoryInfo(k).name;
fullFileName = fullfile(folder, baseFileName);
a = imread(fullFileName);
bwimg =bwareafilt(~a, 1);
s=regionprops(bwimg,'Centroid','Orientation','MajorAxisLength','MinorAxisLength');
theta=s.Orientation;
if theta>=0 && theta<90
phi=90-theta;
y=imrotate(bwimg,phi);
else
phi=90+theta;
y=imrotate(bwimg,-phi);
end
circleCenterX =s.Centroid(1);
circleCenterY =s.Centroid(2);
diameters = mean([s.MajorAxisLength s.MinorAxisLength],2);
r1 =diameters/2;
r2=diameters/3;
r3=diameters/4;
r4=diameters/5;
r5=0.6*diameters;
r6=0.7*diameters;
r7=0.8*diameters;
r8=0.9*diameters;
radii(k,:)=[r1, r2, r3, r4,r5, r6, r7, r8];
[m,n]=size(radii);
t=0:0.001:2*pi;
for i=1:m
for j=1:n
x = circleCenterX + radii(i,j) * sin(t);
y = circleCenterY + radii(i,j) * cos(t);
bw = poly2mask(x,y,256,256);
end
end
end
end

  0 Comments

Sign in to comment.

1 Answer

Answer by Image Analyst
on 16 Sep 2018

Not sure what this code does with making increasingly large circles, but this seems to work:
clc; % Clear the command window.
clearvars;
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
clc;
close all;
clear;
workspace;
folder = pwd; % 'D:\data_set';
filepattern = fullfile(folder, '*.png');
directoryInfo = dir(filepattern);
numImages = length(directoryInfo);
t=linspace(0, 2*pi, 720);
for k = 1 : numImages
baseFileName = directoryInfo(k).name;
fullFileName = fullfile(folder, baseFileName)
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(rgbImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = rgbImage(:, :, 1); % Take red channel.
else
grayImage = rgbImage; % It's already gray scale.
end
subplot(2, 2, 1);
imshow(grayImage);
title(baseFileName, 'Interpreter', 'none');
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
drawnow;
% Now it's gray scale with range of 0 to 255.
binaryImage =bwareafilt(~grayImage, 1);
subplot(2, 2, 2);
imshow(binaryImage);
title('Binary Image', 'Interpreter', 'none');
drawnow;
s=regionprops(binaryImage,'Centroid','Orientation','MajorAxisLength','MinorAxisLength');
if isempty(s)
% Skip this image if there are no blobs.
continue;
end
theta=s.Orientation;
if theta>=0 && theta<90
phi=90-theta;
y=imrotate(binaryImage,phi);
else
phi=90+theta;
y=imrotate(binaryImage,-phi);
end
circleCenterX =s.Centroid(1);
circleCenterY =s.Centroid(2);
diameters = mean([s.MajorAxisLength, s.MinorAxisLength],2)
r1 =diameters/2;
r2=diameters/3;
r3=diameters/4;
r4=diameters/5;
r5=0.6*diameters;
r6=0.7*diameters;
r7=0.8*diameters;
r8=0.9*diameters;
radii(k,:)=[r1, r2, r3, r4, r5, r6, r7, r8];
[rowsR, colsR]=size(radii);
bw = false(rows, columns);
for j=1:colsR
thisRadius = radii(1, j);
x = circleCenterX + thisRadius * sin(t);
y = circleCenterY + thisRadius * cos(t);
bw = bw | poly2mask(x, y, rows, columns);
subplot(2, 2, 3);
imshow(bw);
caption = sprintf('Radius = %.3f', thisRadius);
title(caption);
drawnow;
end
if k < numImages
promptMessage = sprintf('Do you want to Continue processing,\nor Quit processing?');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if contains(buttonText, 'Quit')
break;
end
end
end
msgbox('Done!');

  7 Comments

After using poly2mask we will get filled circle. I want to store their boundary pixels in a file and want to plot them. Actually the file will consists of logical values. If we plot them then will get some peaks and valleys That will be for every images. For every images i will get 8 means total 8000 profiles how to count no of peaks from each profile and store them in a matrix
What ia the use of this line bw = bw | poly2mask(x, y, rows, columns); Will you please explain me ??
It seemed like you were creating circles in the mask. That line of code basically adds them all together. But since yours have all the same centers, and the later ones are the larger ones, you could just do it once with the largest circle. The code seems to be a bizarre algorithm to me - I suggest you rethink what you're trying to do.

Sign in to comment.