[OpenCV] Personal memo

Conclusion

Find the median value of HSV

A style that does not dare to use intensional expressions


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)

Edge acquisition process

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)

Convert from photo to cartoon style

Preparation of screen.jpg 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

[OpenCV] Personal memo
python openCV installation (memo)
Python OpenCV tutorial memo
[Personal memo] Auto-completion of bash
How to use cron (personal memo)
Linux Frequently Used Commands [Personal Memo]
[Personal memo] Python virtual environment command memo
Confirmation of impulse response (personal memo)
[OpenCV] Personal identification with face photo
[Personal memo] Python sequence type / mapping type
find / grep command (beginner's personal memo)
Linux command (basic in basic) personal memo
gzip memo
Pandas memo
HackerRank memo
Python memo
python memo
About Opencv ②
graphene memo
Flask memo
pyenv memo
Matplotlib memo
pytest memo
sed memo
About Opencv ③
Python memo
Install Memo
BeautifulSoup4 memo
networkx memo
python memo
tomcat memo
command memo
Generator memo.
psycopg2 memo
Python memo
SSH memo
Download XBRL file from EDINET (personal memo)
Command memo
Memo: rtl8812
About Opencv ①
Automatic PowerPoint generation with python-pptx (personal memo)
pandas memo
Shell memo
Python memo
Pycharm memo
Python memo
[Personal memo] julia --Using Python library with julia using PyCall
[Personal memo] pip install fails, so solve it