Beispiel #1
0
def art_image(event):

    image_path, output_path = path_data.get_image_path(event)

    # カーネルを定義
    kernel = np.ones((5, 5), np.uint8)
    kernel[0, 0] = kernel[0, 4] = kernel[4, 0] = kernel[4, 4] = 0
    # グレースケールで画像を読み込む.
    # gray = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    # ImageMagickの convert -flatten x.png y.png に対応
    img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
    if img.shape[2] == 4:
        mask = img[:, :, 3] == 0
        img[mask] = [255] * 4
        img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # ノイズ除去が必要なら
    # gray = cv2.fastNlMeansDenoising(gray, h=20)
    # 白い部分を膨張させる
    dilated = cv2.dilate(gray, kernel, iterations=1)
    # 差をとる
    diff = cv2.absdiff(dilated, gray)
    # 白黒反転して2値化
    # _, contour = cv2.threshold(255 - diff, 240, 255, cv2.THRESH_BINARY)
    # あるいは
    image = cv2.adaptiveThreshold(255 - diff, 255,
                                  cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                  cv2.THRESH_BINARY, 7, 8)

    cv2.imwrite(output_path, image)


################################################################
def skin_image(event, userid, color):
    image_path, output_path = path_data.get_image_path(event, userid)

    image = cv2.imread(image_path)  # Load image

    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)  # BGR->HSV変換
    hsv_2 = np.copy(hsv)

    #a = random.randint(1,6)

    if color == 1:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 0] > 6) & (hsv[:, :, 0] < 30),
                                  hsv[:, :, 0] * 0.001, hsv[:, :, 0])  #赤に変更
        hsv_3 = np.copy(hsv_2)
        hsv_3[:, :,
              0] = np.where((hsv_2[:, :, 0] >= 0) & (hsv_2[:, :, 0] < 10),
                            hsv_2[:, :, 0] + 60, hsv_2[:, :, 0])  #緑
        bgr = cv2.cvtColor(hsv_3, cv2.COLOR_HSV2BGR)
    elif color == 2:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 0] > 6) & (hsv[:, :, 0] < 30),
                                  hsv[:, :, 0] * 0.001, hsv[:, :, 0])  #赤に変更
        hsv_3 = np.copy(hsv_2)
        hsv_3[:, :,
              0] = np.where((hsv_2[:, :, 0] >= 0) & (hsv_2[:, :, 0] < 10),
                            hsv_2[:, :, 0] + 110, hsv_2[:, :, 0])  #青
        bgr = cv2.cvtColor(hsv_3, cv2.COLOR_HSV2BGR)
    elif color == 3:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 0] > 6) & (hsv[:, :, 0] < 30),
                                  hsv[:, :, 0] * 0.001, hsv[:, :, 0])  #赤に変更
        hsv_3 = np.copy(hsv_2)
        hsv_3[:, :,
              0] = np.where((hsv_2[:, :, 0] >= 0) & (hsv_2[:, :, 0] < 10),
                            hsv_2[:, :, 0] + 25, hsv_2[:, :, 0])  #黄色
        bgr = cv2.cvtColor(hsv_3, cv2.COLOR_HSV2BGR)
    elif color == 4:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 0] > 6) & (hsv[:, :, 0] < 30),
                                  hsv[:, :, 0] + 160, hsv[:, :, 0])  #ピンク
        hsv_3 = np.copy(hsv_2)
        hsv_3[:, :,
              1] = np.where((hsv_2[:, :, 0] > 140) & (hsv_2[:, :, 0] < 180),
                            hsv[:, :, 1] * 0.8, hsv[:, :, 1])  #ピンク
        bgr = cv2.cvtColor(hsv_3, cv2.COLOR_HSV2BGR)
    elif color == 5:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 0] > 6) & (hsv[:, :, 0] < 30),
                                  hsv[:, :, 0] * 0.001, hsv[:, :, 0])  #赤色
        bgr = cv2.cvtColor(hsv_2, cv2.COLOR_HSV2BGR)
    elif color == 6:

        hsv_2[:, :, 2] = np.where((hsv[:, :, 0] >= 2) & (hsv[:, :, 0] < 30),
                                  hsv[:, :, 2] * 0.4, hsv[:, :, 2])  #黒色
        bgr = cv2.cvtColor(hsv_2, cv2.COLOR_HSV2BGR)

    #hsv_2[:, :, 2] = np.where((hsv_2[:, :, 0]>6) & (hsv_2[:, :, 0]<30) ,hsv_2[:, :, 1] *0.7,hsv_2[:, :, 2]) #黒色
    #0.001 赤
    #0.3 緑

    #bgr = cv2.cvtColor(hsv_3, cv2.COLOR_HSV2BGR)
    cv2.imwrite(output_path, bgr)
def mosic_image(event,userid):
    bool = True
    image_path, output_path = path_data.get_image_path(event,userid)

    # ファイル読み込みo
    image = cv2.imread(image_path)

    # グレースケール変換
    image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # カスケード分類器の特徴量を取得する
    cascade = cv2.CascadeClassifier(main.cascade_path)
    cascade_eye = cv2.CascadeClassifier(main.cascade_eye_path)
    # 物体認識(顔認識)の実行
    # image – CV_8U 型の行列.ここに格納されている画像中から物体が検出されます
    # objects – 矩形を要素とするベクトル.それぞれの矩形は,検出した物体を含みます
    # scaleFactor – 各画像スケールにおける縮小量を表します
    # minNeighbors – 物体候補となる矩形は,最低でもこの数だけの近傍矩形を含む必要があります
    # flags – このパラメータは,新しいカスケードでは利用されません.古いカスケードに対しては,cvHaarDetectObjects 関数の場合と同じ意味を持ちます
    # minSize – 物体が取り得る最小サイズ.これよりも小さい物体は無視されます
    facerect = cascade.detectMultiScale(image_gray, scaleFactor=1.1, minNeighbors=2, minSize=(30, 30))
    eyerect = cascade_eye.detectMultiScale(image_gray, scaleFactor=1.1, minNeighbors=2, minSize=(20, 20))
    print("レクト:{} // {}".format(facerect, eyerect))
    print("ぎっとぽっど")

    color = (255, 0, 0)  # 青

    ratio = 0.05  # 縮小処理時の縮小率(小さいほどモザイクが大きくなる)

    print("通過したじょ")
    if len(eyerect) > 0:
        # for rect in eyerect:
        #     cv2.rectangle(image, tuple(rect[0:2]), tuple(rect[0:2] + rect[2:4]), color, thickness=1)
        #     print(eyerect)
        #     print(rect)
           

        for x, y, w, h in eyerect:  # 引数でeyesで取得した数分forループ
           # y:はHEIGHT、x:はWEIGHT  fxはxの縮小率、fyはyの縮小率
           small = cv2.resize(image[y: y + h, x: x + w], None, fx=ratio, fy=ratio, interpolation=cv2.INTER_NEAREST)
           image[y: y + h, x: x + w] = cv2.resize(small, (w, h), interpolation=cv2.INTER_NEAREST)
           
    else:
        bool = False

    if bool:
        # 認識結果の保存
        cv2.imwrite(output_path, image)
        #cv2.imwrite(output_path2, image)
        return True
    else:
        return False


################################################################
def illust_image(event):
    
    image_path, output_path = path_data.get_image_path(event)

    # 元画像の読み込み
    img = cv2.imread(image_path)

    # 画像のアニメ絵化
    image = illust_filter(img, 30)

    # 結果出力
    cv2.imwrite(output_path, image)

################################################################
Beispiel #5
0
def dot_image(event, userid):

    image_path, output_path = path_data.get_image_path(event, userid)

    # 元画像の読み込み
    img = cv2.imread(image_path)

    # ドット絵化
    dst = pixel_art(img, 0.5, 4)

    # 結果を出力
    print("出力確認:{}".format(output_path))
    cv2.imwrite(output_path, dst)


################################################################
def skin_image(event, userid, color):
    image_path, output_path = path_data.get_image_path(event, userid)

    img = cv2.imread(image_path)  # Load image

    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)  # BGR->HSV変換
    hsv_2 = np.copy(hsv)
    hsv_3 = np.copy(hsv_2)
    hsv_4 = np.copy(hsv_3)
    hsv_5 = np.copy(hsv_4)
    hsv_6 = np.copy(hsv_5)
    hsv_7 = np.copy(hsv_6)
    hsv_8 = np.copy(hsv_7)
    hsv_9 = np.copy(hsv_8)
    hsv_10 = np.copy(hsv_9)

    if color == 1:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 2] >= 0) & (hsv[:, :, 2] < 30),
                                  hsv[:, :, 0] * 0.001, hsv[:, :, 0])
        hsv_3[:, :,
              0] = np.where((hsv_2[:, :, 2] > 30) & (hsv_2[:, :, 2] < 60),
                            hsv_2[:, :, 0] * 0.001, hsv_2[:, :, 0])  #150~180
        hsv_4[:, :,
              0] = np.where((hsv_3[:, :, 2] > 60) & (hsv_3[:, :, 2] < 90),
                            hsv_3[:, :, 0] * 0.001, hsv_3[:, :, 0])  #160~190

        #彩度を上げる
        hsv_5[:, :,
              1] = np.where((hsv_4[:, :, 2] >= 0) & (hsv_4[:, :, 2] < 30),
                            hsv_4[:, :, 1] + 200, hsv_4[:, :, 1])
        hsv_6[:, :,
              1] = np.where((hsv_5[:, :, 2] > 30) & (hsv_5[:, :, 2] < 60),
                            hsv_5[:, :, 1] + 200, hsv_5[:, :, 1])
        hsv_7[:, :,
              1] = np.where((hsv_6[:, :, 2] > 60) & (hsv_6[:, :, 2] < 100),
                            hsv_6[:, :, 1] + 200, hsv_6[:, :, 1])

        #色相を上げる
        hsv_8[:, :,
              0] = np.where((hsv_7[:, :, 2] >= 0) & (hsv_7[:, :, 2] < 30),
                            hsv_7[:, :, 0] + 100, hsv_7[:, :, 0])
        hsv_9[:, :,
              0] = np.where((hsv_8[:, :, 2] > 30) & (hsv_8[:, :, 2] < 60),
                            hsv_8[:, :, 0] + 100, hsv_8[:, :, 0])  #150~180
        hsv_10[:, :,
               0] = np.where((hsv_9[:, :, 2] > 60) & (hsv_9[:, :, 2] < 100),
                             hsv_9[:, :, 0] + 100, hsv_9[:, :, 0])  #160~190

        #明度を上げる
        hsv_11 = np.copy(hsv_10)
        hsv_11[:, :,
               2] = np.where((hsv_8[:, :, 2] >= 0) & (hsv_8[:, :, 2] < 30),
                             hsv_8[:, :, 2] + 90, hsv_8[:, :, 2])  #140~170

        hsv_12 = np.copy(hsv_11)
        hsv_12[:, :,
               2] = np.where((hsv_11[:, :, 2] > 30) & (hsv_11[:, :, 2] < 60),
                             hsv_11[:, :, 2] + 90, hsv_11[:, :, 2])  #150~180

        hsv_13 = np.copy(hsv_12)
        hsv_13[:, :,
               2] = np.where((hsv_12[:, :, 2] > 60) & (hsv_12[:, :, 2] < 100),
                             hsv_12[:, :, 2] + 90, hsv_12[:, :, 2])  #160~190
        bgr = cv2.cvtColor(hsv_12, cv2.COLOR_HSV2BGR)
        cv2.imwrite(output_path, bgr)

    elif color == 2:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 2] >= 0) & (hsv[:, :, 2] < 30),
                                  hsv[:, :, 0] * 0.001, hsv[:, :, 0])
        hsv_3[:, :,
              0] = np.where((hsv_2[:, :, 2] > 30) & (hsv_2[:, :, 2] < 60),
                            hsv_2[:, :, 0] * 0.001, hsv_2[:, :, 0])  #150~180
        hsv_4[:, :,
              0] = np.where((hsv_3[:, :, 2] > 60) & (hsv_3[:, :, 2] < 90),
                            hsv_3[:, :, 0] * 0.001, hsv_3[:, :, 0])  #160~190

        #彩度を上げる
        hsv_5[:, :,
              1] = np.where((hsv_4[:, :, 2] >= 0) & (hsv_4[:, :, 2] < 30),
                            hsv_4[:, :, 1] + 200, hsv_4[:, :, 1])
        hsv_6[:, :,
              1] = np.where((hsv_5[:, :, 2] > 30) & (hsv_5[:, :, 2] < 60),
                            hsv_5[:, :, 1] + 200, hsv_5[:, :, 1])
        hsv_7[:, :,
              1] = np.where((hsv_6[:, :, 2] > 60) & (hsv_6[:, :, 2] < 100),
                            hsv_6[:, :, 1] + 200, hsv_6[:, :, 1])

        #色相を上げる
        hsv_8[:, :,
              0] = np.where((hsv_7[:, :, 2] >= 0) & (hsv_7[:, :, 2] < 30),
                            hsv_7[:, :, 0] + 50, hsv_7[:, :, 0])
        hsv_9[:, :,
              0] = np.where((hsv_8[:, :, 2] > 30) & (hsv_8[:, :, 2] < 60),
                            hsv_8[:, :, 0] + 50, hsv_8[:, :, 0])  #150~180
        hsv_10[:, :,
               0] = np.where((hsv_9[:, :, 2] > 60) & (hsv_9[:, :, 2] < 100),
                             hsv_9[:, :, 0] + 50, hsv_9[:, :, 0])  #160~190

        #明度を上げる
        hsv_11 = np.copy(hsv_10)
        hsv_11[:, :,
               2] = np.where((hsv_8[:, :, 2] >= 0) & (hsv_8[:, :, 2] < 30),
                             hsv_8[:, :, 2] + 90, hsv_8[:, :, 2])  #140~170

        hsv_12 = np.copy(hsv_11)
        hsv_12[:, :,
               2] = np.where((hsv_11[:, :, 2] > 30) & (hsv_11[:, :, 2] < 60),
                             hsv_11[:, :, 2] + 90, hsv_11[:, :, 2])  #150~180

        hsv_13 = np.copy(hsv_12)
        hsv_13[:, :,
               2] = np.where((hsv_12[:, :, 2] > 60) & (hsv_12[:, :, 2] < 100),
                             hsv_12[:, :, 2] + 90, hsv_12[:, :, 2])  #160~190
        bgr = cv2.cvtColor(hsv_12, cv2.COLOR_HSV2BGR)
        cv2.imwrite(output_path, bgr)
    elif color == 3:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 2] >= 0) & (hsv[:, :, 2] < 30),
                                  hsv[:, :, 0] * 0.001, hsv[:, :, 0])
        hsv_3[:, :,
              0] = np.where((hsv_2[:, :, 2] > 30) & (hsv_2[:, :, 2] < 60),
                            hsv_2[:, :, 0] * 0.001, hsv_2[:, :, 0])  #150~180
        hsv_4[:, :,
              0] = np.where((hsv_3[:, :, 2] > 60) & (hsv_3[:, :, 2] < 90),
                            hsv_3[:, :, 0] * 0.001, hsv_3[:, :, 0])  #160~190

        #彩度を上げる
        hsv_5[:, :,
              1] = np.where((hsv_4[:, :, 2] >= 0) & (hsv_4[:, :, 2] < 30),
                            hsv_4[:, :, 1] + 200, hsv_4[:, :, 1])
        hsv_6[:, :,
              1] = np.where((hsv_5[:, :, 2] > 30) & (hsv_5[:, :, 2] < 60),
                            hsv_5[:, :, 1] + 200, hsv_5[:, :, 1])
        hsv_7[:, :,
              1] = np.where((hsv_6[:, :, 2] > 60) & (hsv_6[:, :, 2] < 100),
                            hsv_6[:, :, 1] + 200, hsv_6[:, :, 1])

        #色相を上げる
        hsv_8[:, :,
              0] = np.where((hsv_7[:, :, 2] >= 0) & (hsv_7[:, :, 2] < 30),
                            hsv_7[:, :, 0] + 80, hsv_7[:, :, 0])
        hsv_9[:, :,
              0] = np.where((hsv_8[:, :, 2] > 30) & (hsv_8[:, :, 2] < 60),
                            hsv_8[:, :, 0] + 80, hsv_8[:, :, 0])  #150~180
        hsv_10[:, :,
               0] = np.where((hsv_9[:, :, 2] > 60) & (hsv_9[:, :, 2] < 100),
                             hsv_9[:, :, 0] + 80, hsv_9[:, :, 0])  #160~190

        #明度を上げる
        hsv_11 = np.copy(hsv_10)
        hsv_11[:, :,
               2] = np.where((hsv_8[:, :, 2] >= 0) & (hsv_8[:, :, 2] < 30),
                             hsv_8[:, :, 2] + 90, hsv_8[:, :, 2])  #140~170

        hsv_12 = np.copy(hsv_11)
        hsv_12[:, :,
               2] = np.where((hsv_11[:, :, 2] > 30) & (hsv_11[:, :, 2] < 60),
                             hsv_11[:, :, 2] + 90, hsv_11[:, :, 2])  #150~180

        hsv_13 = np.copy(hsv_12)
        hsv_13[:, :,
               2] = np.where((hsv_12[:, :, 2] > 60) & (hsv_12[:, :, 2] < 100),
                             hsv_12[:, :, 2] + 90, hsv_12[:, :, 2])  #160~190
        bgr = cv2.cvtColor(hsv_12, cv2.COLOR_HSV2BGR)
        cv2.imwrite(output_path, bgr)  #黄色
    elif color == 4:
        print()
    elif color == 5:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 2] >= 0) & (hsv[:, :, 2] < 30),
                                  hsv[:, :, 0] * 0.001, hsv[:, :, 0])
        hsv_3[:, :,
              0] = np.where((hsv_2[:, :, 2] > 30) & (hsv_2[:, :, 2] < 60),
                            hsv_2[:, :, 0] * 0.001, hsv_2[:, :, 0])  #150~180
        hsv_4[:, :,
              0] = np.where((hsv_3[:, :, 2] > 60) & (hsv_3[:, :, 2] < 90),
                            hsv_3[:, :, 0] * 0.001, hsv_3[:, :, 0])  #160~190

        #彩度を上げる
        hsv_5[:, :,
              1] = np.where((hsv_4[:, :, 2] >= 0) & (hsv_4[:, :, 2] < 30),
                            hsv_4[:, :, 1] + 200, hsv_4[:, :, 1])
        hsv_6[:, :,
              1] = np.where((hsv_5[:, :, 2] > 30) & (hsv_5[:, :, 2] < 60),
                            hsv_5[:, :, 1] + 200, hsv_5[:, :, 1])
        hsv_7[:, :,
              1] = np.where((hsv_6[:, :, 2] > 60) & (hsv_6[:, :, 2] < 100),
                            hsv_6[:, :, 1] + 200, hsv_6[:, :, 1])

        #色相を上げる
        hsv_8[:, :,
              0] = np.where((hsv_7[:, :, 2] >= 0) & (hsv_7[:, :, 2] < 30),
                            hsv_7[:, :, 0] + 50, hsv_7[:, :, 0])
        hsv_9[:, :,
              0] = np.where((hsv_8[:, :, 2] > 30) & (hsv_8[:, :, 2] < 60),
                            hsv_8[:, :, 0] + 50, hsv_8[:, :, 0])  #150~180
        hsv_10[:, :,
               0] = np.where((hsv_9[:, :, 2] > 60) & (hsv_9[:, :, 2] < 100),
                             hsv_9[:, :, 0] + 50, hsv_9[:, :, 0])  #160~190

        #明度を上げる
        hsv_11 = np.copy(hsv_10)
        hsv_11[:, :,
               2] = np.where((hsv_8[:, :, 2] >= 0) & (hsv_8[:, :, 2] < 30),
                             hsv_8[:, :, 2] + 90, hsv_8[:, :, 2])  #140~170

        hsv_12 = np.copy(hsv_11)
        hsv_12[:, :,
               2] = np.where((hsv_11[:, :, 2] > 30) & (hsv_11[:, :, 2] < 60),
                             hsv_11[:, :, 2] + 90, hsv_11[:, :, 2])  #150~180

        hsv_13 = np.copy(hsv_12)
        hsv_13[:, :,
               2] = np.where((hsv_12[:, :, 2] > 60) & (hsv_12[:, :, 2] < 100),
                             hsv_12[:, :, 2] + 90, hsv_12[:, :, 2])  #160~190
        bgr = cv2.cvtColor(hsv_12, cv2.COLOR_HSV2BGR)
        cv2.imwrite(output_path, bgr)  #赤色
    elif color == 6:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 2] >= 0) & (hsv[:, :, 2] < 30),
                                  hsv[:, :, 0] * 0.001, hsv[:, :, 0])
        hsv_3[:, :,
              0] = np.where((hsv_2[:, :, 2] > 30) & (hsv_2[:, :, 2] < 60),
                            hsv_2[:, :, 0] * 0.001, hsv_2[:, :, 0])  #150~180
        hsv_4[:, :,
              0] = np.where((hsv_3[:, :, 2] > 60) & (hsv_3[:, :, 2] < 90),
                            hsv_3[:, :, 0] * 0.001, hsv_3[:, :, 0])  #160~190

        #彩度を上げる
        hsv_5[:, :,
              1] = np.where((hsv_4[:, :, 2] >= 0) & (hsv_4[:, :, 2] < 30),
                            hsv_4[:, :, 1] + 200, hsv_4[:, :, 1])
        hsv_6[:, :,
              1] = np.where((hsv_5[:, :, 2] > 30) & (hsv_5[:, :, 2] < 60),
                            hsv_5[:, :, 1] + 200, hsv_5[:, :, 1])
        hsv_7[:, :,
              1] = np.where((hsv_6[:, :, 2] > 60) & (hsv_6[:, :, 2] < 100),
                            hsv_6[:, :, 1] + 200, hsv_6[:, :, 1])

        #色相を上げる
        hsv_8[:, :,
              0] = np.where((hsv_7[:, :, 2] >= 0) & (hsv_7[:, :, 2] < 30),
                            hsv_7[:, :, 0] + 70, hsv_7[:, :, 0])
        hsv_9[:, :,
              0] = np.where((hsv_8[:, :, 2] > 30) & (hsv_8[:, :, 2] < 60),
                            hsv_8[:, :, 0] + 70, hsv_8[:, :, 0])  #150~180
        hsv_10[:, :,
               0] = np.where((hsv_9[:, :, 2] > 60) & (hsv_9[:, :, 2] < 100),
                             hsv_9[:, :, 0] + 70, hsv_9[:, :, 0])  #160~190

        #明度を上げる
        hsv_11 = np.copy(hsv_10)
        hsv_11[:, :,
               2] = np.where((hsv_8[:, :, 2] >= 0) & (hsv_8[:, :, 2] < 30),
                             hsv_8[:, :, 2] + 90, hsv_8[:, :, 2])  #140~170

        hsv_12 = np.copy(hsv_11)
        hsv_12[:, :,
               2] = np.where((hsv_11[:, :, 2] > 30) & (hsv_11[:, :, 2] < 60),
                             hsv_11[:, :, 2] + 90, hsv_11[:, :, 2])  #150~180

        hsv_13 = np.copy(hsv_12)
        hsv_13[:, :,
               2] = np.where((hsv_12[:, :, 2] > 60) & (hsv_12[:, :, 2] < 100),
                             hsv_12[:, :, 2] + 90, hsv_12[:, :, 2])  #160~190
        bgr = cv2.cvtColor(hsv_12, cv2.COLOR_HSV2BGR)
        cv2.imwrite(output_path, bgr)  #茶色

    #茶色から指定色-----------------------------------------------------------------------------------------------------------
    #if color == 1:
    #hsv_2[:, :, 0] = np.where((hsv[:, :, 2]>0) & (hsv[:, :, 2]<255) ,hsv[:, :, 0] +50 ,hsv[:, :, 0])#黒(茶色)から緑
    #hsv_2[:, :, 0] = np.where((hsv[:, :, 0]>6) & (hsv[:, :, 0]<30) ,hsv[:, :, 0] + 50,hsv[:, :, 0]) #緑
    #elif color == 2:
    #hsv_2[:, :, 0] = np.where((hsv[:, :, 2]>6) & (hsv[:, :, 2]<128) ,hsv[:, :, 0] + 110,hsv[:, :, 0]) #青
    #elif color == 3:
    #hsv_2[:, :, 0] = np.where((hsv[:, :, 2]>6) & (hsv[:, :, 2]<128) ,hsv[:, :, 0] + 20,hsv[:, :, 0]) #黄色
    #elif color == 4:
    #hsv_2[:, :, 0] = np.where((hsv[:, :, 2]>0) & (hsv[:, :, 2]<128) ,hsv[:, :, 0] + 176,hsv[:, :, 0]) #ピンク
    #hsv_2[:, :, 1] = np.where((hsv[:, :, 0]>=176) ,hsv[:, :, 1] + 96 ,hsv[:, :, 1])
    #elif color == 5:
    #hsv_2[:, :, 0] = np.where((hsv[:, :, 2]>6) & (hsv[:, :, 2]<128) ,hsv[:, :, 0] *0.001,hsv[:, :, 0]) #赤色
    #elif color == 6:
    #hsv_2[:, :, 2] = np.where((hsv[:, :, 0]>6) & (hsv[:, :, 0]<30) ,hsv[:, :, 2] *0.4,hsv[:, :, 2]) #黒色
    #hsv_2[:, :, 2] = np.where((hsv_2[:, :, 0]>6) & (hsv_2[:, :, 0]<30) ,hsv_2[:, :, 1] *0.7,hsv_2[:, :, 2]) #黒色
    #0.001 赤
    #0.3 緑
    #bgr = cv2.cvtColor(hsv_2, cv2.COLOR_HSV2BGR)
    #----------------------------------------------------------------------------------------------------------------------

    #bgr = cv2.cvtColor(hsv_13, cv2.COLOR_HSV2BGR)
    #cv2.imwrite(output_path, bgr)


#hsv_2[:, :, 0] = np.where((hsv[:, :, 2]>0) & (hsv[:, :, 2]<128) ,hsv[:, :, 0] + 176,hsv[:, :, 0]) #濃い茶色
#hsv_2[:, :, 1] = np.where((hsv[:, :, 2]>0) & (hsv[:, :, 2]<128) ,hsv[:, :, 1] + 96 ,hsv[:, :, 1])

#白
#hsv_2[:, :, 1] = np.where((hsv[:, :, 2]>0) & (hsv[:, :, 2]<128) ,hsv[:, :, 1] + 180 ,hsv[:, :, 1])
#hsv_3[:, :, 2] = np.where((hsv_2[:, :, 2]>0) & (hsv_2[:, :, 2]<128) ,hsv_2[:, :, 2] + 180 ,hsv_2[:, :, 2])
def hair_image2(event, userid, color):
    image_path, output_path = path_data.get_image_path(event, userid)

    img = cv2.imread(image_path)  # Load image

    #height = img.shape[0]
    #width = img.shape[1]
    #img2 = cv2.resize(img , (int(width*0.5), int(height*0.5)))
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)  # BGR->HSV変換
    hsv_2 = np.copy(hsv)
    hsv_3 = np.copy(hsv_2)
    hsv_4 = np.copy(hsv_3)
    hsv_5 = np.copy(hsv_4)
    hsv_6 = np.copy(hsv_5)
    hsv_7 = np.copy(hsv_6)
    hsv_8 = np.copy(hsv_7)
    hsv_9 = np.copy(hsv_8)
    hsv_10 = np.copy(hsv_9)

    #茶色から指定色-----------------------------------------------------------------------------------------------------------
    if color == 1:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 2] > 6) & (hsv[:, :, 2] < 128),
                                  hsv[:, :, 0] + 50, hsv[:, :, 0])  #黒(茶色)から緑
        #hsv_2[:, :, 0] = np.where((hsv[:, :, 0]>6) & (hsv[:, :, 0]<30) ,hsv[:, :, 0] + 50,hsv[:, :, 0]) #緑
        bgr = cv2.cvtColor(hsv_2, cv2.COLOR_HSV2BGR)
        cv2.imwrite(output_path, bgr)
    elif color == 2:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 2] > 6) & (hsv[:, :, 2] < 128),
                                  hsv[:, :, 0] + 110, hsv[:, :, 0])  #青
        bgr = cv2.cvtColor(hsv_2, cv2.COLOR_HSV2BGR)
        cv2.imwrite(output_path, bgr)
    elif color == 3:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 2] > 6) & (hsv[:, :, 2] < 128),
                                  hsv[:, :, 0] + 20, hsv[:, :, 0])  #黄色
        bgr = cv2.cvtColor(hsv_2, cv2.COLOR_HSV2BGR)
        cv2.imwrite(output_path, bgr)
    elif color == 4:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 2] > 0) & (hsv[:, :, 2] < 128),
                                  hsv[:, :, 0] + 176, hsv[:, :, 0])  #ピンク
        hsv_3[:, :, 1] = np.where((hsv_2[:, :, 0] >= 176), hsv_2[:, :, 1] + 96,
                                  hsv_2[:, :, 1])
        bgr = cv2.cvtColor(hsv_3, cv2.COLOR_HSV2BGR)
        cv2.imwrite(output_path, bgr)
    elif color == 5:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 2] > 6) & (hsv[:, :, 2] < 128),
                                  hsv[:, :, 0] * 0.001, hsv[:, :, 0])  #赤色
        bgr = cv2.cvtColor(hsv_2, cv2.COLOR_HSV2BGR)
        cv2.imwrite(output_path, bgr)
    elif color == 6:
        hsv_2[:, :, 1] = np.where((hsv[:, :, 0] >= 0) & (hsv[:, :, 0] < 30),
                                  hsv[:, :, 2] * 0, hsv[:, :, 1])  #黒色
        hsv_3[:, :, 2] = np.where((hsv_2[:, :, 0] > 6) & (hsv_2[:, :, 0] < 30),
                                  hsv_2[:, :, 0] * 0, hsv_2[:, :, 0])  #黒色
        bgr = cv2.cvtColor(hsv_3, cv2.COLOR_HSV2BGR)
        cv2.imwrite(output_path, bgr)
    #0.001 赤
    #0.3 緑
    #bgr = cv2.cvtColor(hsv_2, cv2.COLOR_HSV2BGR)
    #----------------------------------------------------------------------------------------------------------------------


