How to do adaptive thresholding with the input as the ouput of morphological closing operation in the previous stage.
2 views (last 30 days)
Show older comments
I need to pass the output of the morhological closing operation as input to local adaptive thresholding in the next stage.But if I do so, I get error like it could'nt accept this input image type. Can anyone please help me in writing the code to do the same. I have attached my
clc;
k = 0.04;
Threshold = 50000;
sigma = 1;
halfwid = sigma * 3;
[xx, yy] = meshgrid(-halfwid:halfwid, -halfwid:halfwid);
Gxy = exp(-(xx .^ 2 + yy .^ 2) / (2 * sigma ^ 2));
Gx = xx .* exp(-(xx .^ 2 + yy .^ 2) / (2 * sigma ^ 2));
Gy = yy .* exp(-(xx .^ 2 + yy .^ 2) / (2 * sigma ^ 2));
I = imread('Frame 0002.png');
numOfRows = size(I, 1);
numOfColumns = size(I, 2);
% 1) Compute x and y derivatives of image
Ix = conv2(Gx, I);
Iy = conv2(Gy, I);
%size(Ix)
% 2) Compute products of derivatives at every pixel
Ix2 = Ix .^ 2;
Iy2 = Iy .^ 2;
Ixy = Ix .* Iy;
% 3)Compute the sums of the products of derivatives at each pixel
Sx2 = conv2(Gxy, Ix2);
Sy2 = conv2(Gxy, Iy2);
Sxy = conv2(Gxy, Ixy);
im = zeros(numOfRows, numOfColumns);
for x=1:numOfRows,
for y=1:numOfColumns,
x,y
% 4) Define at each pixel(x, y) the matrix H
H = [Sx2(x, y) Sxy(x, y); Sxy(x, y) Sy2(x, y)];
% 5) Compute the response of the detector at each pixel
R = min(Sx2(x,y),Sy2(x,y));
% 6) Threshold on value of R
if (R > Threshold)
im(x, y) = R;
end
end
end
% 7) Compute nonmax suppression
output = im > imdilate(im, [1 1 1; 1 0 1; 1 1 1]);
figure, imshow(I);
figure, imshow(output);
%p=imread('Frame 0002.png');
se = strel('disk',20);
out=imdilate(output,se);
figure, imshow(out);
Ioc = imclose(output,se);
figure, imshow(Ioc);
T = adaptthresh(Ioc,0.4,'ForegroundPolarity','dark');
figure
imshow(T)
BW = imbinarize(Ioc,T);
figure
imshow(BW)
code here.
0 Comments
Answers (1)
Image Analyst
on 12 Jan 2019
In this line:
T = adaptthresh(Ioc,0.4,'ForegroundPolarity','dark');
Ioc is a logical (binary) image. It makes no sense whatsoever to do any kind of thresholding on a binary image. It's already BEEN thresholded.
Re-think your algorithm.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!