Exemple #1
0
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)
Exemple #2
0
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)
Exemple #3
0
def calculateMask(imgsrc):
    gray = kalgorithm.bgr2gray(imgsrc)
    fy, fx = kalgorithm.prewittFilter(gray, K_size=3)
    out1 = fy.astype(np.float32) + fx.astype(np.float32)
    fy, fx = kalgorithm.sobelFilter(gray, K_size=3)
    out2 = fy.astype(np.float32) + fx.astype(np.float32)
    out = out1 + out2
    out = np.clip(out, 0, 255)
    out = kalgorithm.thresholdOtsuBinarization(out)
    return out
Exemple #4
0
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)
Exemple #5
0
def findvline():
    outv = kalgorithm.bgr2gray(
        kalgorithm.imgRead(repairmaskfile + ".mask.line.png"))
    outv = kalgorithm.thresholdBinarization(outv)

    H, W = outv.shape

    yhistogram = np.sum(outv, axis=1)

    ymin = np.min(yhistogram)
    ymax = np.max(yhistogram)
    result = []
    for i in range(len(yhistogram)):
        if i > 5 and i < H - 5 and yhistogram[i] > 1000:
            print(i, yhistogram[i])
            result.append(i)
    print("findvline", np.mean(result))
    return int(np.mean(result))
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 findGrilCenter():
    outv = kalgorithm.bgr2gray(kalgorithm.imgRead(repairmaskfile))
    outv = kalgorithm.thresholdBinarization(outv)

    H, W = outv.shape
    xhistogram = np.sum(outv.astype(np.float32), axis=0)  # 纵向投影

    xmin = np.min(xhistogram)
    xmax = np.max(xhistogram)
    result = 0
    lastvalue = 0
    assert len(xhistogram) == W
    for i in range(len(xhistogram)):
        if i > 5 and i < W - 5 and xhistogram[i] > lastvalue:
            #print(i, xhistogram[i])
            result = i
            lastvalue = xhistogram[i]
    print("findGrilCenter", result)
    return result
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)