#hsv_2[:, :, 0] = np.where((hsv[:, :, 2]>0) & (hsv[:, :, 2]<128) ,hsv[:, :, 0] + 176,hsv[:, :, 0]) #濃い茶色
#hsv_2[:, :, 1] = np.where((hsv[:, :, 2]>0) & (hsv[:, :, 2]<128) ,hsv[:, :, 1] + 96 ,hsv[:, :, 1])

#白
#hsv_2[:, :, 1] = np.where((hsv[:, :, 2]>0) & (hsv[:, :, 2]<128) ,hsv[:, :, 1] + 180 ,hsv[:, :, 1])
#hsv_3[:, :, 2] = np.where((hsv_2[:, :, 2]>0) & (hsv_2[:, :, 2]<128) ,hsv_2[:, :, 2] + 180 ,hsv_2[:, :, 2])
Beispiel #8
0
def hair_image(event, userid, color):
    image_path, output_path = path_data.get_image_path(event, userid)

    img = cv2.imread(image_path)  # Load image

    #height = img.shape[0]
    #width = img.shape[1]
    #img2 = cv2.resize(img , (int(width*0.5), int(height*0.5)))
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)  # BGR->HSV変換
    hsv_2 = np.copy(hsv)
    hsv_3 = np.copy(hsv_2)
    hsv_4 = np.copy(hsv_3)
    hsv_5 = np.copy(hsv_4)
    hsv_6 = np.copy(hsv_5)
    hsv_7 = np.copy(hsv_6)
    hsv_8 = np.copy(hsv_7)
    hsv_9 = np.copy(hsv_8)
    hsv_10 = np.copy(hsv_9)
    #hsv_2[:, :, 0] = np.where((hsv[:, :, 0]>5) & (hsv[:, :, 0]<30) ,hsv[:, :, 0] + 150,hsv[:, :, 0])
    #hsv_2[:, :, 2] = np.where((hsv[:, :, 2]>5) )
    #hsv_2[:, :, 2] = np.where((hsv[:, :, 0]>5) & (hsv[:, :, 0]<30) ,hsv[:, :, 2] *0.5,hsv[:, :, 2])#金髪から黒髪
    #hsv_2[:, :, 2] = np.where((hsv[:, :, 0]>5) & (hsv[:, :, 0]<30) ,hsv[:, :, 2] *0.5,hsv[:, :, 2])
    #hsv_2[:, :, 0] = np.where((hsv[:, :, 2]>0) & (hsv[:, :, 2]<128) ,hsv[:, :, 0] +110 ,hsv[:, :, 0])#黒(茶色)から青
    #hsv_2[:, :, 0] = np.where((hsv[:, :, 2]>0) & (hsv[:, :, 2]<128) ,hsv[:, :, 0] +50 ,hsv[:, :, 0])#黒(茶色)から緑
    #hsv_2[:, :, 0] = np.where((hsv[:, :, 2]>0) & (hsv[:, :, 2]<128) ,hsv[:, :, 0] +160 ,hsv[:, :, 0])#黒(茶色)から赤
    #bgr = cv2.cvtColor(hsv_2, cv2.COLOR_HSV2BGR)
    #cv2.imwrite(output_path,bgr)
    #hsv_2[:, :, 0] = np.where((hsv[:, :, 2]>0) & (hsv[:, :, 2]<128) ,hsv[:, :, 0] +50 ,hsv[:, :, 0])
    if color == 1:
        #色相を0に
        #hsv_2[:, :, 0] = np.where((hsv[:, :, 2]>=0) & (hsv[:, :, 2]<30) ,hsv[:, :, 0] *0.001 ,hsv[:, :, 0])
        #hsv_3[:, :, 0] = np.where((hsv_2[:, :, 2]>30) & (hsv_2[:, :, 2]<60) ,hsv_2[:, :, 0] *0.001 ,hsv_2[:, :, 0])#150~180
        #hsv_4[:, :, 0] = np.where((hsv_3[:, :, 2]>60) & (hsv_3[:, :, 2]<90) ,hsv_3[:, :, 0] *0.001 ,hsv_3[:, :, 0])#160~190
        #明度をあげる
        ##hsv_5[:, :, 2] = np.where((hsv_4[:, :, 2]>=0) & (hsv_4[:, :, 2]<30) ,hsv_4[:, :, 2] + 140 ,hsv_4[:, :, 2])#140~170
        ##hsv_6[:, :, 2] = np.where((hsv_5[:, :, 2]>30) & (hsv_5[:, :, 2]<60) ,hsv_5[:, :, 2] + 120 ,hsv_5[:, :, 2])#150~180
        ##hsv_7[:, :, 2] = np.where((hsv_6[:, :, 2]>60) & (hsv_6[:, :, 2]<90) ,hsv_6[:, :, 2] + 100 ,hsv_6[:, :, 2])#160~190
        #ここから
        #彩度を上げる
        hsv_5[:, :,
              1] = np.where((hsv_4[:, :, 2] >= 0) & (hsv_4[:, :, 2] < 30),
                            hsv_4[:, :, 1] + 230, hsv_4[:, :, 1])
        #hsv_6[:, :, 1] = np.where((hsv_5[:, :, 2]>30) & (hsv_5[:, :, 2]<60) ,hsv_5[:, :, 1] + 230 ,hsv_5[:, :, 1])
        #hsv_7[:, :, 1] = np.where((hsv_6[:, :, 2]>60) & (hsv_6[:, :, 2]<100) ,hsv_6[:, :, 1] + 230 ,hsv_6[:, :, 1])

        #色相を上げる
        hsv_8[:, :,
              0] = np.where((hsv_5[:, :, 2] >= 0) & (hsv_5[:, :, 2] < 30),
                            hsv_5[:, :, 0] + 80, hsv_5[:, :, 0])
        #hsv_9[:, :, 0] = np.where((hsv_8[:, :, 2]>30) & (hsv_8[:, :, 2]<60) ,hsv_8[:, :, 0] +80 ,hsv_8[:, :, 0])#150~180
        #hsv_10[:, :, 0] = np.where((hsv_9[:, :, 2]>60) & (hsv_9[:, :, 2]<100) ,hsv_9[:, :, 0]+80 ,hsv_9[:, :, 0])#160~190

        #明度を上げる
        hsv_11 = np.copy(hsv_8)
        hsv_11[:, :,
               2] = np.where((hsv_8[:, :, 2] >= 0) & (hsv_8[:, :, 2] < 30),
                             hsv_8[:, :, 2] + 180, hsv_8[:, :, 2])  #140~170

        hsv_12 = np.copy(hsv_11)
        #hsv_12[:, :, 2] = np.where((hsv_11[:, :, 2]>30) & (hsv_11[:, :, 2]<60) ,hsv_11[:, :, 2] + 160 ,hsv_11[:, :, 2])#150~180

        hsv_13 = np.copy(hsv_12)
        #hsv_13[:, :, 2] = np.where((hsv_12[:, :, 2]>60) & (hsv_12[:, :, 2]<100) ,hsv_12[:, :, 2] + 140 ,hsv_12[:, :, 2])#160~190
