国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語(yǔ)言編程技術(shù)及教程分享平臺(tái)!
分類(lèi)導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - 利用Matlab提取圖片曲線(xiàn)

利用Matlab提取圖片曲線(xiàn)

2021-12-23 00:07陸嵩 Python

一張圖片,怎樣才可以提取里面曲線(xiàn)的數(shù)據(jù),從而協(xié)助我們完成其他需要的數(shù)據(jù)呢?本文通過(guò)示例來(lái)進(jìn)行說(shuō)明,文中大量的代碼以及圖片都可以幫助小伙伴們了解

利用 matlab 提取圖片曲線(xiàn)

給你一張圖片,如何提取里面曲線(xiàn)的數(shù)據(jù),從而利用這些數(shù)據(jù)進(jìn)行圖像重繪、加工處理、測(cè)距、擬合得到函數(shù)表達(dá)式等操作呢?

行文動(dòng)機(jī)

前段時(shí)間,有個(gè)朋友問(wèn)了我一個(gè)問(wèn)題,大概意思就是要給圖像的流線(xiàn)測(cè)距離,在我的印象里面,matlab 是似乎沒(méi)有這種直接的功能的。

利用Matlab提取圖片曲線(xiàn)

那么換個(gè)角度來(lái)理解一下這個(gè)問(wèn)題,如果給你一張圖像,如何提取里面點(diǎn)的數(shù)據(jù)?其實(shí),有了曲線(xiàn)的數(shù)據(jù),后面想干嘛就干嘛了。

一直沒(méi)空弄這個(gè),今天偷閑,安排!!!

圖像的讀入與裁剪

以下面的圖像作為例子。

利用Matlab提取圖片曲線(xiàn)

我們先導(dǎo)入圖像,進(jìn)行簡(jiǎn)單的裁剪。為什么要裁剪呢?其實(shí)不裁剪也沒(méi)關(guān)系,因?yàn)槲液竺媸腔谙袼攸c(diǎn)的顏色來(lái)提取的曲線(xiàn)。如果你想提取的曲線(xiàn)不能通過(guò)顏色區(qū)分,那么,最好通過(guò)裁剪,把你不想要的部分盡可能地剪掉。

?
1
2
3
4
5
6
%% 讀入圖片,展示,有必要的話(huà)可以適當(dāng)做一些裁剪
a = imread('a.jpg');%讀取到一張圖片
imshow(a);
a = imcrop(a);%使用鼠標(biāo)裁剪一波
imshow(a);
[low_num,col_num,~] = size(a);

顏色拾取

觀察圖像發(fā)現(xiàn),我們要提取的曲線(xiàn)是藍(lán)色的,所以我希望通過(guò)顏色把它區(qū)分出來(lái)。那么我們就要知道這個(gè)曲線(xiàn)的 rgb 值。我希望通過(guò)鼠標(biāo)點(diǎn)選的方式獲取到顏色值。

這里我偷個(gè)懶,直接采用了 開(kāi)源的顏色提取工具。這個(gè)模塊不是我寫(xiě)的,特此聲明,請(qǐng)尊重原創(chuàng)。

?
1
2
3
%% 顏色提取
getcolor();
color = color_list_temp(1,:);
?
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
function getcolor
global control;
global ima;
global gui;
global x_limit;
global y_limit;
global color;
global a;
global color_number;
global color_list;
global page;
global total_page;
global color_list_temp;
color=[];
page=1;
total_page=2;
color_list=[0 0 0];
color_list(1,:)=[];
color_number=1;
rgb_type=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gui.fig=figure('units','pixels',...
    'position',[350 100 800 500],...
    'numbertitle','off',...
    'menubar','none',...
    'resize','off',...
    'name','getcolor',...
    'color',[0.95 0.95 0.95]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
uh1=uimenu('label','設(shè)置');
uimenu(uh1,'label','rgb類(lèi)型設(shè)置','callback',@rgbset)
    function rgbset(~,~)
        gui.rgbfig=figure('units','pixels',...
        'position',[360 370 180 200],...
        'numbertitle','off',...
        'menubar','none',...
        'name','rgbset',...
        'resize','off');
        gui.axes=axes('units','pixels',...
         'parent',gui.rgbfig,... 
        'plotboxaspectratio',[1 1 1],...
        'color',[0.95 0.95 0.95],...
        'box','on', ...
        'xlim',[0 500],...
        'ylim',[0 500], ...
        'xcolor',[0.95 0.95 0.95],...
        'ycolor',[0.95 0.95 0.95],...
        'ydir','reverse', ...
        'xtick',[],'ytick',[]);
        gui.checkbox1=uicontrol('parent',gui.rgbfig,...
            'style','checkbox',...
            'string','范圍:0-1',...
            'position',[45 150 400 30],...
            'fontsize',10,...
            'value',rgb_type,...
            'callback',@ifon1);
        gui.checkbox255=uicontrol('parent',gui.rgbfig,...
            'style','checkbox',...
            'string','范圍:0-255',...
            'position',[45 120 400 30],...
            'fontsize',10,...
            'value',~rgb_type,...
            'callback',@ifon255);
        gui.makesurebutton=uicontrol('parent',gui.rgbfig,...
            'style','pushbutton',...
            'string','確定設(shè)置',...
            'position',[45 70 100 25],...
            'fontsize',10,...
            'callback',@settype);
        
        function ifon1(~,~)
            if(get(gui.checkbox1,'value')==1)
                set(gui.checkbox1,'value',1);
                set(gui.checkbox255,'value',0);
            else
                set(gui.checkbox1,'value',1);
            end              
        end
        function ifon255(~,~)
            if(get(gui.checkbox255,'value')==1)
                set(gui.checkbox255,'value',1);
                set(gui.checkbox1,'value',0);
            else
                set(gui.checkbox255,'value',1);
            end               
        end
        function settype(~,~)
            rgb_type=get(gui.checkbox1,'value');
            if ~isempty(color)
                set(gui.text2,'string',['[',num2str((color/255).*rgb_type+color.*(~rgb_type)),']']);
            end
            show_color(page);
            close(gui.rgbfig)
        end     
    end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
uh2=uimenu('label','保存');
uimenu(uh2,'label','儲(chǔ)存為mat','callback',@saveas_mat)
uimenu(uh2,'label','儲(chǔ)存為txt','callback',@saveas_txt)
uimenu(uh2,'label','儲(chǔ)存為excel','callback',@saveas_exl)
uimenupic=uimenu(uh2,'label','儲(chǔ)存為對(duì)照?qǐng)D');
uimenu(uimenupic,'label','儲(chǔ)存全部頁(yè)碼','callback',@saveas_pic_all);
uimenu(uimenupic,'label','儲(chǔ)存當(dāng)前頁(yè)碼','callback',@saveas_pic_now);
function saveas_mat(~,~)
    try
    [filename, pathname] = uiputfile({'*.mat','mat'});
        color_list_temp=(color_list/255).*rgb_type+color_list.*(~rgb_type);
        save([pathname,filename],'color_list_temp');
    catch
    end 
end
function saveas_txt(~,~)
    try
    [filename, pathname] = uiputfile({'*.txt','記事本'});
        color_list_temp=(color_list/255).*rgb_type+color_list.*(~rgb_type);
        [m,n]=size(color_list_temp);
        fid=fopen([ pathname,filename],'w');
        for ii=1:m
            for jj=1:n
                if jj==n
                    fprintf(fid,'%d\r\n',color_list_temp(ii,jj));
                else
                    fprintf(fid,'%d\r\t',color_list_temp(ii,jj));
                end
            end
        end
        fclose(fid);
    catch
    end
end
function saveas_exl(~,~)
    [filename, pathname] = uiputfile({'*.xlsx','記事本'});
    color_list_temp=(color_list/255).*rgb_type+color_list.*(~rgb_type);
    xlswrite([ pathname,filename],color_list_temp)
    
end
function saveas_pic_all(~,~)
    page_with_color=total_page-1;
    px=50;
    gap_px=10;
    pic=ones(9*px,page_with_color*px+(page_with_color-1)*gap_px,3);
    for p=1:page_with_color
        for ii=(p-1)*9+1:p*9
            for kk=1:3
                if ii<=length(color_list)
                    pic((ii-(p-1)*9-1)*px+1:(ii-(p-1)*9)*px,(p-1)*(px+gap_px)+1:(p-1)*(px+gap_px)+px,kk)=color_list(ii,kk)/255;    
                end
            end
        end
    end
    [filename, pathname] = uiputfile({'*.jpg;*.png','all image files';...
            '*.jpg','jpg';'*.png','png' });
    imwrite(pic,[pathname,filename]);
end
function saveas_pic_now(~,~)
    try
    [m,~]=size(color_list);
    m=m-(page-1)*9;
    m(m>9)=9;
    px=50;
    pic=ones(9*px,1*px,3);
    if m>0
        for ii=(page-1)*9+1:(page-1)*9+m
            for kk=1:3
                pic((ii-1)*px+1:ii*px,1:px,kk)=color_list(ii,kk)/255;     
            end
        end
    else
    end
    [filename, pathname] = uiputfile({'*.jpg;*.png','all image files';...
            '*.jpg','jpg';'*.png','png' });
    imwrite(pic,[pathname,filename]);
    catch
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%uh3=uimenu('label','導(dǎo)入');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gui.text=uicontrol('parent',gui.fig,...
    'style','text',...
    'string','色彩識(shí)別',...
    'horizontalalign','center',...
    'position',[50 440 400 30],...
    'backgroundcolor',[0.85 0.89 0.85],...
    'foregroundcolor','k',...
    'fontsize',15);
gui.text1=uicontrol('parent',gui.fig,...
    'style','text',...
    'string','',...
    'horizontalalign','center',...
    'position',[460 330 100 100],...
    'backgroundcolor',[1 1 1],...
    'foregroundcolor','k',...
    'fontsize',10);
gui.text2=uicontrol('parent',gui.fig,...
    'style','text',...
    'string','',...
    'horizontalalign','center',...
    'position',[350 440 210 30],...
    'backgroundcolor',[1 1 1],...
    'foregroundcolor','k',...
    'fontsize',10);
gui.savecolorbutton=uicontrol('parent',gui.fig,...
    'style','pushbutton',...
    'string','儲(chǔ)存顏色',...
    'position',[460 290 100 30],...
    'backgroundcolor',[0.85 0.89 0.85],...
    'foregroundcolor','k',...
    'fontsize',15,...
    'callback',@save_color);
gui.deletedatabutton=uicontrol('parent',gui.fig,...
    'style','pushbutton',...
    'string','清空數(shù)據(jù)',...
    'position',[460 230 100 30],...
    'backgroundcolor',[0.8 0.9 0.9],...
    'foregroundcolor','k',...
    'fontsize',15,...
    'callback',@clear_data);
gui.deletepicbutton=uicontrol('parent',gui.fig,...
    'style','pushbutton',...
    'string','刪除圖片',...
    'position',[460 180 100 30],...
    'backgroundcolor',[0.8 0.9 0.9],...
    'foregroundcolor','k',...
    'fontsize',15,...
    'callback',@delete_pic);
gui.getcapbutton=uicontrol('parent',gui.fig,...
    'style','pushbutton',...
    'string','屏幕截圖',...
    'position',[460 130 100 30],...
    'backgroundcolor',[0.8 0.9 0.9],...
    'foregroundcolor','k',...
    'fontsize',15,...
    'callback',@get_capture);
gui.getpicbutton=uicontrol('parent',gui.fig,...
    'style','pushbutton',...
    'string','讀取圖片',...
    'position',[460 80 100 30],...
    'backgroundcolor',[0.8 0.9 0.9],...
    'foregroundcolor','k',...
    'fontsize',15,...
    'callback',@getimage);
gui.getcolorbutton=uicontrol('parent',gui.fig,...
    'style','pushbutton',...
    'tag','recc',...
    'string','獲取顏色',...
    'position',[460 30 100 30],...
    'backgroundcolor',[0.8 0.9 0.9],...
    'foregroundcolor','k',...
    'fontsize',15,...
    'callback',@get_color);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:9
    gui.text=uicontrol('parent',gui.fig,...
    'tag',num2str(i),...
    'style','text',...
    'string','',...
    'horizontalalign','left',...
    'position',[600 440-40*(i-1) 30 30],...
    'backgroundcolor',[1 1 1],...
    'foregroundcolor','k',...
    'fontsize',10);   
end
 
for i=1:9
    gui.text=uicontrol('parent',gui.fig,...
    'tag',[num2str(i),'t'],...
    'style','text',...
    'string','',...
    'horizontalalign','center',...
    'position',[640 440-40*(i-1) 150 30],...
    'backgroundcolor',[1 1 1],...
    'foregroundcolor','k',...
    'fontsize',8);   
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gui.inputbutton=uicontrol('parent',gui.fig,...
    'style','pushbutton',...
    'string','清除最后一個(gè)顏色',...
    'position',[600 80 190 30],...
    'backgroundcolor',[0.85 0.89 0.85],...
    'foregroundcolor','k',...
    'fontsize',15,...
    'callback',@delete_last);
%gui.inputbutton=uicontrol('parent',gui.fig,...
    %'style','pushbutton',...
    %'string','導(dǎo)出數(shù)據(jù)',...
    %'position',[600 30 190 30],...
    %'backgroundcolor',[0.85 0.89 0.85],...
    %'foregroundcolor','k',...
    %'fontsize',15,...
    %'callback',@output_data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gui.lpbutton=uicontrol('parent',gui.fig,...
    'style','pushbutton',...
    'string','<上一頁(yè)',...
    'position',[600 30 70 30],...
    'backgroundcolor',[0.85 0.85 0.85],...
    'foregroundcolor','k',...
    'fontsize',12,...
    'callback',@lastpage);
gui.npbutton=uicontrol('parent',gui.fig,...
    'style','pushbutton',...
    'string','下一頁(yè)>',...
    'position',[720 30 70 30],...
    'backgroundcolor',[0.85 0.85 0.85],...
    'foregroundcolor','k',...
    'fontsize',12,...
    'callback',@nextpage);
gui.page=uicontrol('parent',gui.fig,...
    'style','text',...
    'string',[num2str(page),'/',num2str(total_page)],...
    'horizontalalign','center',...
    'position',[670 30 50 27],...
    'backgroundcolor',[0.95 0.95 0.95],...
    'foregroundcolor','k',...
    'fontsize',12);
function lastpage(~,~)
    page=page-1;
    page(page<1)=1;
    set(gui.page,'string',[num2str(page),'/',num2str(total_page)]);
    show_color(page);
end
function nextpage(~,~)
    page=page+1;
    page(page>total_page)=total_page;
    set(gui.page,'string',[num2str(page),'/',num2str(total_page)]);
    show_color(page);
end
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gui.axes=axes('units','pixels',...
      'plotboxaspectratio',[1 1 1],...
      'position',[50 30 400 400],...
      'color',[0.98 0.98 0.98],...
      'box','on', ...
      'xlim',[0 500],...
      'ylim',[0 500], ...
      'xcolor','w','ycolor','w',...
      'ydir','reverse', ...
      'tag','picbagaxes',...
      'xtick',[],'ytick',[]);
hold on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %function output_data(~,~)
        %if color_number>1
            %disp(color_list)
        %end
    %end
    function show_color(cur_page)
        len_list=size(color_list,1);
        for ii=(cur_page-1)*9+1:(cur_page-1)*9+9
            if(ii<=len_list)
                set(findobj('tag',num2str(ii-(cur_page-1)*9)),'backgroundcolor',color_list(ii,:)/255)
                set(findobj('tag',[num2str(ii-(cur_page-1)*9),'t']),'string',['[',num2str((color_list(ii,:)/255).*rgb_type+color_list(ii,:).*(~rgb_type)),']'])
            else
                set(findobj('tag',num2str(ii-(cur_page-1)*9)),'backgroundcolor',[1 1 1])
                set(findobj('tag',[num2str(ii-(cur_page-1)*9),'t']),'string','')
            end
        end 
    end
 
    function save_color(~,~)
        if ~isempty(get(gui.text2,'string'))
            %set(findobj('tag',num2str(color_number)),'backgroundcolor',color/255)
            %set(findobj('tag',[num2str(color_number),'t']),'string',['[',num2str((color/255).*rgb_type+color.*(~rgb_type)),']'])
            color_list(color_number,:)=color;%(color/255).*rgb_type+color.*(~rgb_type);
            color_number=color_number+1;
            if color_number-1>(total_page-1)*9
                page=ceil(color_number/9);
                total_page=total_page+1;
                set(gui.page,'string',[num2str(page),'/',num2str(total_page)]);
                
            end
            show_color(page)
        end
    end
 
    function delete_last(~,~)
        if color_number>=1
            %set(findobj('tag',num2str(color_number-1)),'backgroundcolor',[1 1 1])
            %set(findobj('tag',[num2str(color_number-1),'t']),'string','')
            color_list(end,:)=[];
            color_number=color_number-1;
            if color_number-2<=(total_page-2)*9
                page=ceil((color_number-1)/9);
                total_page=total_page-1;
                set(gui.page,'string',[num2str(page),'/',num2str(total_page)]);
                
            end
            show_color(page)
        end
    end
 
    function delete_pic(~,~)
        control=0;
        set(findobj('tag','picbagaxes'),...
        'xlim',[0 500],...
        'ylim',[0 500],...
        'position',[50 30 400 400],...
        'color',[0.98 0.98 0.98]);
        delete(a);
    end
 
    function clear_data(~,~)
        control=0;
        set(gui.text1,'backgroundcolor',[1 1 1]);
        set(gui.text2,'string','');
        set(findobj('tag','picbagaxes'),...
        'xlim',[0 500],...
        'ylim',[0 500],...
        'position',[50 30 400 400],...
        'color',[0.98 0.98 0.98]);
        set(findobj('tag','recc'),'string','獲取顏色');
        delete(a);
    end
 
    function get_color(~,~)
        if control==0
            set(gui.text1,'backgroundcolor',[1 1 1]);
            set(gui.text2,'string','');      
        end
        control=1;
        set(gcf,'windowbuttonmotionfcn',@whilemovefcn) 
        set(gcf,'windowbuttondownfcn',@whileclickfcn)
    end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    function whilemovefcn(~,~)
        xy=get(gca,'currentpoint');
        x=xy(1,2);y=xy(1,1);
        if x<=x_limit&&y<=y_limit&&x>=0&&y>=0
            x(x>x_limit)=x_limit;
            y(y>y_limit)=y_limit;
            x(x<1)=1;
            y(y<1)=1;
            x=round(x);
            y=round(y);
            if control==1
                color=double([ima(x,y,1),ima(x,y,2),ima(x,y,3)]);
                set(gui.text1,'backgroundcolor',color/255);
                set(gui.text2,'string',['[',num2str((color/255).*rgb_type+color.*(~rgb_type)),']']);
            end
        else
            if control==1
                set(gui.text1,'backgroundcolor',[1 1 1]);
                set(gui.text2,'string','');   
            end
        end
    end
 
    function whileclickfcn(~,~)         
            xy=get(gca,'currentpoint');
            x=xy(1,2);y=xy(1,1);
            if x<=x_limit&&y<=y_limit&&x>=0&&y>=0
                control=0;
                set(gui.text1,'backgroundcolor',color/255);
                set(gui.text2,'string',['[',num2str((color/255).*rgb_type+color.*(~rgb_type)),']']);
                set(findobj('tag','recc'),'string','繼續(xù)取色');
                %disp(color/255)
            end
    end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    function get_capture(~,~)
        screensize=get(0,'screensize');
        screensize=1.5*screensize;
        robot=java.awt.robot();
        rectangle=java.awt.rectangle();
        rectangle.x=0;
        rectangle.y=0;
        rectangle.width=screensize(3);
        rectangle.height=screensize(4);
        image=robot.createscreencapture(rectangle);
        data=image.getdata();
        temp=zeros(screensize(3)*screensize(4)*3,1);
        temp=data.getpixels(0,0,screensize(3),screensize(4),temp); 
        temp=uint8(temp);
        r=temp(1:3:end);
        g=temp(2:3:end);
        b=temp(3:3:end);
        r=reshape(r,[screensize(3),screensize(4)]);
        g=reshape(g,[screensize(3),screensize(4)]);
        b=reshape(b,[screensize(3),screensize(4)]);
        r=r';
        g=g';
        b=b';
        x_limit=screensize(4);
        y_limit=screensize(3);
        leng=max([x_limit,y_limit]);
        set(findobj('tag','picbagaxes'),...
        'xlim',[0 leng],...
        'ylim',[0 leng]);
        ima=cat(3,r,g,b);
        delete(a);
        a=imshow(ima);
    end
    function getimage(~,~)
        warning off;
        try
        [filename, pathname] = uigetfile({'*.jpg;*.tif;*.png;*.gif','all image files';...
            '*.*','all files' });
        ima = imread([ pathname,filename]);
        [x,y,~]=size(ima);
        x_limit=x;y_limit=y;
        leng=max([x_limit,y_limit]);
        set(findobj('tag','picbagaxes'),...
        'xlim',[0 leng],...
        'ylim',[0 leng]);
        delete(a);
        a=imshow(ima);
        catch
        end
    end 
end

利用Matlab提取圖片曲線(xiàn)

顏色轉(zhuǎn)換與色差計(jì)算

用 rgb 比較顏色之間的相似度時(shí),存在很大的問(wèn)題,不建議直接使用,因?yàn)橥粋€(gè)通道的一點(diǎn)改變,會(huì)導(dǎo)致最后融合在一起的顏色發(fā)生巨大變化,而如果三個(gè)通道的同時(shí)改變,卻只會(huì)使最后的明暗發(fā)生變化,色調(diào)并不會(huì)產(chǎn)生巨大變化。而這也是h系列色彩空間普遍存在的問(wèn)題。

所以,經(jīng)過(guò)思考,我決定 rgb 的值轉(zhuǎn)為 hsv 顏色空間,再進(jìn)行色差計(jì)算。

利用Matlab提取圖片曲線(xiàn)

色差計(jì)算,直接利用兩個(gè)點(diǎn)的顏色在椎體上的歐式距離,即通過(guò)計(jì)算如下坐標(biāo)點(diǎn)的歐式距離。我們通過(guò)編寫(xiě) color_dist 函數(shù)計(jì)算。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
%計(jì)算兩個(gè) hsv 顏色之間的距離
function d = color_dist(color_std,color)
[x0,y0,z0] = getpos(color_std(1),color_std(2),color_std(3));
[x,y,z] = getpos(color(1),color(2),color(3));
d = sqrt((x-x0).^2+(y-y0).^2+(z-z0)^2);
end
 
function [x,y,z] = getpos(h,s,v)
r = 1;
h = sqrt(3);
x = r*v*s*cos(h);
y = r*v*s*sin(h);
z = h*(1-v);
end

利用Matlab提取圖片曲線(xiàn)

?
1
2
3
4
5
6
7
8
9
10
%% 顏色轉(zhuǎn)為 hsv,再進(jìn)行色差計(jì)算
a2 = rgb2hsv(a);
color2 = rgb2hsv(color);
d = ones(low_num,col_num)*2;
for i=1:low_num
    for j=1:col_num
        d(i,j) = color_dist(color2,a2(i,j,:));
    end
end
mesh(d);

分離曲線(xiàn)

通過(guò)調(diào)節(jié)閾值參數(shù),可以把我們想要的坐標(biāo)軸過(guò)濾掉。閾值越小,過(guò)濾效果越明顯。

?
1
2
3
4
5
6
7
8
9
%% 根據(jù)色彩,把想要的曲線(xiàn)分離出來(lái)
threshold = 0.5;%可以調(diào)整閾值使分離效果變好
i = (d<threshold);
for i=1:3
    rgb= a(:,:,i);
    rgb(~i)=255;
    a3(:,:,i) = rgb;
end
imshow(a3)

利用Matlab提取圖片曲線(xiàn)

二值化,提取數(shù)據(jù)

二值化圖像是以矩陣形式存儲(chǔ)的。我們根據(jù)色素點(diǎn)在矩陣中的位置,利用行列指標(biāo)和坐標(biāo)軸的標(biāo)準(zhǔn)化關(guān)系,提取數(shù)據(jù),重建坐標(biāo)系。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
%% 二值化,提取數(shù)據(jù)
leftup = [0,1];%標(biāo)注截取的圖片的左上角和右下角
rightdown = [1,0];
b = rgb2gray(a3);
imshow(b);
[pos_row,pow_col] = find(b~=255);
ps = [pos_row-1,pow_col-1];
ps(:,1) = ps(:,1)./(low_num-1);
ps(:,2) = ps(:,2)./(col_num-1);
x = (rightdown(1)-leftup(1)).*ps(:,2)+leftup(1);
y = leftup(2)-abs(rightdown(2)-leftup(2)).*ps(:,1);
scatter(x,y,0.38);
x = [x,y];

利用Matlab提取圖片曲線(xiàn)

看著八九不離十了,但是注意這里的坐標(biāo)點(diǎn)是以散點(diǎn)的形式畫(huà)出來(lái)的。是無(wú)序且沒(méi)有區(qū)分度的,我們甚至無(wú)法使用plot

數(shù)據(jù)點(diǎn)分類(lèi)與排序

肉眼可見(jiàn),這幾條曲線(xiàn)是分隔開(kāi)的。我們?nèi)绾伟堰@些數(shù)據(jù)按曲線(xiàn)分開(kāi)且其上的點(diǎn)按順序排好呢?我的做法是,使用鼠標(biāo)選中你在意的曲線(xiàn)的一端,利用距離延拓法,還原整條曲線(xiàn)。

