I'm using the python function bellow to deskew an image. It's not general (it works with…
- User
I'm using the python function bellow to deskew an image. It's not general (it works with some cases not with others). The function:
import numpy as np def deskew_with_min_area_rect(img): # Step 1: Convert to grayscale to compute angle gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) gray = gray.astype(np.uint8) # Step 2: Threshold and find the angle _, thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU) coords = np.column_stack(np.where(thresh > 0)) angle = cv.minAreaRect(coords)[-1] # Step 3: Fix the angle if angle < -45: angle = -(90 + angle) else: angle = -angle # Step 4: Get rotation matrix and rotate the original color image (h, w) = img.shape[:2] center = (w // 2, h // 2) M = cv.getRotationMatrix2D(center, angle, 1.0) rotated = cv.warpAffine(img, M, (w, h), flags=cv.INTER_LINEAR, borderMode=cv.BORDER_REPLICATE) return rotated Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.