Skip to content

Instantly share code, notes, and snippets.

View monogenea's full-sized avatar

Francisco Lima monogenea

View GitHub Profile
# Convert to RGB so we can draw edges in red
overlay = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
overlay[edges > 0] = [220, 40, 40] # red edges
fig, ax = plt.subplots(figsize=(6, 6))
ax.imshow(overlay)
ax.set_title('Canny edges overlaid (σ=1.4, T=50/150)')
ax.axis('off')
plt.tight_layout()
plt.show()
fig, axes = plt.subplots(2, 3, figsize=(14, 8))
configs = [
(1.4, 20, 60, '↑ Recall, ↑ Noise\nσ=1.4, T=20/60'),
(1.4, 50, 150, 'Balanced\nσ=1.4, T=50/150'),
(1.4, 100, 200, '↓ Recall, ↓ Noise\nσ=1.4, T=100/200'),
(0.5, 50, 150, 'Fine scale\nσ=0.5, T=50/150'),
(2.0, 50, 150, 'Coarse scale\nσ=2.0, T=50/150'),
(4.0, 50, 150, 'Very coarse scale\nσ=4.0, T=50/150'),
]
def run_canny(image, sigma, t_low, t_high, aperture=3):
"""Apply Gaussian blur then Canny edge detection."""
# Kernel size: 2 * ceil(3*sigma) + 1 ensures the kernel covers ±3σ
ksize = 2 * int(np.ceil(3 * sigma)) + 1
blurred = cv2.GaussianBlur(image, (ksize, ksize), sigmaX=sigma)
edges = cv2.Canny(blurred, threshold1=t_low, threshold2=t_high, apertureSize=aperture)
return edges
edges = run_canny(image, sigma=1.4, t_low=50, t_high=150)
# LoG: positive sigma = apply Gaussian of that std, then Laplacian
log_response = gaussian_laplace(image.astype(float), sigma=2.0)
# Zero-crossings: sign changes between neighbouring pixels
def zero_crossings(log_img):
"""Return a binary mask of zero-crossing locations."""
zc = np.zeros_like(log_img, dtype=bool)
# Check horizontal and vertical sign changes
for shift in [(0, 1), (1, 0)]:
shifted = np.roll(log_img, shift=shift, axis=(0, 1))
# Gaussian blur — sigma controlled by ksize (must be odd) and sigmaX
blurred = cv2.GaussianBlur(image, ksize=(5, 5), sigmaX=1.4)
# Sobel gradients in x and y
Gx = cv2.Sobel(blurred, cv2.CV_64F, dx=1, dy=0, ksize=3)
Gy = cv2.Sobel(blurred, cv2.CV_64F, dx=0, dy=1, ksize=3)
# Gradient magnitude
magnitude = np.sqrt(Gx**2 + Gy**2)
magnitude = (magnitude / magnitude.max() * 255).astype(np.uint8)
import cv2 as cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import data
from scipy.ndimage import gaussian_laplace
# Load a greyscale test image (uint8, values 0–255)
image = data.camera()
fig, ax = plt.subplots(figsize=(5, 5))
@monogenea
monogenea / 2-grad.R
Last active December 11, 2021 10:02
# Sun Nov 14 20:13:12 2021 ------------------------------
# Logistic regression mtcars
library(RColorBrewer)
data("mtcars")
# Determine number of iterations
niter <- 100
# Determine learning rate / step size
# Sun Nov 14 19:58:50 2021 ------------------------------
# Regression Oranges
library(RColorBrewer)
data("Orange")
# Determine number of iterations
niter <- 25
# Determine learning rate / step size
#%% Initiate processing
# Init count
count = 0
# Create new window
cv2.namedWindow('stream')
while(vid.isOpened()):
# Perform detection every 60 frames
#%% Load video capture and init VideoWriter
vid = cv2.VideoCapture('input/input.mp4')
vid_w, vid_h = int(vid.get(3)), int(vid.get(4))
out = cv2.VideoWriter('output/output.mp4', cv2.VideoWriter_fourcc(*'mp4v'),
vid.get(cv2.CAP_PROP_FPS), (vid_w, vid_h))
# Check if capture started successfully
assert vid.isOpened()