Because I am learning Matlab's app designer and image processing recently, and because there are a few finished codes related to appdesigner on the Internet, I wrote some code myself to consolidate my knowledge. The code may be a bit complicated and messy, because I just wrote it not long ago . I hope that what I wrote can be helpful to everyone's study. You must go to the Internet to search for relevant principles before looking at the code;
The general form of the template is as follows:
The main functions are blur, sharpening, relative brightness, contrast, mirror rotation, horizontal adjustable angle rotation, press to return to the previous step, press to save the changed picture, press to display a new picture, press to open the picture and display it in the text box Display the path; there is a disadvantage of changing the app, that is, you can’t restore the previous steps after adjusting like the photo album in the mobile phone, this can only return one step, if anyone has an idea to deal with this problem, I hope you can Post your ideas in the comment area and make progress together.
First of all, the simple part such as the background color, the title will not be explained, the following is mainly to talk about the code and function of each control, if there is something wrong, I hope everyone can point out the criticism;
First, build the template and global variables (global variables have a great role and cannot be ignored. The role of original is to save the original picture, the role of changed and upgrade is to save the adapted picture, and last is used to save the picture in the previous step):
The function of the open control is to open the picture and display it in the origina coordinate area. The code is as follows:
The function of the blur control is to add dynamic blur (a function that comes with matlab), and its code is as follows:
The function of the sharpening control is to sharpen the image. Sharpening is to extract edge features, and then overlap with the original image in proportion. The code is as follows:
The function of the relative brightness control is to increase or decrease the brightness, the code is as follows:
The function of the contrast control is to change the contrast (the principle of contrast is that the contrast between light and dark is stronger), and its code is as follows:
The control function of horizontal adjustable angle rotation is to rotate the picture horizontally, and its code is as follows:
The function of the mirror rotation control is to rotate the picture horizontally and vertically, and its code is as follows:
The function of resetting the control is to redisplay the initial picture, eliminating the need to open and select the picture, and it can be used when the modified picture cannot meet the requirements. The code is as follows:
When saving the function of the control, save the picture displayed in the app.UIAXES_2 control. There are many methods. I use the method of copying the window. The code is as follows:
The function of returning to the previous step control is to return to the previous step and only return one step. This is the most restrictive place. I hope everyone can think about the solution together. The code is as follows:
The codes of all controls are below, if you need the file, you can send a private message;
properties (Access = private)
originalpicture; % Description
upgradepicture;
changedpicture;
lastpicture;
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.Lamp.Color='r';
end
% Button pushed function: openButton
function openButtonPushed(app, event)
[filename,pathname]=uigetfile('*.jpg',"search picture");
if isequal(pathname,'')||isequal(filename,'')
msgbox("no picture",'warning','help');
return;
else
app.Lamp.Color='g';
path=strcat(pathname,filename);
app.pathEditField.Value=path;
app.originalpicture=imread(path);
app.originalpicture=rgb2gray(app.originalpicture);
imshow(app.originalpicture,'Parent',app.UIAxes);
app.changedpicture=app.originalpicture;
end
end
% Value changing function: Slider
function SliderValueChanging(app, event)
changingValue = event.Value;
changingValue=double(changingValue);
if isequal(app.originalpicture,'')
msgbox("无图片",'warning','help');
return;
elseif isequal(app.changedpicture,'')
app.lastpicture=app.upgradepicture;
obscureway1=fspecial('motion',changingValue,changingValue);
obscureway1=imfilter(app.originalpicture,obscureway1);
app.upgradepicture=obscureway1;
imshow(obscureway1,'Parent',app.UIAxes_2);
app.changedpicture=app.upgradepicture;
return;
else
app.lastpicture=app.upgradepicture;
obscureway1=fspecial('motion',changingValue,changingValue);
obscureway1=imfilter(app.changedpicture,obscureway1);
app.upgradepicture=obscureway1;
imshow(obscureway1,'Parent',app.UIAxes_2);
app.changedpicture=app.upgradepicture;
end
end
% Value changing function: Slider_2
function Slider_2ValueChanging(app, event)
changingValue = event.Value;
changingValue=double(changingValue);
changingValue1=changingValue./100;
if isequal(app.originalpicture,'')
msgbox("无图片",'warning','help');
return;
elseif isequal(app.changedpicture,'')
app.lastpicture=app.changedpicture;
test = double(app.originalpicture);
Gx = [-1 -2 -1;0 0 0;1 2 1];
x = filter2(Gx,test);
Gy = [-1 0 1;-2 0 2;-1 0 1];
y = filter2(Gy,test);
test1 = sqrt(x.^2 + y.^2);
test2 = test + test1*changingValue1;
imshow(uint8(test2),'Parent',app.UIAxes_2);
app.changedpicture=uint8(tset2);
return;
else
app.lastpicture=app.changedpicture;
test = double(app.changedpicture);
Gx = [-1 -2 -1;0 0 0;1 2 1];
x = filter2(Gx,test);
Gy = [-1 0 1;-2 0 2;-1 0 1];
y = filter2(Gy,test);
test1 = sqrt(x.^2 + y.^2);
test2 = test + test1*changingValue1;
imshow(uint8(test2),'Parent',app.UIAxes_2);
app.changedpicture=uint8(tset2);
return;
end
end
% Value changed function: Spinner
function SpinnerValueChanged(app, event)
value = app.Spinner.Value;
if(value>100)
app.Spinner.Value=0;
value=0;
msgbox('超出范围','warning','help');
end
if isequal(app.originalpicture,'')
msgbox("无图片","warning",'help');
return;
elseif isequal(app.changedpicture,'')
app.lastpicture=app.changedpicture;
brightpicture=imadd(app.originalpicture,value);
imshow(brightpicture,'Parent',app.UIAxes_2);
app.changedpicture=brightpicture;
else
app.lastpicture=app.changedpicture;
brightpicture=imadd(app.changedpicture,value);
imshow(brightpicture,'Parent',app.UIAxes_2);
app.changedpicture=brightpicture;
end
end
% Value changing function: Spinner_2
function Spinner_2ValueChanging(app, event)
changingValue = event.Value;
changingValue=double(changingValue);
changingValue=changingValue./100;
if(changingValue>=0.5||changingValue<0)
app.Spinner_2.Value=0;
changingValue=0;
msgbox('超出范围','warning','help');
end
if isequal(app.originalpicture,'')
msgbox("无图片","warning",'help');
return;
elseif isequal(app.changedpicture,'')
app.lastpicture=app.changedpicture;
contrastpicture=imadjust(app.originalpicture,[changingValue,1-changingValue],[]);
imshow(contrastpicture,'Parent',app.UIAxes_2);
app.changedpicture=contrastpicture;
return;
else
app.lastpicture=app.changedpicture;
contrastpicture=imadjust(app.changedpicture,[changingValue,1-changingValue],[]);
imshow(contrastpicture,'Parent',app.UIAxes_2);
app.changedpicture=contrastpicture;
return;
end
end
% Button pushed function: saveButton
function saveButtonPushed(app, event)
savepicture=figure('Visible','off');
copyobj(app.UIAxes_2,savepicture);
set(gca,'Units','normalized','Position',[0.2 0.2 0.8 0.8]);
saveas(savepicture,'nb.jpg');
end
% Button pushed function: newButton
function newButtonPushed(app, event)
if isequal(app.originalpicture,'')
msgbox("无图片",'warning','help');
else
imshow(app.originalpicture,'Parent',app.UIAxes_2);
end
end
% Button pushed function: lastactionButton
function lastactionButtonPushed(app, event)
if isequal(app.lastpicture,'')
msgbox('无上一步','warning','help');
else
imshow(app.lastpicture,'Parent',app.UIAxes_2);
end
end
% Value changed function: mirrorKnob
function mirrorKnobValueChanged(app, event)
value = app.mirrorKnob.Value;
app.lastpicture=app.changedpicture;
if isequal(value,'x镜面')
mirrorpicture=flip(app.changedpicture,1);
imshow(mirrorpicture,'Parent',app.UIAxes_2);
app.changedpicture=mirrorpicture;
elseif isequal(value,'y镜面')
mirrorpicture=flip(app.changedpicture,2);
imshow(mirrorpicture,'Parent',app.UIAxes_2);
app.changedpicture=mirrorpicture;
end
end
% Value changing function: levelangleKnob
function levelangleKnobValueChanging(app, event)
changingValue = event.Value;
changingValue=double(changingValue);
app.lastpicture=app.changedpicture;
anglepicture=imrotate(app.changedpicture,changingValue);
imshow(anglepicture,'Parent',app.UIAxes_2);
app.changedpicture=anglepicture;
end
end