?
1
2
3
4
5
6
7
8
9
10
%% 數(shù)據(jù)分類(lèi)與排序
n = size(x,1);
gfrom = ginput(1);
[~,mini] = min(sum((x - repmat(gfrom,n,1)).^2,2));
from = x(mini,:);
x(mini,:) = [];
x = [from;x];
tol = 0.01;
x1 = findcurvepath(x,tol);
plot(x1(:,1),x1(:,2))

其中用到了一個(gè)自己寫(xiě)函數(shù) findcurvepath,它可以從一個(gè)數(shù)據(jù)點(diǎn)出發(fā),把所有的數(shù)據(jù)點(diǎn)按距離遠(yuǎn)近,像串珍珠一樣串在一塊。

?
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
function ps1 = findcurvepath(ps0,tol)
%這個(gè)函數(shù)將點(diǎn)就近連接起來(lái)
ps1(1,1:2) = ps0(1,1:2);%將第一個(gè)取出來(lái)
ps0 = ps0(2:end,:);%ps0重置為剩下的部分
p = ps1(end,:);%p是取出來(lái)的最后一個(gè)
while ~isempty(ps0)
    
    i = 1;
    while 1
        distances = p2psdistance(p,ps0);
        inds = find(distances == min(distances));%%%%%%%修復(fù)一個(gè)bug
        if length(inds) > 1
            if size(ps1,1)<i+1
                break;
            end
            p = ps1(end-i,:);%p是取出來(lái)的最后一個(gè)
            i = i+1;
        else
            break;
        end
    end
    
    distances = p2psdistance(p,ps0);
    if min(distances)>tol
        break;
    end
    inds = find(distances == min(distances));%%%%%%%修復(fù)一個(gè)bug
    ind = inds(1);
    ps1(end+1,1:2) = ps0(ind,1:2);
    ps0(ind,:) = [];
    p = ps1(end,:);
