그냥 파일을 읽어서 출력
import cv2 as cv
import sys
img = cv.imread('soccer.jpg')
if img is None:
sys.exit('파일없음')
cv.imshow('soccer img',img)
cv.waitKey()
cv.destroyAllWindows()
rgb채널별로 디스플레이하기
import cv2 as cv
import sys
img = cv.imread('soccer.jpg')
if img is None:
sys.exit('파일 찾으수없음')
cv.imshow('original',img)
cv.imshow('위왼쪽반",img[0:img.shape[0]//2,0:img.shape[1]//2,:}
#img[높이,너비,채널]
cv.imshow('가운데중간',img[img.shape[0]//4:3*img.shape[0]//4,img.shape[1]//4:3*img.shape[1]//4,:])
cv.imshow('r채널',img[:,:,2])
cv.imshow('g채널',img[:,:,1])
cv.imshow('b채널',img[:,:,0])
cv.waitKey()
cv.destroyAllWindows()
영상형태 변환하고 크기 축소하기
import cv2 as cv
import sys
img = cv.imread('soccer.jpg')
if img is None:
sys.exit('파일이 존재하지 않습니다')
grayimg = cv.cvtColor(img,COLOR_BGR2GRAY)
grayimg_small = cv.resize(grayimg,dsize=(0,0),fx=0.5,fy=0.5)
cv.imwrite('grayimg',grayimg)
cv.imwrite('grayimgsmall',grayimg_small)
cv.imshow('원래 이미지',img)
cv.imshow('그레이 이미지',grayimg)
cv.imshow('작아진 그레이 이미지',grayimg_small)
cv.waitKey()
cv.destroyAllWindows()
감마보정하기
import cv2 as cv
import numpy as np
img = cv.imread('soccer.jpg')
if img is None:
sys.exit('파일이 없음')
img=cv.resize(img,dsize=(0,0),fx=0.25,fy=0.25)
def gamma(f,gamma=1.0):
f1=f/255.0
return np.uint8(255*(f1**gamma))
gc = np.hstack((gamma(img,0.5),gamma(img,0.75),gamma(img,1.0),gamma(img,2.0),gamma(img,3.0)))
#np.hstack으로 그림 다 묶어줄때는 괄호 두개씩 써야댐
cv.imshow('감마보정이미지',gc)
cv.waitKey()
cv.destroyAllWindows()
히스토그램 평활화하기
import cv2 as cv
import matplotlib.pyplot as plt
img = cv.imread('soccer.jpg')
if img is None:
sys.exit('파일을 찾을수없음')
grayimg = cv.cvtColor(img,BGR2GRAY)
#그레이스케일 이미지를 히스토그램 평활화 하지않고 출력
plt.imshow(grayimg,cmap='gray'),plt.xticks([]),plt.yticks([]),plt.show()
h = cv.calcHist([grayimg],[0],None,[256],[0,256])
plt.plot(h,color='r',linewidth=1),pltshow()
#그레이스케일 이미지를 히스토그램 평활화하여 출력
equal = cv.equalizeHist(grayimg)
plt.imshow(equal,cmap='gray'),plt.xticks([]),plt.yticks([]),plt.show()
h = cv.calcHist([equal],[0],None,[256],[0,256])
plt.imshow(h,color='r',linewidth=1),plt.show()
오츄알고리즘으로 이진화하기
import cv2 as cv
import sys
img = cv.imread('soccer.jpg')
t,bin_img = cv.threshold(img[:,:,2],0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
print('오츄 알고리즘이 찾은 최적의 임계값=',t)
cv.imshow('r채널',img[:,:,2])
cv.imshow('r채널 이진화',bin_img)
cv.waitKey()
cv.destroyAllWindows()
컨볼루션 적용하기(가우시안 스무딩과 엠보싱)
import cv2 as cv
import numpy as np
img = cv.imread('soccer.jpg')
img = cv.resize(img,dsize=(0,0),fx=0.4,fy=0.4)
grayimg = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
cv.putText(gray,'soccer',(10,20),cv.FONT_HERSHEY_SIMPLEX,0.7,(255,255,255),2)
cv.imshow('오리지날',grayimg)
smooth = np.hstack((cv.GaussianBlur(grayimg,(5,5),0.0),cv.GaussianBlur(grayimg,(9,9),0.0),cv.GaussianBlur(grayimg,(15,15),0.0)))
cv.imshow('스무딩',smooth)
femboss = np.array([[-1.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,1.0]])
gray16 = np.int16(grayimg)
emboss = np.uint8(np.clip(cv.filter2D(gray16,-1,femboss)+128,0,255))
emboss_bad = np.uint8(cv.filter2D(gray16,-1,femboss)+128)
emboss_worse = cv.filter2D(gray16,-1,femboss)
cv.imshow(fghhhghghgf
에지 연산자 사용하기
import cv2 as cv
img = cv.imread('soccer.jpg')
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
grad_x = cv.sobel(gray,cv.CV_32F,1,0,ksize=3)
grad_y = cv.sobel(gray,cv.CV_32F,0,1,ksize=3)
sobel_x = cv.convertScaleAbs(grad_x)
sobel_y = cv.convertScaleAbs(grad_y)
edge_strength = cv.addWeighted(sobel_x,0.5,sobel_y,0.5)
cv.imshow