본문 바로가기

개발/AI

[OpenCV] 컴퓨터 비전과 딥러닝 / Ch03 / 연습 문제

틀릴 수 있음 주의

 

01

(1) 512x512x3

(2) 256x256x3x30

(3) 256x256x128

(4) 512x512x8

(5) 20,000x12

 

02

팽창한 영상

0 0 0 1 0 0 0 0
0 1 1 1 1 1 0 0
0 1 1 1 1 1 1 0
0 1 1 0 1 1 1 0

 

침식한 영상

0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0
0 1 1 0 1 1 0 0

 

03

히스토그램 평활화

0 0 2 2 4
3 4 4 4 4
4 4 5 5 5
5 4 5 6 7
5 5 5 6 7

 

04

(1)

  0 1 2 3 4 5 6 7
0 - - - - - - - -
1 - 3 5 7 9 9 6 -
2 - 0 0 0 0 0 0 -
3 - 0 0 0 0 0 0 -
4 - 0 0 0 0 0 0 -
5 - -3 -5 -7 -9 -9 -6 -
6 - -3 -5 -7 -9 -9 -6 -
7 - - - - - - - -

영상 아래 3개의 화솟값에서 위쪽 3개 화솟값 뺀다.

수평에지 검출하는 역할

 

(2)

  0 1 2 3 4 5 6 7
0                
1   1 -1 5 3 3 6  
2   0 -2 2 0 0 3  
3   0 -2 2 0 0 3  
4   0 -2 2 0 0 3  
5   1 -1 5 3 3 6  
6   -1 -1 -3 -3 -3 -3  
7                

샤프닝 필터는 edge를 선명하게 하여 물체 식별을 돕는다.

다만 부작용으로 잡음이 확대된다.

 

(3)

  0 1 2 3 4 5 6 7
0                
1   1 3 3 3 3 0  
2   1 2 2 0 0 -3  
3   0 2 2 0 0 -3  
4   0 2 2 0 0 -3  
5   -1 -1 -1 -3 -3 -3  
6   -1 -1 -1 -3 -3 -3  
7                

엠보싱 필터는 물체에 돋을 새김 느낌을 준다.

 

05

R(30) 뒤에 R'T를 곱한다

 

06

import numpy as np
import matplotlib.pyplot as plt

sigma=1
e=2.718

def g(x):
    y=(1/(sigma*((2*3.14)**(1/2))))*(e**(-(x*x)/(2*sigma**2)))
    return y

x=np.linspace(-6,6,100)
plt.plot(x,g(x))

plt.show()

 

import numpy as np
import matplotlib.pyplot as plt

sigma=1
e=2.718

fig=plt.figure()
ax = plt.axes(projection='3d')

def g(y,x):
    return (1/((sigma**2)*(2*3.14)))*(e**(-(y*y+x*x)/(2*sigma**2)))

x=np.arange(-6,6,0.1)
y=np.arange(-6,6,0.1)
X,Y=np.meshgrid(x,y)

z=g(y,x)
Z=g(Y,X)


ax.plot_surface(X, Y, Z, cmap='viridis')

plt.show()

 

07

import cv2 as cv
import numpy as np

img=cv.imread("../source/pooh.jpg")
img=cv.resize(img,dsize=(0,0),fx=0.4,fy=0.4)
gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
cv.putText(gray,"pooh",(10,20),cv.FONT_HERSHEY_SIMPLEX,0.7,(255,255,255),2)

#Gaussian Blur
smooth=np.hstack((cv.GaussianBlur(gray,(5,5),0.0),cv.GaussianBlur(gray,(9,9),0.0),cv.GaussianBlur(gray,(15,15),0.0)))
cv.imshow("Gaussian",smooth)

#Median Blur
smooth2=np.hstack((cv.medianBlur(gray,5),cv.medianBlur(gray,9),cv.medianBlur(gray,15)))
cv.imshow("Median",smooth2)

#Gaussian + Median
Gaussian=cv.GaussianBlur(gray,(5,5),0.0)
smooth3=np.hstack((cv.medianBlur(Gaussian,5),cv.medianBlur(Gaussian,9),cv.medianBlur(Gaussian,15)))
cv.imshow("Gaussian+Median",smooth3)

cv.waitKey()
cv.destroyAllWindows()

 

 

08

import cv2 as cv
import numpy as np
import time



def myEqualizeHist(image):
    # 이미지를 그레이스케일로 변환
    gray_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    
    # 이미지의 히스토그램 계산
    hist, bins = np.histogram(gray_image.flatten(), 256, [0,256])
    
    # 누적 분포 함수(CDF) 계산
    cdf = hist.cumsum()
    cdf_normalized = cdf * hist.max() / cdf.max()
    
    # 원본 이미지에 대한 누적 분포 함수의 역함수 계산
    cdf_m = np.ma.masked_equal(cdf, 0)
    cdf_m = (cdf_m - cdf_m.min()) * 255 / (cdf_m.max() - cdf_m.min())
    cdf = np.ma.filled(cdf_m, 0).astype('uint8')
    
    # 히스토그램 평활화 적용
    equalized_image = cdf[gray_image]
    
    return equalized_image




#my function
img=cv.imread("../source/pooh.jpg")
start=time.time()
myEqualizeHist(img)
print("MyFunc time:",time.time()-start)

#openCV function
img2=cv.imread("../sourcr/pooh.jpg",cv.IMREAD_GRAYSCALE)
start=time.time()
cv.equalizeHist(img2)
print("OpenCV time:",time.time()-start)

MyEqualizeHist는 0.04195

OpenCV함수는 0.000998

만큼 걸린다.

 

09

import cv2 as cv
import sys

img=cv.imread('../source/pooh.jpg')

#Function###########################################
def draw(event,x,y,flags,param):
    global ix,iy
    
    if event==cv.EVENT_LBUTTONDOWN:
        ix,iy=x,y
    elif event==cv.EVENT_LBUTTONUP:
        cv.rectangle(img, (ix,iy),(x,y),(0,0,255),2)
        patch=img[iy:y,ix:x,:]
        patch1=cv.resize(patch,dsize=(0,0),fx=5,fy=5,interpolation=cv.INTER_NEAREST)
        patch2=cv.resize(patch,dsize=(0,0),fx=5,fy=5,interpolation=cv.INTER_LINEAR)
        patch3=cv.resize(patch,dsize=(0,0),fx=5,fy=5,interpolation=cv.INTER_CUBIC)
        patchScreen=np.hstack((patch1,patch2,patch3))
        cv.imshow("Resize nearest,linear,cubic",patchScreen)
    cv.imshow("Drawing",img)
    
#Show###############################################
cv.namedWindow("Drawing")
cv.imshow("Drawing",img)
cv.setMouseCallback("Drawing",draw)    


#QUIT################################################
while(True):
    if cv.waitKey(1)==ord("q"):
        cv.destroyAllWindows()
        break