def generateMaskByScaling(imgPath, maskImgPath, dstImgPath, dstMaskImgPath, rStart=0.2, rStop=2, N=20): createPath(dstImgPath) createPath(dstMaskImgPath) for i in listFile(imgPath): imgFile = getFileName(i) img = loadImg(i) # H,W = getImgHW(img) name = imgFile[:imgFile.rfind('.')] maskImgFile = maskImgPath + '\\' + name + '_mask' + '.png' maskImg = loadImg(maskImgFile) # print(i,imgFile,maskImgFile) # start generate new images ratios = np.linspace(rStart, rStop, N) for ratio in ratios: # print('ratio=',ratio) rImg = resizeRtImg(img, ratio) newImgFile = name + '_scale_' + str(ratio) writeImg(rImg, dstImgPath + '\\' + newImgFile + '.png') rMaskImg = resizeRtImg(maskImg, ratio) newMaskImgFile = newImgFile + '_mask' writeImg(rMaskImg, dstMaskImgPath + '\\' + newMaskImgFile + '.png')
def _augmentByGammaCorrection(self, numbers): for gamma in np.linspace(0.1, 5, numbers): # gamma = gamma.round(2) newName = r'/' + self.imgName + 'gamma' + '_' + str(gamma) + '.png' print('newName=', newName) writeImg(GammaCorrection(self.image.copy(), gamma), self.dstImgPath + newName)
def handleMaskLabel(maskImgPath): # multity labels all change to 1 for i in listFile(maskImgPath): img = loadImg(i) # c = np.unique(img) # print(len(c),c) img = np.where(img != 0, 1, 0) # c = np.unique(img) # print(len(c),c) writeImg(img, i)
def _augmentByBlur(self, numbers, fun, funName): for i in range(numbers): size = i + 2 if funName == 'gaussianBlurImg' or funName == 'medianBlurImg': if size % 2 == 0: continue img = fun(self.image.copy(), ksize=size) newName = r'/' + self.imgName + '_' + funName + '_s' + str( size) + '.png' print('newName=', newName) writeImg(img, self.dstImgPath + newName)
def copyNewFile(path): print('start to copy...') for f in listFile(path): name_ = getFileName(f) newName = name_[:name_.rfind('.')] newFile = imgPath + '\\' + newName + '.png' newMaskFile = maskImgPath + '\\' + newName + '_mask' + '.png' newAnnoFile = annotPath + '\\' + newName + '.txt' print(newFile, newMaskFile) copyFile(f, newFile) copyFile(fAnnot, newAnnoFile) writeImg(maskImg, newMaskFile)
def main(): args = cmd_line() src = args.src dst = args.dst createPath(dst) print('src=', src, 'dst=', dst) for i in pathsFiles(src, 'jpg'): # png fileName = getFileName(i) img = loadImg(i) dstFile = args.dst + '\\' + fileName print(i, fileName, dstFile) predImg = getPredictionMaskImg(img) writeImg(predImg, dstFile)
def _augmentByBrightnessAndContrast(self, numbers): beta = 50 for alpha in np.linspace(0.2, 2, numbers): newName = r'/' + self.imgName + 'alphaBeta' + '_' + str( alpha) + '_' + str(beta) + '.png' print('newName=', newName) img = adjustBrightnessAndContrast(self.image.copy(), alpha, beta) writeImg(img, self.dstImgPath + newName) alpha = 0.6 for beta in np.linspace(10, 100, numbers): newName = r'/' + self.imgName + 'alphaBeta' + '_' + str( alpha) + '_' + str(beta) + '.png' print('newName=', newName) img = adjustBrightnessAndContrast(self.image.copy(), alpha, beta) writeImg(img, self.dstImgPath + newName)
def testFileLabel(imgPath, LabelPath, dstRecImgPath): createPath(dstRecImgPath) for i in listFile(imgPath): imgFile = getFileName(i) img = loadImg(i) H, W = getImgHW(img) print(i, imgFile, H, W) labelFile = LabelPath + '\\' + imgFile[:imgFile.rfind('.')] + '.txt' # labels = getLabelFileLabels(labelFile) # print(labels) coordinats = getCoordinatesFromLabels(H, W, labelFile) # print(coordinats) recImg = rectangleImgFromCoordinates(img.copy(), coordinats) destFile = dstRecImgPath + '\\' + imgFile[:imgFile.rfind('.')] + '_rec' + '.png' print(destFile) writeImg(recImg, destFile)
def generateMaskByClipping(imgPath, maskImgPath, annotPath, dstImgPath, dstMaskImgPath, N=10): createPath(dstImgPath) createPath(dstMaskImgPath) for i in listFile(imgPath): imgFile = getFileName(i) fAnnot = getImgAnnotFile(annotPath, i) img = loadImg(i) H, W = getImgHW(img) # print(fAnnot,H,W) name = imgFile[:imgFile.rfind('.')] maskImgFile = maskImgPath + '\\' + name + '_mask' + '.png' maskImg = loadImg(maskImgFile) coordinates = getFileCoordinates(fAnnot) boundCoordinate = getBoundaryCoordinate(coordinates) print(i, H, W, coordinates, boundCoordinate) clipXmin = np.random.randint(boundCoordinate[0], size=N) clipYmin = np.random.randint(boundCoordinate[1], size=N) clipXmax = np.random.randint(W - boundCoordinate[2], size=N) clipYmax = np.random.randint(H - boundCoordinate[3], size=N) for Xmin, YMin, Xmax, Ymax in zip(clipXmin, clipYmin, clipXmax, clipYmax): clipCoordinate = (Xmin, YMin, Xmax + boundCoordinate[2], Ymax + boundCoordinate[3]) clipImg, _ = clipImgCoordinate(img.copy(), clipCoordinate, coordinates) # print(coordinates,clipCoordinate,newCoordinates,'NewH=',clipImg.shape[0],'NewW=',clipImg.shape[1]) clipImgName = imgFile[:imgFile.rfind('.')] + '_' + str( clipCoordinate[0]) + '_' + str(clipCoordinate[1]) + '_' + str( clipCoordinate[2]) + '_' + str(clipCoordinate[3]) # print(clipImgName) writeImg(clipImg, dstImgPath + '\\' + clipImgName + '.png') clipMaskImg, _ = clipImgCoordinate(maskImg.copy(), clipCoordinate, coordinates) writeImg(clipMaskImg, dstMaskImgPath + '\\' + clipImgName + '_mask' + '.png')
def generateMaskByFlipping(imgPath, maskImgPath, dstImgPath, dstMaskImgPath): createPath(dstImgPath) createPath(dstMaskImgPath) for i in listFile(imgPath): imgFile = getFileName(i) img = loadImg(i) # H,W = getImgHW(img) name = imgFile[:imgFile.rfind('.')] maskImgFile = maskImgPath + '\\' + name + '_mask' + '.png' maskImg = loadImg(maskImgFile) # print(i,imgFile,maskImgFile) rImg = flipImg(img) newImgFile = name + '_flip' writeImg(rImg, dstImgPath + '\\' + newImgFile + '.png') rMaskImg = flipImg(maskImg) newMaskImgFile = newImgFile + '_mask' writeImg(rMaskImg, dstMaskImgPath + '\\' + newMaskImgFile + '.png')
def _augmentRgbChn(self): chns = ['b', 'g', 'r'] for i, img in enumerate(cv2.split(self.image)): newName = r'/' + self.imgName + '_' + chns[i] + '.png' print('newName=', newName) writeImg(img, self.dstImgPath + newName)