Skip to content

RGB color images cause error #1

@CallMeMax

Description

@CallMeMax

Brief Summary

Using the jupyter notebooks ADMM or GD with RGB color images does not work and causes the notebook to raise a python error.

Steps to Reproduce

  1. In cell [2] in the ADMM.ipynb and GD.ipynb notebooks, change the path for psfname and/or imgname to a RGB image (e.g., .jpg file).
  2. run the notebook

Expected Versus Actual Behavior

Apply the ADMM or GD algorithm to the grayscale image of the input. Usually the color range of the image is mainly grayscale, but the file format still loads it as 3 color channels.

Python error stack trace

Cell [23] in ADMM.ipynb

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[23], line 2
      1 #psf, data = loadData(True)
----> 2 final_im = runADMM(psf, data)
      3 plt.imshow(final_im, cmap='gray')
      4 plt.title('Final reconstructed image after {} iterations'.format(iters))

Cell In[22], line 2, in runADMM(psf, data)
      1 def runADMM(psf, data):
----> 2     H_fft = precompute_H_fft(psf)
      3     X,U,V,W,xi,eta,rho = init_Matrices(H_fft)
      4     X_divmat = precompute_X_divmat()

Cell In[20], line 2, in precompute_H_fft(psf)
      1 def precompute_H_fft(psf):
----> 2     return fft.fft2(fft.ifftshift(CT(psf)))

Cell In[10], line 12, in CT(b)
     10 v_pad = (full_size[0] - sensor_size[0])//2
     11 h_pad = (full_size[1] - sensor_size[1])//2
---> 12 return np.pad(b, ((v_pad, v_pad), (h_pad, h_pad)), 'constant',constant_values=(0,0))

File <__array_function__ internals>:180, in pad(*args, **kwargs)

File ~\Documents\lab_submisison\.venv\lib\site-packages\numpy\lib\arraypad.py:743, in pad(array, pad_width, mode, **kwargs)
    740     raise TypeError('`pad_width` must be of integral type.')
    742 # Broadcast to shape (array.ndim, 2)
--> 743 pad_width = _as_pairs(pad_width, array.ndim, as_index=True)
    745 if callable(mode):
    746     # Old behavior: Use user-supplied function with np.apply_along_axis
    747     function = mode

File ~\Documents\lab_submisison\.venv\lib\site-packages\numpy\lib\arraypad.py:518, in _as_pairs(x, ndim, as_index)
    514     raise ValueError("index can't contain negative values")
    516 # Converting the array with `tolist` seems to improve performance
    517 # when iterating and indexing the result (see usage in `pad`)
--> 518 return np.broadcast_to(x, (ndim, 2)).tolist()

File <__array_function__ internals>:180, in broadcast_to(*args, **kwargs)

File ~\Documents\lab_submisison\.venv\lib\site-packages\numpy\lib\stride_tricks.py:412, in broadcast_to(array, shape, subok)
    366 @array_function_dispatch(_broadcast_to_dispatcher, module='numpy')
    367 def broadcast_to(array, shape, subok=False):
    368     """Broadcast an array to a new shape.
    369 
    370     Parameters
   (...)
    410            [1, 2, 3]])
    411     """
--> 412     return _broadcast_to(array, shape, subok=subok, readonly=True)

File ~\Documents\lab_submisison\.venv\lib\site-packages\numpy\lib\stride_tricks.py:348, in _broadcast_to(array, shape, subok, readonly)
    345     raise ValueError('all elements of broadcast shape must be non-'
    346                      'negative')
    347 extras = []
--> 348 it = np.nditer(
    349     (array,), flags=['multi_index', 'refs_ok', 'zerosize_ok'] + extras,
    350     op_flags=['readonly'], itershape=shape, order='C')
    351 with it:
    352     # never really has writebackifcopy semantics
    353     broadcast = it.itviews[0]

ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (2,2)  and requested shape (3,2)

Cell [13] in GD.ipynb

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[13], line 2
      1 psf, data = loaddata()
----> 2 final_im = grad_descent(psf, data)
      3 plt.imshow(final_im, cmap='gray')
      4 plt.title('Final reconstruction after {} iterations'.format(iters))

Cell In[9], line 2, in grad_descent(h, b)
      1 def grad_descent(h, b):
----> 2     H, Hadj, v, utils = initMatrices(h)
      3     crop = utils[0]
      4     pad = utils[1]

Cell In[5], line 12, in initMatrices(h)
     10 endj = startj + init_shape[1]
     11 hpad = np.zeros(padded_shape)
---> 12 hpad[starti:endi, startj:endj] = h
     14 H = fft.fft2(fft.ifftshift(hpad), norm="ortho")
     15 Hadj = np.conj(H)

ValueError: could not broadcast input array from shape (90,160,3) into shape (90,160,8)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions