[英]OpenCV: filter by color in image
如何在python中使用OpenCV僅顯示RGB圖像中的單一顏色? 想法是能夠以與RGB光譜中某種顏色的強度相對應的灰度打印圖像。
您可以使用split
:
blue, green, red = cv2.split(img)
或者,因為這在時間上是一項昂貴的操作,並且您只需要一個通道(例如紅色),就可以像下面這樣直接切片原始圖像:
red = img[:,:,2]
這將返回灰度圖像,其中較亮的像素是原始圖像中具有較高紅色值的像素。
希望這對您有所幫助!
# import usefull libraries
import numpy as np
import cv2
# Set global parameters
RED = 0
GREEN = 1
BLUE = 2
# Load the image
img_color = cv2.imread("fruits.jpg", flags=cv2.IMREAD_COLOR)
# Filter the image by desired color
img_color_filtered = np.asarray([y[RED] for x in img_color for y in x]).reshape((img_color.shape[:2]))
img = Image.open(f"./smoke/first/mistake_cat" + ".png")
data = np.array(img)
red, green, blue, alpha = data.T # Трансформируем матрицу и распаковываем
img_red = Image.fromarray(red)
img_green = Image.fromarray(green)
img_blue = Image.fromarray(blue)
img_alpha = Image.fromarray(alpha)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.