def show_differences(image, segments, ground_classes, result_classes): image = image.copy() good = (ground_classes == result_classes) good.shape = (len(good), ) #transform nx1 matrix into vector draw_segments(image, segments[good, :], (0, 255, 0)) draw_segments(image, segments[numpy.logical_not(good), :], (0, 0, 255)) show_image_and_wait_for_key(image, "differences")
def show_differences( image, segments, ground_classes, result_classes): image= image.copy() good= (ground_classes==result_classes) good.shape= (len(good),) #transform nx1 matrix into vector draw_segments( image, segments[good,:], (0,255,0) ) draw_segments( image, segments[numpy.logical_not(good),:], (0,0,255) ) show_image_and_wait_for_key(image, "differences")
def display(self, display_before=False): copy= self.image.copy() if display_before: show_image_and_wait_for_key(copy, "image before segmentation") copy.fill( (255,255,255) ) cv2.drawContours(copy, self.contours, contourIdx=-1, color=(0,0,0)) show_image_and_wait_for_key( copy, "ContourSegmenter contours") copy= self.image.copy() draw_segments( copy, self.segments) show_image_and_wait_for_key(copy, "image after segmentation by "+self.__class__.__name__)
def display(self, display_before=False): copy = self.image.copy() if display_before: show_image_and_wait_for_key(copy, "image before segmentation") copy.fill((255, 255, 255)) cv2.drawContours(copy, self.contours, contourIdx=-1, color=(0, 0, 0)) show_image_and_wait_for_key(copy, "ContourSegmenter contours") copy = self.image.copy() draw_segments(copy, self.segments) show_image_and_wait_for_key( copy, "image after segmentation by " + self.__class__.__name__)
def display( self, display_before=False, image_override=None ): '''shows the effect of this filter''' if not image_override is None: copy= image_override else: try: copy= self.image.copy() except AttributeError: raise Exception("You need to set the Filter.image attribute for displaying") copy= BrightnessProcessor(brightness=0.6).process( copy ) s, g= self._input, self.good_segments_indexes draw_segments( copy, s[g], (0,255,0) ) draw_segments( copy, s[True-g], (0,0,255) ) show_image_and_wait_for_key( copy, "segments filtered by "+self.__class__.__name__)
def ground(self, imagefile, segments, _=None): '''asks the user to label each segment as either a character or "<" for unknown''' print '''For each shown segment, please write the character that it represents, or spacebar if it's not a character. To undo a classification, press backspace. Press ESC when completed, arrow keys to move''' i = 0 if imagefile.isGrounded(): classes = classes_from_numpy(imagefile.ground.classes) segments = imagefile.ground.segments else: classes = [BLANK_CLASS] * len( segments ) #char(10) is newline. it represents a non-assigned label, and will b filtered done = False allowed_chars = map( ord, string.digits + string.letters + string.punctuation) while not done: image = imagefile.image.copy() draw_segments(image, [segments[i]]) draw_classes(image, segments, classes) key = show_image_and_wait_for_key(image, "segment " + str(i)) if key == 27: #ESC break elif key == 8: #backspace classes[i] = BLANK_CLASS i += 1 elif key == 32: #space classes[i] = NOT_A_SEGMENT i += 1 elif key == 65361: #<- i -= 1 elif key == 65363: #-> i += 1 elif key in allowed_chars: classes[i] = unichr(key) i += 1 if i >= len(classes): i = 0 if i < 0: i = len(classes) - 1 classes = numpy.array(classes) is_segment = classes != NOT_A_SEGMENT classes = classes[is_segment] segments = segments[is_segment] classes = list(classes) classes = classes_to_numpy(classes) print "classified ", numpy.count_nonzero( classes != classes_to_numpy(BLANK_CLASS) ), "characters out of", max(classes.shape) imagefile.set_ground(segments, classes)
def display(self, display_before=False, image_override=None): '''shows the effect of this filter''' if not image_override is None: copy = image_override else: try: copy = self.image.copy() except AttributeError: raise Exception( "You need to set the Filter.image attribute for displaying" ) copy = BrightnessProcessor(brightness=0.6).process(copy) s, g = self._input, self.good_segments_indexes draw_segments(copy, s[g], (0, 255, 0)) draw_segments(copy, s[True - g], (0, 0, 255)) show_image_and_wait_for_key( copy, "segments filtered by " + self.__class__.__name__)
def ground( self, imagefile, segments, _=None ): '''asks the user to label each segment as either a character or "<" for unknown''' print '''For each shown segment, please write the character that it represents, or spacebar if it's not a character. To undo a classification, press backspace. Press ESC when completed, arrow keys to move''' i=0 if imagefile.isGrounded(): classes= classes_from_numpy( imagefile.ground.classes) segments= imagefile.ground.segments else: classes= [BLANK_CLASS]*len(segments) #char(10) is newline. it represents a non-assigned label, and will b filtered done= False allowed_chars= map( ord, string.digits+string.letters+string.punctuation ) while not done: image= imagefile.image.copy() draw_segments( image, [segments[ i ]]) draw_classes( image, segments, classes ) key= show_image_and_wait_for_key( image, "segment "+str(i)) if key==27: #ESC break elif key==8: #backspace classes[i]= BLANK_CLASS i+=1 elif key==32: #space classes[i]= NOT_A_SEGMENT i+=1 elif key==65361: #<- i-=1 elif key==65363: #-> i+=1 elif key in allowed_chars: classes[i]= unichr(key) i+=1 if i>=len(classes): i=0 if i<0: i=len(classes)-1 classes= numpy.array( classes ) is_segment= classes != NOT_A_SEGMENT classes= classes[ is_segment ] segments= segments[ is_segment ] classes= list(classes) classes= classes_to_numpy( classes ) print "classified ",numpy.count_nonzero( classes != classes_to_numpy(BLANK_CLASS) ), "characters out of", max(classes.shape) imagefile.set_ground( segments, classes )
def preprocess_with_display(image): copy = image.copy() cv2.imshow('Display', image) cv2.waitKey(0) image = cv2.GaussianBlur(image, (5, 5), 0) # Blurring the image cv2.imshow('Display', image) print "After Guassian blurring" cv2.waitKey(0) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Grey scaling the image cv2.imshow('Display', image) # Displaying the image print "After Greyscaling" cv2.waitKey(0) image = cv2.adaptiveThreshold( image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 10) ''' Thresholding the image (Converts greyscale to binary), using adaptive threshold for best results ''' cv2.imshow('Display', image) im2, contours, hierarchy = cv2.findContours( image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) ''' Finding the contours in the image''' image.fill(255) cv2.drawContours(image, contours, -1, (0, 0, 255)) ''' Drawing the contours on the image before displaying''' cv2.imshow('Display', image) print "After detecting Contours" cv2.waitKey(0) contours.reverse() segments = segments_to_numpy([cv2.boundingRect(c) for c in contours]) segments = numpy.delete(segments, 0, 0) euler_list, inner_segments = find_euler_and_inner_segments(segments, 1) segments, euler_list, central_x, central_y = segment_blocks( segments, inner_segments, euler_list) draw_segments(copy, segments) '''Draw the segments on the copy image (cant add color to greyscaled image)''' cv2.imshow('Display', copy) print "After Segmentation" cv2.waitKey(0) return image, segments, euler_list, central_x, central_y