본문 바로가기
Coding/Opencv

OpenCV: scipy ndimage(다차원 이미지 처리)

by climba 2021. 3. 15.

scipy.ndimage는 임의의 차원의 어레이로 동작하도록 설계되어 일반적인 영상 처리 및 분석 기능을 제공합니다.

 

 

즉 같은 사진을 회전시켜 여러 각도에서 테스트 해 보고싶을 때 유용합니다.

 

import matplotlib.pyplot as plt
import cv2
from scipy import ndimage

def mosaic(img,rect,size):
    (x1,y1,x2,y2) = rect
    w = x2 - x1
    h = y2 - y1
    i_rect = img[y1:y2,x1:x2]
    i_small = cv2.resize(i_rect,(size,size))
    i_mos = cv2.resize(i_small,(w,h),interpolation = cv2.INTER_AREA)
    img2 = img.copy()
    img2[y1:y2,x1:x2] = i_mos
    return img2

cascade_file = "haarcascade_frontalface_alt.xml"
cascade = cv2.CascadeClassifier(cascade_file)
img = cv2.imread("girl2.png")

def face_detect(img):
    img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    face_list = cascade.detectMultiScale(img_gray, minSize=(120,120))
    for (x,y,w,h) in face_list:
        print("얼굴의 좌표 =",x,y,w,h)
        red = (0,0,255)
        cv2.rectangle(img,(x,y),(x+w,y+h),red,thickness=10)

for i in range(0,9):
    ang = i*10
    print("--"+str(ang)+"--")
    img_r = ndimage.rotate(img,ang)
    face_detect(img_r)
    plt.subplot(3,3,i+1)
    plt.axis("off")
    plt.title("angle="+str(ang))
    plt.imshow(cv2.cvtColor(img_r,cv2.COLOR_BGR2RGB))

plt.show()

 

=> 모자이크 처리 할때 같은 사진이라도 각도를 다르게 하면 인식하지 못합니다.

 

=> 20-30도 정도 회전까지 인식 가능합니다.

댓글