dailenson / One-DM

Official Code for ECCV 2024 paper — One-Shot Diffusion Mimicker for Handwritten Text Generation
MIT License
298 stars 28 forks source link

Not see code convert laplace for image #6

Open C0NGTRI123 opened 2 months ago

C0NGTRI123 commented 2 months ago

I find the file to convert image to lap_ref but don't see that. You can public this file, I try to use opencv but they not same output you return

Fyzjym commented 1 month ago

Same question about the LPLS convert process. Thanks again for this great work.

imqp commented 1 month ago

Same problem

C0NGTRI123 commented 1 month ago

I have implementation LPLS convert process, but image can't not correct with dataset public but they can use. Hope the author public code

convert_laplacian.py

import cv2
import os
from tqdm import tqdm

def apply_laplacian_filter(img):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # Apply high pass filter Laplacian
    edge = cv2.Laplacian(img, -1, ksize=3, scale=1.5, delta=0.2,
                         borderType=cv2.BORDER_DEFAULT
                         )
    return edge

def binarize_image(img, threshold=128):
    _, img = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY)
    return img

def save_image(save_dir, img_dir):
    # read subdirectories
    for root, dirs, files in tqdm(os.walk(img_dir), desc="Processing directories"):
        img_ext = [".jpg", ".png", ".jpeg"]
        files = [file for file in files if file.endswith(tuple(img_ext))]
        for file in files:
            img_path = os.path.join(root, file)
            img = cv2.imread(img_path)
            high_pass_filter = apply_laplacian_filter(img)
            high_pass_filter = binarize_image(high_pass_filter)

            relative_path = os.path.relpath(root, img_dir)
            target_dir = os.path.join(save_dir, relative_path)
            os.makedirs(target_dir, exist_ok=True)

            save_path = os.path.join(target_dir, file)
            cv2.imwrite(save_path, high_pass_filter)
            # cv2.imshow(f"{os.path.basename(img_path)}", high_pass_filter)
            # cv2.waitKey(0)
            # cv2.destroyAllWindows()

if __name__ == "__main__":
    img_dir = "../data/RIMES-new"
    save_dir = img_dir.replace("-new", "_laplace")
    # img_dir = "../data/IAM64-new"
    # save_dir = img_dir + "_laplace"
    os.makedirs(save_dir, exist_ok=True)
    save_image(save_dir, img_dir)
dailenson commented 1 month ago

Thanks for your attention! We provide the Laplace image implementation below:

import torch, cv2
import torch.nn.functional as F
import numpy as np
from PIL import Image
import os

laplace = torch.tensor([[0, 1, 0],
                        [1, -4, 1],
                        [0, 1, 0]], dtype=torch.float, requires_grad=False).view(1, 1, 3, 3)

def laplace_ostu(file):
    image = cv2.imread(file, 1)
    img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    x = torch.from_numpy(img.transpose([2, 0, 1])).unsqueeze(0).float()
    y = F.conv2d(x, laplace.repeat(1, 3, 1, 1), stride=1, padding=1, )
    y = y.squeeze().numpy()
    y = np.clip(y, 0, 255)
    y = y.astype(np.uint8)
    ret, threshold = cv2.threshold(y, 0, 255, cv2.THRESH_OTSU)
    return threshold

if __name__ == '__main__':
    source_file = 'source.png'
    saved_img = 'target.png'
    threshold = laplace_ostu(source_file)
    cv2.imwrite(saved_img, threshold)