local binary pattern transform

479 views Asked by At

Here is my local binary pattern function:

def lbp(x):

imgUMat = np.float32(x)

gray = cv2.cvtColor(imgUMat, cv2.COLOR_RGB2GRAY)

radius = 2
n_points = 8 * radius

METHOD = 'uniform'
    
lbp = local_binary_pattern(gray, n_points, radius, METHOD)
lbp = torch.from_numpy(lbp).long()
    
return lbp

Here I call lbp function:

input_img = plt.imread(trn_fnames[31])
x = lbp(input_img)

When I use x.shape it is:

torch.Size([600, 600])

Sounds good!!!

But my problem is when I use transforms.Lambda(lbp) in my transform function, my output image is torch.Size([600])

tfms = transforms.Compose([

transforms.Lambda(lbp)])

train_ds = datasets.ImageFolder(trn_dir, transform = tfms)

(train_ds[0][0][0]).shape
torch.Size([600])!!! >>>> my problem

I need torch.Size([600, 600])

I also different ways such as this:

tfms = transforms.Compose([

transforms.Lambda(lbp),
transforms.ToPILImage(),
transforms.Resize((sz, sz))])

And I got this error:

TypeError: pic should be Tensor or ndarray. Got <class ‘torch.Tensor’>.

I also added

transforms.ToTensor()])

But still have the same error:

TypeError: pic should be Tensor or ndarray. Got <class ‘torch.Tensor’>.

I’ll appreciate to your comments please! Thank you.

1

There are 1 answers

0
Vahid Zarghami On
def lbp_transform(x):
    radius = 2
    n_points = 8 * radius
    METHOD = 'uniform'
    imgUMat = np.float32(x)
    gray = cv2.cvtColor(imgUMat, cv2.COLOR_RGB2GRAY)
    lbp = local_binary_pattern(gray, n_points, radius, METHOD)
    lbp = torch.from_numpy(lbp).float()
    return lbp


x = np.random.randn(600, 600, 3)
out = lbp_transform(x)
print(out.shape)
> torch.Size([600, 600])

tfms = transforms.Compose([
    transforms.Lambda(lbp_transform),
    transforms.ToPILImage(),
    transforms.Resize((300, 300)),
    transforms.ToTensor()
])

out = tfms(x)
print(out.shape)
> torch.Size([1, 300, 300])