-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvideo_processing.m
2298 lines (2079 loc) · 66.4 KB
/
video_processing.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function varargout = video_processing(varargin)
% VIDEO_PROCESSING MATLAB code for video_processing.fig
% VIDEO_PROCESSING, by itself, creates a new VIDEO_PROCESSING or raises the existing
% singleton*.
%
% H = VIDEO_PROCESSING returns the handle to a new VIDEO_PROCESSING or the handle to
% the existing singleton*.
%
% VIDEO_PROCESSING('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in VIDEO_PROCESSING.M with the given input arguments.
%
% VIDEO_PROCESSING('Property','Value',...) creates a new VIDEO_PROCESSING or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before video_processing_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to video_processing_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help video_processing
% Last Modified by GUIDE v2.5 09-Jul-2017 13:33:26
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @video_processing_OpeningFcn, ...
'gui_OutputFcn', @video_processing_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before video_processing is made visible.
function video_processing_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to video_processing (see VARARGIN)
global pause_self vid
global MSES
global compressRatioS
compressRatioS = [];
MSES = [];
pause_self = 1;
vid = -1;
ha=axes('units','normalized','position',[0 0 1 1]);
uistack(ha,'down')
II=imread('uestc2.jpg');
image(II)
colormap gray
axes(handles.axes8)
imshow('uestc.jpg');
axes(handles.axes9)
imshow('tongxin.jpg');
% set(ha,'handlevisibility','off','visible','off');
% Choose default command line output for video_processing
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes video_processing wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = video_processing_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global vid
vid = videoinput('winvideo',1,'YUY2_640x480');
set(vid,'ReturnedColorSpace','rgb');
vidRes = get(vid,'VideoResolution');
nBands = get(vid,'NumberOfBands');
hImage = image(zeros(vidRes(2),vidRes(1),nBands),'parent',handles.axes1);
preview(vid,hImage);
start(vid)
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global vid
if vid ~= -1
delete(vid);
end;
axes(handles.axes2)
hold off
plot(1,1)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global vid
global mypic
mypic = getsnapshot(vid); %截取的彩色图像
axes(handles.axes1);
imshow(mypic);
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global mypic
global I1
if numel(size(mypic))==2
I1 = mypic;
1
else
I1 = rgb2gray(mypic); %灰色图像
end
axes(handles.axes2);
imshow(I1);
axes(handles.axes3);
imhist(I1)
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global I1
I2 = histeq(I1);
axes(handles.axes5);
imshow(I2);
axes(handles.axes4);
imhist(I2)
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global mypic
[FileName,PathName] = uigetfile({'*.jpg;*.png;*.pgm;*.tif;*.bmp'},'Select figure File');
path = strcat(PathName,FileName);
mypic = imread(path);
axes(handles.axes1);
imshow(mypic);
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function get_function(~, eventdata, handles)
global select_function
select_function = get(handles.popumenu1, 'value');
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%% 对灰度图像的操作
global select_function
global I1
global I2
select_function = get(handles.popupmenu1, 'value');
disp(select_function)
%% 均衡
if select_function == 2
I2 = histeq(I1);
axes(handles.axes5);
imshow(I2);
axes(handles.axes4);
imhist(I2)
end
%% 'prewitt'边缘检测
if select_function == 3
i = 0.45
set(handles.pushbutton7,'UserData',1);
while(i>0 & get(handles.pushbutton7,'UserData') ==1)
I2=edge(I1,'prewitt',i);
%以自动阈值选择法地图像进行Roberts算子检测
% [I2,thresh1]=edge(I1,'roberts');
%返回当前Roberts算子边缘检测的阈值
axes(handles.axes5);
imshow(I2);
title(['K:',num2str(i)]);
axes(handles.axes4);
imhist(I2)
i = i - 0.003
pause(0.01)
drawnow
end
guidata(hObject, handles);
end
%% 动态边缘检测
if select_function == 4
i = 0.45
set(handles.pushbutton7,'UserData',1);
while(i>0 & get(handles.pushbutton7,'UserData') ==1)
I2=edge(I1,'canny',i);
%以自动阈值选择法地图像进行Roberts算子检测
% [I2,thresh1]=edge(I1,'roberts');
%返回当前Roberts算子边缘检测的阈值
axes(handles.axes5);
imshow(I2);
title(['K:',num2str(i)]);
axes(handles.axes4);
imhist(I2)
i = i - 0.003
pause(0.01)
drawnow
end
guidata(hObject, handles);
end
%% 特定区域锐化滤波
if select_function == 5
axes(handles.axes2)
% BW = roipoly(I1);
H = fspecial('motion',20,45);
I2 = imfilter(I1,H,'replicate');
axes(handles.axes5);
imshow(I2);
axes(handles.axes4);
imhist(I2)
end
%% 模糊处理
if select_function == 6
H = fspecial('disk',10);
I2 = imfilter(I1,H,'replicate');
axes(handles.axes5);
imshow(I2);
xlabel('模糊图像');
axes(handles.axes4);
imhist(I2)
end
%% 黑白二值化
if select_function == 7
level=graythresh(I1); % 设置黑白转换阈值
I2=im2bw(I1,level); % 将图像转换为黑白图像
% 根据上面直方图选择阈值150,划分图像的前景和背景
axes(handles.axes5);
imshow(I2);
xlabel('黑白二值化图像');
axes(handles.axes4);
imhist(I2)
end
%% 膨胀图像
if select_function == 8
se = strel('ball',5,5); %创建椭圆Strel对象
I2=imdilate(I1,se); %膨胀图像
axes(handles.axes5);
imshow(I2);
xlabel('膨胀图像');
axes(handles.axes4);
imhist(I2)
end
%% 腐蚀图像
if select_function == 9
se = strel('ball',5,5); %创建椭圆Strel对象
I2=imerode(I1,se); %膨胀图像
axes(handles.axes5);
imshow(I2);
xlabel('腐蚀图像');
axes(handles.axes4);
imhist(I2)
end
%% 特定区域锐化滤波
if select_function == 10
axes(handles.axes2)
BW = roipoly(I1);
h = fspecial('motion');
I2 = roifilt2(h,I1,BW);
axes(handles.axes5);
imshow(I2);
axes(handles.axes4);
imhist(I2)
end
%% 改变亮度
if select_function == 11
im = 0.1
set(handles.pushbutton7,'UserData',1);
while(im < 10 & get(handles.pushbutton7,'UserData') ==1)
I2 = immultiply(I1, im);
axes(handles.axes5);
imshow(I2);
xlabel('改变亮度');
title(['缩放系数',num2str(im)]);
axes(handles.axes4);
imhist(I2);
pause(0.01)
im = im+0.02;
drawnow
end
guidata(hObject, handles);
end
%% 动态调整图片亮度
if select_function == 12
I2_tem=-double(I1)+255; %B=-1*A+255,图像求补,注意把A的类型转换为double
axes(handles.axes5);
I2 = uint8(I2_tem);
imshow(I2);
% xlabel('改变亮度');
title('图像求补'); %再把double类型转换为unit8
axes(handles.axes4);
imhist(I2)
end
%% 动态加入椒盐噪音
if select_function == 13
set(handles.pushbutton7,'UserData',1);
im = 0.01
while(im < 0.8 & get(handles.pushbutton7,'UserData') ==1)
I2 = imnoise(I1,'salt & pepper',im);
axes(handles.axes5);
imshow(I2);
title(['加上盐椒噪声后的图像',num2str(im)]);
axes(handles.axes4);
imhist(I2)
im = im + 0.01;
pause(0.1);
drawnow;
end
guidata(hObject, handles);
end
%% 改变对比度
if select_function == 14
I2 = imadjust(I1,stretchlim(I1),[0 1]);
axes(handles.axes5);
imshow(I2);
title('改变对比度');
axes(handles.axes4);
imhist(I2)
end
%% 转换为索引色
if select_function == 15
I2 = grayslice(I1, 16);
axes(handles.axes5);
imshow(I2,jet(16));
title('RGB转索引色');
axes(handles.axes4);
imhist(I2);
end
%% 特定区域高斯滤波
if select_function == 16
axes(handles.axes2)
BW = roipoly(I1);
h = fspecial('gaussian');
I2 = roifilt2(h,I1,BW);
axes(handles.axes5);
imshow(I2);
axes(handles.axes4);
imhist(I2)
end
%% 特定区域平均滤波
if select_function == 17
axes(handles.axes2)
BW = roipoly(I1);
h = fspecial('average');
I2 = roifilt2(h,I1,BW);
axes(handles.axes5);
imshow(I2);
axes(handles.axes4);
imhist(I2)
end
%% 特定区域边缘提取
if select_function == 18
axes(handles.axes2)
BW = roipoly(I1);
h = fspecial('sobel');
I2 = roifilt2(h,I1,BW);
axes(handles.axes5);
imshow(I2);
axes(handles.axes4);
imhist(I2)
end
%% 特定区域马赛克
if select_function == 19
axes(handles.axes2)
BW = roipoly(I1);
h = fspecial('disk',30);
I2 = roifilt2(h,I1,BW);
axes(handles.axes5);
imshow(I2);
axes(handles.axes4);
imhist(I2)
end
%% 尺寸缩小
if select_function == 20
axes(handles.axes2)
I2 = imresize(I1,0.9*size(I1));
axes(handles.axes5);
imshow(I2);
axes(handles.axes4);
imhist(I2)
end
%% 尺寸缩小
if select_function == 21
axes(handles.axes2)
I2 = imresize(I1,0.8*size(I1));
axes(handles.axes5);
imshow(I2);
axes(handles.axes4);
imhist(I2)
end
%% 尺寸缩小
if select_function == 22
axes(handles.axes2)
I2 = imresize(I1,0.7*size(I1));
axes(handles.axes5);
imshow(I2);
axes(handles.axes4);
imhist(I2)
end
%% 尺寸缩小
if select_function == 23
axes(handles.axes2)
I2 = imresize(I1,1.1*size(I1));
axes(handles.axes5);
imshow(I2);
axes(handles.axes4);
imhist(I2)
end
%% 尺寸缩小
if select_function == 24
axes(handles.axes2)
I2 = imresize(I1,1.2*size(I1));
axes(handles.axes5);
imshow(I2);
axes(handles.axes4);
imhist(I2)
end
% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global I1
global I2
I1 = I2;
axes(handles.axes2);
imshow(I1);
axes(handles.axes3);
imhist(I1)
% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global I1
[f,p]=uiputfile({'*.jpg'},'保存文件');
str=strcat(p,f);
pix=getframe(handles.axes2);
imwrite(pix.cdata,str,'jpg')
% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% uiresume(handles.aexs4)
set(handles.pushbutton7,'UserData',0);
guidata(hObject, handles);
% --- Executes on button press in pushbutton11.
function pushbutton11_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global mypic
[f,p]=uiputfile({'*.jpg'},'保存文件');
str=strcat(p,f);
pix=getframe(handles.axes1);
imwrite(pix.cdata,str,'jpg')
% --- Executes on button press in pushbutton12.
function pushbutton12_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton12 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global select_function
global mypic
global I2
select_function = get(handles.popupmenu1, 'value');
disp(select_function)
%% 均衡
if select_function == 2
sourcePic=mypic;
[m,n,o]=size(sourcePic);
%grayPic=rgb2gray(sourcePic);
%% 通道一
grayPic=sourcePic(:,:,1);
gp=zeros(1,256); %计算各灰度出现的概率
for i=1:256
gp(i)=length(find(grayPic==(i-1)))/(m*n);
end
newGp=zeros(1,256); %计算新的各灰度出现的概率
S1=zeros(1,256);
S2=zeros(1,256);
tmp=0;
for i=1:256
tmp=tmp+gp(i);
S1(i)=tmp;
S2(i)=round(S1(i)*256);
end
for i=1:256
newGp(i)=sum(gp(find(S2==i)));
end
newGrayPic=grayPic; %填充各像素点新的灰度值
for i=1:256
newGrayPic(find(grayPic==(i-1)))=S2(i);
end
nr=newGrayPic;
%% 通道二
grayPic=sourcePic(:,:,2);
gp=zeros(1,256); %计算各灰度出现的概率
for i=1:256
gp(i)=length(find(grayPic==(i-1)))/(m*n);
end
newGp=zeros(1,256); %计算新的各灰度出现的概率
S1=zeros(1,256);
S2=zeros(1,256);
tmp=0;
for i=1:256
tmp=tmp+gp(i);
S1(i)=tmp;
S2(i)=round(S1(i)*256);
end
for i=1:256
newGp(i)=sum(gp(find(S2==i)));
end
newGrayPic=grayPic; %填充各像素点新的灰度值
for i=1:256
newGrayPic(find(grayPic==(i-1)))=S2(i);
end
ng=newGrayPic;
%% 通道三
grayPic=sourcePic(:,:,3);
gp=zeros(1,256); %计算各灰度出现的概率
for i=1:256
gp(i)=length(find(grayPic==(i-1)))/(m*n);
end
newGp=zeros(1,256); %计算新的各灰度出现的概率
S1=zeros(1,256);
S2=zeros(1,256);
tmp=0;
for i=1:256
tmp=tmp+gp(i);
S1(i)=tmp;
S2(i)=round(S1(i)*256);
end
for i=1:256
newGp(i)=sum(gp(find(S2==i)));
end
newGrayPic=grayPic; %填充各像素点新的灰度值
for i=1:256
newGrayPic(find(grayPic==(i-1)))=S2(i);
end
nb=newGrayPic;
%% 合并之前成果
res=cat(3,nr,ng,nb);
axes(handles.axes1)
imshow(res,[]);
end
%% 边缘检测
if select_function == 3
I2=edge(mypic,'prewitt',0.05);
%以自动阈值选择法地图像进行Roberts算子检测
% [I2,thresh1]=edge(mypic,'roberts');
%返回当前Roberts算子边缘检测的阈值
axes(handles.axes5);
imshow(I2);
axes(handles.axes4);
imhist(I2)
end
%% 动态边缘检测
if select_function == 4
i = 0.45
set(handles.pushbutton12,'UserData',1);
while(i>0 & get(handles.pushbutton12,'UserData') ==1)
I2=edge(mypic,'prewitt',i);
%以自动阈值选择法地图像进行Roberts算子检测
% [I2,thresh1]=edge(mypic,'roberts');
%返回当前Roberts算子边缘检测的阈值
axes(handles.axes5);
imshow(I2);
title(['K:',num2str(i)]);
axes(handles.axes4);
imhist(I2)
i = i - 0.003
pause(0.01)
drawnow
end
guidata(hObject, handles);
end
%% 动态模糊处理
if select_function == 5
H = fspecial('motion',20,45);
I2 = imfilter(mypic,H,'replicate');
axes(handles.axes5);
imshow(I2);
xlabel('动态模糊图像');
axes(handles.axes4);
imhist(I2)
end
%% 模糊处理
if select_function == 6
H = fspecial('disk',10);
I2 = imfilter(mypic,H,'replicate');
axes(handles.axes5);
imshow(I2);
xlabel('模糊图像');
axes(handles.axes4);
imhist(I2)
end
%% 黑白二值化
if select_function == 7
level=graythresh(mypic); % 设置黑白转换阈值
I2=im2bw(mypic,level); % 将图像转换为黑白图像
% 根据上面直方图选择阈值150,划分图像的前景和背景
axes(handles.axes5);
imshow(I2);
xlabel('黑白二值化图像');
axes(handles.axes4);
imhist(I2);
end
%% 膨胀图像
if select_function == 8
se = strel('ball',5,5); %创建椭圆Strel对象
I2=imdilate(mypic,se); %膨胀图像
axes(handles.axes5);
imshow(I2);
xlabel('膨胀图像');
axes(handles.axes4);
imhist(I2)
end
%% 腐蚀图像
if select_function == 9
se = strel('ball',5,5); %创建椭圆Strel对象
I2=imerode(mypic,se); %膨胀图像
axes(handles.axes5);
imshow(I2);
xlabel('腐蚀图像');
axes(handles.axes4);
imhist(I2)
end
%% 特定区域锐化滤波
if select_function == 10
BW = roipoly(mypic);
h = fspecial('unsharp');
I2 = roifilt2(h,mypic,BW);
axes(handles.axes5);
imshow(I2);
end
%% 改变亮度
if select_function == 11
im = 0.1
set(handles.pushbutton12,'UserData',1);
while(im < 10 & get(handles.pushbutton12,'UserData') ==1)
I2 = immultiply(mypic, im);
axes(handles.axes5);
imshow(I2);
xlabel('改变亮度');
title(['缩放系数',num2str(im)]);
pause(0.01)
im = im+0.02;
drawnow
end
guidata(hObject, handles);
end
%% 动态调整图片亮度
if select_function == 12
I2_tem=-double(mypic)+255; %B=-1*A+255,图像求补,注意把A的类型转换为double
axes(handles.axes5);
I2 = uint8(I2_tem);
imshow(I2);
% xlabel('改变亮度');
title('图像求补'); %再把double类型转换为unit8
end
%% 动态加入椒盐噪音
if select_function == 13
set(handles.pushbutton12,'UserData',1);
im = 0.01
while(im < 0.8 & get(handles.pushbutton12,'UserData') ==1)
I2 = imnoise(mypic,'salt & pepper',im);
axes(handles.axes5);
imshow(I2);
title(['加上盐椒噪声后的图像',num2str(im)]);
im = im + 0.01;
pause(0.1);
drawnow;
end
guidata(hObject, handles);
end
%% 改变对比度
if select_function == 14
I2 = imadjust(mypic,stretchlim(mypic),[0 1]);
axes(handles.axes5);
imshow(I2);
title('改变对比度');
end
%% 转换为索引色
if select_function == 15
[I2,map] = rgb2ind(mypic,128);
axes(handles.axes5);
imshow(I2);
colormap(map);
title('RGB转索引色');
end
%% 特定区域高斯滤波
if select_function == 16
axes(handles.axes1)
BW = roipoly(mypic);
h = fspecial('gaussian');
I2 = roifilt2(h,mypic,BW);
axes(handles.axes5);
imshow(I2);
end
%% 特定区域平均滤波
if select_function == 17
axes(handles.axes1)
BW = roipoly(mypic);
h = fspecial('average');
I2 = roifilt2(h,mypic,BW);
axes(handles.axes5);
imshow(I2);
end
%% 特定区域边缘提取
if select_function == 18
axes(handles.axes1)
BW = roipoly(mypic);
h = fspecial('sobel');
I2 = roifilt2(h,mypic,BW);
axes(handles.axes5);
imshow(I2);
end
%% 特定区域马赛克
if select_function == 19
axes(handles.axes1)
BW = roipoly(mypic);
h = fspecial('disk',30);
I2 = roifilt2(h,mypic,BW);
axes(handles.axes5);
imshow(I2);
end
% --- Executes on button press in pushbutton14.
function pushbutton14_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton14 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.pushbutton12,'UserData',0);
guidata(hObject, handles);
% --- Executes on button press in pushbutton16.
function pushbutton16_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton16 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global vid jieshu;
jieshu=1;
faceDetector = vision.CascadeObjectDetector();
hvp=vision.VideoPlayer('WindowCaption','face detect system','WindowPosition',[100,100,640,480]);
hcsc=vision.ColorSpaceConverter;
hcsc.Conversion='RGB to YCbCr';
hsi = vision.ShapeInserter('BorderColor','Custom','CustomBorderColor',[255 255 0]);
% hsi=vision.ShapeInserter('Shape','Rectangles', 'BorderColor','Custom','CustomBorderColor',[255,0 0]);
while jieshu
frame=getsnapshot(vid);
frame1=im2double(frame);
Pts= step(faceDetector, frame1);
frame=step(hsi,frame1,Pts);
step(hvp,frame);
end
close(hvp);
function varargout = facedetect_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function axespreview_CreateFcn(hObject, eventdata, handles)
set(hObject,'xTick',[]);
set(hObject,'ytick',[]);
set(hObject,'box','on');
% --- Executes during object creation, after setting all properties.
function axesshow_CreateFcn(hObject, eventdata, handles)
set(hObject,'xTick',[]);
set(hObject,'ytick',[]);
set(hObject,'box','on');
function pushbutton17_Callback(hObject, eventdata, handles)
global jieshu;
jieshu=0;
% --- Executes during object creation, after setting all properties.
function pushbutton17_CreateFcn(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function axes6_CreateFcn(hObject, eventdata, handles)
I=imread('uestc.jpg');
imshow(I);
% --- Executes on button press in pushbutton18.
function pushbutton18_Callback(hObject, eventdata, handles)
global mypic %axes1的图像
% global I1 %axes2的图像
global MSES
global compressRatioS
select_function2 = get(handles.popupmenu2, 'value');
RGB = mypic;
% RGB=imresize(RGB,[168,224]);%因为1.jpg大小为169*220,所以我改为168*224
imwrite(RGB,'H:\matlab\GUI_STUDY\GUI_sum\image_compess\start.jpg'); %保存压缩前图像
%下面是对RGB三个分量进行分离,此时他们依然为整数
R=RGB(:,:,1);
G=RGB(:,:,2);
B=RGB(:,:,3);
start_info = imfinfo('H:\matlab\GUI_STUDY\GUI_sum\image_compess\start.jpg')
axes(handles.axes1)
imshow(RGB),title('原来的RGB图像');
start_size = start_info.FileSize;
xlabel(num2str(start_size))
%% matlab压缩功能测试
imwrite(RGB,'H:\matlab\GUI_STUDY\GUI_sum\image_compess\matlab自动压缩一次.jpg'); %保存压缩前图像
%由于1.jpg和压缩前.jpg大小差距很大,从中可以看出matlab对图像进行了压缩
RGB=imread('H:\matlab\GUI_STUDY\GUI_sum\image_compess\matlab自动压缩一次.jpg');%读取图片
imwrite(RGB,'H:\matlab\GUI_STUDY\GUI_sum\image_compess\matlab自动压缩二次.jpg'); %保存压缩前图像
RGB=imread('H:\matlab\GUI_STUDY\GUI_sum\image_compess\matlab自动压缩二次.jpg');%读取图片
imwrite(RGB,'H:\matlab\GUI_STUDY\GUI_sum\image_compess\matlab自动压缩三次.jpg'); %保存压缩前图像
RGB=imread('H:\matlab\GUI_STUDY\GUI_sum\image_compess\matlab自动压缩三次.jpg');%读取图片
imwrite(RGB,'H:\matlab\GUI_STUDY\GUI_sum\image_compess\matlab自动压缩五次.jpg'); %保存压缩前图像
%测试结束
%% 转化为YUV图像
Y=0.299*double(R)+0.587*double(G)+0.114*double(B);
U=-0.169*double(R)-0.3316*double(G)+0.5*double(B);
V=0.5*double(R)-0.4186*double(G)-0.0813*double(B);
YUV=cat(3,Y,U,V);%YUV图像
axes(handles.axes4)
imshow(uint8(YUV)),title('通过计算得到的YUV图像')
%% 进行DCT变换
T=dctmtx(8);%产生一个8*8的DCT变换矩阵
%进行DCT变换 BY BU BV是double类型
BY=blkproc(Y,[8 8],'P1*x*P2',T,T');
BU=blkproc(U,[8 8],'P1*x*P2',T,T');
BV=blkproc(V,[8 8],'P1*x*P2',T,T');
a=[16 11 10 16 24 40 51 61;
12 12 14 19 26 58 60 55;
14 13 16 24 40 57 69 55;
14 17 22 29 51 87 80 62;
18 22 37 56 68 109 103 77;
24 35 55 64 81 104 113 92;
49 64 78 87 103 121 120 101;
72 92 95 98 112 100 103 99;]; %亮度的量化模板系数
b=[17 18 24 47 99 99 99 99;
18 21 26 66 99 99 99 99;
24 26 56 99 99 99 99 99;
47 66 99 99 99 99 99 99;
99 99 99 99 99 99 99 99;
99 99 99 99 99 99 99 99;
99 99 99 99 99 99 99 99;
99 99 99 99 99 99 99 99;]; %颜色的量化模板系数
%BY2 BU2 BV2是double类型
BY2=blkproc(BY,[8 8],'x./P1',a);
BU2=blkproc(BU,[8 8],'x./P1',b);
BV2=blkproc(BV,[8 8],'x./P1',b);
%这里进行取整量化,BY3 BU3 BV3是uint8类型
BY3=int8(BY2);
BU3=int8(BU2);
BV3=int8(BV2);
%BY4 BU4 BV4是double类型
BY4=blkproc(double(BY3),[8 8],'x.*P1',a);
BU4=blkproc(double(BU3),[8 8],'x.*P1',b);
BV4=blkproc(double(BV3),[8 8],'x.*P1',b);
%测试代码
%BY4=blkproc(BY2,[8 8],'x.*P1',a);
%BU4=blkproc(BU2,[8 8],'x.*P1',b);
%BV4=blkproc(BV2,[8 8],'x.*P1',b);
%% 根据不同的点数进行压缩
mask = chooseMask(select_function2);
%% 9点 select == 2
%BY5 BU5 BV5是double类型
BY5=blkproc(BY4,[8 8],'P1.*x',mask);
BU5=blkproc(BU4,[8 8],'P1.*x',mask);
BV5=blkproc(BV4,[8 8],'P1.*x',mask);