Is poly2mask taking too much of the time?

조회 수: 1 (최근 30일)
Zara Khan
Zara Khan 2018년 9월 16일
댓글: Image Analyst 2018년 9월 17일
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

답변 (1개)

Image Analyst
Image Analyst 2018년 9월 16일
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
Zara Khan
Zara Khan 2018년 9월 16일
What ia the use of this line bw = bw | poly2mask(x, y, rows, columns); Will you please explain me ??
Image Analyst
Image Analyst 2018년 9월 17일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by