OpenCV / / 2022. 11. 2. 16:05

OpenCV - 기초

import cv2
import matplotlib.pyplot as plt 

print(cv2.version.opencv_version)

imagefile='./lena.jpg'

img=cv2.imread(imagefile)
print(img.shape) # (590(가로), 529(세로), 3)(채널 수 RGB(3)))

img2=cv2.imread(imagefile,0) #흑백으로 열기 
print(img2.shape) # (590, 529)
  
cv2.startWindowThread()
cv2.imshow('lena color',img)
cv2.imshow('lena grayscale', img2)
cv2.waitKey()
cv2.destroyAllWindows()

#폴더 이미지 확인 
imagefile ='lena.jpg'
img= cv2.imread(imagefile)
img2 =cv2.imread(imagefile,0)

cv2.imwrite('./out/lena.bmp',img)
cv2.imwrite('./out/lena.png',img)
cv2.imwrite('./out/lena.png',img2)
cv2.imwrite('./out/lena.jpg',img2)


imagefile='./lena.jpg'
img_bgr = cv2.imread(imagefile)
plt.axis('off')

img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
plt.axis('off')

plt.imshow(img_bgr)
plt.imshow(img_rgb)
plt.show()
'''gray image display '''
img_gray = cv2.imread(imagefile,cv2.IMREAD_GRAYSCALE)
plt.axis('off')
plt.imshow(img_gray,cmap='gray',interpolation='nearest')
plt.show()
'''margin image save'''
img_gray = cv2.imread(imagefile,cv2.IMREAD_GRAYSCALE)
plt.figure(figsize=(4,4))
plt.subplots_adjust(left=0, right=0.5,bottom=0,top=0.5)
plt.imshow(img_gray,cmap='gray')
plt.axis('off')
plt.show()

'''subplot image display'''
path = './datafolder/' # lena,apple,baboon,orange이미지 2행 2열로 표현하기
img_bgr1 = cv2.imread(path+'lena.jpg')
img_bgr2 = cv2.imread(path+'apple.jpg')
img_bgr3 = cv2.imread(path+'baboon.jpg')
img_bgr4 = cv2.imread(path+'orange.jpg')

img_rgb1 = cv2.cvtColor(img_bgr1, cv2.COLOR_BGR2RGB)
img_rgb2 = cv2.cvtColor(img_bgr2, cv2.COLOR_BGR2RGB)
img_rgb3 = cv2.cvtColor(img_bgr3, cv2.COLOR_BGR2RGB)
img_rgb4 = cv2.cvtColor(img_bgr4, cv2.COLOR_BGR2RGB)
fig = plt.figure(figsize=(5,5))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax1.axis('off')
ax2.axis('off')
ax3.axis('off')
ax4.axis('off')
ax1.imshow(img_rgb1)
ax2.imshow(img_rgb2)
ax3.imshow(img_rgb3)
ax4.imshow(img_rgb4)
plt.show()
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유