コンピュータビジョンの応用15 - 画像回転補正問題を解決する画像回転検証コードの角度計算モデルの応用

皆さんこんにちは、私は Wei Xue AI です。今日はコンピューター ビジョン 15 のアプリケーション、つまり画像回転補正の問題を解決するための画像回転検証コードの角度計算モデルのアプリケーションを紹介します。CV の分野では、画像回転検証コードの角度計算モデルが使用されています。回転画像補正の問題を解決するために広く使用されており、画像検証コードの機械認識の問題を効果的に解決します。回転画像検証コードは、ユーザーの身元を確認するためによく使用されますが、画像がさまざまな角度で回転している可能性があるため、識別するのが困難です。この記事では、回転画像の角度を正確に推定し、回転角度を通じて自動的に補正できる深層学習に基づく角度計算モデルを提案します。
この論文では、深層畳み込みニューラル ネットワークを使用して、回転画像に対して特徴抽出と表現学習を実行し、高レベルの抽象表現を取得しました。回帰ヘッドを追加し、回転角度ラベル データをトレーニングに使用することにより、モデルは回転された画像の角度を予測できます。実験結果は、提案したモデルが複数のデータセットで優れたパフォーマンスを達成し、回転画像補正の問題を効果的に解決できることを示しています。このモデルは優れた堅牢性と幅広い適応性を備えており、実際のアプリケーションにおける検証コードの認識精度とユーザーエクスペリエンスを向上させることができます。

ここに画像の説明を挿入します

1. データセットの生成方法

次の手順を使用します。

1. まず、大量の風景写真をダウンロードし、それから写真を処理することを選択できます。

2. opencv を使用して画像を円に切り取り、円形の画像を生成します。

3. 円形画像を 60 度から 180 度までランダムに回転し、切り取った画像アドレスと回転角度ラベルの値を含む辞書データとして保存します。コードは次のとおりです。

import cv2
import numpy as np

