繁体   English   中英

通过条件操纵像素来掩盖图像

[英]Masking an Image by Manipulating Pixels through Conditions

我已经使用RGBA颜色空间在python中读取了一个图像。 图像的大小是640乘960并存储到名为img_array的数组中。 现在,数组中的每个元素都包含[R,G,B,A],例如[21,34,53,255]。 我想通过将像素转换为黑色[0,0,0,255]来过滤我的图像像素,这不符合下面的条件。

R > 95 and G > 40 and B > 20 and R > G and R > B and | R - G | > 15 and A > 15

我将如何在python中执行此操作? 我所知道的是使用cv2.inrange()将像素设置为黑色,这不在下边界和上边界内。 以下是我的示例代码:

 #import the necessary packages
 import imutils
 import numpy as np
 import argparse
 import cv2

 # construct the argument parse and parse the arguments
 ap = argparse.ArgumentParser()
 ap.add_argument("-i", "--image",help = "path to the image file")
 args = vars(ap.parse_args())

 #read image with alpha channel
 img_array = cv2.imread(args["image"], -1)

 rgba_lower_bound = np.array([0, 59, 59,2], dtype = "uint8")
 rgba_upper_bound = np.array([20, 255, 255,255], dtype = "uint8")    
 skinMask = cv2.inRange(img_array, rgb_lower_bound, rgb_upper_bound)
 skin = cv2.bitwise_and(img_array, img_array, mask = skinMask)
 cv2.imshow("images", skin)

请帮我解决一下这个。

假设R, G, B, A都是具有相同形状的numpy数组,由以下内容创建:

R, G, B, A = cv2.split(img_array)

只需使用相同的条件创建一个掩码; 因为它们是numpy数组,所以使用&而不是and

mask = (R > 95) & (G > 40) & (B > 20) & (R > G) & (R > B) & (abs(R - G) > 15) & (A > 15)

然后设置不满足条件黑色的一切:

img_array[~mask] = [0, 0, 0, 255]

注意这里的mask将是双声道的,并将被广播到img_array所有声道。 还要注意~反转一个numpy布尔数组,所以这是maskFalse索引,这就是你想要的。


关于透明度的更多信息:如果alpha通道为0,则表示完全透明,如果为255(对于无符号的8位图像),则表示不透明。 如果您希望图像在这些位置透明而不是黑色,您只需将掩模反转,将其转换为uint8数组,然后将其合并回一个图像,如下所示:

R, G, B, A = cv2.split(img_array)
mask = (R > 95) & (G > 40) & (B > 20) & (R > G) & (R > B) & (abs(R - G) > 15) & (A > 15)
new_A = 255*(~mask).astype(np.uint8)
new_img_array = cv2.merge([R, G, B, new_A])

这样你就不会丢失R,G,B中的任何颜色信息,如果你想保留它。

你可以这样做:

def set_to_black(T, image):
    # grab the image dimensions
    h = image.shape[0]
    w = image.shape[1]

    # loop over the image, pixel by pixel
    for y in range(0, h):
        for x in range(0, w):
            if (conditional) # I couldn't be bothered to write it all out
            # Set the pixel to black
            image[y, x] = [0, 0, 0, 255]

    # return the thresholded image
    return image

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM