I'm trying to extract edge from an image. I used the following algorithm.Input image(e11) which is a 512 * 512 grayscale image is also given.
The matlab code I'm written is given below
e11 = imread("lena.jpg");
e11= double(e11);
gradientim = imdilate(e11,se) - imerode(e11,se);
negativeim = imcomplement(gradientim);
bottomhatim = imclose(negativeim,se) - e11 ;
AVG = mean2(e11);
%-------Computing binary image--------
for i=1:(height)
for j=1:(width)
if(AVG > bottomhatim(i,j,:))
bottomhatim(i,j,:) = 1;
else
bottomhatim(i,j,:) = 0;
end
end
end
CC = bwconncomp(bottomhatim);
edge = bottomhatim - CC;
While doing step 7, Since the type of the connected component(CC) is 'struct' ,I'm getting an error as follows
"Undefined function 'minus' for input arguments of type 'struct'".
Is "bwconncomp" function can be used to find the largest connected region?Is there any alternate function for this?Please help me to correct this code.Thanks in advance.
You are assuming CC
is an array but it's actually a structure. Specifically, this is what the docs say about bwconncomp
:
bwconncomp Find connected components in binary image.
CC = bwconncomp(BW) returns the connected components CC found in BW.
BW is a binary image that can have any dimension. CC is a structure
with four fields:
Connectivity Connectivity of the connected components (objects).
ImageSize Size of BW.
NumObjects Number of connected components (objects) in BW.
PixelIdxList 1-by-NumObjects cell array where the kth element
in the cell array is a vector containing the linear
indices of the pixels in the kth object.
By your algorithm description, you want to find the largest connected component and subtract this from your image to get the final image stored in edge
. I recommend you use bwlabel
instead, which returns a label map that labels each distinct object with a unique ID. The second output of bwlabel
returns how many objects were detected.
I would use bwlabel
, combined with histc
to count up how many pixels belonged to each region. We would use this to determine the region that has the largest area. You'd then make a mask that consists of this object, then use this and subtract with your image to get the final output edge
.
Specifically, do this at the end of your code:
%// Do bwlabel on image
[L, num] = bwlabel(bottomhatim);
%// Count how many values belong to each object, ignoring background
counts = histc(L(:), 1:num);
%// Find which object gives us the largest area
[~,max_id] = max(counts);
%// Make a mask and then subtract
CC = L == max_id;
edge = imsubtract(bottomhatim, CC);