Laishram Pongthangamba Meitei commited on
Commit
7ef25c7
·
verified ·
1 Parent(s): 92b6151

Upload 3 files

Browse files
lib_image_processing/__init__.py ADDED
File without changes
lib_image_processing/contrast_brightness_lib.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+
3
+ def controller(img, brightness=255,
4
+ contrast=127):
5
+
6
+ brightness = int((brightness - 0) * (255 - (-255)) / (510 - 0) + (-255))
7
+
8
+ contrast = int((contrast - 0) * (127 - (-127)) / (254 - 0) + (-127))
9
+
10
+ if brightness != 0:
11
+
12
+ if brightness > 0:
13
+
14
+ shadow = brightness
15
+
16
+ max = 255
17
+
18
+ else:
19
+
20
+ shadow = 0
21
+ max = 255 + brightness
22
+
23
+ al_pha = (max - shadow) / 255
24
+ ga_mma = shadow
25
+
26
+ # The function addWeighted calculates
27
+ # the weighted sum of two arrays
28
+ cal = cv2.addWeighted(img, al_pha,
29
+ img, 0, ga_mma)
30
+
31
+ else:
32
+ cal = img
33
+
34
+ if contrast != 0:
35
+ Alpha = float(131 * (contrast + 127)) / (127 * (131 - contrast))
36
+ Gamma = 127 * (1 - Alpha)
37
+
38
+ # The function addWeighted calculates
39
+ # the weighted sum of two arrays
40
+ cal = cv2.addWeighted(cal, Alpha,
41
+ cal, 0, Gamma)
42
+
43
+ # # putText renders the specified text string in the image.
44
+ # cv2.putText(cal, 'B:{},C:{}'.format(brightness,
45
+ # contrast), (10, 30),
46
+ # cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
47
+
48
+ return cal
lib_image_processing/removebg_lib.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ # import matplotlib.pyplot as plt
4
+ #== Parameters =======================================================================
5
+ BLUR = 21
6
+ CANNY_THRESH_1 = 10
7
+ CANNY_THRESH_2 = 200
8
+ MASK_DILATE_ITER = 10
9
+ MASK_ERODE_ITER = 10
10
+
11
+ def get_mask(contrasted=None,canny_thr1 = 7,canny_thr2=20):
12
+ blurred = cv2.GaussianBlur(contrasted, (3, 3), 0)
13
+ edges = cv2.Canny(blurred, 7, 20)
14
+ edges = cv2.dilate(edges, None)
15
+ edges = cv2.erode(edges, None)
16
+ #-- Find contours in edges, sort by area ---------------------------------------------
17
+ contour_info = []
18
+ # _, contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
19
+ # Previously, for a previous version of cv2, this line was:
20
+ contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
21
+ # Thanks to notes from commenters, I've updated the code but left this note
22
+ for c in contours:
23
+ contour_info.append((
24
+ c,
25
+ cv2.isContourConvex(c),
26
+ cv2.contourArea(c),
27
+ ))
28
+ contour_info = sorted(contour_info, key=lambda c: c[2], reverse=True)
29
+ max_contour = contour_info[0]
30
+ mask = np.zeros(edges.shape)
31
+ cv2.fillConvexPoly(mask, max_contour[0], (255))
32
+ #-- Smooth mask, then blur it --------------------------------------------------------
33
+ mask = cv2.dilate(mask, None, iterations=MASK_DILATE_ITER)
34
+ mask = cv2.erode(mask, None, iterations=MASK_ERODE_ITER)
35
+ mask = cv2.GaussianBlur(mask, (BLUR, BLUR), 0)
36
+ # mask_stack = np.dstack([mask]*3) # Create 3-channel alpha mask
37
+ #-- Blend masked img into MASK_COLOR background --------------------------------------
38
+ # mask_stack = mask_stack.astype('float32') / 255.0 # Use float matrices,
39
+ # img = img.astype('float32') / 255.0 # for easy blending
40
+ mask_stack = mask/255.0
41
+ # gray = gray/255.0
42
+ masked = (mask_stack * contrasted) + ((1-mask_stack)) # Blend
43
+ masked = (masked).astype('uint8')
44
+ # masked = masked/255
45
+ return masked