2

I’d like to rotate fingerprint image from skew to vertical center

By python with opencv

I’m beginner.

From this

enter image description here

To this

enter image description here

nathancy
  • 42,661
  • 14
  • 115
  • 137
azaz0133
  • 43
  • 4
  • Use morphology open to fill in the fingerprint curves. Then get the outline and fit an ellipse to it. Then get the ellipse major axis orientation and use that angle to rotate your image to a major axis vertical orientation. – fmw42 Aug 29 '19 at 16:32
  • maybe fingerprint isn't legit. It can be shape as circle – azaz0133 Aug 29 '19 at 16:48
  • If it is a circle, there is no unique orientation, since the print lines are curved. That makes the issue much harder. I have no solution for that. Can you specify how to know what the proper orientation would be if the outline is a circle? – fmw42 Aug 29 '19 at 17:41

1 Answers1

4

Given an image containing a rotated blob at an unknown angle, the skew can be corrected with this approach

  • Detect blob in the image
  • Compute angle of rotated blob
  • Rotate the image to correct skew

To detect the blob in the image, we convert to grayscale and adaptive threshold to obtain a binary image

enter image description here

image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = 255 - gray
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

Next we compute the angle of the rotated blob using cv2.minAreaRect() and calculate the skew angle

# Compute rotated bounding box
coords = np.column_stack(np.where(thresh > 0))
angle = cv2.minAreaRect(coords)[-1]

if angle < -45:
    angle = -(90 + angle)
else:
    angle = -angle
print(angle)

43.72697067260742

Finally we apply an affine transformation to correct the skew

# Rotate image to deskew
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)

Here's the result

enter image description here

import cv2
import numpy as np

image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = 255 - gray
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

# Compute rotated bounding box
coords = np.column_stack(np.where(thresh > 0))
angle = cv2.minAreaRect(coords)[-1]

if angle < -45:
    angle = -(90 + angle)
else:
    angle = -angle
print(angle)

# Rotate image to deskew
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)

cv2.imshow('thresh', thresh)
cv2.imshow('rotated', rotated)
cv2.waitKey()
nathancy
  • 42,661
  • 14
  • 115
  • 137