Spaces:
Sleeping
Sleeping
import numpy as np | |
import streamlit as st | |
import cv2 | |
import pandas as pd | |
# HSV H(色相)の変更 | |
def changedH(bgr_img, shift): | |
hsvimage = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV_FULL) # BGR->HSV | |
hi = hsvimage[:,:,0].astype(np.int32) | |
if shift < 0: | |
nhi = hi.flatten() | |
for px in nhi: | |
if px < 0: | |
px = 255 - px | |
nhi = nhi.reshape(hsvimage.shape[:2]) | |
hi = nhi.astype(np.uint8) | |
chimg = (hi + shift) % 255 | |
hsvimage[:,:,0] = chimg | |
hsv8 = hsvimage.astype(np.uint8) | |
return cv2.cvtColor(hsv8,cv2.COLOR_HSV2BGR_FULL) # HSV->BGR | |
# HSV S(彩度),V(明度)の変更 | |
def changedSV(bgr_img, alpha, beta, color_idx): | |
hsvimage = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV_FULL) # BGR->HSV | |
hsvf = hsvimage.astype(np.float32) | |
hsvf[:,:,color_idx] = np.clip(hsvf[:,:,1] * alpha+beta, 0, 255) | |
hsv8 = hsvf.astype(np.uint8) | |
return cv2.cvtColor(hsv8,cv2.COLOR_HSV2BGR_FULL) | |
# HSV S(彩度)の変更 | |
def changedS(bgr_img, alpha, beta): | |
return changedSV(bgr_img, alpha, beta, 1) | |
# HSV V(明度)の変更 | |
def changedV(bgr_img, alpha, beta): | |
return changedSV(bgr_img, alpha, beta, 2) | |
def main(): | |
st.subheader("RGB値") | |
R = st.slider(label='Red', | |
min_value=0, | |
max_value=255, | |
value=100, | |
) | |
st.write(f'{R}') | |
G = st.slider(label='Green', | |
min_value=0, | |
max_value=255, | |
value=100, | |
) | |
st.write(f'{G}') | |
B = st.slider(label='Blue', | |
min_value=0, | |
max_value=255, | |
value=100, | |
) | |
st.write(f'{B}') | |
R_nmp = np.array([R] * 80 * 200).reshape(80, 200).astype(np.uint8) | |
G_nmp = np.array([G] * 80 * 200).reshape(80, 200).astype(np.uint8) | |
B_nmp = np.array([B] * 80 * 200).reshape(80, 200).astype(np.uint8) | |
img_nmp = np.stack([R_nmp, G_nmp, B_nmp], 2) | |
st.image( | |
img_nmp, | |
width=640, | |
caption=f"R:{R} G:{G} B:{B}" | |
) | |
# HSV変換 | |
img_hsv = cv2.cvtColor(img_nmp, cv2.COLOR_RGB2HSV) | |
# HSV値を取得 | |
H = img_hsv.T[0].flatten().min() | |
S = img_hsv.T[1].flatten().min() | |
V = img_hsv.T[2].flatten().min() | |
# 表示 | |
st.subheader("HSV値") | |
st.info(f"H:{H} S:{S} V:{V}") | |
# RGBのサンプル | |
df = pd.DataFrame( | |
[ | |
["白", 255, 255, 255], | |
["黄色", 255, 255, 0], | |
["薄い灰色", 192, 192, 192], | |
["水色", 0, 255, 255], | |
["黄緑", 0, 255, 0], | |
["灰色", 128, 128, 128], | |
["黄土色", 128, 128, 0], | |
["濃い桃色", 255, 0, 255], | |
["青緑", 0, 128, 128], | |
["赤", 255, 0, 0], | |
["緑", 0, 128, 0], | |
["紫", 128, 0, 128], | |
["茶色", 128, 0, 0], | |
["青", 0, 0, 255], | |
["紺", 0, 0, 128], | |
["黒", 0, 0, 0], | |
], | |
columns = ["色", "R", "G", "B"] | |
) | |
st.sidebar.subheader("RGBサンプル") | |
st.sidebar.table(df.set_index("色")) | |
st.sidebar.markdown('日立HP[JP1/Automatic Job Management System 3 操作ガイド 11.11 RGB値による色の指定](http://itdoc.hitachi.co.jp/manuals/3020/30203S0933/AJSV0206.HTM) より引用') | |
if __name__ == '__main__': | |
main() |