Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clearvars,
- close all,
- clc,
- addpath('dataset');
- source = '01058v.jpg';
- %source = '00876a.tif';
- %resultImg = GetColourPicture(source,'C');
- imshow(source);
- select = 0;
- aux = split(source,'.');
- [imgName, imgFormat] = deal(aux{1}, aux{2});
- img = imread(source, imgFormat);
- if select == 0
- %Cut the image
- imgCropped = ImproveImg(img);
- modifImg = cat(3, edge(imgCropped(:,:,1),'canny',0.1),...
- edge(imgCropped(:,:,2),'canny',0.1), edge(imgCropped(:,:,3),'canny',0.1));
- resultImg = CreateWithFProd(imgCropped, 1, modifImg);
- [b, g, r] = myGrayWorld(resultImg);
- correctedImg = cat(3, b*resultImg(:,:,1), g*resultImg(:,:,2), r*resultImg(:,:,3));
- imshowpair(resultImg, correctedImg, 'montage');
- imwrite(im2uint8(correctedImg) ,strcat(imgName,'_method_',select,'_color.jpg'),'jpg');
- resultImg = correctedImg;
- end
- function [factorB, factorG, factorR] = myGrayWorld(img)
- means = [mean2(img(:,:,1)), mean2(img(:,:,2)), mean2(img(:,:,3))];
- suma = sum(means);
- factors = suma./means; %valid factors (every mul by scalar will be valid too
- %---------FACTORS THAT PRODUCES EVERY MEAN UNDER 255------------
- [~, max_index] = max(means);
- factorsMean = factors./factors(max_index); %valid factors with each mean under 255
- %-----FACTORS THAT PRODUCES EVERY PIXEL VALUE UNDER 255---------
- [~, min_index] = min(means);
- factorsPx = factors./factors(min_index); %valid factors with each px under 255
- %-----------------GET OUTPUT OF THE SECONDS----------------------
- factorB = factorsPx(1); factorG = factorsPx(2); factorR = factorsPx(3);
- end
- function [ resultImg ] = CreateWithFProd( imgOrig, baseChannel, imgModif )
- %ImgOrig: Original Image, than will be used for the output
- %baseChannel: The channel that will not be moved. The static channel
- %imgModif: A modification of the img (with te same dimension that imgOrig),
- %that will be used to have the calculs (For example a convolutionedImg).
- %-------------------------- TAKING VARIABLES ---------------------------
- %emule a default parameter baseChannel = 1
- if nargin<3 || baseChannel>3
- baseChannel = 1; %Base channel declared in RGB! not BGR
- end
- if nargin<3
- imgModif = imgOrig;
- end
- tic%====================================================================
- %This constant will be used in the formula of Fourier to Convolution
- sqrtOf2pi = sqrt(2*pi);
- %The fourier fast transform of the base channel.
- fourierOfBase = fft2(double(imgModif(:,:,baseChannel)));
- %------------------------- ADJUST THE IMAGES ---------------------------
- for dim = 1:size(imgOrig,3)
- if dim ~= baseChannel
- %flips the img to compensate the intrinsic flip of the convolution
- imageFlip = flip(flip(double(imgModif(:,:,dim))), 2);
- %makes the fourier trasform of it
- fourierOfChannel = fft2(imageFlip);
- %The inverse fourier transform of the point point product of two images,
- %multiplied by the sqrt of 2pi, is equals to the convolution of two
- %images.
- ppProd = fourierOfBase.*fourierOfChannel;
- cnv = sqrtOf2pi*ifft2(ppProd);
- %In the same image the max value of convolution will be in center
- %else, this max value will aproximates the shift between 2 images
- [row, col] = find(cnv==max(max(cnv)));
- %move vertical
- imgOrig(:,:,dim) = circshift(imgOrig(:,:,dim), row, 1);
- %move horitzontal
- imgOrig(:,:,dim) = circshift(imgOrig(:,:,dim), col, 2);
- disp(sprintf('Image was shifted [%d, %d] on channel %d (Base size = %d x %d)'...
- ,row, col, dim, size(imgOrig(:,:,1),1), size(imgOrig(:,:,1),2)));
- end
- end
- toc%====================================================================
- %-------------------------- SETTING RETURN -----------------------------
- %changes from RGB to BGR space
- resultImg = cat(3, imgOrig(:,:,3), imgOrig(:,:,2), imgOrig(:,:,1));
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement