The error is becauseL is the size of the image anddx is any size up to the number of connected components in your image. The two sizes are never going to be the same, hence you can't use == for that.
First, yourfind should be:
idx = find(Area >= 500);
To then remove all the objects in BW that have an area greater than 500, useismember:
Best Answer