Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.39 KB | None | 0 0
  1. function max_pic = find_local_max(pic,h,true_max)
  2. % Function by Jakob Lønborg Christensen s183985 F2020
  3. %
  4. % function that finds local maximum from strel h
  5. %
  6. % returns a logical matrice where 1=maxima and 0=not maxima
  7. if nargin < 3
  8. true_max = 1;
  9. end
  10. if nargin == 1
  11. if size(pic,3) == 1
  12. h = ones(3,3);
  13. else
  14. h = ones(3,3,3);
  15. end
  16. end
  17. if true_max
  18. h((numel(h)+1)/2) = 0;
  19. max_pic = imdilate(pic,strel(h))<pic;
  20. else
  21. max_pic = imdilate(pic,strel(h))==pic;
  22. end
  23. end
  24.  
  25.  
  26.  
  27.  
  28. function max_pic = find_local_min(pic,h,true_max)
  29. % Function by Jakob Lønborg Christensen s183985 F2020
  30. %
  31. % function that finds local minima from strel h
  32. %
  33. % returns a logical matrice where 1=maxima and 0=not maxima
  34. pic = -pic;
  35. if nargin < 3
  36. true_max = 1;
  37. end
  38. if nargin == 1
  39. if size(pic,3) == 1
  40. h = ones(3,3);
  41. else
  42. h = ones(3,3,3);
  43. end
  44. end
  45. if true_max
  46. h((numel(h)+1)/2) = 0;
  47. max_pic = imdilate(pic,strel(h))<pic;
  48. else
  49. max_pic = imdilate(pic,strel(h))==pic;
  50. end
  51. end
  52.  
  53. function [centers,r_vec] = find_voxel_blobs(voxel_pic,t_vec,num_blobs,find_min,find_max)
  54. if nargin == 3
  55. find_min = 1;
  56. find_max = 1;
  57. end
  58. if find_min && find_max
  59. voxel_pic_edit = abs(voxel_pic);
  60. elseif find_min
  61. voxel_pic_edit = voxel_pic;
  62. elseif find_max
  63. voxel_pic_edit = -voxel_pic;
  64. else
  65. voxel_pic_edit = voxel_pic;
  66. end
  67. max_pic = find_local_max(voxel_pic_edit);
  68. threshold = 0.4;
  69. if num_blobs < 1
  70. num_max = sum(max_pic,'all');
  71. avg_val = sum(double(max_pic).*voxel_pic_edit,'all')/num_max;
  72. thresh_val = avg_val*threshold;
  73. max_pic = (double(max_pic).*voxel_pic_edit>thresh_val);
  74. num_blobs = sum(max_pic,'all');
  75. end
  76. centers = zeros(num_blobs,2);
  77. voxel_pic_edit = voxel_pic_edit.*double(find_local_max(voxel_pic_edit));
  78. voxel_pic_vec = reshape(voxel_pic_edit,numel(voxel_pic_edit),1);
  79. [B,I] = maxk(voxel_pic_vec,num_blobs);
  80. [centers(:,1),centers(:,2),rad_idx] = ind2sub(size(voxel_pic),I);
  81. r_vec = sqrt(2*t_vec(rad_idx));
  82. end
  83.  
  84. function g = get_gauss_filt(t,order_deriv,size)
  85. % Function by Jakob Lønborg Christensen s183985 F2020
  86. %
  87. % Computes a 1d gaussian filter.
  88. %
  89. % Inputs:
  90. % t = positive real numbers. The stan. dev. of the filter
  91. % order_deriv = {0,1,2}. Number of times the derivative of the
  92. % gaussian is taken. default: 0
  93. % size = Naturals. Makes the returned vector 2*size+1 long.
  94. % default: around 3*x sd
  95. % Outputs:
  96. % g = vector of size 1xN. 1d gaussian filter kernel.
  97. %
  98. if nargin == 1
  99. order_deriv = 0;
  100. end
  101. if nargin < 3
  102. size = ceil(3*t);
  103. end
  104. x = -size:size;
  105. g = 1/sqrt(2*pi*t^2)*exp(-x.^2/(2*t));
  106. if order_deriv == 1
  107. g = g.*(-x/t);
  108. elseif order_deriv == 2
  109. g = g.*(x.^2-t)/t^2;
  110. end
  111. end
  112.  
  113. function [voxel_pic] = make_blob_voxel(pic,t_vec)
  114. voxel_pic = zeros([size(pic),length(t_vec)]);
  115. for i=1:length(t_vec)
  116. g2 = get_gauss_filt(t_vec(i),2);
  117. g0 = get_gauss_filt(t_vec(i),0);
  118. lap_1 = imfilter(pic,g2,'replicate');
  119. lap_1 = imfilter(lap_1,g0','replicate');
  120. lap_2 = imfilter(pic,g2','replicate');
  121. lap_2 = imfilter(lap_2,g0,'replicate');
  122. lap_full = lap_1 + lap_2;
  123. voxel_pic(:,:,i) = lap_full*t_vec(i)^2;
  124. end
  125. end
  126.  
  127. function pic_highlight = plot_blob_maxima(pic,t,num_blobs,lambda)
  128. if nargin == 3
  129. lambda = 0.5;
  130. end
  131. r = sqrt(2*t);
  132. centers = zeros(num_blobs,2);
  133. Idx = zeros(1,2);
  134.  
  135. pic_edit = abs(pic);
  136. pic_circle = (pic-min(pic,[],'all'))*((max(pic,[],'all')-min(pic,[],'all'))^-1);
  137.  
  138. h = fspecial('disk',r)/max(fspecial('disk',r),[],'all');
  139. for i=1:num_blobs
  140.  
  141. [~,idx] = max(pic_edit,[],'all','linear');
  142. [Idx(1),Idx(2)] = ind2sub(size(pic),idx);
  143.  
  144. pic_black = zeros(size(pic));
  145. pic_black(Idx(1),Idx(2)) = 1;
  146.  
  147. pic_black = 1-imfilter(pic_black,h);
  148. pic_circle = pic_circle.*pic_black + (1-pic_black)*(1-lambda).*pic_circle+...
  149. (1-pic_black)*lambda.*reshape([0,1,0.3],1,1,3);
  150. centers(i,:) = Idx;
  151. pic_edit = pic_edit.*pic_black;
  152. end
  153.  
  154. imshow(pic_circle)
  155. end
  156.  
  157. function pic_plot = plot_circles_on_pic(pic,centers,r_vec)
  158. lambda = 0.7;
  159. r_vec = round(r_vec);
  160. n = size(centers,1);
  161. if length(r_vec) == 1
  162. r_vec = repmat(r_vec,n,1);
  163. end
  164. color = reshape([0,1,0.3],1,1,3);
  165. pic_plot = pic;
  166. for i=1:n
  167. pic_black = zeros(size(pic));
  168. h_big = fspecial('disk',r_vec(i))/max(fspecial('disk',r_vec(i)),[],'all');
  169. h_small = fspecial('disk',r_vec(i)*0.9-1)/max(fspecial('disk',r_vec(i)*0.9-1),[],'all');
  170. pic_black(centers(i,1),centers(i,2)) = 1;
  171. pic_black = imfilter(pic_black,h_big) - imfilter(pic_black,h_small);
  172. pic_plot = pic_plot.*(1-pic_black) + pic_plot.*pic_black*(1-lambda) + pic_black*lambda.*color;
  173. end
  174. end
  175.  
  176.  
  177.  
  178.  
  179. %% WEEK 2 advanced billed analyse
  180. clc,clear
  181. DirW2 = 'C:\Users\jakob\Desktop\DTU\Videregående Billedanalyse\Week2\EX_2_data\EX_2_data\';
  182. %% ex 2.1.1 (1)
  183. t = 10;
  184. g0 = get_gauss_filt(t,0);
  185. g1 = get_gauss_filt(t,1);
  186. g2 = get_gauss_filt(t,2);
  187. plot([g0;g1;g2]')
  188. %% (2)
  189.  
  190. blob_unif = im2double(imread([DirW2,'test_blob_uniform.png']));
  191. imshow(blob_unif)
  192. %% (2) (3)
  193. t_vec = 2.^(1:8);
  194. figure
  195. for i=1:8
  196. g2 = get_gauss_filt(t_vec(i),2);
  197. g0 = get_gauss_filt(t_vec(i),0);
  198. blob_unif_filt_1 = imfilter(blob_unif,g2);
  199. blob_unif_filt_1 = imfilter(blob_unif_filt_1,g0');
  200. blob_unif_filt_2 = imfilter(blob_unif,g2');
  201. blob_unif_filt_2 = imfilter(blob_unif_filt_2,g0);
  202. blob_unif_filt = blob_unif_filt_1 + blob_unif_filt_2;
  203. subplot(2,4,i), imagesc(blob_unif_filt), title(['t=',num2str(t_vec(i))])
  204. %max(blob_unif_filt,[],'all')
  205. end
  206. %% ex 2.1.2 (1) Detecting blobs on one scale
  207.  
  208.  
  209. t_vec = 2.^((1:5)+6);
  210. figure
  211. for i=1:5
  212. g2 = get_gauss_filt(t_vec(i),2);
  213. g0 = get_gauss_filt(t_vec(i),0);
  214. blob_unif_filt_1 = imfilter(blob_unif,g2,'replicate');
  215. blob_unif_filt_1 = imfilter(blob_unif_filt_1,g0','replicate');
  216. blob_unif_filt_2 = imfilter(blob_unif,g2','replicate');
  217. blob_unif_filt_2 = imfilter(blob_unif_filt_2,g0,'replicate');
  218. blob_unif_filt = blob_unif_filt_1 + blob_unif_filt_2;
  219. subplot(2,5,i),imshow(blob_unif) , title(['t=',num2str(t_vec(i))])
  220. subplot(2,5,5+i), plot_blob_maxima(blob_unif_filt,t_vec(i),14)
  221. end
  222. %% ex 2.1.3 Detecting blobs on multiple scales
  223. pic1 = blob_unif;
  224. pic2 = im2double(imread([DirW2,'test_blob_varying.png']));
  225. pic3 = im2double(imread([DirW2,'CT_lab_high_res.png']));
  226. pic4 = pic3(300:end-300,300:end-300);
  227. g0 = get_gauss_filt(2,0);
  228. pic5 = imfilter(pic4,g0);
  229. pic5 = imfilter(pic5,g0');
  230. pic = pic2;
  231. t_vec = (8:2:64).^2/2;
  232. [voxel_pic] = make_blob_voxel(pic,t_vec);
  233. [centers,r_vec] = find_voxel_blobs(voxel_pic,t_vec,16,1,1);
  234. plot_circles_on_pic(pic,centers,r_vec)
  235. %% ex 2.1.4 Detecting blobs in real data
  236. pic = im2double(imread([DirW2,'CT_lab_high_res.png']));
  237. pic = pic(300:end-300,300:end-300);
  238. g0 = get_gauss_filt(2,0);
  239. pic = imfilter(imfilter(pic,g0),g0');
  240. %imshow(pic)
  241.  
  242. t_vec = (7:0.5:10).^2/2;
  243. [voxel_pic] = make_blob_voxel(pic,t_vec);
  244. [centers,r_vec] = find_voxel_blobs(voxel_pic,t_vec,200,0,1);
  245. plot_circles_on_pic(pic,centers,r_vec)
  246. %% ex 2.1.5 Localize blobs. show centers with gaussian filter
  247. pic = im2double(imread([DirW2,'CT_lab_high_res.png']));
  248. %pic2 = pic(300:(end-300),300:(end-300));
  249. g0 = get_gauss_filt(12,0);
  250. pic3 = imfilter(imfilter(pic,g0),g0');
  251. col = reshape([0,0.3,1],1,1,3);
  252. imshow(find_local_max(pic3).*col+pic*0.3*max(pic,[],'all')^-1)
  253. %% show resulting circles with guassian filter
  254. t_vec = (5:0.3:11).^2/2;
  255. [voxel_pic] = make_blob_voxel(pic,t_vec);
  256.  
  257. [centers,r_vec] = find_voxel_blobs(voxel_pic.*find_local_max(pic3),t_vec,-1,0,1);
  258.  
  259. imshow(pic)
  260. viscircles(centers(:,[2,1]), r_vec,'Color','g','LineWidth',0.3);
  261. %% compare centers in gaussian filter (red) and imerode+gaussian filter (green)
  262.  
  263. g0 = get_gauss_filt(12,0);
  264. pic4 = imerode(pic,fspecial('disk',3));
  265. pic5 = imfilter(imfilter(pic4,g0),g0');
  266. pic_test = pic*0.3.*reshape(ones(3,1),1,1,3);
  267. pic_test(:,:,2) = pic_test(:,:,2) + find_local_max(pic5);
  268. pic_test(:,:,1) = pic_test(:,:,1) + find_local_max(pic3);
  269. imshow(pic_test)
  270. %% show resulting circles with imerode+gaussian filter
  271. t_vec = (5:0.3:11).^2/2;
  272. [voxel_pic] = make_blob_voxel(pic,t_vec);
  273.  
  274. [centers,r_vec] = find_voxel_blobs(voxel_pic.*find_local_max(pic5),t_vec,-1,0,1);
  275.  
  276. imshow(pic)
  277. viscircles(centers(:,[2,1]), r_vec,'Color','g','LineWidth',0.3);
  278. %% load 4 best fiber pics
  279. pic_names = {'CT_lab_med_res.png','CT_synchrotron.png','Optical.png','SEM.png'};
  280. for i=1:length(pic_names)
  281. pic_list{i} = im2double(imread([DirW2,pic_names{i}]));
  282. end
  283. pic_list{4} = pic_list{4}(1:692,:);
  284. pic_list{3}(1:43,1:171) = 0;
  285. montage(pic_list)
  286.  
  287. %%
  288. erode_vec = [2,3,3,3];
  289. gauss_vec = [3,8,8,8];
  290. r_vals = [3,5.5;
  291. 3.3,7.5;
  292. 3.5,8;
  293. 5,9];
  294. for i=4
  295. name = pic_names{i};
  296. picture = pic_list{i};
  297. g0 = get_gauss_filt(gauss_vec(i),0);
  298. picture4 = imerode(picture,fspecial('disk',erode_vec(i)));
  299. picture5 = imfilter(imfilter(picture4,g0),g0');
  300. t_vec = linspace(r_vals(i,1),r_vals(i,2),12).^2/2;
  301. [voxel_pic] = make_blob_voxel(picture,t_vec);
  302.  
  303. [centers,r_vec] = find_voxel_blobs(voxel_pic.*find_local_max(picture5),t_vec,-1,0,1);
  304.  
  305. imshow(picture)
  306. viscircles(centers(:,[2,1]), r_vec,'Color','g','LineWidth',0.3);
  307. centers_list{i} = centers;
  308. r_vec_list{i} = r_vec;
  309. end
  310. %%
  311. subplot(2,2,1), imshow(picture4)
  312. subplot(2,2,2), imshow(picture*0.3+find_local_max(picture5))
  313. subplot(2,2,3), hist(r_vec,12)
  314. subplot(2,2,4), imshow(picture5*max(picture5,[],'all')^-1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement