如何将倾斜的指纹图像旋转到垂直直立位置 [英] How to rotate skewed fingerprint image to vertical upright position

查看:190
本文介绍了如何将倾斜的指纹图像旋转到垂直直立位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将指纹图像从偏斜旋转到垂直中心

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

通过python与opencv

By python with opencv

我是初学者.

从这里

对此

推荐答案

给出一个包含旋转角度为未知角度的斑点的图像,可以使用这种方法校正偏斜

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

  • 检测图像中的斑点
  • 计算旋转斑点的角度
  • 旋转图像以校正偏斜

要检测图像中的斑点,我们将其转换为灰度和自适应阈值以获得二进制图像

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

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]

接下来,我们使用cv2.minAreaRect()计算旋转的斑点的角度并计算偏斜角

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

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)

这是结果

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()

这篇关于如何将倾斜的指纹图像旋转到垂直直立位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