I have what I believe should be a relatively easy problem to solve that I have not been able to find an elegant solution for. I need to compare two binary 2D images and count if any of the elements from either image intersect/overlap with the other.
I would like to count how many blobs intersect with quadrilaterals. A blob may intersect with multiple quadrilaterals and each intersection would count, but multiple intersections within the same quadrilateral by the same blob, only count once.
I would like to count the number of quadrilaterals not intersecting with any blobs.
I would like to count the number of blobs not intersecting with any quadrilaterals.
These are the images
I created bounding boxes based on blob images with the following:
CreatePolygon[image_] :=
Module[{BinarizedImage, BoundaryBoxesCoords, Polygons, size},
size = ImageDimensions[image];
BinarizedImage = Binarize[image];
BoundaryBoxesCoords =
Last /@ ComponentMeasurements[
MorphologicalComponents[BinarizedImage], "MinimalBoundingBox"];
Polygons =
Graphics[{Red, Polygon /@ BoundaryBoxesCoords}, ImageSize -> size,
PlotRange -> {{0, First@size}, {0, Last@size}}]]
I tried doing a count of image items by overlapping the elements and counting objects consisting of amalgamated colors, but this doesn't help with conditions where blobs intersect the same quadrilateral multiple times. Using the inverse is also not helpful in solving the other two problems as I had hoped.
CreateHits[QuadImage_, BlobImage_] :=
Module[{CompositeImage, GrayThreshold, Hits},
CompositeImage =
ColorConvert[
ImageCompose[Binarize[BlobImage], {QuadImage, .5}],
"Grayscale"];
GrayThreshold = {.6, .8};
Hits =
First@Last@
ComponentMeasurements[
MorphologicalComponents[
Binarize[CompositeImage, GrayThreshold]], "Count"]]
CreateHits[Image1, Image2]
I don't think the ideal solution lies with the approach I've tried, however, which is a visual solution. What I'd like is to extract the coordinates for each blob and each quadrilateral area and just compare these object lists to determine overlaps. E.g., Blob 3 shares coordinates with Quadrilaterals 2 & 4. I can't figure out, however, how to use the output of MorphologicalComponents to find this information to retrieve the coordinates for each object or to store each cluster as a separate item. This is bothersome because MorphologicalComponents has clearly identified those things.
I could brute force the code and create separate images for each blob and/or quadrilateral and do the comparison computation that way, but besides being much more computationally expensive it is, as I said, not particularly elegant.