Clear Filters
Clear Filters

I am getting more time complexity ,can you possibly minimize the code

1 view (last 30 days)
please review the attached code below,and give me some suggestions to reduce the time complexity

Answers (6)

Rik
Rik on 22 Feb 2018
for k = 1 : numel(h)
indexH = floor(8 * h(k));
indexS = floor(2 * s(k));
indexV = floor(2 * v(k));
h(k)=indexH;
s(k)=indexS;
v(k)=indexV;
end
Replace it with:
h=floor(8*h);s=floor(2*s);v=floor(2*v);
Another part you can optimize easily is this:
for i=1:72
A(i)=length(find (G==i));
end
You should replace this with something like histcounts, you might even not have to reshape your array.
Otherwise, use the profiler to find out what part of your code is the bottleneck and optimize/rethink that. You should be aware that reading from disk is very slow compared to the rest of the operation Matlab does. Also, you last loop doesn't do anything useful, but imshow is very slow. image is much faster in general, because it puts a lot of management task on you (clearing the axes when appropriate etc.).
  4 Comments
teja jayavarapu
teja jayavarapu on 22 Feb 2018
Also, you last loop doesn't do anything useful, but imshow is very slow. image is much faster in general, because it puts a lot of management task on you (clearing the axes when appropriate etc.).
You mentioned above about some loop I didn't get it
Walter Roberson
Walter Roberson on 22 Feb 2018
Rik is the person who mentioned "last loop". Rik was referring to
for z=1:999
for M=1:12
if (CP(z)==B(M))
subplot(5,3,M+3)
img = fullfile(pathname, sprintf('%d%s', z, str) );
imshow( img )
title( sprintf('z = %d', z) )
end
end
drawnow();
end
I explained below why this is a waste of time, that the functionality can be replaced by sort() and a simple loop.
The issue about imshow() is that imshow() does a lot of work on your behalf, some of which is not necessary to do if you know what you are doing with image() and calls to axes(). As your concern at this time is performance rather that design, you should replace
imshow( img )
with
image_data = imread(img);
image(image_data);
axis image equal

Sign in to comment.


Walter Roberson
Walter Roberson on 22 Feb 2018
You sort CP into B and then you do a double nested loop looking for places the 12 smallest sorted values equal the unsorted values. That is a waste of time. Instead when you do the sorting, use the two output form of sort() as that returns indices directly and no searches would be needed.
  2 Comments
Walter Roberson
Walter Roberson on 1 Mar 2018
Replace
for z=1:999
for M=1:12
if (CP(z)==B(M))
subplot(5,3,M+3)
img = fullfile(pathname, sprintf('%d%s', z, str) );
imshow( img )
title( sprintf('z = %d', z) )
end
end
drawnow();
end
with
[~, sortidx] = sort(CP);
for M = 1 : 12
z = sortidx(M);
img = fullfile(pathname, sprintf('%d%s', z, str) )
subplot(5,3,M+3)
imshow( img )
title( sprintf('z = %d', z) )
end

Sign in to comment.


teja jayavarapu
teja jayavarapu on 28 Feb 2018
Edited: teja jayavarapu on 28 Feb 2018
can you please find error in my code so i can proceed further.I am unable retrive the images with this code properly
  8 Comments
Jan
Jan on 2 Mar 2018
@teja: Meanwhile I'm completely confused by this thread. I find some code, some explanations and some inputs distributes over the question, some answers and some comments. I suggest to open a new thread, include all requires information in the question, provide the code directly as text instead of an attachment, but attach the input data. Then add a link to this thread here and note, that the new thread is a clarification and summary.

Sign in to comment.


teja jayavarapu
teja jayavarapu on 2 Mar 2018
I have resized all the images to 256x256 using coverted file below,I think you got all the required information if i am less of any content please let me know

teja jayavarapu
teja jayavarapu on 3 Mar 2018
Are you still getting the same time complexity

