Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not see code convert laplace for image #6

Open
C0NGTRI123 opened this issue Sep 18, 2024 · 4 comments
Open

Not see code convert laplace for image #6

C0NGTRI123 opened this issue Sep 18, 2024 · 4 comments

Comments

@C0NGTRI123
Copy link

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
Copy link

Fyzjym commented Sep 30, 2024

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

@imqp
Copy link

imqp commented Oct 2, 2024

Same problem

@C0NGTRI123
Copy link
Author

C0NGTRI123 commented Oct 2, 2024

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
Copy link
Owner

dailenson commented Oct 8, 2024

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)

@dailenson dailenson pinned this issue Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants