MATLAB (IMAGE SPLITTING MATRIX) COMPUTATION SPEED ISSUE
7 views (last 30 days)
Show older comments
Hi, Im still finding my way around MATLAB but my MATLAB computation starts out fast then as it progresses it gets slower and slower, and takes about 2 hours to compute. Im not really sure how to fix it. If someone could optimize it for better performance, It would be well appreciated. I have attached my code.
% Demo to divide an image up into blocks (non-overlapping tiles).
% The first way to divide an image up into blocks is by using mat2cell().
% In this demo, I demonstrate that with a color image.
% Another way to split the image up into blocks is to use indexing.
% In this demo, I demonstrate that method with a grayscale image.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'Picture1.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
% Read the image from disk.
rgbImage = imread(fullFileName);
% Test code if you want to try it with a gray scale image.
% Uncomment line below if you want to see how it works with a gray scale image.
% rgbImage = rgb2gray(rgbImage);
% Display image full screen.
imshow(rgbImage);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
drawnow;
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
%==========================================================================
% The first way to divide an image up into blocks is by using mat2cell().
blockSizeR = 30; % Rows in block.
blockSizeC = 30; % Columns in block.
% Figure out the size of each block in rows.
% Most will be blockSizeR but there may be a remainder amount of less than that.
wholeBlockRows = floor(rows / blockSizeR);
blockVectorR = [blockSizeR * ones(1, wholeBlockRows), rem(rows, blockSizeR)];
% Figure out the size of each block in columns.
wholeBlockCols = floor(columns / blockSizeC);
blockVectorC = [blockSizeC * ones(1, wholeBlockCols), rem(columns, blockSizeC)];
% Create the cell array, ca.
% Each cell (except for the remainder cells at the end of the image)
% in the array contains a blockSizeR by blockSizeC by 3 color array.
% This line is where the image is actually divided up into blocks.
if numberOfColorBands > 1
% It's a color image.
ca = mat2cell(rgbImage, blockVectorR, blockVectorC, numberOfColorBands);
else
ca = mat2cell(rgbImage, blockVectorR, blockVectorC);
end
% Now display all the blocks.
plotIndex = 1;
numPlotsR = size(ca, 1);
numPlotsC = size(ca, 2);
fileID = fopen('AB.txt','wt');
for r = 1 : numPlotsR
for c = 1 : numPlotsC
%fprintf('plotindex = %d, c=%d, r=%d damage=%d\n', plotIndex, c, r,length(B));
% A= fprintf(fileID,'%f \n', length(B));
%
% Specify the location for display of the image.
subplot(numPlotsR, numPlotsC, plotIndex);
% Extract the numerical array out of the cell
% just for tutorial purposes.
rgbBlock = ca{r,c};
imshow(rgbBlock); % Could call imshow(ca{r,c}) if you wanted to.
[rowsB columnsB numberOfColorBandsB] = size(rgbBlock);
% Make the caption the block number.
caption = sprintf('Block #%d of %d\n%d rows by %d columns', ...
plotIndex, numPlotsR*numPlotsC, rowsB, columnsB);
title(caption);
drawnow;
% Increment the subplot to the next location.
plotIndex = plotIndex + 1;
img1=rgbBlock;
imshow(img1)
img1=rgb2gray(img1);
imshow(img1);
img2=im2bw(img1,graythresh(img1));
imshow(img2)
img2=~img2;
imshow(img2)
%B = bwboundaries(img2);
%imshow(img2)
%
BW=img2;
%
[B,L] = bwboundaries(BW,'noholes');
hypotFun = @(x) hypot(max(x(:, 1)) - min(x(:, 1)), max(x(:, 2)) - min(x(:, 2)));
% Set minimum length
minimumLength = 5;
% Logical indexing for allowable length
check = cellfun(hypotFun, B) > minimumLength;
Bnew = B(check);
hold on
imshow(label2rgb(L, @gray, [.5 .5 .5]))
hold on
thnumbers= num2str(length(Bnew));
fprintf(' \n');
%text(10,10,strcat('\color{green}',num2str(length(B))))
%csvwrite('countlines.csv',num2str(length(B)))
%CC = bwconncomp(img_blobs);
%num_edge = B.NumObjects % line count
hold on
%
for k = 1:length(B);
boundary = B{k};
text(10,10,strcat('\color{green}',num2str(length(Bnew))));
%fprintf('damage_number = %d\n',length(B));
end
end
end
4 Comments
Walter Roberson
on 25 Jun 2018
Why are you imshow() at least four different versions of the image in exactly the same place? You are not setting any transparency so the newer images are going to completely cover up the earlier images.
img1=rgbBlock;
imshow(img1)
img1=rgb2gray(img1);
imshow(img1);
img2=im2bw(img1,graythresh(img1));
imshow(img2)
img2=~img2;
imshow(img2)
those four imshow() are requesting drawing in the exact same location as each other.
Answers (1)
Image Analyst
on 25 Jun 2018
Inside your inner loop, just before you call imshow(), call cla first.
hold off;
cla reset;
See if that speeds it up.
See Also
Categories
Find more on Convert Image Type 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!