import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread('d:/data/mandrill.png')
print(img)
img_plot = plt.imshow(img)
plt.show()
!pip install opencv-python
import cv2
img_gray = cv2.imread('d:/data/mandrill.png', cv2.IMREAD_GRAYSCALE)
img_color = cv2.imread('d:/data/mandrill.png', cv2.IMREAD_COLOR)
cv2.imshow('grayscale', img_gray)
cv2.imshow('color image', img_color)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
img = cv2.imread('d:/data/mandrill.png', 1)
cv2.line(img, (0,0), (200,200), (0,0,255), 5)
cv2.arrowedLine(img, (0,200), (200,20), (0,0,255), 5)
cv2.imshow('lined', img)
cv2.waitKey(0)
cv2.rectangle(img, (0,200), (200,20), (0,0,0), 5)
cv2.putText(img, "hello", (70,70), fontFace = 2, fontScale = 1, color = (0,0,0))
import cv2
global img1, img2
def on_change_weight(x):
weight = x / 100
img_merged = cv2.addWeighted(img1, 1-weight, img2, weight, 0)
cv2.imshow('Display', img_merged)
cv2.namedWindow('Display')
cv2.createTrackbar('weight', 'Display', 0, 100, on_change_weight)
img1 = cv2.imread('d:/data/green_back.png')
img2 = cv2.imread('d:/data/iceberg.jpg')
img1 = cv2.resize(img1, (300,400))
img2 = cv2.resize(img2, (300,400))
cv2.imshow('Display', img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
mask_image = cv2.imread('d:/data/mask_python.png')
back_image = cv2.imread('d:/data/iceberg.jpg')
mask_image = cv2.resize(mask_image, (300,400))
back_image = cv2.resize(back_image, (300,400))
mask_ANDed = cv2.bitwise_and(mask_image, back_image)
mask_ORed = cv2.bitwise_or(mask_image, back_image)
mask_XORed = cv2.bitwise_xor(mask_image, back_image)
mask_Inverted = cv2.bitwise_and(cv2.bitwise_not(mask_image), back_image)
cv2.imshow('mask', mask_image)
cv2.imshow('back', back_image)
cv2.imshow('mask and', mask_ANDed)
cv2.imshow('mask or', mask_ORed)
cv2.imshow('mask xor', mask_XORed)
cv2.imshow('mask inv', mask_Inverted)
cv2.waitKey(0)
import numpy as np
import cv2
image = cv2.imread('d:/data/mandrill.png')
image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
blue_low = np.array([80, 0, 0])
blue_high = np.array([130, 255, 255])
mask = cv2.inRange(image_hsv, blue_low, blue_high)
cv2.imshow('original', image)
cv2.imshow('mask', mask)
cv2.waitKey(0)
extracted = cv2.bitwise_and(image, image, mask=mask)
import numpy as np
import cv2
org = cv2.imread('d:/data/mandrill.png', 1)
kernel1 = np.ones((3,3), np.float32) / 9
kernel2 = np.ones((9,9), np.float32) / 81
averaged33 = cv2.filter2D(org, -1, kernel1)
averaged99 = cv2.filter2D(org, -1, kernel2)
cv2.imshow('original', org)
cv2.imshow('filtered1', averaged33)
cv2.imshow('filtered2', averaged99)
cv2.waitKey(0)
import numpy as np
import cv2
org = cv2.imread('d:/data/mandrill.png', 1)
averaged33 = cv2.GaussianBlur(org, (3,3), 1)
averaged99 = cv2.GaussianBlur(org, (9,9), 1)
cv2.imshow('original', org)
cv2.imshow('Gaussian 33', averaged33)
cv2.imshow('Gaussian 99', averaged99)
cv2.waitKey(0)
import numpy as np
import cv2
original_image = cv2.imread('d:/data/mandrill.png', 1)
result_image1 = cv2.GaussianBlur(original_image, (9,9), 1)
result_image2 = cv2.medianBlur(original_image, 9)
result_image3 = cv2.bilateralFilter(original_image, 9, 50, 50)
import cv2
global color_image, gray_image
# Æ®·¢¹Ù°¡ º¯°æµÇ¸é ±× °ªÀ» ÀÓ°èÄ¡·Î ȸ»öÁ¶ À̹ÌÁö¸¦ ÀÌÁøÈÇÏ¿© â¿¡ ±×¸²
def on_change_threshold(x):
_, th_image = cv2.threshold(gray_image, x, 255, cv2.THRESH_BINARY)
cv2.imshow('Thresholding', th_image)
# À©µµ¸¦ »ý¼ºÇÔ
cv2.namedWindow('Thresholding')
cv2.createTrackbar('threshold', 'Thresholding', 0, 255, on_change_threshold)
# ÃÐºÒ À̹ÌÁö¸¦ Àаí, ȸ»öÁ¶ À̹ÌÁö¸¦ ÁغñÇÔ
color_image = cv2.imread('d:/data/candles.jpg', cv2.IMREAD_COLOR)
gray_image = cv2.cvtColor(color_image, cv2.COLOR_BGR2GRAY)
# óÀ½¿¡´Â ¿øº» À̹ÌÁö¸¦ ±×¸² (Æ®·¢¹Ù¸¦ º¯°æÇϸé ÀÓ°èÄ¡¿¡ µû¶ó ÀÌÁøÈ °á°ú Ãâ·Â)
cv2.imshow('Thresholding', color_image)
cv2.waitKey(0)
# image¸¦ ȸ»öÁ¶·Î ÀоîµéÀδÙ.
img_gray = cv2.imread('d:/data/green_back.png', cv2.IMREAD_GRAYSCALE)
# adaptiveThreshold¸¦ Àû¿ëÇÑ´Ù
# ÁÖº¯ 9x9 Çȼ¿ °ø°£ÀÇ Æò±Õ°ª - 5°¡ ÀÓ°èÄ¡°¡ µÇ°í, À̺¸´Ù Å©¸é 255, ±×·¸Áö ¾ÊÀ¸¸é 0
img_edge = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, blockSize=9, C= 0)
# °á°ú´Â À±°û¼± Çȼ¿ÀÌ µå·¯³´Ù
cv2.imshow('edge', img_edge)
cv2.waitKey(0)
img = cv2.imread('d:/data/book.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)
cv2.imshow('gray', gray)
cv2.imshow('binary', thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
img_hsv = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV) # HSV °ø°£À¸·Î
l_bound = np.array([40, 100, 50]) # ³ì»ö »ö»ó ÇÏÇÑ
u_bound = np.array([80, 255, 255]) # ³ì»ö »ö»ó »óÇÑ
mask = cv2.inRange(img_hsv, l_bound, u_bound) # ³ì»ö Çȼ¿ ã±â
mask_inv = cv2.bitwise_not(mask) # ³ì»ö ¾Æ´Ñ Çȼ¿ ã±â
import numpy as np
import cv2
img1 = cv2.imread('d:/data/green_back.png') # Àü°æ À̹ÌÁö Àбâ
img2 = cv2.imread('d:/data/iceberg.png') # ¹è°æ À̹ÌÁö Àбâ
front_image = cv2.resize(mask_image, (300,400))
back_image = cv2.resize(back_image, (300,400))
img_hsv = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV) # HSV °ø°£À¸·Î ¿Å±è
l_bound = np.array([40, 100, 50]) # ³ì»ö »ö»óÀÇ ÇÏÇÑ
u_bound = np.array([80, 255, 255]) # ³ì»ö »ö»óÀÇ »óÇÑ
mask = cv2.inRange(img_hsv, l_bound, u_bound) # ³ì»ö Çȼ¿ ã±â
mask_inv = cv2.bitwise_not(mask) # ³ì»öÀÌ ¾Æ´Ñ Çȼ¿ ã±â
# ³ì»ö Çȼ¿µé¸¸ ÃßÃâÇϱâ
extracted = cv2.bitwise_and(front_image, front_image, mask = mask)
# ³ì»ö ¾Æ´Ñ Çȼ¿¸¸ ÃßÃâÇϱâ
removed = cv2.bitwise_and(front_image, front_image, mask = mask_inv)
# ³ì»ö°ú °ãÄ¡´Â ¹è°æ ÃßÃâ
background = cv2.bitwise_and(back_image, back_image, mask=mask)
# ³ì»ö Á¦°Å Àü°æ + ¹è°æ
merged = cv2.bitwise_or(removed, background)
cv2.imshow('mask', mask)
cv2.imshow('mask_inv', mask_inv)
cv2.imshow('removed', removed)
cv2.imshow('background', background)
cv2.imshow('merged', merged)
cv2.waitKey(0)
img = cv2.imread('d:/data/book.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)
cv2.imshow('gray', gray)
cv2.imshow('binary', thresh)
import numpy as np
import cv2
img = cv2.imread('d:/data/book.png')
# ÀâÀ½Àº Á¦°ÅÇÏ¸é¼ Æ¯Â¡Àº À¯ÁöÇÒ ¼ö ÀÖ´Â ¾ç¹æÇâ ÇÊÅÍ Àû¿ë
blur_bilateral = cv2.bilateralFilter(img, 11, 75, 75)
# ȸ»öÁ¶·Î º¯È¯
gray = cv2.cvtColor(blur_bilateral, cv2.COLOR_BGR2GRAY)
# Àα٠Çȼ¿°ú ºñ±³ÇÑ ÀÌÁøÈ
thresh = cv2.adaptiveThreshold(gray, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, 7)
cv2.imshow('original', img)
cv2.imshow('bilateral', blur_bilateral)
cv2.imshow('gray', gray)
cv2.imshow('binary', thresh)
cv2.waitKey(0)