import cv2
import numpy as np
image = cv2.imread('./testimages/test.jpg') #File reading
#Color extraction in HSV
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV_FULL) #Convert image to HSV
hue = hsv[:, :, 0]#Hue Hue
sat = hsv[:, :, 1]#Saturation Saturation
val = hsv[:, :, 2]#Brightness Value
print(hue, sat, val)
print(hue.shape, sat.shape, val.shape)
print(hue[0], sat[0], val[0])
##Find the intermediate value of hue
h_num = []
for h in hue:
for hn in h:
h_num.append(hn)
ave_h = sum(h_num)/len(h_num)
print(ave_h)
##Find the median saturation
s_num = []
for s in sat:
for sn in s:
s_num.append(sn)
ave_s = sum(s_num)/len(s_num)
print(ave_s)
##Find the median of lightness
v_num = []
for v in val:
for vl in v:
v_num.append(vl)
ave_v = sum(v_num)/len(v_num)
print(ave_v)
import numpy as np
import cv2
img_BGR = cv2.imread("./testimages/test.jpg ")
img_Lab = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2Lab)
img_Lab_L, img_Lab_a, img_Lab_b = cv2.split(img_Lab)
cv2.imshow("pic", img_Lab_a)
cv2.waitKey(0)
cv2.destroyAllWindows()
min_val = 30
max_val = 70
img_canny = cv2.Canny(img_Lab_a, min_val, max_val)
cv2.imshow("pic", img_canny)
cv2.waitKey(0)
cv2.destroyAllWindows()
#cv2.imwrite('out.jpg', img_canny)
Preparation of screen.jpg
import cv2
import time
import numpy as np
#cap = cv2.VideoCapture(0)
#Tribalization threshold * Adjust the value yourself
th1, th2 = 60, 60
#r, img = cap.read()
img = cv2.imread("./testdir/test.jpg ")
#Get input image in grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Screen image loading
screen = cv2.imread("screen.jpg ", 0)
#Screen image resizing
screen = cv2.resize(screen, (gray.shape[1], gray.shape[0]))
#Edge detection and color inversion
edge = 255 - cv2.Canny(gray, 80, 120)
#Tribalization of images
gray[gray <= th1] = 0
gray[gray >= th2] = 255
gray[np.where((gray > th1) & (gray < th2))] = screen[np.where((gray > th1) & (gray < th2))]
#Match the binary image and the edge image
gray = cv2.bitwise_and(gray, edge)
#Save image
cv2.imwrite("Sample.jpg ", gray)
Recommended Posts