Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. function [ resultImg ] = ImproveImg( img )
  2.  
  3. securityPx = 2;
  4. [h, ~] = size(img);
  5. hCut = uint32(floor(h/3));
  6. images = cat(3,img(1:hCut, :), img(hCut+1:hCut*2, :), img((hCut*2)+1:hCut*3, :));
  7. minMaxMatrix = zeros(2,2,3,'uint16'); %Matrix will be [xMin, xMax; yMin, yMax]
  8. %-----------------FILLING THE MIN MAX MATRIX-----------------------------
  9. for dim = 1:size(images,3)
  10. img = images(:,:,dim);
  11. edges = edge(img,'canny',0.1);
  12. %Extract the peaks...
  13. for i = 1:length(['x','y'])
  14. %With the sum of the edges probably, will detect the white rect lines (borders).
  15. sumatory = sum(edges,i);
  16. %The following code is only for extract the highest peaks of the
  17. %graphic
  18. [peaks, location] = findpeaks(sumatory);
  19. bigPeaks = peaks>((mean(peaks)+std(peaks)));
  20. borderMaxTamL = uint16(length(bigPeaks)/8);
  21. borderMaxTamR = borderMaxTamL*7;
  22. try
  23. minMaxMatrix(i,1,dim) = location(find(bigPeaks(1:borderMaxTamL), 1, 'last'))+securityPx;
  24. catch
  25. minMaxMatrix(i,1,dim) = 1;
  26. end
  27. try
  28. minMaxMatrix(i,2,dim) = location((find(bigPeaks(borderMaxTamR:end), 1, 'first')+...
  29. borderMaxTamR))-securityPx;
  30. catch
  31. minMaxMatrix(i,2,dim) = size(img,mod(i,2)+1);
  32. end
  33. %end of peaks extraction
  34. end
  35. %One more dimension filled
  36. end
  37. %-------------------------MIN MAX MATRIX FILLED-------------------------
  38.  
  39. %Now need to adjust the minMaxMatrix to have the same dimension for the 3
  40. %images
  41. %Setting L-R Borders
  42. minMaxMatrix(1,1,:) = max(minMaxMatrix(1,1,:));
  43. minMaxMatrix(1,2,:) = min(minMaxMatrix(1,2,:));
  44.  
  45. %Setting Top-Bottom borders
  46. VerticalSizes = squeeze(minMaxMatrix(2,2,:) - minMaxMatrix(2,1,:));
  47. [minSize, minIdx] = min(VerticalSizes);
  48. for dim=1:size(images,3)
  49. if dim~=minIdx
  50. difference = VerticalSizes(dim)-minSize;
  51. pxToErase = uint16(floor(single(difference)/2));
  52. if(mod(difference,2)==0) %If is pair
  53. minMaxMatrix(2,1,dim) = minMaxMatrix(2,1,dim)+pxToErase;
  54. else
  55. minMaxMatrix(2,1,dim) = minMaxMatrix(2,1,dim)+pxToErase+1;
  56. end
  57. minMaxMatrix(2,2,dim) = minMaxMatrix(2,2,dim)-pxToErase;
  58. end
  59. end
  60.  
  61. resultImg = cat(3,...
  62. images(minMaxMatrix(2,1,1):minMaxMatrix(2,2,1),minMaxMatrix(1,1,1):minMaxMatrix(1,2,1),1),...
  63. images(minMaxMatrix(2,1,2):minMaxMatrix(2,2,2),minMaxMatrix(1,1,2):minMaxMatrix(1,2,2),2),...
  64. images(minMaxMatrix(2,1,3):minMaxMatrix(2,2,3),minMaxMatrix(1,1,3):minMaxMatrix(1,2,3),3));
  65. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement