Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function emap = canny1(img, tlow, thigh, fsize, sigma)
- % INITIALIZING EMAP:
- emap = zeros(size(img));
- % CONVERTING TO GRAY SCALE:
- size(size(img))
- %img = rgb2gray(img);
- % SMOOTHING FILTER | REDUCE NOISE:
- filter = fspecial('gaussian', fsize, sigma);
- img = im2double(img);
- img = imfilter(img, filter);
- % FINDING IMAGE DERIVATIVES:
- % dx = img - [img(2:end, :); img(1,:)];
- % dy = img - [img(:,2:end) img(:,1)];
- [dx, dy] = gradient(img);
- % GRADIENT MAGNITUDE:
- magnitude = sqrt(dx.^2 + dy.^2);
- % GRADIENT DIRECTION:
- direction = atan2(dy, dx) * (180.0 / pi);
- % ROUNDING DIRECTION:
- negatives = direction < 0; % Getting the negative value locations.
- direction = direction + negatives * 180; % Correcting them.
- % SORTING DIRECTIONS USING HISTOGRAM COUNT:
- roundAngTo = [-inf, 45, 90, 135, inf];
- % W-E, NE-SW, N-S, NW-SE
- % [0-45 -> 1], [45-90 -> 2] , [90-135 -> 3], [135-180 -> 4]
- [notNeeded, newDirection] = histc(direction, roundAngTo);
- nonSupress = magnitude;
- % NON-MAXIMUM SUPPRESSION:
- for m = 2:(size(img, 1) - 1)
- for n = 2:(size(img, 2) - 1)
- switch newDirection(m,n)
- case 1
- % INTERPOLATE [W-E]:
- if((magnitude(m,n) > magnitude(m - 1, n)) || (magnitude(m,n) > magnitude(m + 1, n)))
- nonSupress(m,n) = 0;
- if(magnitude(m,n) > thigh)
- emap(m,n) = 1;
- elseif(magnitude(m,n) > tlow)
- % CHECK PERPENDICULAR:
- if((magnitude(m, n - 1) > thigh) || (magnitude(m, n + 1) > thigh))
- emap(m,n) = 1;
- end
- end
- end
- case 2
- % INTERPOLATE [NE-SW]:
- if((magnitude(m,n) > magnitude(m + 1, n - 1)) || (magnitude(m,n) > magnitude(m - 1, n + 1)))
- nonSupress(m,n) = 0;
- if(magnitude(m,n) > thigh)
- emap(m,n) = 1;
- elseif(magnitude(m,n) > tlow)
- % CHECK PERPENDICULAR:
- if((magnitude(m - 1, n - 1) > thigh) || (magnitude(m + 1, n + 1) > thigh))
- emap(m,n) = 1;
- end
- end
- end
- case 3
- % INTERPOLATE [N-S]:
- if((magnitude(m,n) > magnitude(m, n - 1)) || (magnitude(m,n) > magnitude(m, n + 1)))
- nonSupress(m,n) = 0;
- if(magnitude(m,n) > thigh)
- emap(m,n) = 1;
- elseif(magnitude(m,n) > tlow)
- % CHECK PERPENDICULAR:
- if((magnitude(m + 1, n) > thigh) || (magnitude(m - 1, n) > thigh))
- emap(m,n) = 1;
- end
- end
- end
- case 4
- % INTERPOLATE [NW-SE]:
- if((magnitude(m,n) > magnitude(m - 1, n - 1)) || (magnitude(m,n) > magnitude(m + 1, n + 1)))
- nonSupress(m,n) = 0;
- if(magnitude(m,n) > thigh)
- emap(m,n) = 1;
- elseif(magnitude(m,n) > tlow)
- % CHECK PERPENDICULAR:
- if((magnitude(m - 1, n + 1) > thigh) || (magnitude(m + 1, n - 1) > thigh))
- emap(m,n) = 1;
- end
- end
- end
- end
- end
- end
- % CONVERTING TO BINARY MASK:
- nonSupress = nonSupress > 0;
- % SHOW RESULT:
- subplot(2,3,1); imshow(img), title('Smoothed Image')
- subplot(2,3,2), imshow(magnitude), title('Magnitude');
- subplot(2,3,3), imshow(dx), title('dx');
- subplot(2,3,4), imshow(dy), title('dy');
- subplot(2,3,5), imshow(nonSupress), title('Supression');
- subplot(2,3,6), imshow(emap), title('EMAP');
- %figure; imagesc(nonSupress);
- %figure; imshow(emap,[]);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement