Spaces:
Running
Running
File size: 6,641 Bytes
6c0075d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
import os.path
import cv2
import numpy as np
from skimage import measure
def imageResize(Image, downsizeRatio):
##This program resize the original image
##Input: original image and downsizeRatio (user defined parameter: 0.75, 0.5 or 0.2)
##Output: the resized image according to the given ratio
if downsizeRatio < 1:#len(ImgFileList)
ImgResized = cv2.resize(Image, dsize=None, fx=downsizeRatio, fy=downsizeRatio)
else:
ImgResized = Image
ImgResized = np.uint8(ImgResized)
return ImgResized
def creatMask(Image, threshold = 10):
##This program try to creat the mask for the filed-of-view
##Input original image (RGB or green channel), threshold (user set parameter, default 10)
##Output: the filed-of-view mask
if len(Image.shape) == 3: ##RGB image
gray = cv2.cvtColor(Image, cv2.COLOR_BGR2GRAY)
Mask0 = gray >= threshold
else: #for green channel image
Mask0 = Image >= threshold
# ######get the largest blob, this takes 0.18s
cvVersion = int(cv2.__version__.split('.')[0])
Mask0 = np.uint8(Mask0)
contours, hierarchy = cv2.findContours(Mask0, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
areas = [cv2.contourArea(c) for c in contours]
max_index = np.argmax(areas)
Mask = np.zeros(Image.shape[:2], dtype=np.uint8)
cv2.drawContours(Mask, contours, max_index, 1, -1)
ResultImg = Image.copy()
if len(Image.shape) == 3:
ResultImg[Mask ==0] = (255,255,255)
else:
ResultImg[Mask==0] = 255
return ResultImg, Mask
def shift_rgb(img, *args):
result_img = np.empty_like(img)
shifts = args
max_value = 255
# print(shifts)
for i, shift in enumerate(shifts):
lut = np.arange(0, max_value + 1).astype("float32")
lut += shift
lut = np.clip(lut, 0, max_value).astype(img.dtype)
if len(img.shape)==2:
print(f'=========grey image=======')
result_img = cv2.LUT(img,lut)
else:
result_img[..., i] = cv2.LUT(img[...,i],lut)
return result_img
def cropImage_bak(Image, Mask):
Image = Image.copy()
Mask = Mask.copy()
leftLimit, rightLimit, upperLimit, lowerLimit = getLimit(Mask)
if len(Image.shape) == 3:
ImgCropped = Image[upperLimit:lowerLimit, leftLimit:rightLimit, :]
MaskCropped = Mask[upperLimit:lowerLimit, leftLimit:rightLimit]
ImgCropped[:20, :, :] = 0
ImgCropped[-20:, :, :] = 0
ImgCropped[:, :20, :] = 0
ImgCropped[:, -20:, :] = 0
MaskCropped[:20, :] = 0
MaskCropped[-20:, :] = 0
MaskCropped[:, :20] = 0
MaskCropped[:, -20:] = 0
else: #len(Image.shape) == 2:
ImgCropped = Image[upperLimit:lowerLimit, leftLimit:rightLimit]
MaskCropped = Mask[upperLimit:lowerLimit, leftLimit:rightLimit]
ImgCropped[:20, :] = 0
ImgCropped[-20:, :] = 0
ImgCropped[:, :20] = 0
ImgCropped[:, -20:] = 0
MaskCropped[:20, :] = 0
MaskCropped[-20:, :] = 0
MaskCropped[:, :20] = 0
MaskCropped[:, -20:] = 0
cropLimit = [upperLimit, lowerLimit, leftLimit, rightLimit]
return ImgCropped, MaskCropped, cropLimit
########################################################
###new function to get the limit for cropping.
###try to get higher speed than np.where, but not working.
def getLimit(Mask):
Mask1 = Mask > 0
colSums = np.sum(Mask1, axis=1)
rowSums = np.sum(Mask1, axis=0)
maxColSum = np.max(colSums)
maxRowSum = np.max(rowSums)
colList = np.where(colSums >= 0.01*maxColSum)[0]
rowList = np.where(rowSums >= 0.01*maxRowSum)[0]
leftLimit0 = np.min(rowList)
rightLimit0 = np.max(rowList)
upperLimit0 = np.min(colList)
lowerLimit0 = np.max(colList)
margin = 50
leftLimit = np.clip(leftLimit0-margin, 0, Mask.shape[1])
rightLimit = np.clip(rightLimit0+margin, 0, Mask.shape[1])
upperLimit = np.clip(upperLimit0 - margin, 0, Mask.shape[0])
lowerLimit = np.clip(lowerLimit0 + margin, 0, Mask.shape[0])
return leftLimit, rightLimit, upperLimit, lowerLimit
def cropImage(Image, Mask):
##This program will crop the filed of view based on the mask
##Input: orginal image, origimal Mask (the image needs to be RGB resized image)
##Output: Cropped image, Cropped Mask, the cropping limit
height, width = Image.shape[:2]
rowsMask0, colsMask0 = np.where(Mask > 0)
minColIndex0, maxColIndex0 = np.argmin(colsMask0), np.argmax(colsMask0)
minCol, maxCol = colsMask0[minColIndex0], colsMask0[maxColIndex0]
minRowIndex0, maxRowIndex0 = np.argmin(rowsMask0), np.argmax(rowsMask0)
minRow, maxRow = rowsMask0[minRowIndex0], rowsMask0[maxRowIndex0]
upperLimit = np.maximum(0, minRow - 50) #20
lowerLimit = np.minimum(maxRow + 50, height) #20
leftLimit = np.maximum(0, minCol - 50) #lowerLimit = np.minimum(maxCol + 50, width) #20
rightLimit = np.minimum(maxCol + 50, width)
if len(Image.shape) == 3:
ImgCropped = Image[upperLimit:lowerLimit, leftLimit:rightLimit, :]
MaskCropped = Mask[upperLimit:lowerLimit, leftLimit:rightLimit]
ImgCropped[:20, :, :] = 0
ImgCropped[-20:, :, :] = 0
ImgCropped[:, :20, :] = 0
ImgCropped[:, -20:, :] = 0
MaskCropped[:20, :] = 0
MaskCropped[-20:, :] = 0
MaskCropped[:, :20] = 0
MaskCropped[:, -20:] = 0
elif len(Image.shape) == 2:
ImgCropped = Image[upperLimit:lowerLimit, leftLimit:rightLimit]
MaskCropped = Mask[upperLimit:lowerLimit, leftLimit:rightLimit]
ImgCropped[:20, :] = 0
ImgCropped[-20:, :] = 0
ImgCropped[:, :20] = 0
ImgCropped[:, -20:] = 0
MaskCropped[:20, :] = 0
MaskCropped[-20:, :] = 0
MaskCropped[:, :20] = 0
MaskCropped[:, -20:] = 0
else:
pass
cropLimit = [upperLimit, lowerLimit, leftLimit, rightLimit]
return ImgCropped, MaskCropped, cropLimit
if __name__ == '__main__':
if not os.path.exists(os.path.join('../data','AV_DRIVE','test','mask')):
os.makedirs(os.path.join('../data','AV_DRIVE','test','mask'))
for file in os.listdir(os.path.join('../data','AV_DRIVE','test','images')):
# suffix file name
if file.endswith('.jpg') or file.endswith('.png'):
# read image
img = cv2.imread(os.path.join('../data','AV_DRIVE','test','images',file))
_,mask = creatMask(img)
# save mask
cv2.imwrite(os.path.join('../data','AV_DRIVE','test','mask',file),mask) |