Skip to content

Instantly share code, notes, and snippets.

View GianlucaCarlini's full-sized avatar

Gianluca Carlini GianlucaCarlini

View GitHub Profile
@GianlucaCarlini
GianlucaCarlini / corrupt_image.py
Last active January 14, 2025 14:28
Function to corrupt an image by modifying some of its patches. The image is first unfolded into a number o non overlapping patches of specified dimension. The patches are modified by either reordering them or masking them; finally, the patches are folded back to the original image. I believe this could be useful for SSL approaches
import numpy as np
import torch
import torch.nn as nn
def corrupt_image(
image: torch.Tensor,
patch_size: int = 32,
frac: float = 0.2,
mode: str = "disc",
retrun_proportion: bool = False,
@GianlucaCarlini
GianlucaCarlini / view_array_patches.py
Created June 19, 2023 20:07
Generate a view of the original array as a collection of (possibly overlapping) patches, with the option to pad the image in a Tensorflow fashion.
import numpy as np
def view_array_patches(
img: np.ndarray, patch_size: tuple, strides: tuple, padding: str = "valid"
) -> np.ndarray:
"""Generates a patched view of an array.
Args:
img (np.ndarray): The input array to be patched.
@GianlucaCarlini
GianlucaCarlini / image2graph.py
Created May 29, 2023 11:21 — forked from Nico-Curti/image2graph.py
Extract graph from skeleton image using OpenCV
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cv2
import numpy as np
from itertools import combinations
__author__ = ['Nico Curti', 'Gianluca Carlini']
__email__ = ['nico.curti2@unibo.it', 'gianluca.carlini3@unibo.it']