使用 python 从图像中提取颜色
Extract colors from image using python
我有一张黑白图像和一张带有彩色笔触的图像。我想要做的是提取颜色笔划,模糊它们,然后将原始图像与模糊图像混合。我通过将两个图像彼此相减来提取颜色笔划,但我在黑色背景上得到这些颜色笔划,而我需要它们在白色上与原始图像混合。这是我的代码的一部分:
def imageblur(cimg):
return cv2.blur(cimg, (50, 50))
bw = glob('path1')
colorful = glob('path2')
output_dir = 'path3'
index = 0
for i,j in zip(bw, colorful):
img1 = cv2.imread(i)
img2 = cv2.imread(j)
color = cv2.subtract(img1,img2)
color = imageblur(color)
mask = Image.fromarray(np.uint8(color))
img = Image.fromarray(np.uint8(img1))
im = Image.blend(img, mask, 0.5)
#color = img1 + color
im.save(os.path.join(output_dir, str(index) + '.jpg'))
index += 1
print(index)
样本images
如果您想将 RGB 图像的背景从黑色更改为白色,您可以这样做:
#thresholds all black color in your "color-on-black-background" image
#to maximum value (255,255,255) (white) and sets the rest (yours colors)
#to (0,0,0) (black)
thresholded=cv2.inRange(img,(0,0,0),(0,0,0))
#add both images
res=img+cv2.cvtColor(thresholded,cv2.COLOR_GRAY2BGR)
输入(图片):
阈值(阈值):
输出(分辨率):
使用样本图像得到结果。处理略有改动。
import numpy as np
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
def show(title, img, color=True):
if color:
plt.imshow(img[:,:,::-1]), plt.title(title), plt.show()
else:
plt.imshow(img, cmap='gray'), plt.title(title), plt.show()
img = cv2.imread('color_strokes.jpg')
show('original', img)
mask=cv2.inRange(img,(0,0,0),(150,150,150))
show('mask', mask, False)
res=255-cv2.cvtColor(mask,cv2.COLOR_GRAY2BGR)
show('result', res, False)
输出:
image = plt.imread('image.jpg')
# Extract 2-D arrays of the RGB channels: red, blue, green
red, blue, green = image[:,:,0], image[:,:,1], image[:,:,2]
从图像中提取颜色:
import colorgram
# It is the number of colors you want to extract from the image
nb_colors = 30
# Returns a list of tuples form (r, g, b)
image_colors = colorgram.extract("image.jpg", nb_colors)
我有一张黑白图像和一张带有彩色笔触的图像。我想要做的是提取颜色笔划,模糊它们,然后将原始图像与模糊图像混合。我通过将两个图像彼此相减来提取颜色笔划,但我在黑色背景上得到这些颜色笔划,而我需要它们在白色上与原始图像混合。这是我的代码的一部分:
def imageblur(cimg):
return cv2.blur(cimg, (50, 50))
bw = glob('path1')
colorful = glob('path2')
output_dir = 'path3'
index = 0
for i,j in zip(bw, colorful):
img1 = cv2.imread(i)
img2 = cv2.imread(j)
color = cv2.subtract(img1,img2)
color = imageblur(color)
mask = Image.fromarray(np.uint8(color))
img = Image.fromarray(np.uint8(img1))
im = Image.blend(img, mask, 0.5)
#color = img1 + color
im.save(os.path.join(output_dir, str(index) + '.jpg'))
index += 1
print(index)
样本images
如果您想将 RGB 图像的背景从黑色更改为白色,您可以这样做:
#thresholds all black color in your "color-on-black-background" image
#to maximum value (255,255,255) (white) and sets the rest (yours colors)
#to (0,0,0) (black)
thresholded=cv2.inRange(img,(0,0,0),(0,0,0))
#add both images
res=img+cv2.cvtColor(thresholded,cv2.COLOR_GRAY2BGR)
输入(图片):
阈值(阈值):
输出(分辨率):
使用样本图像得到结果。处理略有改动。
import numpy as np
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
def show(title, img, color=True):
if color:
plt.imshow(img[:,:,::-1]), plt.title(title), plt.show()
else:
plt.imshow(img, cmap='gray'), plt.title(title), plt.show()
img = cv2.imread('color_strokes.jpg')
show('original', img)
mask=cv2.inRange(img,(0,0,0),(150,150,150))
show('mask', mask, False)
res=255-cv2.cvtColor(mask,cv2.COLOR_GRAY2BGR)
show('result', res, False)
输出:
image = plt.imread('image.jpg')
# Extract 2-D arrays of the RGB channels: red, blue, green
red, blue, green = image[:,:,0], image[:,:,1], image[:,:,2]
从图像中提取颜色:
import colorgram
# It is the number of colors you want to extract from the image
nb_colors = 30
# Returns a list of tuples form (r, g, b)
image_colors = colorgram.extract("image.jpg", nb_colors)