728x90
# Task 1 : 이미지 이진화
# cv2.threshold(src, thresh, max, type, dst=None)

src = cv2.imread('gradient.jpg', cv2.IMREAD_GRAYSCALE)

_, bin = cv2.threshold(src, 127, 255, cv2.THRESH_BINARY)
_, bin_inv = cv2.threshold(src, 127, 255, cv2.THRESH_BINARY_INV)
_, trunc = cv2.threshold(src, 127, 255, cv2.THRESH_TRUNC)
_, tozero = cv2.threshold(src, 127, 255, cv2.THRESH_TOZERO)
_, tozero_inv = cv2.threshold(src, 127, 255, cv2.THRESH_TOZERO_INV)
_, otsu = cv2.threshold(src, 127, 255, cv2.THRESH_OTSU)
#창 한꺼번에 띄우기용 이름,영상 배열
images = [bin, bin_inv, trunc, tozero, tozero_inv,otsu]
images_name = ['bin', 'bin_inv', 'trunc', 'tozero', 'tozero_inv','otsu']
#창 한꺼번에 띄우기
for i,j in zip(images_name, images):

    cv2.imshow(i,j)

cv2.waitKey()
cv2.destroyAllWindows()


#Task 2 : Adaptivethresholding
# cv2.adaptiveThreshold((gray)src, maxval, adaptiveMethod(cv2.ADAPTIVE_THRESH_GAUSSIAN,MEAN_C), thresholdType, blockSize, 블록내평균값에서 뺄값)

src = cv2.imread('sudoku.jpg',cv2.IMREAD_GRAYSCALE)

mean = cv2.adaptiveThreshold(src,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 15,2)
gauss = cv2.adaptiveThreshold(src,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15,2)

cv2.imshow('src',src)
cv2.imshow('mean',mean)
cv2.imshow('gauss',gauss)

cv2.waitKey()
cv2.destroyAllWindows()



cv2.imshow('src',src)
cv2.waitKey()
cv2.destroyAllWindows()


Task3 : 필터링- 2D convolution
cv2.filter2D(src, ddepth(출력영상데이터타입-1로 ㄱ), kernel, anchor(고정점 위치, (-1,-1)이면 중앙을 고정점으로 사용), delta(추가적으로 더할 값), borderType, dst)

src = cv2.imread('lena.jpg')

# 평균값 필터 방법  1.
kernel = np.ones((5,5), np.float32)/25 #Averaging필터 생성
dst = cv2.filter2D(src, -1, kernel)
cv2.imshow('src',src)
cv2.imshow('dst',dst)
cv2.waitKey()
cv2.destroyAllWindows()


#평균값 필터 방법 2
cv2.blur(src,(5,5))
cv2.imshow('src',src)
cv2.imshow('dst',dst)
cv2.waitKey()
cv2.destroyAllWindows()


# Task4 : 가우시안필터링
# cv2.GaussianBlur(src, ksize, sigmaX, dst=None, sigmaY=None, borderType=None)

src = cv2.imread('lena.jpg')
dst = cv2.GaussianBlur(src, (0,0), 3,None,3)
cv2.imshow('src',src)
cv2.imshow('dst',dst)
cv2.waitKey()
cv2.destroyAllWindows()


# Task5 : 중간값 필터링 
# cv2.medianBlur(src, ksize, dst=None)

src = cv2.imread('noise.bmp')
dst = cv2.medianBlur(src,5)
cv2.imshow('src',src)
cv2.imshow('dst',dst)
cv2.waitKey()
cv2.destroyAllWindows()


#Task6 : 양방향필터(엣지를 살리며 필터링)
# cv2.bilateralFilter(src, d(-1줘), sigmaColor(색공간표준편차), sigmaSpace(좌표공간표준편차), dst=None, borderType=None)

src = cv2.imread('lena.jpg')
dst = cv2.bilateralFilter(src,-1, 10,5)
cv2.imshow('src',src)
cv2.imshow('dst',dst)
cv2.waitKey()
cv2.destroyAllWindows()


# Task7 : 모폴로지 침식과 팽창 한꺼번에 총정리
# cv2.erode(src, kernel, dst=None, anchor=None, iterations=None, borderType=None, borderValue=None)
# cv2.dilate(src, kernel, dst=None, anchor=None, iterations=None, borderType=None, borderValue=None)
# cv2.morphologyEx(src, op, kernel, dst=None, anchor=None, iterations=None, borderType=None, borderValue=None)
# cv2.getStructuringElement(shape, ksize, anchor=None) #shape 에다가 cv2.MORPH_RECT , CROSS, ELLIPSE쓰면  그런 모양의 커널이 생김

src = cv2.imread('j_letter.png')
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(11,11))

dilation = cv2.dilate(src, kernel)
erosion = cv2.erode(src, kernel)
opening = cv2.morphologyEx(src, cv2.MORPH_OPEN,kernel) # 열기 -> 침식팽창(하얀 점들이 사라짐)
closing = cv2.morphologyEx(src, cv2.MORPH_CLOSE,kernel)# 닫기 - > 팽창침식 (검은 노이즈가 사라짐)

cv2.imshow('src',src)
cv2.imshow('dilation',dilation)
cv2.imshow('erosion',erosion)
cv2.imshow('opening',opening)
cv2.imshow('closing',closing)

cv2.waitKey()
cv2.destroyAllWindows()


# Task 8 이미지 Gradient 
# cv2.Sobel(src, ddepth(-1), dx, dy, dst=None, ksize=None, scale=None, delta=None, borderType=None)
# cv2.Laplacian(src, ddepth(-1), dst=None, ksize=None, scale=None, delta=None, borderType=None)

src = cv2.imread('sudoku copy.jpg')
dx = cv2.Sobel(src, -1, 2, 0, (3,3))
dy = cv2.Sobel(src, -1, 0, 2, (3,3))
laplacian = cv2.Laplacian(src,-1)
h, w  = src.shape[:2]


cv2.imshow('src',src)
cv2.imshow('dx',dx)
cv2.imshow('dy',dy)
cv2.imshow('laplacian',laplacian)


cv2.waitKey()
cv2.destroyAllWindows()


# Task 9 캐니엣지검출
# 1. 5X5가우시안필터로 노이즈감소
# 2. 그래디언트 강도 탐지
# 3. 비최대 억제
# 4. Hysteresis Thresholding ====> threshold1 ~~ threshold2 사이의 미분값만 엣지로 검출함 
# cv2.Canny(image, threshold1, threshold2, edges=None, apertureSize=None, L2gradient=None)

src = cv2.imread('sudoku copy.jpg', cv2.IMREAD_GRAYSCALE)
th = cv2.adaptiveThreshold(src, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 2)

kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
th = cv2.morphologyEx(th, cv2.MORPH_CLOSE, kernel)
edge = cv2.Canny(th, 100,200)
cv2.imshow('src',src)
cv2.imshow('th',th)
cv2.imshow('edge',edge)
cv2.waitKey()
cv2.destroyAllWindows()

728x90

+ Recent posts