def main_ocean_wave(): if os.path.exists(outfile + "_ocean_wave_mask.png"): return imgsrc = kalgorithm.imgRead(repairfile).astype(np.float32) sky = kalgorithm.imgRead(skyfile).astype(np.float32) masksrc = kalgorithm.bgr2gray(kalgorithm.imgRead(repairmaskfile)) vline = findvline() fisher_girl_mask = 255 - kalgorithm.bgr2gray( kalgorithm.imgRead(outfile + "_fisher_girl_mask.png")) # 背景减去 渔女,剩下 海平面。 outv = masksrc - masksrc * (fisher_girl_mask / 255.0) outv[:vline, ...] = 0 kalgorithm.imgSave(outfile + "_ocean_wave_mask.png", outv) outv[:vline, ...] = 255 outv[vline:, ...] = 0 outv = outv.astype(np.float32) outv[:vline, ...] = outv[:vline, ...] * (1 - (fisher_girl_mask[:vline, ...] / 255.0)) outv = np.clip(outv, 0, 255) kalgorithm.imgSave(outfile + "_sky_mask.png", outv)
def main_replace_sky(): mask_fishergirl = outfile + "_fisher_girl_mask.png" mask_sea = outfile + "_ocean_wave_mask.png" mask_sky = outfile + "_sky_mask.png" imgsrc = kalgorithm.imgRead(repairfile).astype(np.float32) sky = kalgorithm.imgRead(skyfile).astype(np.float32) mask_sky = kalgorithm.imgRead(mask_sky).astype(np.float32) mask_sky = kalgorithm.meanFilter(mask_sky) # 均值滤波,接头处就不生硬了。 sky = kalgorithm.blInterpolate(sky, 0.33333333334, 0.33333333334) print("imgsrc", imgsrc.shape, "sky", sky.shape) # (744, 1100, 3) (618, 1100, 3) print("mask_sky", mask_sky.shape) # (744, 1100, 3) newsky = np.zeros((mask_sky.shape[0], mask_sky.shape[1], sky.shape[2]), np.uint8) newsky[:, :] = 0 newsky[:sky.shape[0], :sky.shape[1]] = sky print("newsky", newsky.shape) # 搞成一样大,才可以做乘法,合成图片。 mask_sky = mask_sky / 255 print(newsky.shape, mask_sky.shape, imgsrc.shape) finalimage = newsky * mask_sky + imgsrc * (1 - mask_sky) kalgorithm.imgSave(outfile + "_sky_cloud.png", finalimage)
def main(): inputfile = r"./input_images/phase2/phase2_broken.jpg" imgsrc = kalgorithm.imgRead(inputfile) outputfile = r"./output_images/phase2/phase2_broken_sharpen_spatial.jpg" img = kalgorithm.sharpenImage3d(imgsrc.copy()) kalgorithm.imgSave(outputfile, img)
def main_fisher_girl_mask(): if os.path.exists(outfile + "_fisher_girl_mask.png"): return imgsrc = kalgorithm.imgRead(repairfile).astype(np.float32) sky = kalgorithm.imgRead(skyfile).astype(np.float32) # 使用色彩追踪和形态学运算得到图像中感兴趣区域 # RGB > HSV mask = BGR2HSV(imgsrc) # color tracking mask = get_mask(mask) # masking out = masking(imgsrc, mask) # 把太黑的一起识别出来,认为是陆地。主要识别小岛。 out = kalgorithm.bgr2gray(out) mask = kalgorithm.thresholdOtsuBinarization(out).astype(np.float32) / 255 # closing,移除部分毛刺 mask = Morphology_Closing(mask, time=1) # 更多白区域,移除小黑点。 # opening,域女再变肥一点。 mask = Erode(mask, erodeTime=1) # masking out = masking(imgsrc, mask) #display(out) kalgorithm.imgSave(outfile + "_fisher_girl.png", out) # 把海岛准确识别出来了。 kalgorithm.imgSave(outfile + "_fisher_girl_mask.png", mask * 255)
def encode(srcInputFile, waterMarkFile, dstOutputFile): srcimg = kalgorithm.imgRead(srcInputFile) watermark = blInterpolate(kalgorithm.imgRead(waterMarkFile), 2, 2) print(watermark.shape) watermark = bgr2gray(watermark).astype(np.float) watermark = 255 - watermark watermark = thresholdOtsuBinarization(watermark) #kalgorithm.imgShow(watermark) #kalgorithm.imgShow(srcimg) WH, WW = watermark.shape IMGH, IMGW, IMGC = srcimg.shape srcimg = srcimg // 4 * 4 for y in range(WH): for x in range(WW): #print(srcimg[y, x]) if watermark[y, x]: srcimg[y, x, 0] = srcimg[y, x, 0] | 0x2 srcimg[y, x, 1] = srcimg[y, x, 1] | 0x2 srcimg[y, x, 2] = srcimg[y, x, 2] | 0x2 kalgorithm.imgSave(dstOutputFile, srcimg) cv2.imwrite(dstOutputFile, srcimg, [int(cv2.IMWRITE_JPEG_QUALITY), 100]) kalgorithm.imgShow(srcimg) return
def main(): inputfile = r"./input_images/phase2/phase2_broken.jpg" outputfile = r"./output_images/phase2/phase2_sharpen_frequency_gaussian.jpg" img = cv2.imread(inputfile) newimg = kalgorithm.gaussianHighFrequencyFilterImage(img) # https://blog.csdn.net/qq_30815237/article/details/98655630 #newimg = kalgorithm.histEqualization(newimg) # 高频滤波后一般都要直方图均衡一下 kalgorithm.imgSave(outputfile, newimg) kalgorithm.imgShow(newimg)
def main(): inputfile = r"./input_images/phase2/phase2_broken.jpg" imgsrc = kalgorithm.imgRead(inputfile) kalgorithm.pltHistAndImage(imgsrc.astype(np.uint8), "phase2_broken.Original") img = kalgorithm.histEqualization(imgsrc.copy()) outfile = r"./output_images/phase2/phase2_broken_hist_equalization.jpg" kalgorithm.imgSave(outfile, img) kalgorithm.pltHistAndImage(img, "phase2_broken.histEqualization") img = kalgorithm.histManipulation(imgsrc.copy()) outfile = r"./output_images/phase2/phase2_broken_hist_manipulation.jpg" kalgorithm.imgSave(outfile, img) kalgorithm.pltHistAndImage(img, "phase2_broken.histManipulation")
def mainfixfile(): srcfile = "./output_images/phase2/phase2_broken_nn.jpg.png" dstfile = "./output_images/phase3/phase3_repair_original.png" imgsrc = None if not os.path.exists(dstfile): img = kalgorithm.imgRead(srcfile) H, W, C = img.shape img = kalgorithm.nnInterpolateRound(img, int(H / 3), int(W / 3)) # 一上来就修复图片 # kalgorithm.imgSave(dstfile, img) from phase2_broken_repair import mainfix imgsrc = mainfix(img, dstfile, 240, onlyeasy=True) else: imgsrc = kalgorithm.imgRead(dstfile).astype(np.float32) if not os.path.exists(dstfile + ".mask.png"): out = calculateMask(imgsrc) kalgorithm.imgSave(dstfile + ".mask.png", out) # 分离出水平线 if not os.path.exists(repairmaskfile): out = kalgorithm.imgRead(dstfile + ".mask.png").astype(np.float32) out = kalgorithm.bgr2gray(out) out = morphologyErodeLine(out, 1, linelen=40) out = morphologyDilateLine(out, 1, linelen=40) kalgorithm.imgSave(dstfile + ".mask.line.png", out) # 根据水平线,矫正原图。 angle = findAngle(out) # 找到偏移角度。 print("angle", angle) imgsrc = kalgorithm.imgRead(dstfile).astype(np.float32) imgsrc = kalgorithm.affineRotation(imgsrc, angle) # 修复边缘。 while maskfill(imgsrc): pass kalgorithm.imgSave(repairfile, imgsrc) out = calculateMask(imgsrc) kalgorithm.imgSave(repairmaskfile, out) ## 计算海平面的那条线。准确分离。 out = morphologyErodeLine(out, 1, linelen=40) out = morphologyDilateLine(out, 3, linelen=80) kalgorithm.imgSave(repairmaskfile + ".mask.line.png", out)
def decode(dstOutputFile, recoverWaterMark): srcimg = kalgorithm.imgRead(dstOutputFile) #srcimg = srcimg % 4 / 3 * 255 IMGH, IMGW, IMGC = srcimg.shape for y in range(IMGH): for x in range(IMGW): a, b, c = srcimg[y, x, 0], srcimg[y, x, 1], srcimg[y, x, 2] if (a % 4 in (2, 3)) and (b % 4 in (2, 3)) and (c % 4 in (2, 3)): srcimg[y, x] = 255 else: srcimg[y, x] = 0 #srcimg = blInterpolate(srcimg, 0.2, 0.2) kalgorithm.imgShow(srcimg) kalgorithm.imgSave(recoverWaterMark, srcimg) return
def phase1_optional(inputImgPath: str, outImgPath: str): img = kalgorithm.imgRead(inputImgPath) img = 255 - img img = cropImage(img) # 剪裁 kalgorithm.imgSave(outImgPath + ".1.jpg", img) img = rotateImage(img) # 旋转 kalgorithm.imgSave(outImgPath + ".2.jpg", img) img = cropImage(img) # 再剪裁 kalgorithm.imgSave(outImgPath, img)
def mainfix(imgsrc, outputfile, threshold=250, onlyeasy=False, savemask=False): imggray = kalgorithm.bgr2gray(imgsrc).astype(np.uint8) imggray = kalgorithm.thresholdBinarization(imggray, threshold) #kalgorithm.imgShow(imggray) if savemask: kalgorithm.imgSave(outputfile + ".mask.png", imggray) if not onlyeasy: mask = imggray.copy().astype(bool, copy=False) outimg = imgsrc.copy() pyheal.inpaint(outimg, mask, 5) kalgorithm.imgSave(outputfile + ".heal.png", outimg) # 根据 mask 对原图进行临近填充 maskfill(imggray, imgsrc) kalgorithm.imgSave(outputfile, imgsrc) return imgsrc
def main(): inputfile = r"./input_images/phase2/phase2_broken.jpg" imgsrc = kalgorithm.imgRead(inputfile) # 均值滤波器 meanFilterOutput = r"./output_images/phase2/phase2_broken_mean_filter.jpg" img = kalgorithm.meanFilter(imgsrc.copy(), ksize=5) kalgorithm.imgSave(meanFilterOutput, img) # 中值滤波 medianFilterOutput = r"./output_images/phase2/phase2_broken_median_filter.jpg" img = kalgorithm.medianFilter(imgsrc.copy(), ksize=5) kalgorithm.imgSave(medianFilterOutput, img) # 高斯滤波 gaussianFilterOutput = r"./output_images/phase2/phase2_broken_gaussian_filter.jpg" img = kalgorithm.gaussianFilter(imgsrc.copy(), ksize=5) kalgorithm.imgSave(gaussianFilterOutput, img)
def maintask(xcode): # 在前面结果的基础上,继续工作。 prefile = r"./output_images/phase3/phase3_sky.jpg_sky_cloud.png" imgsrc = kalgorithm.imgRead(prefile).astype(np.float32) mask_fishergirl = inputfile + "_fisher_girl_mask.png" mask_sea = inputfile + "_ocean_wave_mask.png" mask_sky = inputfile + "_sky_mask.png" sky = kalgorithm.imgRead(skyfile).astype(np.float32) mask_sea = kalgorithm.imgRead(mask_sea).astype(np.float32) sky = kalgorithm.blInterpolate(sky, 0.33333333334, 0.33333333334) # 做一个天空的镜像 skydst = sky.copy() skyH, skyW, skyC = sky.shape for y in range(skyH): for x in range(skyW): skydst[y, x] = sky[skyH - 1 - y, skyW - 1 - x] sky = skydst print("imgsrc", imgsrc.shape, "sky", sky.shape) # (744, 1100, 3) (618, 1100, 3) print("mask_sea", mask_sea.shape) # (744, 1100, 3) # 把天空镜像搞成一样大,才可以做乘法。 newsky = np.zeros((mask_sea.shape[0], mask_sea.shape[1], sky.shape[2]), np.uint8) newsky[:, :] = 0 peak = findvline() + 1 # 前面算出来了这个分割位置 print("peak", peak) newsky[peak:, :] = sky[:newsky.shape[0] - peak, :] print("newsky", newsky.shape) #display(newsky) # 水面 云彩 倒影 mask_sea[:peak, ...] = 0 mask_sea = kalgorithm.bgr2gray(mask_sea) # 不要边缘(前面自己写的边缘修复算法存在些许缺陷) mask_sea[..., :5] = 0 mask_sea[..., mask_sea.shape[1] - 5:] = 0 # 海水搞大一点,倒影更明显一点。 if not os.path.exists(outfile + "_mask_sea.png"): mask_sea = morphologyDilate(mask_sea, 1) # 膨胀 mask_sea = morphologyDilateLine(mask_sea, 2, linelen=3) # 水平膨胀 mask_sea = kalgorithm.meanFilter2D(mask_sea, K_size=7) # 滤波 mask_sea[:peak + 1, ...] = 0 # 膨胀后的边界清理 mask_sea = kalgorithm.meanFilter2D(mask_sea) # 滤波 kalgorithm.imgSave(outfile + "_mask_sea.png", mask_sea) #display(mask_sea) mask_sea = kalgorithm.imgRead(outfile + "_mask_sea.png").astype( np.float32) / 255 print(imgsrc.shape, mask_sea.shape) outv = ((newsky * mask_sea * 0.35) + # 下面部分,天空倒影 (imgsrc * mask_sea * 0.4) + # 下面部分,原图 (imgsrc * (1 - mask_sea) * 0.8)) # 其他部分 H, W, C = outv.shape GSIZE = H GaussianMask = gaussianMask(outv, dr=GSIZE / 4) if xcode: hsv = kalgorithm.bgr2hsv(outv) print("outv", outv.shape, outv[0, 0]) for y in range(H): for x in range(W): hsv[y, x, 0] = xcode #(35*2+hsv[y, x, 0])/3 hsv[y, x, 2] = hsv[y, x, 2] * GaussianMask[y, x] outv = kalgorithm.hsv2bgr(hsv, outv) #display(outv) else: print("outv", outv.shape, outv[0, 0]) for y in range(H): for x in range(W): outv[y, x, 2] = outv[y, x, 2] * GaussianMask[y, x] outv[y, x, 1] = outv[y, x, 1] * GaussianMask[y, x] outv[y, x, 0] = outv[y, x, 0] * GaussianMask[y, x] #display(outv) # 降低画面曝光度,伽马变换 if True: # 鱼女剪影 imgsrc = imgsrc #alg.gammaCorrection(imgsrc, g=0.5) print(imgsrc.shape, imgsrc[0, 0]) hsv = kalgorithm.bgr2hsv(imgsrc) print(imgsrc.shape, imgsrc[0, 0]) H, W, C = imgsrc.shape for y in range(H): for x in range(W): hsv[y, x, 2] = hsv[y, x, 2] / 3 imgsrc = kalgorithm.hsv2bgr(hsv, imgsrc) #display(imgsrc) # 渔女剪影效果 # 背景调暗,渔女调整的更暗。 outvgril = imgsrc #alg.gammaCorrection(outv, g=0.3) mask_fishergirl = 255 - kalgorithm.imgRead(mask_fishergirl) mask_fishergirl = kalgorithm.meanFilter(mask_fishergirl) mask_fishergirl = mask_fishergirl.astype(np.float) / 255. outv = outv * (1 - mask_fishergirl) + outvgril * mask_fishergirl outv = np.clip(outv, 0, 255) # 对最终结果再微调。 if True: pass # 哎,算了,就这样吧。 #cv2.putText(outv, "{}".format(xcode), (10, 40), cv2.FONT_HERSHEY_SIMPLEX, # 1, (0, 255, 255), 1, cv2.LINE_AA) if xcode: kalgorithm.imgSave(outfile + "_glod.png", outv) else: kalgorithm.imgSave(outfile, outv)
def nnInterpolateProc(imgsrc, imgdst, scale): img = kalgorithm.nnInterpolate(imgsrc, scale, scale) kalgorithm.imgSave(imgdst, img)
def phase1(inputImgPath: str, outImgPath: str): img = kalgorithm.imgRead(inputImgPath) img = 255 - img kalgorithm.imgSave(outImgPath, img)