Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [ resultImg ] = ImproveImg( img )
- securityPx = 2;
- [h, ~] = size(img);
- hCut = uint32(floor(h/3));
- images = cat(3,img(1:hCut, :), img(hCut+1:hCut*2, :), img((hCut*2)+1:hCut*3, :));
- minMaxMatrix = zeros(2,2,3,'uint16'); %Matrix will be [xMin, xMax; yMin, yMax]
- %-----------------FILLING THE MIN MAX MATRIX-----------------------------
- for dim = 1:size(images,3)
- img = images(:,:,dim);
- edges = edge(img,'canny',0.1);
- %Extract the peaks...
- for i = 1:length(['x','y'])
- %With the sum of the edges probably, will detect the white rect lines (borders).
- sumatory = sum(edges,i);
- %The following code is only for extract the highest peaks of the
- %graphic
- [peaks, location] = findpeaks(sumatory);
- bigPeaks = peaks>((mean(peaks)+std(peaks)));
- borderMaxTamL = uint16(length(bigPeaks)/8);
- borderMaxTamR = borderMaxTamL*7;
- try
- minMaxMatrix(i,1,dim) = location(find(bigPeaks(1:borderMaxTamL), 1, 'last'))+securityPx;
- catch
- minMaxMatrix(i,1,dim) = 1;
- end
- try
- minMaxMatrix(i,2,dim) = location((find(bigPeaks(borderMaxTamR:end), 1, 'first')+...
- borderMaxTamR))-securityPx;
- catch
- minMaxMatrix(i,2,dim) = size(img,mod(i,2)+1);
- end
- %end of peaks extraction
- end
- %One more dimension filled
- end
- %-------------------------MIN MAX MATRIX FILLED-------------------------
- %Now need to adjust the minMaxMatrix to have the same dimension for the 3
- %images
- %Setting L-R Borders
- minMaxMatrix(1,1,:) = max(minMaxMatrix(1,1,:));
- minMaxMatrix(1,2,:) = min(minMaxMatrix(1,2,:));
- %Setting Top-Bottom borders
- VerticalSizes = squeeze(minMaxMatrix(2,2,:) - minMaxMatrix(2,1,:));
- [minSize, minIdx] = min(VerticalSizes);
- for dim=1:size(images,3)
- if dim~=minIdx
- difference = VerticalSizes(dim)-minSize;
- pxToErase = uint16(floor(single(difference)/2));
- if(mod(difference,2)==0) %If is pair
- minMaxMatrix(2,1,dim) = minMaxMatrix(2,1,dim)+pxToErase;
- else
- minMaxMatrix(2,1,dim) = minMaxMatrix(2,1,dim)+pxToErase+1;
- end
- minMaxMatrix(2,2,dim) = minMaxMatrix(2,2,dim)-pxToErase;
- end
- end
- resultImg = cat(3,...
- images(minMaxMatrix(2,1,1):minMaxMatrix(2,2,1),minMaxMatrix(1,1,1):minMaxMatrix(1,2,1),1),...
- images(minMaxMatrix(2,1,2):minMaxMatrix(2,2,2),minMaxMatrix(1,1,2):minMaxMatrix(1,2,2),2),...
- images(minMaxMatrix(2,1,3):minMaxMatrix(2,2,3),minMaxMatrix(1,1,3):minMaxMatrix(1,2,3),3));
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement