def SegmentImage(image, filledImage, contours): #Create a list for all the segments found in the image segments = list() #Create a collective image that has all segments imagecollective = np.zeros((image.shape[0], image.shape[1])) imagecollective[:, :] = 0.5 #Segment every contour for contour in contours: #Create new segment segment = Segment() #Get aspect ratio segment.BoundingBox = BoundingBox(np.min(contour[:, 1]), np.max(contour[:, 1]), np.min(contour[:, 0]), np.max(contour[:, 0])) segment.BoundingBox.ToInt() #Create the segment image (will be replaced with segment class) segment.CreateImage() #Copy the segment from the contour we have to the segment rr, cc = polygon(contour[:, 0], contour[:, 1], image.shape) rr = rr.astype(int) cc = cc.astype(int) segment.Image[rr - segment.BoundingBox.miny, cc - segment.BoundingBox.minx] = image[rr, cc] segment.FilledImage[rr - segment.BoundingBox.miny, cc - segment.BoundingBox.minx] = filledImage[rr, cc] imagecollective[rr, cc] = image[rr, cc] segments.append(segment) return imagecollective, segments