end
end
function distances = p2psdistance(p,ps)
distances = sqrt((p(1)-ps(:,1)).^2+(p(2)-ps(:,2)).^2);
end

利用Matlab提取圖片曲線(xiàn)

后話(huà)

我不喜歡做 gui,因?yàn)閬y七八糟的界面用起來(lái)確實(shí)令人心煩。另外一方面,不管是 gui 或者說(shuō)進(jìn)一步的 exe 桌面程序,都依賴(lài)于 matlab 的環(huán)境。也就是說(shuō),你要運(yùn)行代碼,就必須先裝 matlab 的環(huán)境。試想,一個(gè)安裝過(guò)和簡(jiǎn)單用過(guò) matlab 的人,都是有能力可以通過(guò)代碼修改一些參數(shù)的,那么你做 gui 不是畫(huà)蛇添足么。

到此這篇關(guān)于利用matlab提取圖片曲線(xiàn)的文章就介紹到這了,更多相關(guān)matlab提取圖片曲線(xiàn)內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/lusongno1/article/details/119744335

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美黄色免费网址 | 激情久久网| 欧美久久综合 | 国产欧美精品区一区二区三区 | 亚洲国产精品99久久久久久久久 | 精品乱子伦一区二区三区 | 日韩欧一区二区三区 | 久久亚| 玖玖久久 | 日韩精品一区二区三区在线播放 | 久久九九99 | 日韩精品日韩激情日韩综合 | 欧美成年黄网站色视频 | 国产不卡免费视频 | 岛国黄色大片 | 久久av综合 | 91久久精品国产亚洲a∨麻豆 | 中文字幕一区二区三区精彩视频 | 国内激情自拍 | 久久艹天天艹 | www.久久久 | 91色视频在线观看 | 日韩精品久久久久 | 精品在线一区二区 | 国产女人爽到高潮免费视频 | 久久综合九色综合网站 | 国产美女精品人人做人人爽 | 午夜精品久久久久久久99黑人 | 成人aaaa免费全部观看 | 91精彩视频在线观看 | 黄色小视频在线观看 | 亚洲性视频在线 | 高清视频一区 | 欧洲精品二区 | 中文字幕在线一区 | 自拍偷拍亚洲欧美 | 日韩av高清 | 欧美久久综合 | 高清三区 | 欧美日韩国产精品一区二区 | 国产九九九 |