teja jayavarapu
teja jayavarapu on 5 Mar 2018
disp('query')
[filename, pathname]=uigetfile({'*.jpg'},'queryimage');
% This code is for LOCAL COLOUR HISTOGRAM _________________________________________________________________________________________________________
%part-1:This is for query Image
_____________________________________________________________________________________
tic
img = fullfile(pathname, filename);
a=imread(img);
figure('Name','LOcAL COLOUR RETRIVED IMAGES','NumberTitle','off');
subplot(4,5,3)
imshow(img)
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
drawnow();
%% totalPixelsOfImage = rows*cols*numOfBands;
[r, c, numOfBands] = size(a);
%dividing image into 8x8 subblocks
bs=64;%block size
row_blocks = floor(r / bs);
col_blocks = floor(c / bs);
nob=row_blocks*col_blocks;
used_rows = row_blocks * bs;
used_cols = col_blocks * bs;
C = mat2cell(a(1:used_rows, 1:used_cols, :), bs * ones(1,row_blocks), bs * ones(1,col_blocks),
size(a,3) );%here dividing image into blocks
C = C(:);%16 number of cells for the image
A = cell(nob, 1);%declaring
for i=1:nob
D = rgb2hsv(C{i});
% split image into h, s & v planes
h = D(:, :, 1);
s = D(:, :, 2);
v = D(:, :, 3);
maxValueForH = max(h(:));
maxValueForS = max(s(:));
maxValueForV = max(v(:));
% Create the histogram quantized into 8×3×3 bin
h = ceil(7 * h(:,:)/maxValueForH);
s = ceil(2 * s(:,:)/maxValueForS);
v = ceil(2 * v(:,:)/maxValueForV);
G=9*h+3*s+v;%normalization of the image
G=G+1;
x = accumarray(G(:),1);%count the number of occurances and stores in the bin
x=x'; %As input data is 72x1, so to convert into 1x72
A{i}=x; %storing all values of x for each block in A
end
% query ends __________________________________________________________________________________________________________________________________________-
%here the comparision of database images with query image
disp('database')
CP=zeros(1,999);
str='.jpg';
for z=1:999
c = fullfile(pathname, sprintf('%d%s', z, str));
a=imread(c);
%% totalPixelsOfImage = rows*cols*numOfBands;
[r, c, numOfBands] = size(a);
%dividing image into 8x8 subblocks
bs=64;%block size
row_blocks = floor(r / bs);
col_blocks = floor(c / bs);
nob=row_blocks*col_blocks;
used_rows = row_blocks * bs;
used_cols = col_blocks * bs;
Z = mat2cell(a(1:used_rows, 1:used_cols, :), bs * ones(1,row_blocks), bs * ones(1,col_blocks),
size(a,3) );
Z = Z(:);
B = cell(nob, 1);
for i=1:nob
D = rgb2hsv(C{i});
h = D(:, :, 1);
s = D(:, :, 2);
v = D(:, :, 3);
maxValueForH = max(h(:));
maxValueForS = max(s(:));
maxValueForV = max(v(:));
% Create the histogram quantized into 8×3×3 bin
h = ceil(7 * h(:,:)/maxValueForH);
s = ceil(2 * s(:,:)/maxValueForS);
v = ceil(2 * v(:,:)/maxValueForV);
V=9*h+3*s+v;
V=G+1;
y = accumarray(V(:),1);
y=y';
B{i}=y;
end
for i=1:nob
temp = euclideanDistance(A{i}, B{i});
CP(z) = CP(z) + temp;
end
end
[~,P]=sort(CP);
for M=1:12
z=P(M);
img = fullfile(pathname, sprintf('%d%s', z, str) );
subplot(5,3,M+3)
imshow( img )
title( sprintf('z = %d', z) )
drawnow()
end
disp('retrived')
toc
  2 Comments
Jan
Jan on 6 Mar 2018
@Pavan teja: Who are you? This is the thread of teja jayavarapu, so why do you ask for help here?
Please do not make it hard to read your code. As you can see in the answers to your question, a nicely formatted code is much better to read. See http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup. Currently your code is such ugly, that it hurts to read it.
Walter and Rik have posted some suggestions already and you did not post a comment. Do the ideas work?
It is still not clear to me, what you are asking for. Do you want to simplify the code to make it nicer to read or to decrease the runtime?

Sign in to comment.

Categories

Find more on Introduction to Installation and Licensing 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!