0

For my research, I would like to compile a code to change the picture pixel value from RGB to HSV and then to RGB if the V value is above a certain value. But, to do that, I have to use cv2.cvtColor(src,code). When I used cv2.cvtColor(image, cv2.COLOR_BGR2HSV), It worked nice! But When I used cv2.cvtColor( list or str , cv2.COLOR_HSV2BGR), It made ERROR.... I think src has to be image. Unfortunately, I had to use type list or str or ndarray.. I tried hard to make them image, but it's really hard task I think.. If you run the code below, numpy array is showing only with V channel first. After close the window, I can see this error. Traceback (most recent call last):

File "/home/sjw/PycharmProjects/untitled/hiccc.py", line 18, in img[i][j] = cv2.cvtColor(y, cv2.COLOR_HSV2BGR) TypeError: src is not a numpy array, neither a scalar

But my question is not only knowing this error but also made a completed code. I need your help..Please.... :(

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('/home/sjw/Pictures/seulgi.jpg')
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
img_h, img_s, img_v = cv2.split(img2)

plt.imshow(img_v, interpolation='nearest')
plt.show()

for i in range(1, 1001):
for j in range(1, 1501):
    if img_v[i][j] > 125:
        y=''.join(str(v) for v in img_v[i:i][j:j])
        print(type(y))

        img[i][j] = cv2.cvtColor(y, cv2.COLOR_HSV2BGR)

cv2.imshow('sadd',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Kinght 金
  • 17,681
  • 4
  • 60
  • 74
ISHS
  • 9
  • 3

1 Answers1

0

For the whole image, replace the image where V(in HSV) <|> th with HSV.

enter image description here

import cv2
import numpy as np

fname = "test.png"
img = cv2.imread(fname)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

## darker 
pos1 = np.where(hsv[:,:,-1]<200)
dst1 = img.copy()
dst1[pos1] = hsv[pos1]
cv2.imwrite("dst1.png", dst1)

## lighter 
pos2 = np.where(hsv[:,:,-1]>200)
dst2 = img.copy()
dst2[pos2] = hsv[pos2]
cv2.imwrite("dst2.png", dst2)
Kinght 金
  • 17,681
  • 4
  • 60
  • 74
  • oh, when I applied your brilliant code, I was really surprised.... But to understand it, would you let me know about hsv[:,:,-1]? I can understand that is meaning V channel but I can't understand about -1 – ISHS Oct 18 '18 at 10:34
  • `hsv` is 3-channels image: `hsv[:,:,2] => hsv[:,:,-1] => V` – Kinght 金 Oct 19 '18 at 00:32