Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. clearvars,
  2. close all,
  3. clc,
  4. addpath('dataset');
  5.  
  6.  
  7. source = '01058v.jpg';
  8. %source = '00876a.tif';
  9. %resultImg = GetColourPicture(source,'C');
  10. imshow(source);
  11.  
  12. select = 0;
  13.  
  14. aux = split(source,'.');
  15. [imgName, imgFormat] = deal(aux{1}, aux{2});
  16. img = imread(source, imgFormat);
  17.  
  18. if select == 0
  19. %Cut the image
  20. imgCropped = ImproveImg(img);
  21. modifImg = cat(3, edge(imgCropped(:,:,1),'canny',0.1),...
  22. edge(imgCropped(:,:,2),'canny',0.1), edge(imgCropped(:,:,3),'canny',0.1));
  23. resultImg = CreateWithFProd(imgCropped, 1, modifImg);
  24.  
  25.  
  26. [b, g, r] = myGrayWorld(resultImg);
  27. correctedImg = cat(3, b*resultImg(:,:,1), g*resultImg(:,:,2), r*resultImg(:,:,3));
  28. imshowpair(resultImg, correctedImg, 'montage');
  29. imwrite(im2uint8(correctedImg) ,strcat(imgName,'_method_',select,'_color.jpg'),'jpg');
  30. resultImg = correctedImg;
  31. end
  32.  
  33.  
  34. function [factorB, factorG, factorR] = myGrayWorld(img)
  35. means = [mean2(img(:,:,1)), mean2(img(:,:,2)), mean2(img(:,:,3))];
  36. suma = sum(means);
  37. factors = suma./means; %valid factors (every mul by scalar will be valid too
  38. %---------FACTORS THAT PRODUCES EVERY MEAN UNDER 255------------
  39. [~, max_index] = max(means);
  40. factorsMean = factors./factors(max_index); %valid factors with each mean under 255
  41. %-----FACTORS THAT PRODUCES EVERY PIXEL VALUE UNDER 255---------
  42. [~, min_index] = min(means);
  43. factorsPx = factors./factors(min_index); %valid factors with each px under 255
  44. %-----------------GET OUTPUT OF THE SECONDS----------------------
  45. factorB = factorsPx(1); factorG = factorsPx(2); factorR = factorsPx(3);
  46. end
  47.  
  48.  
  49. function [ resultImg ] = CreateWithFProd( imgOrig, baseChannel, imgModif )
  50. %ImgOrig: Original Image, than will be used for the output
  51. %baseChannel: The channel that will not be moved. The static channel
  52. %imgModif: A modification of the img (with te same dimension that imgOrig),
  53. %that will be used to have the calculs (For example a convolutionedImg).
  54. %-------------------------- TAKING VARIABLES ---------------------------
  55. %emule a default parameter baseChannel = 1
  56. if nargin<3 || baseChannel>3
  57. baseChannel = 1; %Base channel declared in RGB! not BGR
  58. end
  59. if nargin<3
  60. imgModif = imgOrig;
  61. end
  62. tic%====================================================================
  63. %This constant will be used in the formula of Fourier to Convolution
  64. sqrtOf2pi = sqrt(2*pi);
  65. %The fourier fast transform of the base channel.
  66. fourierOfBase = fft2(double(imgModif(:,:,baseChannel)));
  67.  
  68. %------------------------- ADJUST THE IMAGES ---------------------------
  69. for dim = 1:size(imgOrig,3)
  70. if dim ~= baseChannel
  71. %flips the img to compensate the intrinsic flip of the convolution
  72. imageFlip = flip(flip(double(imgModif(:,:,dim))), 2);
  73. %makes the fourier trasform of it
  74. fourierOfChannel = fft2(imageFlip);
  75. %The inverse fourier transform of the point point product of two images,
  76. %multiplied by the sqrt of 2pi, is equals to the convolution of two
  77. %images.
  78. ppProd = fourierOfBase.*fourierOfChannel;
  79. cnv = sqrtOf2pi*ifft2(ppProd);
  80. %In the same image the max value of convolution will be in center
  81. %else, this max value will aproximates the shift between 2 images
  82. [row, col] = find(cnv==max(max(cnv)));
  83. %move vertical
  84. imgOrig(:,:,dim) = circshift(imgOrig(:,:,dim), row, 1);
  85. %move horitzontal
  86. imgOrig(:,:,dim) = circshift(imgOrig(:,:,dim), col, 2);
  87.  
  88. disp(sprintf('Image was shifted [%d, %d] on channel %d (Base size = %d x %d)'...
  89. ,row, col, dim, size(imgOrig(:,:,1),1), size(imgOrig(:,:,1),2)));
  90. end
  91. end
  92. toc%====================================================================
  93. %-------------------------- SETTING RETURN -----------------------------
  94. %changes from RGB to BGR space
  95. resultImg = cat(3, imgOrig(:,:,3), imgOrig(:,:,2), imgOrig(:,:,1));
  96. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement