Code takes too much time to compile. Maybe some advice?
1 次查看(过去 30 天)
显示 更早的评论
My Matlab code is taking too much time to compile. It basically has a lot of iterations. The real work of the code is to individually get rgb values(For a specific row and col of the picture matrix) using some logic and then creating a fresh new rgb matrix and finally converting the rgb matrix into an image. Maybe I would need some advice on making the compilation time quicker. Your help will be much appreciated. Also I tried to put a waitbar, bu the waitbar doesnt show any progress at all.
My Code h = waitbar(0,'Please wait...'); for i=1:1000 numberOfImages=2;
k=0;
for row=1:1584 for col=1:2780
for i=1:numberOfImages
filename=[num2str(i), '.png'];
rgbImage=imread(filename, 'png');
R = rgbImage(row, col, 1);
G = rgbImage(row, col, 2);
B = rgbImage(row, col, 3);
RGBMatrix(i,:) =[R,G,B];
end
for imageNumber=1:numberOfImages
match=0;
jitter=0;
for j=numberOfImages:-1:1
RedChannelDifference=abs(double(RGBMatrix(j,1))-double(RGBMatrix(imageNumber,1)));
GreenChannelDifference=abs(double(RGBMatrix(j,2))-double(RGBMatrix(imageNumber,2)));
BlueChannelDifference=abs(double(RGBMatrix(j,3))-double(RGBMatrix(imageNumber,3)));
if (RedChannelDifference+GreenChannelDifference+BlueChannelDifference)<=10
jitter=jitter+RedChannelDifference+GreenChannelDifference+BlueChannelDifference;
match=match+1;
end
end
match=match-1;
matchMatrixOfAllImages(imageNumber,:)=[match];
end
%fprintf('The maximum value of matches is : %d \n', max(matchMatrixOfAllImages(:)));
index=find(matchMatrixOfAllImages == max(matchMatrixOfAllImages(:)));
if size(index,1)>1
jitterMatrixOfAllImages(imageNumber,:)=[jitter];
% fprintf('Jitter values of ONLY MATCHES');
jitterMatrixOfMatchedImages=jitterMatrixOfAllImages(index,:);
indexForJitter=find(jitterMatrixOfMatchedImages == min(jitterMatrixOfMatchedImages(:)));
if size(indexForJitter,1)>1
indexOfPictureNeeded=indexForJitter(1,:);
filenameOfImage1=[num2str(indexOfPictureNeeded), '.png'];
rgbImage1=imread(filename, 'png');
R1 = rgbImage1(row, col, 1);
G1 = rgbImage1(row, col, 2);
B1 = rgbImage1(row, col, 3);
RGBMatrix1(indexOfPictureNeeded,:) =[R1,G1,B1];
k=k+1;
out(k,:) =RGBMatrix1 ;
else
indexOfPictureNeeded=indexForJitter(1,:);
filenameOfImage1=[num2str(indexOfPictureNeeded), '.png'];
rgbImage1=imread(filename, 'png');
R1 = rgbImage1(row, col, 1);
G1 = rgbImage1(row, col, 2);
B1 = rgbImage1(row, col, 3);
RGBMatrix1(indexOfPictureNeeded,:) =[R1,G1,B1];
k=k+1;
out(k,:) =RGBMatrix1 ;
end
else
jitterMatrixOfAllImages(imageNumber,:)=[jitter];
jitterMatrixOfMatchedImages=jitterMatrixOfAllImages(index,:);
indexForJitter=find(jitterMatrixOfMatchedImages == min(jitterMatrixOfMatchedImages(:)));
indexOfPictureNeeded1=indexForJitter(1,:);
filename1=[num2str(indexOfPictureNeeded1), '.png'];
rgbImage2=imread(filename, 'png');
R1 = rgbImage2(row, col, 1);
G1 = rgbImage2(row, col, 2);
B1 = rgbImage2(row, col, 3);
RGBMatrix2(indexOfPictureNeeded1,:) =[R1,G1,B1];
k=k+1;
out(k,:) =RGBMatrix2 ;
end
end
end imshow(imagesc(out)); waitbar(i/1000,h) end
4 个评论
KSSV
2018-3-22
It is not a good practice to copy and run the code. Copy it in a editor, save it with .m extension and run the code through this file.
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!