Python 使用阈值图像更改实际图像中的亮度值
我想做什么:Python 使用阈值图像更改实际图像中的亮度值,python,python-3.x,image,numpy,opencv,Python,Python 3.x,Image,Numpy,Opencv,我想做什么: img = cv2.imread('1.bmp') img = cv2.GaussianBlur(img,(5,5),0) """ Take the image and slipt it into the HSV colour spectrem """ h,s,v= cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV)) """ Equalise the histogram for the V value """ eq_V = c
img = cv2.imread('1.bmp')
img = cv2.GaussianBlur(img,(5,5),0)
""" Take the image and slipt it into the HSV colour spectrem """
h,s,v= cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV))
""" Equalise the histogram for the V value """
eq_V = cv2.equalizeHist(v)
""" Merge all the values back into one image """
eq_image = cv2.cvtColor(cv2.merge([h, s, eq_V]), cv2.COLOR_HSV2BGR)
ret1,th1 = cv2.threshold(eq_image,127,255,cv2.THRESH_BINARY)
[rows, columns, channels] = img.shape
blacks = np.zeros((rows,columns,channels))
whites = np.zeros((rows,columns,channels))
for row in range(rows):
for column in range(columns):
if th1[row,column].all() == 0:
""" Equalise the v value """
blacks[row,column] = v[row,column]
else:
""" Equalise the v value """
whites[row,column] = v[row,column]
addTogether = cv2.add(blacks,whites)
cv2.imshow("frame", addTogether.astype(np.uint8))
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('1.bmp')
img = cv2.GaussianBlur(img,(5,5),0)
""" Take the image and slipt it into the HSV colour spectrem """
h,s,v= cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV))
""" Equalise the histogram for the V value """
eq_V = cv2.equalizeHist(v)
""" Merge all the values back into one image """
eq_image = cv2.cvtColor(cv2.merge([h, s, eq_V]), cv2.COLOR_HSV2BGR)
ret1,th1 = cv2.threshold(eq_image,127,255,cv2.THRESH_BINARY)
[rows, columns, channels] = img.shape
blacks = np.zeros((rows,columns,channels))
whites = np.zeros((rows,columns,channels))
for row in range(rows):
for column in range(columns):
if th1[row,column].all() == 0:
""" Equalise the v value """
blacks[row,column] = v[row,column]
else:
""" Equalise the v value """
whites[row,column] = v[row,column]
addTogether = cv2.add(blacks,whites)
cv2.imshow("frame", addTogether.astype(np.uint8))
cv2.waitKey(0)
cv2.destroyAllWindows()
我的问题
我正在努力的部分是第4步。如上所述。我目前正在构建一个基于whats black和whats what what what what what what what what white的图像,然后将这些图像添加到一起,以返回到我的原始图像状态
我似乎无法做/弄清楚的是如何调整图像hsv的v值,然后如何将整个图像与色调和饱和度值旁边的新v值合并在一起
来自最新尝试的图像
@Sounak im使用该行表示如果
th1
中的像素是黑色的,那么我想更改该像素的亮度(在这种情况下,将其稍微暗一点)在eq_图像
中,如果在th1
中为白色,则在eq_图像
中将像素的亮度更改为稍亮一点,我认为您的addTogether
与v
完全相同,但有3个通道,因为我不确定如何在黑色和白色numpy数组中调整v
,th\u v\u up=(cv2.bitwise\u和(v,th1)+cv2.bitwise\u和(np.ones(v.shape).astype(np.uint8)*5,th1))
将与th1
中的白色像素相对应的像素值增加5th1\u n=cv2。按位\u非(th1)
按位\u向下=(cv2.按位\u和(v,th1\u n)-cv2.按位\u和(np.one(v.shape).aType(np.uint8)*5,th1\n))
@Sounak im使用该行表示如果th1
中的像素是黑色的,那么我想更改该像素的亮度(在这种情况下,使其稍微暗一点)在eq_图像
中,如果在th1
中为白色,则在eq_图像
中将像素的亮度更改为稍亮一点,我认为您的addTogether
与v
完全相同,但有3个通道,因为我不确定如何在黑色和白色numpy数组中调整v
,th\u v\u up=(cv2.bitwise\u和(v,th1)+cv2.bitwise\u和(np.ones(v.shape).astype(np.uint8)*5,th1))
将把与th1
中的白色像素对应的像素值增加5th1\u n=cv2.bitwise\u not(th1)
th\u v\u down=(cv2.bitwise\u和(v,th1\u n)-cv2.bitwise\u和(np.ones(v.shape).astype(np.uint8)*5,th1\u n))