2

I was just seeing this example and trying to denoise an Image by using Dictionary Learning. This code is working for gray scale Image but not working for 3 chanel or Color Image.

here is my code =>

from time import time

import matplotlib.pyplot as plt
import numpy as np
import scipy as sp

from sklearn.decomposition import MiniBatchDictionaryLearning
from sklearn.feature_extraction.image import extract_patches_2d
from sklearn.feature_extraction.image import reconstruct_from_patches_2d

from sklearn.utils.fixes import sp_version
from sklearn.datasets import load_sample_image

from scipy import ndimage

from skimage import color
from skimage import io

from PIL import Image

from sklearn.decomposition import SparseCoder
from sklearn.decomposition import sparse_encode

from skimage import data,restoration
from scipy.misc import imfilter, imread
from scipy.signal import convolve2d as conv2


from skimage import data, img_as_float
from skimage.restoration import denoise_nl_means


from scipy import ndimage as ndi
from skimage import feature

c = np.asarray(Image.open('starfish.jpg'))
n0 = np.asarray(Image.open('starfish.jpg'))

c = c / 255
n0 = n0 / 255 

height, width, chanel = n0.shape

n0 = n0 + 0.075 * np.random.randn(height, width, chanel)

patchsize = (7,14)
t0 = time()

data = extract_patches_2d(c,(7,14))
data = data.reshape(data.shape[0], -1)

data = data - np.mean(data, axis=0)
data = data / np.std(data, axis=0)

t1 = time()
print('Total time : ',round((t1-t0),2),' sec')

print('Learning the dictionary ....')

t2 = time()
n_iter = 1000

dico = MiniBatchDictionaryLearning(n_components=100,alpha=2,n_iter=n_iter)

V = dico.fit(data).components_
print(V.shape)
t3 = time()
print('No of iteration : ',n_iter)
print('Total time taken for Dictionary learning : ',round((t3-t2),2),' sec')

t4 = time()

n0_data = extract_patches_2d(n0,(7,14))
print(n0_data.shape)
print(n0_data)

n0_data = n0_data.reshape(n0_data.shape[0], -1)

intercept = np.mean(n0_data, axis=0)
n0_data = n0_data - intercept


dico.set_params(transform_algorithm='omp',transform_n_nonzero_coefs = 1)
code = dico.transform(n0_data)
patches = np.dot(code,V)

patches = patches + intercept
print(patches)
print(patches.shape)
print(patches[0].shape)

patches = patches.reshape(len(n0_data), *patchsize)


result =  reconstruct_from_patches_2d(patches,(height, width))

denoise = denoise_nl_means(result, 1, 9, 0.08, multichannel=True)

edge =feature.canny(denoise,sigma=3)

print(edge)


plt.imshow(result,cmap='gray')
plt.show()


print('Total time taken for sparse modeling : ',round((time()-t4),2))

This same code is working perfectly if I convert in Gray Scale by using

this lines =>

c = np.asarray(Image.open('starfish.jpg').convert('L'))
n0 = np.asarray(Image.open('starfish.jpg').convert('L'))

error is =>

File "color.py", line 103, in

patches = patches.reshape(len(n0_data), *patchsize)

ValueError: total size of new array must be unchanged

How do I solve this problem??

Community
  • 1
  • 1
Sudip Das
  • 1,178
  • 1
  • 9
  • 24
  • 2
    I think a similar question was asked before (not the same so I'm not saying it's a duplicate but it may help you), have a look here : http://stackoverflow.com/questions/26295491/valueerror-total-size-of-new-array-must-be-unchanged – zoubida13 Mar 01 '17 at 15:26
  • thanks for sharing can u tell me what will be the shape for my program @zoubida13 – Sudip Das Mar 01 '17 at 16:24
  • Post complete stack trace of error. – Vivek Kumar Mar 02 '17 at 01:35
  • The error stems from the fact that when using grayscale images - `patches.shape=(46375, 98)`. So reshape works because 98 can be converted into (7,14). But when using all channels, `patches.shape=(46375, 294)`. So it cannot be converted into (7,14) but can be converted into (7,14,3). – Vivek Kumar Mar 02 '17 at 06:02
  • @VivekKumar yes, but at last How do I convert that one?? – Sudip Das Mar 02 '17 at 06:25
  • @VivekKumar I just need conversion code where I have made mistake – Sudip Das Mar 02 '17 at 06:26
  • I just want to do this one for color image, How i will do this what are the changes needed ?? @VivekKumar – Sudip Das Mar 02 '17 at 07:01
  • Your code you posted only handles height and width, not the channels. You need to handle the channels, wherever you are getting patches from the image or image from the patches – Vivek Kumar Mar 02 '17 at 10:39
  • yes,you are telling me for that line =>n0_data = extract_patches_2d(n0,(7,14)) – Sudip Das Mar 02 '17 at 12:25
  • but after extracting the patches if i print =>print(n0_data.shape).. then it is giving me (46248, 7, 14, 3) ... means 46248 no of patches, (7,14) size and 3 no of channel. – Sudip Das Mar 02 '17 at 12:32
  • @VivekKumar can I contact u personally by fb or mail. I have in serious problem. – Sudip Das Mar 02 '17 at 12:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137057/discussion-between-vivek-kumar-and-sudip-das). – Vivek Kumar Mar 02 '17 at 12:48

0 Answers0