def crop_to_circle(image_path):
    # 加载图像
    image = cv2.imread(image_path)

    # 创建一个与图像大小相同的黑色背景
    mask = np.zeros_like(image)

    # 获取图像的中心点坐标
    height, width, _ = image.shape
    center = (width // 2, height // 2)

    # 定义半径为图像宽高中的较小值
    radius = min(center[0], center[1])

    # 在mask上绘制一个白色圆形区域
    cv2.circle(mask, center, radius, (255, 255, 255), -1)

    # 将mask作为掩模,将图像与掩模进行按位与操作
    masked_image = cv2.bitwise_and(image, mask)

    cropped_image =  "circular_cropped_image.jpg"
    # 保存裁剪后的图像
    cv2.imwrite(cropped_image, masked_image)

    # 随机生成旋转角度
    random_angle = np.random.randint(60, 181)

    # 对图像进行旋转
    M = cv2.getRotationMatrix2D(center, random_angle, 1.0)
    rotated_image = cv2.warpAffine(masked_image, M, (width, height))

    # 显示结果
    cv2.imshow("Original Image", image)
    cv2.imshow("Circular Cropped + Rotated Image", rotated_image)

    # 保存裁剪加旋转后的图像
    rotated_image_path = "rotated_image.jpg"
    cv2.imwrite(rotated_image_path, rotated_image)

    cv2.waitKey(0)
    cv2.destroyAllWindows()

    return cropped_image, rotated_image_path,random_angle

# 使用示例
image_path = "111.png"
cropped_image,rotated_image_path,random_angle = crop_to_circle(image_path)

# 保存图片地址和旋转角度标签的字典数据
data = {
    
    
    "image_path":"111.png",
    'cropped_image':cropped_image,
    "rotated_path": rotated_image_path,
    "rotation_angle": random_angle
}
print(data)

ここに画像の説明を挿入します

2. 回転画像認識の原理

画像があり、それを反時計回りに角度θだけ回転させたいとします。θローテーションのプロセスは次のステップに分けることができます。

1. 座標変換: 画像の中心点を原点(0, 0) (0, 0)に移動します。( 0 ,0 の場所。各ピクセルの座標から画像の中心の座標を減算して、画像の中心を原点に揃えることができます。

2. 回転変換: 各ピクセル点P (x, y) P(x, y)P ( x ,y )、回転行列変換を適用して新しい座標P ' ( x ' , y ' ) P'(x', y') をP' (x「、y)_ 回転行列は次のように表されます。

R ( θ ) = [ cos ⁡ ( θ ) − sin ⁡ ( θ ) sin ⁡ ( θ ) cos ⁡ ( θ ) ] R(\theta) = \begin{bmatrix} \cos(\theta) & -\sin( \theta) \\ \sin(\theta) & \cos(\theta) \end{bmatrix}R ( i )=[cos ( θ )sin ( θ )。( θ )cos ( θ )。]

ここで、θ θθは回転角、cos ⁡ \coscossin ⁡ \sinsinはコサイン関数とサイン関数です。

3. 座標変換: 画像の中心点を元の位置に移動します。画像の中心の座標に各ピクセルの座標を加算して、画像を元の位置に戻すことができます。

4. 補間処理: ピクセルの座標は整数ですが、回転した座標に小数点の座標が現れることがあります。したがって、補間方法を使用して、回転された座標上のピクセル値を決定する必要があります。一般的に使用される補間方法には、最近傍補間、双線形補間などが含まれます。

3. モデルの選択

この記事では主に resnet50 の修正モデルを使用します。モデルの具体的な構造についてはここでは省略します。誰でも簡単に使用できるように、トレーニング済みモデルのダウンロードは次のとおりです。

リンク: https://pan.baidu.com/s/1aJQ4OD6LwQlLlVwJsMj6aA?pwd=rypw抽出
コード: rypw

モデルフォルダーを構築し、そのフォルダーに resnet50_keras2.hdf5 ファイルを置きます。

コード:

from keras.applications.imagenet_utils import preprocess_input
from keras.models import load_model
from keras.optimizers import SGD
import keras.backend as K

import os
import math
import cv2
import numpy as np
import requests


class RotateCaptcha():
    def __init__(self):
        # 加载模型
        model_location = os.path.join('.', 'models', 'resnet50_keras2.hdf5')
        self.model = load_model(model_location, custom_objects={
    
    'angle_error': self.angle_error})
        self.model.compile(loss='categorical_crossentropy',
                           optimizer=SGD(lr=0.01, momentum=0.9),
                           metrics=[self.angle_error])
        # 图像长宽尺寸
        self.size = (224, 224)

    def showImg(self, image):
        '''
        展示图片
        '''
        cv2.imshow('image', image)
        cv2.waitKey(0)

    def getImgFromDisk(self, imgPath):
        image = cv2.imread(imgPath)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        return image

    def predictAngle(self, image):
        diameter = image.shape[0]  # 直径
        side_length = math.floor((diameter / 2) * 1.414)  # 圆内正方形最大边长
        cropped = math.floor((diameter - side_length) / 2)
        image = image[cropped:cropped + side_length, cropped:cropped + side_length]
        image = cv2.resize(image, self.size)

        image = np.expand_dims(image, axis=0)

        x = preprocess_input(image)
        y_pred = np.argmax(self.model.predict(x), axis=1)

        return y_pred[0]

    def rotate(self, image, angle):

        image_size = (image.shape[1], image.shape[0])
        image_center = tuple(np.array(image_size) / 2)

        # 将 OpenCV 3x2旋转矩阵转换为3x3
        rot_mat = np.vstack(
            [cv2.getRotationMatrix2D(image_center, angle, 1.0), [0, 0, 1]]
        )

        rot_mat_notranslate = np.matrix(rot_mat[0:2, 0:2])

        image_w2 = image_size[0] * 0.5
        image_h2 = image_size[1] * 0.5

        # 获取图像角点的旋转坐标
        rotated_coords = [
            (np.array([-image_w2, image_h2]) * rot_mat_notranslate).A[0],
            (np.array([image_w2, image_h2]) * rot_mat_notranslate).A[0],
            (np.array([-image_w2, -image_h2]) * rot_mat_notranslate).A[0],
            (np.array([image_w2, -image_h2]) * rot_mat_notranslate).A[0]
        ]

        # 查找新图像的大小
        x_coords = [pt[0] for pt in rotated_coords]
        x_pos = [x for x in x_coords if x > 0]
        x_neg = [x for x in x_coords if x < 0]

        y_coords = [pt[1] for pt in rotated_coords]
        y_pos = [y for y in y_coords if y > 0]
        y_neg = [y for y in y_coords if y < 0]

        right_bound = max(x_pos)
        left_bound = min(x_neg)
        top_bound = max(y_pos)
        bot_bound = min(y_neg)

        new_w = int(abs(right_bound - left_bound))
        new_h = int(abs(top_bound - bot_bound))


        trans_mat = np.matrix([
            [1, 0, int(new_w * 0.5 - image_w2)],
            [0, 1, int(new_h * 0.5 - image_h2)],
            [0, 0, 1]
        ])

        affine_mat = (np.matrix(trans_mat) * np.matrix(rot_mat))[0:2, :]


        result = cv2.warpAffine(
            image,
            affine_mat,
            (new_w, new_h),
            flags=cv2.INTER_LINEAR
        )

        return result

    def angle_difference(self, x, y):

        return 180 - abs(abs(x - y) - 180)

    def angle_error(self, y_true, y_pred):

        diff = self.angle_difference(K.argmax(y_true), K.argmax(y_pred))
        return K.mean(K.cast(K.abs(diff), K.floatx()))


if __name__ == '__main__':
    rotateCaptcha = RotateCaptcha()
    rotated_image = rotateCaptcha.getImgFromDisk('222.jpg')

    predicted_angle = rotateCaptcha.predictAngle(rotated_image)  # 预测还原角度
    print("需旋转角度:{}".format(predicted_angle))

    corrected_image = rotateCaptcha.rotate(rotated_image, -predicted_angle)  
    rotateCaptcha.showImg(corrected_image)

実行結果:
1/1 [==============================] - 1s 668ms/ステップ
必要な回転角度: 51

生成された結果は選択する必要がある角度を返しますので、その角度に基づいて次の検証コードの回転を実行して検証します。

おすすめ

転載: blog.csdn.net/weixin_42878111/article/details/132909656