Suggestions to improve script performance?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Roger Breton
il 24 Gen 2022
Modificato: Cris LaPierre
il 28 Gen 2022
This is the result of my script, graphically :
It's not a DaVinci but it's 'working'. I tried to use what I thought was Matlab "good" techniques but I'm sure I'm light years behind what good programmers would do... I suspect there are lot's of opportunities for improvements in my code and I would be grateful if you could point out the way. Just a few tips, I'm sure, will go a long way towards getting the improvements I'm looking for. It's not the end of the world, as it is, but you'll see, when you try out the code, that each 'presses' on the Slider takes a long time to execute :
clc
global Axe_a_b;
fig=figure('Name','Roger Breton sRGB CIE ab diagram');
Test = fig.Position;
fig.Position = [1600 500 700 700];
ax = axes(fig);
axis([-128 128 -128 128]);
xticks([-128 -112 -96 -80 -64 -48 -32 -16 0 16 32 48 64 80 96 112 128]);
yticks([-128 -112 -96 -80 -64 -48 -32 -16 0 16 32 48 64 80 96 112 128]);
hold on;
CIE_a = 128;
CIE_b = 65; % 128 / 65, Steps of 4
% Algorithm :
% Each call to the function calculates a new colormap
% using a lower b* value
% Keeping L* range constant, between each function call :
% Slice L* = 50
Axe_L = transpose(linspace(50, 50, 65));
% Create one Column vector of 65 constant a* values,
% to fill the diagram, columns by columns,
% from Left (a* = -128) to right (a* = 128)
Axe_a_b = transpose(linspace(-128, 128, 65));
% MarkerSize = 100
for row = 1:65
Col = linspace(CIE_a, CIE_a, 65)';
RowX=[Axe_a_b, Col];
colorListAB = CalculateColorList(CIE_a, CIE_b, Axe_L)
scatter(RowX(:,1),RowX(:,2),100, colorListAB, 'filled', 's');
CIE_a = CIE_a - 4;
end
uicontrol('Style', 'Slider', 'Position',[655 500 20 150], 'Value', 0.5, 'Callback', {@sliderCB});
global txt_ctrl;
txt_ctrl = uicontrol('Style', 'text', 'Position',[640 450 50 50], 'String', ['50']);
set(txt_ctrl, 'FontSize', 20);
function [colorListAB] = CalculateColorList(CIE_a,CIE_b, Axe_L)
% CIE_a = Row
% CIE_b = Col
global Axe_a_b;
% Create one Column vector of 65 constant a* values
% 1st call = a* 128
% 2nd call = a* 124
% 3rd call = a* 120 ...
Axe_L_ROW = transpose(linspace(CIE_a, CIE_a, CIE_b));
Lab_Axe_AB = [Axe_L, Axe_a_b, Axe_L_ROW];
RGB_Axe_AB = lab2rgb(Lab_Axe_AB)
[row,col] = size(RGB_Axe_AB);
% Process Green/Magenta axis first
for ROW = 1:row
if RGB_Axe_AB(ROW,1) < 0
RGB_Axe_AB(ROW,1) = 0;
% Show white color for out of gamut colors
RGB_Axe_AB(ROW,1) = 0.5;
RGB_Axe_AB(ROW,2) = 0.5;
RGB_Axe_AB(ROW,3) = 0.5;
end
if RGB_Axe_AB(ROW,1) > 1
RGB_Axe_AB(ROW,1) = 1;
% Show white color for out of gamut colors
RGB_Axe_AB(ROW,1) = 0.5;
RGB_Axe_AB(ROW,2) = 0.5;
RGB_Axe_AB(ROW,3) = 0.5;
end
if RGB_Axe_AB(ROW,2) < 0
RGB_Axe_AB(ROW,2) = 0;
% Show white color for out of gamut colors
RGB_Axe_AB(ROW,1) = 0.5;
RGB_Axe_AB(ROW,2) = 0.5;
RGB_Axe_AB(ROW,3) = 0.5;
end
if RGB_Axe_AB(ROW,2) > 1
RGB_Axe_AB(ROW,2) = 1;
% Show white color for out of gamut colors
RGB_Axe_AB(ROW,1) = 0.5;
RGB_Axe_AB(ROW,2) = 0.5;
RGB_Axe_AB(ROW,3) = 0.5;
end
if RGB_Axe_AB(ROW,3) < 0
RGB_Axe_AB(ROW,3) = 0;
% Show white color for out of gamut colors
RGB_Axe_AB(ROW,1) = 0.5;
RGB_Axe_AB(ROW,2) = 0.5;
RGB_Axe_AB(ROW,3) = 0.5;
end
if RGB_Axe_AB(ROW,3) > 1
RGB_Axe_AB(ROW,3) = 1;
% Show white color for out of gamut colors
RGB_Axe_AB(ROW,1) = 0.5;
RGB_Axe_AB(ROW,2) = 0.5;
RGB_Axe_AB(ROW,3) = 0.5;
end
end
RGB_Axe_AB_ColorMap = colormap(RGB_Axe_AB)
colormapAB = RGB_Axe_AB_ColorMap;
colorListAB = colormapAB;
end
function sliderCB(SliderH, EventData)
global txt_ctrl;
global Axe_a_b;
CIE_a = 128;
CIE_b = 65;
% Retrieve slider current value of
Slider_L_New = SliderH.Value * 100;
set(txt_ctrl,'String',num2str(round(Slider_L_New)));
Axe_L_New = transpose(linspace(Slider_L_New, Slider_L_New, 65));
for row = 1:65
Col = linspace(CIE_a, CIE_a, 65)';
RowX=[Axe_a_b, Col];
colorListAB = CalculateColorList(CIE_a, CIE_b, Axe_L_New)
scatter(RowX(:,1),RowX(:,2),100, colorListAB, 'filled', 's');
CIE_a = CIE_a - 4;
end
end
Any help is appreciated.
7 Commenti
Steven Lord
il 26 Gen 2022
To generate a vector of constant values you can use repmat, ones, or zeros.
v1 = repmat(42, 1, 5)
v2 = 42 + zeros(1, 5)
v3 = 42 * ones(1, 5)
You can also look at the functions listed in the See Also section on the documentation pages for those functions for other related functions.
Risposta accettata
Cris LaPierre
il 25 Gen 2022
Modificato: Cris LaPierre
il 26 Gen 2022
You can get around using for loops if you meshgrid your X and Y vectors. I don't love the globals, either. Another way to speed up your code is minimize what gets printed to the screen. Here is what I had time to put together for you.
Note that the control code works, but I had to comment it out to get the code to run in this platform.
fig=figure('Name','Roger Breton sRGB CIE ab diagram');
Test = fig.Position;
fig.Position = [1600 500 700 700];
ax = axes(fig);
axis([-128 128 -128 128]);
xticks([-128 -112 -96 -80 -64 -48 -32 -16 0 16 32 48 64 80 96 112 128]);
yticks([-128 -112 -96 -80 -64 -48 -32 -16 0 16 32 48 64 80 96 112 128]);
CIE_a = 128;
CIE_b = 65; % 128 / 65, Steps of 4
% Algorithm :
Axe_L = 50;
Axe_a_b = linspace(-128, 128, 65);
[X,Y] = meshgrid(Axe_a_b,Axe_a_b);
colorListAB = CalculateColorList(X, Y, Axe_L);
scatter(Y(:),X(:),100, colorListAB, 'filled', 's');
%% Works, but commenting out because can't be used in this platform
% txt_ctrl = uicontrol('Style', 'text', 'Position',[640 450 50 50], 'String', ['50']);
% set(txt_ctrl, 'FontSize', 20);
%
% uicontrol('Style', 'Slider', 'Position',[655 500 20 150], 'Value', 0.5, 'Callback', {@sliderCB,txt_ctrl});
function [colorListAB] = CalculateColorList(X,Y, Axe_L)
Axe_L = ones(size(X))*Axe_L;
Lab_Axe_AB = [Axe_L(:), Y(:), X(:)];
RGB_Axe_AB = lab2rgb(Lab_Axe_AB);
ind1 = min(RGB_Axe_AB,[],2);
ind2 = max(RGB_Axe_AB,[],2);
RGB_Axe_AB(ind1<0,:) = 0.5;
RGB_Axe_AB(ind2>1,:) = 0.5;
colorListAB = colormap(RGB_Axe_AB);
end
function sliderCB(SliderH, EventData,txt_ctrl)
CIE_a = 128;
CIE_b = 65;
% Retrieve slider current value of
Slider_L_New = SliderH.Value * 100;
set(txt_ctrl,'String',num2str(round(Slider_L_New)));
Axe_L_New = Slider_L_New;
Axe_a_b = linspace(-128, 128, 65);
[X,Y] = meshgrid(Axe_a_b,Axe_a_b);
colorListAB = CalculateColorList(X, Y, Axe_L_New);
scatter(Y(:),X(:),100, colorListAB, 'filled', 's');
end
9 Commenti
Cris LaPierre
il 28 Gen 2022
Modificato: Cris LaPierre
il 28 Gen 2022
This replaced all your if statements for when any value in a row was <0. The colon here indicates all columns, not rows.
There are two steps to doing this.
ind1 = min(RGB_Axe_AB,[],2); % returns the min value in each row
ind2 = max(RGB_Axe_AB,[],2); % returns the max value in each row
% identifies each row where the min is <0.
% Changes all values in the corresponding row of RGB_Axe_AB to 0.5
RGB_Axe_AB(ind1<0,:) = 0.5;
% identifies each row where the max is >1.
% Changes all values in the corresponding row of RGB_Axe_AB to 0.5
RGB_Axe_AB(ind2>1,:) = 0.5;
Più risposte (1)
Benjamin Thompson
il 25 Gen 2022
The MATLAB editor has some helpful suggestions, removing unnecessary output and so on. Have you tried using the Profile button in the editor to run it and find the places where the most time is spent?
4 Commenti
Stephen23
il 26 Gen 2022
Images are just arrays. Take a look here: https://www.mathworks.com/help/matlab/creating_plots/image-types.html#f2-12468
Updating a IMAGE graphics object would be much more efficient than what you are doing now:
Vedere anche
Categorie
Scopri di più su Graphics Object Programming in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!