SHARE
TWEET

Untitled

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