def __find_objects_of_masks(self, masks, visualize=False): """ Searches for objects in a list of image masks.""" # contains all previous masks. Is initialized completely black. previousMasks = np.zeros_like(masks[0]) maskNumber = 1 objects = [] for mask in masks: # subtract the previous masks from this mask, so that we do not get any duplicate areas. mask = mask - previousMasks # are there any remaining parts if utils.is_image_black(mask): continue # add the new feature to previousMasks previousMasks = cv.bitwise_or(previousMasks, mask) # apply edge detection mask = self.apply_edge_detection(mask, iterations=2, showSteps=False, imageConversion=cv.COLOR_GRAY2RGB) # search for objects in the mask utils.display_images([mask], None, titles=["Mask " + str(maskNumber)], rows=1, columns=1, show=visualize) for roi, roiImg, area in self.yield_image_objects(mask, areaThreshold=1000): utils.display_images([roiImg], titles=["Object Mask" + str(maskNumber) + "Area: " + str(area)], rows=1, columns=1, show=visualize) objects.append((roi, roiImg)) maskNumber += 1 return objects
def __color_filter_image(self, colors, visualize=False): """ Tries to find objects in the image using color filtering by applying the color-list. Keyword arguments: colors -- the colors to filter visualize -- show the filtering process returns: image masks """ hsvImage = cv.cvtColor(self.originalImage, cv.COLOR_BGR2HSV) resultImage = None masks = [] titles = [] for color in colors: # convert color from RGB tuple to np HSV array color = np.uint8([[color]]) hsvColor = cv.cvtColor(color, cv.COLOR_RGB2HSV) # define the thresholds for the color values hue = hsvColor[0][0][0] thLower = max(0, hue - 10) thUpper = min(179, hue + 10) thLowerColor = np.array([thLower, 100, 100]) thUpperColor = np.array([thUpper, 255, 255]) # threshold image with the color mask = cv.inRange(hsvImage, thLowerColor, thUpperColor) masks.append(mask) titles.append("Filter RGB: {0},{1},{2}".format(color[0][0][0], color[0][0][1], color[0][0][2])) if resultImage is None: resultImage = mask else: resultImage = cv.bitwise_or(resultImage, mask) # use the mask to bring out only the elements with the specified color #cv.imshow('mask',mask) #cv.imshow('res',resultImage) cv.imwrite("C:/Users/felix/Desktop/Segmentation/results/resultsMask_{0}{1}{2}.jpg".format(color[0][0][0], color[0][0][1], color[0][0][2]), mask) cv.imwrite("C:/Users/felix/Desktop/Segmentation/results/resultsMaskResult_{0}{1}{2}.jpg".format(color[0][0][0], color[0][0][1], color[0][0][2]), mask) if visualize: # apply masks images = [cv.cvtColor(cv.bitwise_and(self.originalImage, self.originalImage, mask=m), cv.COLOR_BGR2RGB) for m in masks] # append filtered image images.append(cv.cvtColor(cv.bitwise_and(self.originalImage, self.originalImage, mask=resultImage), cv.COLOR_BGR2RGB)) titles.append("combined images") images.extend(masks) titles.extend(["Image Mask " + str(i) for i in xrange(len(masks))]) utils.display_images(images, None, titles) return masks
def create_feature_vector(self, image): """ Creates the feature vector out of histograms.""" # convert image depending on desired color space for the histogram temp = [] colors = ("b", "g", "r") try: if Settings.H_COLOR_SPACE == ColorSpace.HSV: temp = cv.cvtColor(image, cv.COLOR_BGR2HSV) colors = ("h", "s", "v") elif Settings.H_COLOR_SPACE == ColorSpace.RGB: temp = cv.cvtColor(image, cv.COLOR_BGR2RGB) colors = ("r", "g", "b") else: temp = image except: logging.exception("Could not convert image to {0}.".format( Settings.H_COLOR_SPACE)) cv.imshow("Error CV", image) utils.display_images([image], None, ["Error"], 1, 1) # split image into x different parts. imageParts = [] height, width = temp.shape[:2] partHeight = int(height / self.imageRows) partWidth = int(width / self.imageCols) scaleMaxPossibleValue = partWidth * partHeight # modify height and width in case partHeight and partWidth don't add up to the real width and height. # in this case the image would be cut into more than x parts because some leftover pixels would be included. height = int(partHeight * self.imageRows) width = int(partWidth * self.imageCols) for y in xrange(0, height, partHeight): for x in xrange(0, width, partWidth): imageParts.append( utils.crop_image(temp, x, y, partWidth, partHeight)) histogram = [] for img in imageParts: for i, color in enumerate(colors): hist = cv.calcHist([img], [i], None, [Settings.H_BINS], Settings.H_COLOR_RANGE) if Settings.H_SCALE_HIST: # max possible value is w * h of imagePart hist /= scaleMaxPossibleValue histogram.extend(hist) return np.array(np.concatenate(histogram))
def segment_image(self): """ Menu for image segmentation.""" choice = utils.menue("[Image Segmentation", ["Segment imgage", "Learn custom marker"], showBackToMain=True) if choice == 2: self.learn_marker() return elif choice == 3: return im = load_image(utils.get_path_via_file_ui(), 1, 1) im = utils.equalize_image_size(im, 564000) seg = ObjectSegmentation() seg.process_image(im) im = seg.visulize_regions() cv.imwrite(utils.get_output_path() + "resultsMarker_regions.jpg", im) try: plt.imshow(cv.cvtColor(im, cv.COLOR_BGR2RGB), 'gray') plt.xticks([]), plt.yticks( []) # to hide tick values on X and Y axis plt.show() utils.save_plt_figure(plt, "image_segments") except: print "Could not plot / show image. Saving instead." utils.save_plt_figure(plt, "image_segments") colorSeg = ColorSegmentation() colorSeg.process_image(seg.get_meal_image()) resultImg, images, titles = colorSeg.visulize_regions(seg) # if matplotlib does not work use cv for i in range(len(images)): cv.imwrite( "C:/Users/felix/Desktop/Segmentation/results/resultsMarker_regionsArea{0}.jpg" .format(titles[i]), images[i]) cv.imshow("Area {0}".format(titles[i]), images[i]) cv.imshow("Result", resultImg) cv.imwrite( "C:/Users/felix/Desktop/Segmentation/results/resultsMarker_resultsImage.jpg", resultImg) cv.waitKey(0) # matplotlib utils.display_images([resultImg], titles="Result", columns=1, rows=1) utils.display_images(images, titles=titles)
def choose_image(self, segmentIndex, grayscale=True, classes=[], resizeFactor=1): """ Gives the user the possibility to choose from a set of images. """ if segmentIndex not in self.segmentRatio: raise AttributeError(segmentIndex + " is not in segments") if not classes: classes = self.classes #Load flag for cv.imread. loadFlag = cv.IMREAD_GRAYSCALE if grayscale else cv.IMREAD_UNCHANGED # select class class_ = classes[randint(0, len(classes) - 1)] images = [] number = 1 images = [img for img, _ in self.load_data(segmentIndex, 1, [class_], grayscale, resizeFactor, False)] titles = range(1, len(images) + 1) print "Choose the image to select, close the dialog and enter the number." imgNumber = 0 while(True): utils.display_images(images, titles=titles, columns=8) input = raw_input("Pick image number or type 'again' to see them again: ") if input == "again": continue else: try: imgNumber = int(input) except: print "Please insert a valid number." continue if imgNumber <= 0: print "Please insert a valid number bigger than zero." continue else: break return images[imgNumber - 1]
def __sort_image_masks(self, masks, visualize=False): """ Sorts and filters the image masks based on the area (mask sum) of the mask region.""" # create a new list with the sum of the mask and the mask itself. # a higher sum means a mask with more white on it. sumsOfMasks = [(np.sum(mask), mask) for mask in masks] # eliminate (almost) completely black masks sumsOfMasks = [item for item in sumsOfMasks if not utils.is_image_black(None, imageSum=item[0])] # sort the sumOfMasks list by ordering the mask sum # so that masks with small white areas appear first. sumsOfMasks = sorted(sumsOfMasks, key=operator.itemgetter(0), reverse=False) # Show sorted masks tempList = [(AREAS), (MASKS)] titles, masks = ([t for t, _ in sumsOfMasks], [m for _, m in sumsOfMasks]) utils.display_images(masks, None, titles, show=visualize) # since all masks are grayscale no conversion needed return masks