static IImageRegion g22_mostOverlappingRegionInOtherImage(IImageRegion region, IImageRegions regions2) { WidthAndHeight image1 = region.image(); WidthAndHeight image2 = regions2.image(); assertSameSize(image1, image2); // pixel positions in image 1 region int[] pixels = region.pixelsAsIntArray(); // region matrix of image 2 int[] regionMatrix = regions2.regionMatrix(); // count matching pixels for each image 2 region int nRegions = regions2.nRegions(); int[] counts = new[nRegions+1]; for (pos : pixels) counts[regionMatrix[pos]]++; ret regions2.getRegion(indexOfHighest(counts)); }