Speed up my Matlab Code
1 回表示 (過去 30 日間)
古いコメントを表示
I'm working on a Matlab GUI. I found parfor or other methods on the site, but it doesn't work in my code. I have a project called Differences between 2 different pictures. I'il leave the code down. The time between scans is too much and I want to reduce this time. So it scans the 1: 100 (pixel) range in about 15 seconds. I want to scan much faster. Because maybe I could do scans like 1: 700. Is there any way you know how to increase speed? It could be Parfor or anything.
for i = 100
drawnow();
handles = guidata(hObject);
guidata(hObject, handles);
axes(handles.axes2);
image(gCam2);
pos = [x,y,dx,dy];
rectangle('Position', pos, 'EdgeColor', 'b');
axes(handles.axes4);
crop2 = imcrop(gCam2, pos);
im2 = image(crop2);
axes(handles.axes5);
diffi = rgb2gray(imabsdiff(crop1,crop2));
im3 = image(diffi);
v = sum(sum(diffi))/dx/dy;
if v<immin
immin= v;
xmin = x;
str1 = 'Xmin = ';
str2 = 'Ymin = ';
str1 = strcat(str1,num2str(xmin));
str2 = strcat(str2,num2str(immin));
set(handles.text6,'String',str1);
set(handles.text8,'String',str2);
end
x = x - 1;
end
2 件のコメント
回答 (1 件)
Adam Danz
2019 年 12 月 31 日
編集済み: Adam Danz
2020 年 1 月 6 日
Just about every line can be improved.
- drawnow() is a major time killer. It updates all graphics objects that exist and you're doing that on each iteration. If it's not really necessary to update the graphics on each iteration get rid of this. An alternative is to use drawnow limitrate which will update the graphcs less frequently.
- There are several lines that appear to produce the same result on each iteration. Those lines can be moved outside of the loop, prior to the loop.
- Calling axes() is another time killer. Instead, remove those lines and specify the axes in the plotting commands. Example: image(handles.axes4, gCam2) -or- rectangle(handles.axes4, 'Position' ...)
- Why use 'x' to control the iterations when your i-variable does exactly that?
for i = 100
drawnow(); % major time killer
handles = guidata(hObject); % Move out of loop
guidata(hObject, handles); % Move out of loop (why do you need this line?)
axes(handles.axes2); % Specify plot handles instead
image(gCam2); % Specify plot handles instead
pos = [x,y,dx,dy]; % Where do these variables come from? Move out of loop.
rectangle('Position', pos, 'EdgeColor', 'b'); % Specify plot handles
axes(handles.axes4); % Specify plot handles instead
crop2 = imcrop(gCam2, pos); % Specify plot handles instead
im2 = image(crop2); % Specify plot handles instead
axes(handles.axes5); % Specify plot handles instead
diffi = rgb2gray(imabsdiff(crop1,crop2));
im3 = image(diffi); % Specify plot handles instead
v = sum(sum(diffi))/dx/dy;
if v<immin
immin= v;
xmin = x;
str1 = 'Xmin = '; % Move out of loop
str2 = 'Ymin = '; % Move out of loop
str1 = strcat(str1,num2str(xmin));
str2 = strcat(str2,num2str(immin));
set(handles.text6,'String',str1);
set(handles.text8,'String',str2);
end
x = x - 1; % why not use i instead of doing this?
end
An even faster approach would be to collect all the needed data and then animate the data in a separate loop that just updates the image values.
2 件のコメント
Adam Danz
2020 年 1 月 7 日
"This gui code and this code is actually 600 lines and I just wrote the part for this site. Because this is the part I thought it was slowing down"
Thank you for taking the time to create a short example of the problem. I think this section is slowing down your code for reasons listed in my answer - mainly the drawnow() and axes() parts.
If you haven't done so already, run Matlab's profiler on this section of your GUI code in order to produce a report of execution time that may reveal processing bottlenecks. You could save and upload that report here for feedback, if you wish.
". I don't have to show this scan, but when it's done I have to show the coordinates and the square it matches."
Removing drawnow() will definitely speed things up.
参考
カテゴリ
Help Center および File Exchange で Graphics Performance についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!