Advertisement
Guest User

Untitled

a guest
Sep 11th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. %%%% Justin Sutherland
  2. %%%% 9/3/2019
  3. %%%%
  4. %%%% MATLAB script for implementing the adaptive thresholding image
  5. %%%% processing algorithm with background removal/adjustment. Also - an
  6. %%%% initial test of a k-medoid clustering algorithm as a possibile
  7. %%%% canditate for a vein injection site algorithm.
  8. %%% Start by pulling desired image into suitable data type
  9. rawData1 = importdata('test_image_v2.png');
  10. [~,name] = fileparts('test_image_v2.png');
  11. image.(matlab.lang.makeValidName(name)) = rawData1;
  12. %%% Convert to grayscale image
  13. temp = image.test_image_v2(:, :, 1);
  14. [B, L] = bwboundaries(temp);
  15. cmap = gray(256);
  16. cmap(9,:) = [1,0,0];
  17. colormap(cmap); % Apply the custom colormap.
  18. imagesc(temp)
  19. title('Original Image')
  20. %%% Now perform masking operation
  21. %%% Create mask by thresholding the entire image into a binary image;
  22. %%% If above threshold -> Black; below threshold -> White
  23. %%% Adjust bias value until desired result occurs.
  24. bias = 0.2;
  25. level = graythresh(temp) - bias;
  26. mask = imbinarize(temp,level);
  27. figure
  28. imshowpair(temp,mask,'montage')
  29. title('Original Image versus Mask')
  30. %%% Now create an image that hightlights the veins as effectively as
  31. %%% possible. Use the mask to remove the remaining border that the adaptive
  32. %%% thresholding sometimes is unable to remove effectively.
  33. new_image = RemoveBackground(temp, mask);
  34. %%% now perform adaptive filtering
  35. level = adaptthresh(new_image, 0.67); %%% this 0.67 threshold is arbitrary and I am actively changing it from run to run...
  36. seg_I = imbinarize(new_image,level);
  37. seg_I = RemoveBackground(seg_I, mask);
  38. figure
  39. title('Image After Filtering')
  40. imshow(seg_I, [])
  41. %%% Now, let's do a k-medoid operation on this final image. The k-medoid
  42. %%% operation should return some n-number of centroids.
  43. %%% In order to do this, we need to make a 2D array that contains only
  44. %%% values from the image greater than a certain grayscale threshold. Let's
  45. %%% call this thresh.
  46. %%% Also, this array needs have the following data: x and y coordinates.
  47. datapoints = getPoints(seg_I, 0.5);
  48. datapoints = transpose([datapoints(2, :); datapoints(1, :)]);
  49. %%% Alright, with these datapoints, now perform the k-medoid operation.
  50. %%% Literally stole this segment of code directly from Mathworks example on
  51. %%% their website.
  52. n_medoids = 1;
  53. opts = statset('Display','final');
  54. [idx,C] = kmedoids(datapoints,n_medoids,'Distance','cityblock',...
  55. 'Replicates',5,'Options',opts);
  56. figure;
  57. plot(datapoints(:, 1), datapoints(:, 2), '.', 'LineWidth', 0.1)
  58. hold on
  59. plot(C(:,1),C(:,2),'kx',...
  60. 'MarkerSize',15,'LineWidth',3)
  61. legend('Cluster 1','Cluster 2','Centroids',...
  62. 'Location','NW')
  63. title 'Cluster Assignments and Centroids'
  64. hold off
  65. function [datapoints] = getPoints(image, thresh)
  66. %%% Function for converting image to set of 2D arrays
  67. i = 1;
  68. datapoints_x = [];
  69. datapoints_y = [];
  70. iterations = size(image);
  71. for j = 1:iterations(1)
  72. for k = 1:iterations(2)
  73. if image(j, k) < thresh
  74. datapoints_x(i) = -j;
  75. datapoints_y(i) = k;
  76. i = i + 1;
  77. end
  78. end
  79. end
  80. datapoints = [datapoints_x; datapoints_y];
  81. end
  82. function [new_image] = RemoveBackground(original, mask)
  83. image_size = size(original);
  84. if size(mask) ~= image_size
  85. 'Cannot remove background of two different image sizes'
  86. return;
  87. end
  88.  
  89. new_image = original;
  90. %%% iterate through each image, checking values vs mask. If the mask
  91. %%% value is > ~200 then make the respective pixel on the original
  92. %%% image
  93. for j = 1:(image_size(1))
  94. for k = 1:(image_size(2))
  95. if mask(j, k) < 0.5
  96. new_image(j, k) = 10;
  97. end
  98. end
  99. end
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement