実現したいこと
スパースモデリングの画像を作りたいです。
発生している問題・分からないこと
それを機能させようとネットのコードを広い実行してみましたがうまいこと行きません。最後のmain関数を実行するときに Gauss_Saidel関数の中が良くない動きをしているようです。
エラーメッセージ
error
1Traceback (most recent call last): 2 File "C:\Users\asano\Desktop\20t413\スパース表現\py1\py5.py", line 107, in <module> 3 main() 4 File "C:\Users\asano\Desktop\20t413\スパース表現\py1\py5.py", line 70, in main 5 u_n = Gauss_Saidel(u,d_x,d_y, b_x ,b_y,f, MU,LAMBDA) 6 File "C:\Users\asano\Desktop\20t413\スパース表現\py1\py5.py", line 42, in Gauss_Saidel 7 U = np.hstack([u[:,1:X_N], np.reshape(u[-1,:],[Y_N,1] )]) + np.hstack([np.reshape(u[0,:],[Y_N,1]), u[:,0:Y_N-1]]) \ 8 File "<__array_function__ internals>", line 180, in hstack 9 File "C:\Users\asano\AppData\Roaming\Python\Python39\site-packages\numpy\core\shape_base.py", line 345, in hstack 10 return _nx.concatenate(arrs, 1) 11 File "<__array_function__ internals>", line 180, in concatenate 12ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 1333 and the array at index 1 has size 2000
該当のソースコード
import bdb import dataclasses import numpy as np import cv2 import matplotlib.pyplot as plt import random def add_noise(I_t): mu = np.mean(I_t) sigma = np.std(I_t) dB = 3 I_noise = 10**(-dB/20)*np.reshape([random.gauss(mu, sigma) for i in range(np.size(I_t))], np.shape(I_t)) I = I_t + I_noise max_I = np.max(I) min_I = np.min(I) I = np.round((I - min_I)*255/(max_I - min_I)) return I img_path = "sample.png" img_load = cv2.imread(img_path) I_t = cv2.cvtColor(img_load, cv2.COLOR_RGB2GRAY) f = add_noise(I_t) [X_N,Y_N] = np.shape(f) plt.subplot(1,2,1) plt.imshow(I_t) plt.title("Original") plt.subplot(1,2,2) plt.imshow(f) plt.title("Noisy") def add_noise(I_t): mu = np.mean(I_t) sigma = np.std(I_t) dB = 3 I_noise = 10**(-dB/20)*np.reshape([random.gauss(mu, sigma) for i in range(np.size(I_t))], np.shape(I_t)) I = I_t + I_noise max_I = np.max(I) min_I = np.min(I) I = np.round((I - min_I)*255/(max_I - min_I)) return I def Gauss_Saidel(u, d_x, d_y, b_x, b_y, f, MU, LAMBDA): U = np.hstack([u[:,1:X_N], np.reshape(u[-1,:],[Y_N,1] )]) + np.hstack([np.reshape(u[0,:],[Y_N,1]), u[:,0:Y_N-1]]) \ + np.vstack([u[1:X_N,:], np.reshape(u[:,-1],[1,X_N] )]) + np.vstack([np.reshape(u[:,0],[1,X_N] ), u[0:X_N-1,:]]) D= np.vstack([np.pad(d_x[:,0],[1,X_N] ), d_x[0:Y_N-1,:]]) - d_x \ + np.hstack([np.pad(d_y[0,:],[Y_N,1] ), d_y[:,0:X_N-1]]) - d_y B = -np.vstack([np.pad(b_x[:,0],[1,X_N] ), b_x[0:Y_N-1,:]]) + b_x \ - np.hstack([np.pad(b_y[0,:],[Y_N,1] ), b_y[:,0:X_N-1]]) + b_y R=U+D+bdb W=MU X=LAMBDA G = X/(W+ 4*X)*R+ W/(W + 4*X)*f return G def shrink(x,y): t = np.abs(x) - y S = np.sign(x)*(t > 0) * t return S def main(): CYCLE = 100 MU = 5.0*10**(-2) LAMBDA = 1.0*10**(-2) TOL = 5.0*10**(-1) u = f d_x = np.zeros([X_N,Y_N]) d_y = np.zeros([X_N,Y_N]) b_x = np.zeros([X_N,Y_N]) b_y = np.zeros([X_N,Y_N]) for cyc in range(CYCLE): u_n = Gauss_Saidel(u,d_x,d_y, b_x ,b_y,f, MU,LAMBDA) Err = np.max(np.abs(u_n[2:X_N-2,2:Y_N-2] - u[2:X_N-2,2:Y_N-2])) if np.mod(cyc,10)==0: print([cyc,Err]) if Err < TOL: break else: u = u_n nablax_u = np.vstack([u[1:X_N,:], np.reshape(u[:,-1],[1,X_N] )]) - u nablay_u = np.hstack([u[:,1:X_N], np.reshape(u[-1,:],[Y_N,1] )]) - u d_x = shrink(nablax_u + b_x, 1/LAMBDA) d_y = shrink(nablay_u + b_y, 1/LAMBDA) b_x = b_x + (nablax_u - d_x) b_y = b_y + (nablay_u - d_y) plt.figure() plt.subplot(1,2,1) plt.gray() plt.imshow(f) plt.title('Noisy') plt.axis("off") plt.subplot(1,2,2) plt.gray() plt.imshow(np.round(u)) x1, y1 = [0,X_N], [50,50] plt.plot(x1, y1) plt.title('Reconstructed') plt.axis("off") plt.figure() plt.subplot(2,1,1) plt.plot(f[50,:]) plt.subplot(2,1,2) plt.plot(u[50,:]) plt.show() if __name__ == "__main__": main()
試したこと・調べたこと
上記の詳細・結果
調べてみたところ同様のエラーが見つかり、いろいろと試行錯誤しましたがうまいこと行きませんでした。
ValueError: operands could not be broadcast together with shapes (1775556,) (1333,2000)のようなエラーやIndexError: too many indices for array: array is 2-dimensional, but 3 were indexedのようなエラーが出てきてしまいすべてに共通しているのがインデックス内の配列のサイズについての記載でした。
初心者過ぎて行き詰っています。勝手なお願いですが誰かお助けいただきたいです。
補足
特になし

0 コメント