#ここまで
#色相追加
#彩度をあげる
##hsv_8[:, :, 1] = np.where((hsv_7[:, :, 2]>140) & (hsv_7[:, :, 2]<190) & (hsv_4[:, :, 2]>=0) & (hsv_4[:, :, 2]<30) ,hsv_7[:, :, 1] + 100 ,hsv_7[:, :, 1])
#hsv_8[:, :, 1] = np.where((hsv_7[:, :, 2]>140) & (hsv_7[:, :, 2]<190) & (hsv_7[:, :, 1]<100) ,hsv_7[:, :, 1] + 100 ,hsv_7[:, :, 1])#& (hsv_4[:, :, 1]<20)
#hsv_9[:, :, 1] = np.where(((hsv_8[:, :, 2]>150) & (hsv_8[:, :, 2]<180)) & (hsv_8[:, :, 1]<100) ,hsv_8[:, :, 1] + 100,hsv_8[:, :, 1])
#hsv_10[:, :, 1] = np.where(((hsv_9[:, :, 2]>160) & (hsv_9[:, :, 2]<190)) & (hsv_9[:, :, 1]<100) ,hsv_9[:, :, 1] + 100 ,hsv_9[:, :, 1])
#hsv_5[:, :, 2] = np.where((hsv_4[:, :, 2]>90) & (hsv_4[:, :, 2]<120) ,hsv_4[:, :, 2] + 80 ,hsv_4[:, :, 2])
#hsv_3[:, :, 1] = np.where(((hsv[:, :, 2]>30) & (hsv[:, :, 2]<128)) & (hsv[:, :, 1]<100) ,hsv_2[:, :, 1] + 100 ,hsv_2[:, :, 1],)
#hsv_3[:, :, 2] = np.where(((hsv_2[:, :, 2]>0) & (hsv_2[:, :, 2]<200)) & (hsv_2[:, :, 1]<255) ,hsv_2[:, :, 1] -190 ,hsv_2[:, :, 2])
#hsv_2[:, :, 0] = np.where((hsv[:, :, 0]>6) & (hsv[:, :, 0]<30) ,hsv[:, :, 0] + 50,hsv[:, :, 0]) #緑
    elif color == 2:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 2] > 6) & (hsv[:, :, 2] < 128),
                                  hsv[:, :, 0] + 110, hsv[:, :, 0])  #青
    elif color == 3:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 2] > 6) & (hsv[:, :, 2] < 128),
                                  hsv[:, :, 0] + 20, hsv[:, :, 0])  #黄色
    elif color == 4:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 2] > 0) & (hsv[:, :, 2] < 128),
                                  hsv[:, :, 0] + 176, hsv[:, :, 0])  #ピンク
        hsv_2[:, :, 1] = np.where((hsv[:, :, 0] >= 176), hsv[:, :, 1] + 96,
                                  hsv[:, :, 1])
    elif color == 5:
        hsv_2[:, :, 0] = np.where((hsv[:, :, 2] > 6) & (hsv[:, :, 2] < 128),
                                  hsv[:, :, 0] * 0.001, hsv[:, :, 0])  #赤色
    elif color == 6:
        hsv_2[:, :, 2] = np.where((hsv[:, :, 0] > 6) & (hsv[:, :, 0] < 30),
                                  hsv[:, :, 2] * 0.4, hsv[:, :, 2])  #黒色

    #茶色から指定色-----------------------------------------------------------------------------------------------------------
    #if color == 1:
    #hsv_2[:, :, 0] = np.where((hsv[:, :, 2]>0) & (hsv[:, :, 2]<255) ,hsv[:, :, 0] +50 ,hsv[:, :, 0])#黒(茶色)から緑
    #hsv_2[:, :, 0] = np.where((hsv[:, :, 0]>6) & (hsv[:, :, 0]<30) ,hsv[:, :, 0] + 50,hsv[:, :, 0]) #緑
    #elif color == 2:
    #hsv_2[:, :, 0] = np.where((hsv[:, :, 2]>6) & (hsv[:, :, 2]<128) ,hsv[:, :, 0] + 110,hsv[:, :, 0]) #青
    #elif color == 3:
    #hsv_2[:, :, 0] = np.where((hsv[:, :, 2]>6) & (hsv[:, :, 2]<128) ,hsv[:, :, 0] + 20,hsv[:, :, 0]) #黄色
    #elif color == 4:
    #hsv_2[:, :, 0] = np.where((hsv[:, :, 2]>0) & (hsv[:, :, 2]<128) ,hsv[:, :, 0] + 176,hsv[:, :, 0]) #ピンク
    #hsv_2[:, :, 1] = np.where((hsv[:, :, 0]>=176) ,hsv[:, :, 1] + 96 ,hsv[:, :, 1])
    #elif color == 5:
    #hsv_2[:, :, 0] = np.where((hsv[:, :, 2]>6) & (hsv[:, :, 2]<128) ,hsv[:, :, 0] *0.001,hsv[:, :, 0]) #赤色
    #elif color == 6:
    #hsv_2[:, :, 2] = np.where((hsv[:, :, 0]>6) & (hsv[:, :, 0]<30) ,hsv[:, :, 2] *0.4,hsv[:, :, 2]) #黒色
    #hsv_2[:, :, 2] = np.where((hsv_2[:, :, 0]>6) & (hsv_2[:, :, 0]<30) ,hsv_2[:, :, 1] *0.7,hsv_2[:, :, 2]) #黒色
    #0.001 赤
    #0.3 緑
    #bgr = cv2.cvtColor(hsv_2, cv2.COLOR_HSV2BGR)
    #----------------------------------------------------------------------------------------------------------------------

    bgr = cv2.cvtColor(hsv_11, cv2.COLOR_HSV2BGR)
    cv2.imwrite(output_path, bgr)


#hsv_2[:, :, 0] = np.where((hsv[:, :, 2]>0) & (hsv[:, :, 2]<128) ,hsv[:, :, 0] + 176,hsv[:, :, 0]) #濃い茶色
#hsv_2[:, :, 1] = np.where((hsv[:, :, 2]>0) & (hsv[:, :, 2]<128) ,hsv[:, :, 1] + 96 ,hsv[:, :, 1])

#白
#hsv_2[:, :, 1] = np.where((hsv[:, :, 2]>0) & (hsv[:, :, 2]<128) ,hsv[:, :, 1] + 180 ,hsv[:, :, 1])
#hsv_3[:, :, 2] = np.where((hsv_2[:, :, 2]>0) & (hsv_2[:, :, 2]<128) ,hsv_2[:, :, 2] + 180 ,hsv_2[:, :, 2])