首页 > 解决方案 > Aadhaar 卡正确方向、旋转或翻转图像

问题描述

我尝试使用方向来旋转 Aadhaar 卡片图像,但由于它也旋转了完整的 Aadhaar 图像而失败。完整的 Aadhar 卡片图像也会旋转,任何人都可以建议应该做些什么来纠正这个方向问题。

签出我的代码

'''

import numpy as np
import cv2
from PIL import Image
im = cv2.imread("D:\\Mayur\\model\\Dilation\\1.jpg", cv2.IMREAD_UNCHANGED)

def image_rotate(image):
im = cv2.imread(image, cv2.IMREAD_UNCHANGED)
# Compute difference between each two color channels for finding colored pixels.
cdiff = cv2.absdiff(im[:,:,0], im[:,:,1])//3 + cv2.absdiff(im[:,:,0], im[:,:,2])//3 + 
cv2.absdiff(im[:,:,1], im[:,:,2])//3;
ret, cmask = cv2.threshold(cdiff, 10, 255, cv2.THRESH_BINARY)

# Find clusters.
# https://answers.opencv.org/question/194566/removing-noise-using-connected-components/
nlabel,labels,stats,centroids = cv2.connectedComponentsWithStats(cmask, connectivity=8)

# Find second largest cluster (the largest is the background?):
stats[np.argmax(stats[:, cv2.CC_STAT_AREA])] = 0
idx_max_size = np.argmax(stats[:, cv2.CC_STAT_AREA])
center = centroids[idx_max_size]

# True if the center of the centroid is at the right side of the image
is_center_at_right_side = center[0] > im.shape[1]//2

if is_center_at_right_side:
    rotated_im = im[::-1, ::-1, :].copy()  # Reverse left/right and up/down
    cv2.imshow('rotated_im', rotated_im)
    cv2.waitKey(0)
    
# Draw green circle at the center (for testing)
#cv2.circle(im, (int(center[0]), int(center[1])), 10, (0, 255, 0), thickness=10)

#cv2.imshow('cmask', cmask)
cv2.imshow('im', im)
cv2.waitKey(0)

fname = image.split('\\')
#print(fname)
fname1 =  fname[-1].split(".")
#print(fname1)

if is_center_at_right_side:
    rotated_im = cv2.rotate(im, cv2.ROTATE_90_COUNTERCLOCKWISE)
    fname = image_path.split('\\')
    #print(fname)
    fname1 =  fname[-1].split(".")
    #print(fname1)

    #cv2.destroyAllWindows()
    cv2.imwrite(masked_outputPath + fname1[0] +'.jpg',rotated_im)
else:
    rotated_im = cv2.rotate(im, cv2.ROTATE_90_CLOCKWISE)
    fname = image_path.split('\\')
    #print(fname)
    fname1 =  fname[-1].split(".")
    #print(fname1)

    #cv2.destroyAllWindows()
    cv2.imwrite(masked_outputPath + fname1[0] +'.jpg',rotated_im)


image_rotate("D:\\Mayur\\model\\Dilation\\1.jpg")

''' 有什么建议我应该做些什么改变?

标签: pythonimageopencvimage-processingcomputer-vision

解决方案


推荐阅读