공공데이터 활용 과제
국토교통부_아파트 매매 실거래 상세 전라북도 전주시_실시간 운행정보 서비스 농촌진흥청 국립농업과학원_농업기상 기본 관측데이터 조회 한국공항공사_항공기운항정보 국립중앙의료원-자동심장충격기정보조회서비스(AED)
국토교통부_아파트 매매 실거래 상세 전라북도 전주시_실시간 운행정보 서비스 농촌진흥청 국립농업과학원_농업기상 기본 관측데이터 조회 한국공항공사_항공기운항정보 국립중앙의료원-자동심장충격기정보조회서비스(AED)
보호 글이라서 요약이 없습니다.
사용 파일 : 엑셀 샘플 파일 사용 시트 : tbook 다양한 검색 방법 엑셀의 고급 필터 데이터베이스의 SQL 판다스의 필터 출판사가 성안당인 것만 검색 엑셀 SQL : SELECT * FROM tbook WHERE 출판사=”성안당”; 판다스 WHERE 절에 사용할 수 있는 연산자 비교 : =, <>, <, <=, >, >= 범위 :… Continue Reading 엑셀, 데이터베이스, 판다스를 활용한 검색
조교 청강 신청방법
아나콘다 설치 파이썬 기본문법 넘파이 샘플데이터 : 샘플1, 샘플2, 샘플3, harim_english, harim_eng_nan 판다스1 판다스2 판다스3
API Pandas API : https://pandas.pydata.org/docs/reference/index.html 리눅스 명령어 사전 : https://terms.naver.com/list.naver?cid=59321&categoryId=59321 환경 코랩 사용 웹브라우저 서버1번사용 : http://a100.jj.ac.kr:8005 (ID: a학번, PW: ) 서버2번사용 : http://a101.jj.ac.kr:8005 (ID: a학번, PW: ) putty 접속 서버1 : URL(a100.jj.ac.kr), Port(10022) 서버2 : URL(a101.jj.ac.kr), Port(10022) 암호변경 : passwd 폴더관련 : cd mkdir rmdir 파일관련 : cp mv… Continue Reading 2022년 겨울 특강(Pandas, Matplotlib)
Scipy 라이브러리의 signal 모듈을 이용하여 컨볼루션 Scipi API : https://docs.scipy.org/doc/scipy/reference/index.html signal.convolve2d() API : https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.convolve2d.html signal.convolve2d() 함수 이용하여 컨볼루션
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
from PIL import Image import matplotlib.pyplot as plt import numpy as np from scipy import signal #filter = [[1/9., 1/9., 1/9.], [1/9., 1/9., 1/9.], [1/9., 1/9., 1/9.]] #스무딩 filter = np.ones((11,11)) / 121 #filter = [[0., -1., 0.],[-1., 4., -1.],[0., -1., 0.]] # 라플라스 #filter = [[-1., 0., 1.],[-1., 0., 1.],[-1., 0., 1.]] # Prewitt 1 #filter = [[1., 1., 1.],[0., 0., 0.],[-1., -1., -1.]] # Prewitt 2 #filter = [[-1., 0., 1.],[-2., 0., 2.],[-1., 0., 1.]] # Sobel 1 #filter2 = [[1., 2., 1.],[0., 0., 0.],[-1., -2., -1.]] # Sobel 2 #filter = [[0., 1.],[-1., 0.]] # Robers 1 #filter = [[1., 0.],[0., -1.]] # Robers 2 im = Image.open('./images/chess.png') #im = Image.open('./images/chess_football.png') im = im.convert('L') im = np.array(im) im2 = signal.convolve2d(im, filter, mode='same') plt.figure(figsize=(10,5)) plt.subplot(1,2,1); plt.axis('off'); plt.imshow(im, cmap='gray') plt.subplot(1,2,2); plt.axis('off'); plt.imshow(im2, cmap='gray') |
skimage의 corner_harris() 함수를 사용하여 feature 찾기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
from PIL import Image import matplotlib.pyplot as plt import numpy as np from skimage.feature import corner_harris #im = Image.open('./images/chess.png') im = Image.open('./images/chess_football.png') im2 = im.copy() im2 = im2.convert('L') im2 = np.array(im2) coordinates = corner_harris(im2, k=0.001) # k=0.2 im = np.array(im) im[coordinates>0.01*coordinates.max()]=[255,0,0,255] plt.figure(figsize=(10,5)) plt.axis('off') plt.imshow(im, cmap='gray') |
API : https://scikit-image.org/docs/stable/api/skimage.feature.html#corner-harris Ransac 알고리즘을 사용한 영상 매칭 영상1을 불러와서 affine변환한 영상2 생성후 두 영상에서 harris corner특징 찾기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
from PIL import Image import matplotlib.pyplot as plt import numpy as np from skimage.io import imread from skimage.util import img_as_float from skimage.color import rgb2gray from skimage.feature import corner_harris, corner_peaks, corner_subpix from skimage.transform import AffineTransform, warp from skimage.exposure import rescale_intensity from skimage.measure import ransac temple = rgb2gray(img_as_float(imread('./images/temple.JPG'))) image_original = np.zeros(list(temple.shape) + [3]) image_original[..., 0] = temple mgrid = np.mgrid[0:image_original.shape[0], 0:image_original.shape[1]] gradient_row, gradient_col = (mgrid / float(image_original.shape[0])) image_original[..., 1] = gradient_row image_original[..., 2] = gradient_col image_original = rescale_intensity(image_original) # 밝기 조정 # 와핑 영상 생성- 크기 변경, 회전, 평행 이동 affine_trans = AffineTransform(scale=(0.8, 0.9), rotation=0.1, translation=(120, -20)) image_warped = warp(image_original, affine_trans .inverse, output_shape=image_original.shape) image_original_gray = rgb2gray(image_original) # 명암도 영상 image_warped_gray = rgb2gray(image_warped) # 해리스 코너 측을 사용하여 코너 추출 coordinates = corner_harris(image_original_gray) coordinates[coordinates > 0.01*coordinates.max()] = 1 coordinates_original = corner_peaks(coordinates, threshold_rel=0.0001, min_distance=5) coordinates = corner_harris(image_warped_gray) coordinates[coordinates > 0.01*coordinates.max()] = 1 coordinates_warped = corner_peaks(coordinates, threshold_rel=0.0001, min_distance=5) # 서브 화소 코너 위치 결정 coordinates_original_subpix = corner_subpix(image_original_gray, coordinates_original, window_size=9) coordinates_warped_subpix = corner_subpix(image_warped_gray, coordinates_warped, window_size=9) coordinates[coordinates < 0] = 0 # 결과 확인용 출력 print(temple.shape, image_original.shape, mgrid.shape) #print(temple) #print(image_original[..., 0]) #print(coordinates.shape, coordinates_original.shape) print(coordinates_original[:5]) print(coordinates_original_subpix[:5]) #np.savetxt('./a.txt', coordinates, fmt='%.1f') |
Ransac 알고리즘 함수 구현
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# 중심 화소까지의 거리에 따라 화소 가중치 계산 def gaussian_weights(window_ext, sigma=1): y, x = np.mgrid[-window_ext:window_ext+1, -window_ext:window_ext+1] g_w = np.zeros(y.shape, dtype = np.double) g_w[:] = np.exp(-0.5 * (x**2 / sigma**2 + y**2 / sigma**2)) g_w /= 2 * np.pi * sigma * sigma return g_w def match_corner(coordinates, window_ext=3): row, col = np.round(coordinates).astype(np.intp) window_original = image_original[row-window_ext:row+window_ext+1, \ col-window_ext:col+window_ext+1, :] # 중심 화소까지의 거리에 따른 화소의 가중치 weights = gaussian_weights(window_ext, 3) weights = np.dstack((weights, weights, weights)) #왜곡 영상에서 모든 코너에 대한 차 제곱의 합 계산 SSDs = [] for row, col in coordinates_warped: window_warped = image_warped[row-window_ext:row+window_ext+1, \ col-window_ext:col+window_ext+1, :] if window_original.shape == window_warped.shape: SSD = np.sum(weights * (window_original - window_warped)**2) SSDs.append(SSD) # 일치관계로서 최소 SSD로 코너 사용 min_idx = np.argmin(SSDs) if len(SSDs) > 0 else -1 if min_idx >= 0: return coordinates_warped_subpix[min_idx] else: return [None] |
침식(erosion) vs 팽창(dilation)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
from PIL import Image import matplotlib.pyplot as plt import numpy as np from skimage.morphology import binary_erosion, binary_dilation, binary_opening, binary_closing, rectangle, disk im = Image.open('./images/circles.jpg') im = im.convert('L') im = np.array(im).astype('float') im2 = np.array(im.copy()).astype('float') im3 = np.array(im.copy()).astype('float') im2 = binary_erosion(im2, disk(3)) im3 = binary_dilation(im3, disk(2)) #im2 = binary_erosion(im2, rectangle(10,10)) plt.figure(figsize=(12,5)) plt.subplot(1,3,1), plt.axis('off'), plt.imshow(im, cmap='gray') plt.subplot(1,3,2), plt.axis('off'), plt.imshow(im2, cmap='gray') plt.subplot(1,3,3), plt.axis('off'), plt.imshow(im3, cmap='gray') plt.show() |
열림(openning) vs 닫힘(closing)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
from PIL import Image import matplotlib.pyplot as plt import numpy as np from skimage.morphology import binary_erosion, binary_dilation, binary_opening, binary_closing, rectangle, disk im = Image.open('./images/circles.jpg') im = im.convert('L') im = np.array(im).astype('float') im2 = np.array(im.copy()).astype('float') im3 = np.array(im.copy()).astype('float') im2 = binary_opening(im2, disk(3)) im3 = binary_closing(im3, disk(1)) plt.figure(figsize=(12,5)) plt.subplot(1,3,1), plt.axis('off'), plt.imshow(im, cmap='gray') plt.subplot(1,3,2), plt.axis('off'), plt.imshow(im2, cmap='gray') plt.subplot(1,3,3), plt.axis('off'), plt.imshow(im3, cmap='gray') plt.show() |
미분과 그레디언트
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
from PIL import Image import matplotlib.pyplot as plt import numpy as np from scipy import signal ker_x = [[-1, 1]] # 1행, 2열 ker_y = [[-1], [1]] # 2행, 1열 im = Image.open('./images/chess.png') im = im.convert('L') im = np.array(im) im2 = signal.convolve2d(im, ker_x, mode='same') im3 = signal.convolve2d(im, ker_y, mode='same') im4 = np.sqrt(im2**2 + im3**2) plt.figure(figsize=(10,10)) plt.subplot(2,2,1), plt.axis('off'), plt.imshow(im, cmap='gray') plt.subplot(2,2,2), plt.axis('off'), plt.imshow(im2, cmap='gray') plt.subplot(2,2,3), plt.axis('off'), plt.imshow(im3, cmap='gray') plt.subplot(2,2,4), plt.axis('off'), plt.imshow(im4, cmap='gray') plt.show() |
라플라시안 : 영상의 2차 미분
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
from PIL import Image import matplotlib.pyplot as plt import numpy as np from scipy import signal filter = [[0., -1., 0.],[-1., 4., -1.],[0., -1., 0.]] # 라플라스 im = Image.open('./images/chess.png') im = im.convert('L') im = np.array(im) im2 = signal.convolve2d(im, filter, mode='same') plt.figure(figsize=(10,5)) plt.subplot(1,2,1), plt.axis('off'), plt.imshow(im, cmap='gray') plt.subplot(1,2,2), plt.axis('off'), plt.imshow(im2, cmap='gray') plt.show() |
라플라시안을 사용한 샤프닝
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
from PIL import Image import matplotlib.pyplot as plt import numpy as np from scipy import signal filter = [[0., -1., 0.],[-1., 4., -1.],[0., -1., 0.]] # 라플라스 im = Image.open('./images/me8.JPG') im = im.convert('L') im = np.array(im) im2 = signal.convolve2d(im, filter, mode='same') im2 = np.clip(im+im2, 0, 255) plt.figure(figsize=(10,5)) plt.subplot(1,2,1), plt.axis('off'), plt.imshow(im, cmap='gray') plt.subplot(1,2,2), plt.axis('off'), plt.imshow(im2, cmap='gray') plt.show() |
언샤프 마스킹 : 영상을 샤프닝하는 기술로 영상에서 영상의 흐려진 버전을 뺌 – sharpened = original + (original – blurred) * amount
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
from PIL import Image import matplotlib.pyplot as plt import numpy as np from scipy import signal, ndimage filter = [[0., -1., 0.],[-1., 4., -1.],[0., -1., 0.]] # 라플라스 im = Image.open('./images/me4.jpg') im = im.convert('L') im = np.array(im) im2 = ndimage.gaussian_filter(im, 1) im3 = np.clip(im-im2, 0, 255) im4 = ndimage.gaussian_filter(im, 3) im4 = np.clip(im-im4, 0, 255) im5 = ndimage.gaussian_filter(im, 5) im5 = np.clip(im-im5, 0, 255) im6 = ndimage.gaussian_filter(im, 7) im6 = np.clip(im-im6, 0, 255) plt.figure(figsize=(10,5)) plt.subplot(1,6,1), plt.axis('off'), plt.imshow(im, cmap='gray') plt.subplot(1,6,2), plt.axis('off'), plt.imshow(im2, cmap='gray') plt.subplot(1,6,3), plt.axis('off'), plt.imshow(im3, cmap='gray') plt.subplot(1,6,4), plt.axis('off'), plt.imshow(im4, cmap='gray') plt.subplot(1,6,5), plt.axis('off'), plt.imshow(im5, cmap='gray') plt.subplot(1,6,6), plt.axis('off'), plt.imshow(im6, cmap='gray') plt.show() |
소벨 에지 검출기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
from PIL import Image import matplotlib.pyplot as plt import numpy as np from scipy import signal ker_x = [[-1., 0., 1.],[-2., 0., 2.],[-1., 0., 1.]] # Sobel 1 ker_y = [[1., 2., 1.],[0., 0., 0.],[-1., -2., -1.]] # Sobel 2 im = Image.open('./images/tajmahal1.jpg') im = im.convert('L') im = np.array(im) im2 = signal.convolve2d(im, ker_x, mode='same') im2 = np.clip(im2, 0, 255) im3 = signal.convolve2d(im, ker_y, mode='same') im3 = np.clip(im3, 0, 255) im4 = np.sqrt(im2**2 + im3**2) im4 = np.clip(im4, 0, 255) plt.figure(figsize=(10,8)) plt.subplot(2,2,1), plt.axis('off'), plt.imshow(im, cmap='gray') plt.subplot(2,2,2), plt.axis('off'), plt.imshow(im2, cmap='gray') plt.subplot(2,2,3), plt.axis('off'), plt.imshow(im3, cmap='gray') plt.subplot(2,2,4), plt.axis('off'), plt.imshow(im4, cmap='gray') plt.show() |
소벨 에지 검출기(라이브러리 사용)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
from PIL import Image import matplotlib.pyplot as plt import numpy as np from skimage import filters im = Image.open('./images/tajmahal1.jpg') im = im.convert('L') im = np.array(im) im2 = filters.sobel_h(im) im2 = np.clip(im2, 0, 255) im3 = filters.sobel_v(im) im3 = np.clip(im3, 0, 255) im4 = filters.sobel(im) im4 = np.clip(im4, 0, 255) plt.figure(figsize=(10,8)) plt.subplot(2,2,1), plt.axis('off'), plt.imshow(im, cmap='gray') plt.subplot(2,2,2), plt.axis('off'), plt.imshow(im2, cmap='gray') plt.subplot(2,2,3), plt.axis('off'), plt.imshow(im3, cmap='gray') plt.subplot(2,2,4), plt.axis('off'), plt.imshow(im4, cmap='gray') plt.show() |
로버츠, 프리윗, 스칼, 소벨, 라플라스 에지 검출기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
from PIL import Image import matplotlib.pyplot as plt import numpy as np from skimage import filters im = Image.open('./images/tajmahal1.jpg') im = im.convert('L') im = np.array(im) im2 = filters.roberts(im) im3 = filters.scharr(im) im4 = filters.prewitt(im) im5 = filters.sobel(im) im6 = np.clip(filters.laplace(im), 0, 1) plt.figure(figsize=(10,6)) plt.subplot(2,3,1), plt.axis('off'), plt.imshow(im, cmap='gray') plt.subplot(2,3,2), plt.axis('off'), plt.imshow(im2, cmap='gray') plt.subplot(2,3,3), plt.axis('off'), plt.imshow(im3, cmap='gray') plt.subplot(2,3,4), plt.axis('off'), plt.imshow(im4, cmap='gray') plt.subplot(2,3,5), plt.axis('off'), plt.imshow(im5, cmap='gray') plt.subplot(2,3,6), plt.axis('off'), plt.imshow(im6, cmap='gray') plt.show() |
캐니 에지 검출기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import numpy as np from PIL import Image from skimage import filters, feature, img_as_float import matplotlib.pyplot as plt from scipy import signal, ndimage im = Image.open('./images/tiger3.jpg') im = im.convert('L') im = np.array(im) edges1 = feature.canny(im) edges2 = feature.canny(im, sigma=3) plt.figure(figsize=(12,8)) plt.subplot(1,3,1), plt.axis('off'), plt.imshow(im, cmap='gray') plt.subplot(1,3,2), plt.axis('off'), plt.imshow(edges1, cmap='gray') plt.subplot(1,3,3), plt.axis('off'), plt.imshow(edges2, cmap='gray') |
색상히스토그램
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
from PIL import Image import matplotlib.pyplot as plt import numpy as np im = Image.open('./images/parrot.png') im = im.convert('L') im2 = np.array(im) print(im2.shape) im2 = im2.reshape(-1) print(im2.shape) plt.figure(figsize=(10, 5)) plt.subplot(1,2,1), plt.imshow(im, cmap='gray') plt.subplot(1,2,2), plt.hist(im2, bins=64, range=(0,256), color='gray', edgecolor='black', alpha=0.5) plt.show() |
색상 값 이동
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from PIL import Image import matplotlib.pyplot as plt import numpy as np im = Image.open('./images/parrot.png') im = im.convert('L') im2 = np.array(im, dtype='float') - 50 im2 = np.clip(im2, 0, 255) plt.figure(figsize=(10, 5)) plt.subplot(1,2,1), plt.imshow(im2, cmap='gray') plt.subplot(1,2,2), plt.hist(im2.reshape(-1), bins=256, color='gray', alpha=0.5) plt.show() |
파워-로우 변환
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
from PIL import Image import matplotlib.pyplot as plt import numpy as np im = Image.open('./images/parrot.png') im = im.convert('L') im = np.array(im, dtype='float') im2 = im ** 0.3 im3 = im ** 3 plt.figure(figsize=(15, 10)) plt.subplot(2,3,1), plt.imshow(im, cmap='gray') plt.subplot(2,3,2), plt.imshow(im2, cmap='gray') plt.subplot(2,3,3), plt.imshow(im3, cmap='gray') plt.subplot(2,3,4), plt.hist(im.reshape(-1), bins=256, color='gray', alpha=0.5) plt.subplot(2,3,5), plt.hist(im2.reshape(-1), bins=256, color='gray', alpha=0.5) plt.subplot(2,3,6), plt.hist(im3.reshape(-1), bins=256, color='gray', alpha=0.5) plt.show() |
콘트라스트 스트레칭
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from PIL import Image from skimage import img_as_ubyte, img_as_float import matplotlib.pyplot as plt import numpy as np im = Image.open('./images/parrot.png') im = im.convert('L') im = np.array(im, dtype='float') im2 = (255 * im - 22950) / 48; im2 = np.clip(im2, 0, 255) # (90 ~ 138) im3 = (255 * im - 12750) / 48; im3 = np.clip(im3, 0, 255) # (50 ~ 98) plt.figure(figsize=(15, 10)) plt.subplot(2,3,1), plt.imshow(im, cmap='gray') plt.subplot(2,3,2), plt.imshow(im2, cmap='gray') plt.subplot(2,3,3), plt.imshow(im3, cmap='gray') plt.subplot(2,3,4), plt.hist(im.reshape(-1), bins=254, range=(1,254), color='gray', alpha=0.5) plt.subplot(2,3,5), plt.hist(im2.reshape(-1), bins=254, range=(1,254), color='gray', alpha=0.5) plt.subplot(2,3,6), plt.hist(im3.reshape(-1), bins=254, range=(1,254), color='gray', alpha=0.5) plt.show() |
임계화(thresholding)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from PIL import Image from skimage import img_as_ubyte import matplotlib.pyplot as plt import numpy as np im = Image.open('./images/parrot.png') im = im.convert('L') im = np.array(im, dtype='float') im2 = np.copy(im); im2[im2 <= 100] = 0; im2[im2 > 100] = 1 # thres = 100 im3 = np.copy(im); im3[im3 <= 150] = 0; im3[im3 > 150] = 1 # thres = 150 plt.figure(figsize=(15, 10)) plt.subplot(2,3,1), plt.imshow(im, cmap='gray') plt.subplot(2,3,2), plt.imshow(im2, cmap='gray') plt.subplot(2,3,3), plt.imshow(im3, cmap='gray') plt.subplot(2,3,4), plt.hist(im.reshape(-1), bins=256, color='gray', alpha=0.5) plt.subplot(2,3,5), plt.hist(im2.reshape(-1), bins=256, color='gray', alpha=0.5) plt.subplot(2,3,6), plt.hist(im3.reshape(-1), bins=256, color='gray', alpha=0.5) plt.show() |
히스토그램 평활화
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from PIL import Image from skimage import exposure import matplotlib.pyplot as plt import numpy as np im = Image.open('./images/parrot.png') im = im.convert('L') im = np.array(im, dtype='float') / 255.0 im2 = exposure.equalize_hist(im) #전역 평활화 ( 전역 연산 ) im3 = exposure.equalize_adapthist(im, clip_limit=0.03) #지역 평활화 ( 적응 연산 ) plt.figure(figsize=(15, 10)) plt.subplot(2,3,1), plt.imshow(im, cmap='gray') plt.subplot(2,3,2), plt.imshow(im2, cmap='gray') plt.subplot(2,3,3), plt.imshow(im3, cmap='gray') plt.subplot(2,3,4), plt.hist(im.reshape(-1), bins=256, color='gray', alpha=0.5) plt.subplot(2,3,5), plt.hist(im2.reshape(-1), bins=256, color='gray', alpha=0.5) plt.subplot(2,3,6), plt.hist(im3.reshape(-1), bins=256, color='gray', alpha=0.5) plt.show() |