Python + OpenCV:使用来自另一个图像的RGB像素屏蔽图像

时间:2014-01-13 22:10:12

标签: python arrays image opencv matrix

我有一个RGB视频和该视频中的一个关键帧。在该关键帧中,用户将应用二进制掩码。 我想创建一个视频掩码,其中像素具有关键帧掩码区域中存在的值。

换句话说,我想创建一个存在于关键帧掩码中的RGB像素值列表,并在列表中存在像素值的条件下创建所有其他帧的掩码。像素值可以是(0,0,0) - (255,255,255)

我目前的实施虽然在技术上是正确的,但效率极低,而且我认为必须有更好的东西。

    count = 0
    for x in sequence
        img = cv2.imread(x)
        curr = np.zeros(img.shape[:2],dtype = np.uint16)
        for x in range(img.shape[0]):
            for y in range(img.shape[1]):
               tuple = (img[x][y][0],img[x][y][1],img[x][y][2])
                if tuple not in dict:
                    dict[tuple] = count
                    curr[x][y] = count
                    count+=1
                else:
                    curr[x][y] = dict[tuple]
        newsequence.append(curr)


    #in another function, generate mask2, the mask of the keyframe
    immask = cv2.bitwise_and(newsequence[keyframe],newsequence[keyframe],mask=mask2[index].astype('uint8'))
    immask = [x for x in immask.flatten() if x != 0]

    #for thresholding purposes (if at least 80% of pixels with that value are selected in the keyframe)
    valcount= np.bincount(immask)
    truecount = np.bincount(newsequence[keyframe].flatten())
    frameset = set(immask)
    framemask = list(frameset)
    framemask = [x for x in framemask if (float(valcount[x])/float(truecount[x]))>0.8]

    for frame in range(0,numframes):
        for val in framemask:        
            mask[frame] = np.where((newsequence[frame]==val),255,0).astype('uint8')

0 个答案:

没有答案