Speed up my Matlab Code

1 回表示 (過去 30 日間)
ohe uys
ohe uys 2019 年 12 月 31 日
コメント済み: Adam Danz 2020 年 1 月 7 日
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 件のコメント
darova
darova 2019 年 12 月 31 日
Your for loops works only once and it works slow?
for i = 100
ohe uys
ohe uys 2020 年 1 月 7 日
Yeah, because one pixel shifts. This shift is already a slow process, i.e 1 pixel can be thought of as 1 second. If it tries to shift 100 pixels, it means 100 seconds. I asked you for an idea to shorten this process.

サインインしてコメントする。

回答 (1 件)

Adam Danz
Adam Danz 2019 年 12 月 31 日
編集済み: Adam Danz 2020 年 1 月 6 日
Just about every line can be improved.
  1. 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.
  2. 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.
  3. 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' ...)
  4. 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 件のコメント
ohe uys
ohe uys 2020 年 1 月 7 日
Mr @ Adam Danz 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. Now I want you to think like this. You have 2 photos from two different angles but the same place. You selected a frame in the first one. In the second photo there is a frame and you asked him to go to the first frame. It scans the squares, that is, the pixels. Drawnow is being used because it is a scanning process. x is used to show the coordinates of the pixel. Xmin = and Ymin = also list the instantaneous changes of these coordinates. When scanning is finished, it finds the best match and gives the coordinates and the square drawn there. I don't have to show this scan, but when it's done I have to show the coordinates and the square it matches.
Adam Danz
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 ExchangeGraphics Performance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by