在图像中保留不是b的像素

2024-05-16 09:33:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想处理一个彩色图像,只保留不是黑色的像素。我写了一些代码,但它似乎并不像我认为的那样:

import cv2
import numpy as np
import tensorly as tl

def eq_svtn(T, b, M, Omega):
X = 0
for i in range(3):
    X += b[i]*tl.fold(M[i],i,T.shape)
X = X/np.sum(b)

for i in range(T.shape[0]):
    for j in range(T.shape[1]):
        if Omega[i, j, :].all() != 0:
            X[i, j, :] = T[i, j, :]
return X

def silrtc(X,b,K,O):
a = np.random.dirichlet(np.ones(3),size=1)
a = a[0,:]
M = [0, 0, 0]
for i in range(K):
    X1 = tl.unfold(X, 0)
    X2 = tl.unfold(X, 1)
    X3 = tl.unfold(X, 2)
    u1, s1, v1 = LA.svd(X1, full_matrices=False)
    u2, s2, v2 = LA.svd(X2, full_matrices=False)
    u3, s3, v3 = LA.svd(X3, full_matrices=False)
    U = [u1,u2,u3]
    V = [v1,v2,v3]
    S = [s1,s2,s3]
    for j in range (3):
        M[j] = shrinkage_op(U[j],V[j],S[j],a[j]/b[j],X.shape)
    X = eq_svtn(X,b,M,O)
return X


im = cv2.imread('target.jpg')
original_shape = im.shape
zzo = np.zeros(im.shape)
shp = (int(original_shape[0]),int(mt.floor(0.9*original_shape[1])),int(original_shape[2]))
shp2 = (int(original_shape[0]),int(mt.ceil(0.1*original_shape[1])),int(original_shape[2]))
zz = np.zeros(shp)
oo = 0xFF*np.ones(shp2)
cct = np.concatenate((zz,oo),1)
np.random.shuffle(cct.reshape(-1, 3))
cct = cct.astype(np.uint8)
im2 = np.bitwise_and(im,cct)
b = [0.0001, 0.0001,0.0001]
xx = silrtc(im2,b,64,cct)

黑色像素是[0,0,0],b是由3个浮点组成的数组,M是一组二维数组(矩阵),T是包含黑色像素的图像(三维数组,张量),X是去除黑色像素的彩色图像。你知道吗


Tags: inimportfornprange像素laint