def process(self, cv_image, mode = 'Mean', kernel_size = 3): return { 'Mean': SpellBase.to_kivy_texture(cv2.blur(cv_image,(kernel_size, kernel_size))), 'Median': SpellBase.to_kivy_texture(cv2.medianBlur(cv_image,kernel_size)), 'Mode': SpellBase.to_kivy_texture(self.modeBlur(cv_image,kernel_size)), 'Gaussian': SpellBase.to_kivy_texture(cv2.GaussianBlur(cv_image,(kernel_size,kernel_size),0)) }[mode]
def process(self, cv_image, horizontal, vertical): if horizontal and vertical: return SpellBase.to_kivy_texture(cv2.flip(cv_image,-1)) if horizontal == True: return SpellBase.to_kivy_texture(cv2.flip(cv_image,1)) if vertical == True: return SpellBase.to_kivy_texture(cv2.flip(cv_image,0)) return SpellBase.to_kivy_texture(cv_image)
def process(self, cv_image): height, width, channels = cv_image.shape b, g, r = cv2.split(cv_image) empty = np.zeros((height, width, 1), np.uint8) # Acquire Red, Green, Blue and Gray image red_img = cv2.merge((empty, empty, r)) green_img = cv2.merge((empty, g, empty)) blue_img = cv2.merge((b, empty, empty)) red_texture = SpellBase.to_kivy_texture(red_img) green_texture = SpellBase.to_kivy_texture(green_img) blue_texture = SpellBase.to_kivy_texture(blue_img) return (red_texture, green_texture, blue_texture)
def process(self, cv_image, source, target, size, cut = False): result = cv_image.copy() interest = cv_image[source[1]:source[1]+size[1], source[0]:source[0]+size[0]] if cut: result[source[1]:source[1]+size[1], source[0]:source[0]+size[0]] = (0, 0, 0) result[target[1]:target[1]+size[1], target[0]:target[0]+size[0]] = interest return SpellBase.to_kivy_texture(result)
def process(self, cv_image, factor): height,width = cv_image.shape[:2] sampled = cv_image.copy() for y in range(0, height): for x in range(0, width): sampled[y, x] = cv_image[y - y % factor, x - x % factor] return SpellBase.to_kivy_texture(sampled)
def process(self, cv_image, do_separate=True): # Thresholding gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) # Noise removal kernel = np.ones((3,3), np.uint8) opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2) # Sure background area sure_bg = cv2.dilate(opening, kernel, iterations=3) # Finding sure foreground area sure_fg = None if do_separate: dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5) ret, sure_fg = cv2.threshold(dist_transform, 0.7*dist_transform.max(), 255, 0) else: sure_fg = cv2.erode(opening, kernel, iterations=2) # Finding unknown region sure_fg = np.uint8(sure_fg) unknown = cv2.subtract(sure_bg, sure_fg) # Marker labelling ret, markers = cv2.connectedComponents(sure_fg) # Add one to all labels so that sure background is not 0 but 1 markers = markers+1 # Now mark the region of unknown with zero markers[unknown==255] = 0 # Apply watershed result = cv_image.copy() markers = cv2.watershed(cv_image, markers) result[markers == -1] = [255, 0, 0] return (SpellBase.to_kivy_texture(cv2.cvtColor(opening,cv2.COLOR_GRAY2RGB)), SpellBase.to_kivy_texture(cv2.cvtColor(unknown,cv2.COLOR_GRAY2RGB)), SpellBase.to_kivy_texture(cv2.applyColorMap(cv2.convertScaleAbs(markers), cv2.COLORMAP_JET)), SpellBase.to_kivy_texture(result))
def process(self, cv_image, mode = 'Prewitt', kernel_size = 3, direction = 'North', canny_min = 100, canny_max = 200): gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY) smoothed = cv2.GaussianBlur(gray,(5,5),0) edge = { 'Prewitt': self.prewitt(smoothed), 'Sobel': cv2.equalizeHist(cv2.Sobel(smoothed, cv2.CV_8U, 1, 1, ksize=kernel_size)), 'Canny': cv2.Canny(smoothed,canny_min, canny_max), 'Laplacian': cv2.equalizeHist(cv2.Laplacian(smoothed, cv2.CV_8U)), 'Prewitt Compass': self.prewitt2(smoothed, direction), }[mode] return SpellBase.to_kivy_texture(cv2.cvtColor(edge, cv2.COLOR_GRAY2RGB))
def process(self, cv_image, K): Z = cv_image.reshape((-1, 3)) # convert to np.float32 Z = np.float32(Z) # define criteria, number of clusters(K) and apply kmeans() criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) ret, label, center = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_PP_CENTERS) # Convert bake to uint8 and make original image center = np.uint8(center) res = center[label.flatten()] res2 = res.reshape((cv_image.shape)) return SpellBase.to_kivy_texture(res2)
def process(self, cv_image, rotation): (oldY,oldX) = cv_image.shape[:2] #note: numpy uses (y,x) convention but most OpenCV functions use (x,y) M = cv2.getRotationMatrix2D((oldX/2,oldY/2), rotation, 1) #rotate about center of image. r = np.deg2rad(rotation) newX,newY = (abs(np.sin(r)*oldY) + abs(np.cos(r)*oldX),abs(np.sin(r)*oldX) + abs(np.cos(r)*oldY)) #the warpAffine function call, below, basically works like this: # 1. apply the M transformation on each pixel of the original image # 2. save everything that falls within the upper-left "dsize" portion of the resulting image. #So I will find the translation that moves the result to the center of that region. (tx,ty) = ((newX-oldX)/2,(newY-oldY)/2) M[0,2] += tx #third column of matrix holds translation, which takes effect after rotation. M[1,2] += ty rotatedImg = cv2.warpAffine(cv_image, M, dsize=(int(newX),int(newY))) return SpellBase.to_kivy_texture(rotatedImg)
def process(self, cv_image, operation="Erosion", kernel_type="Uniform", kernel_size=3): gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY) kernel = None if kernel_type == "Rect": kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_size, kernel_size)) elif kernel_type == "Ellipse": kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kernel_size, kernel_size)) elif kernel_type == "Cross": kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (kernel_size, kernel_size)) else: kernel = np.ones((kernel_size, kernel_size), np.uint8) return { "Erosion": SpellBase.to_kivy_texture( cv2.cvtColor(cv2.erode(gray, kernel, iterations=1), cv2.COLOR_GRAY2RGB) ), "Dilation": SpellBase.to_kivy_texture( cv2.cvtColor(cv2.dilate(gray, kernel, iterations=1), cv2.COLOR_GRAY2RGB) ), "Opening": SpellBase.to_kivy_texture( cv2.cvtColor(cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel), cv2.COLOR_GRAY2RGB) ), "Closing": SpellBase.to_kivy_texture( cv2.cvtColor(cv2.morphologyEx(gray, cv2.MORPH_CLOSE, kernel), cv2.COLOR_GRAY2RGB) ), "Gradient": SpellBase.to_kivy_texture( cv2.cvtColor(cv2.morphologyEx(gray, cv2.MORPH_GRADIENT, kernel), cv2.COLOR_GRAY2RGB) ), "Top Hat": SpellBase.to_kivy_texture( cv2.cvtColor(cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, kernel), cv2.COLOR_GRAY2RGB) ), "Black Hat": SpellBase.to_kivy_texture( cv2.cvtColor(cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT, kernel), cv2.COLOR_GRAY2RGB) ), }[operation]
def process(self, cv_image, cv_mask_image): return SpellBase.to_kivy_texture(cv2.bitwise_and(cv_image, cv_image, mask = cv_mask_image))
def process(self, cv_image, map = 'Autumn'): gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY) return { 'Autumn': SpellBase.to_kivy_texture(cv2.applyColorMap(gray, cv2.COLORMAP_AUTUMN)), 'Bone': SpellBase.to_kivy_texture(cv2.applyColorMap(gray, cv2.COLORMAP_BONE)), 'Jet': SpellBase.to_kivy_texture(cv2.applyColorMap(gray, cv2.COLORMAP_JET)), 'Winter': SpellBase.to_kivy_texture(cv2.applyColorMap(gray, cv2.COLORMAP_WINTER)), 'Rainbow': SpellBase.to_kivy_texture(cv2.applyColorMap(gray, cv2.COLORMAP_RAINBOW)), 'Ocean': SpellBase.to_kivy_texture(cv2.applyColorMap(gray, cv2.COLORMAP_OCEAN)), 'Summer': SpellBase.to_kivy_texture(cv2.applyColorMap(gray, cv2.COLORMAP_SUMMER)), 'Spring': SpellBase.to_kivy_texture(cv2.applyColorMap(gray, cv2.COLORMAP_SPRING)), 'Cool': SpellBase.to_kivy_texture(cv2.applyColorMap(gray, cv2.COLORMAP_COOL)), 'HSV': SpellBase.to_kivy_texture(cv2.applyColorMap(gray, cv2.COLORMAP_HSV)), 'Pink': SpellBase.to_kivy_texture(cv2.applyColorMap(gray, cv2.COLORMAP_PINK)), 'Hot': SpellBase.to_kivy_texture(cv2.applyColorMap(gray, cv2.COLORMAP_HOT)) }[map]
def process(self, cv_image, scale): height, width = cv_image.shape[:2] resized = cv2.resize(cv_image, (int(scale[0]*width), int(scale[1]*height))) return SpellBase.to_kivy_texture(resized)