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

please review the attached code below,and give me some suggestions to reduce the time complexity

Answers (6)

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

Which last loop is very slow in my program, can I make it faster by any other means
Which "last loop" do you mean?
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
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.

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

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.

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

As we do not have your images and data files, we cannot run the code ourselves to test it. You will need to post the error message you are encountering.
The below is the link for database images I have tested 1000 images.
Give us something to focus on. I am not going to spend my time downloading the files and running your code and hoping I happen to notice what you see as being in error.
I also do not understand why you did not make the change I indicated in https://www.mathworks.com/matlabcentral/answers/384231-i-am-getting-more-time-complexity-can-you-possibly-minimize-the-code#answer_306556 so that means I would have to go back through the code and re-debug that section to ensure that it really does say what I already figured out it said in the previous version, which is a waste of my time if nothing changed in that regards.
I didn't mean to bother you. I am unable to upload images,so I provided the link and I didn't get,what you told previously about the change,can you please elaborate it
I have made the changes stated by you but it is displaying first 12 for every image passed can you please verify it
sorry i forgot to attach the euclidean distance file,can you check the code with this pleasee
Please let me know that you are willing to help me or not
@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.

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
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

Please respond can you help me with this or not
@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.

Asked:

on 22 Feb 2018

Commented:

Jan
on 6 Mar 2018

Community Treasure Hunt

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

Start Hunting!