diff options
Diffstat (limited to 'utils/raspberrypi/ctt/ctt_geq.py')
-rw-r--r-- | utils/raspberrypi/ctt/ctt_geq.py | 48 |
1 files changed, 24 insertions, 24 deletions
diff --git a/utils/raspberrypi/ctt/ctt_geq.py b/utils/raspberrypi/ctt/ctt_geq.py index dd798f4a..f8395e43 100644 --- a/utils/raspberrypi/ctt/ctt_geq.py +++ b/utils/raspberrypi/ctt/ctt_geq.py @@ -13,25 +13,25 @@ Uses green differences in macbeth patches to fit green equalisation threshold model. Ideally, all macbeth chart centres would fall below the threshold as these should be corrected by geq. """ -def geq_fit(Cam,plot): +def geq_fit(Cam, plot): imgs = Cam.imgs """ green equalisation to mitigate mazing. Fits geq model by looking at difference between greens in macbeth patches """ - geqs = np.array([ geq(Cam,Img)*Img.againQ8_norm for Img in imgs ]) + geqs = np.array([ geq(Cam, Img)*Img.againQ8_norm for Img in imgs ]) Cam.log += '\nProcessed all images' - geqs = geqs.reshape((-1,2)) + geqs = geqs.reshape((-1, 2)) """ data is sorted by green difference and top half is selected since higher green difference data define the decision boundary. """ - geqs = np.array(sorted(geqs,key = lambda r:np.abs((r[1]-r[0])/r[0]))) + geqs = np.array(sorted(geqs, key = lambda r: np.abs((r[1]-r[0])/r[0]))) length = len(geqs) - g0 = geqs[length//2:,0] - g1 = geqs[length//2:,1] + g0 = geqs[length//2:, 0] + g1 = geqs[length//2:, 1] gdiff = np.abs(g0-g1) """ find linear fit by minimising asymmetric least square errors @@ -40,7 +40,7 @@ def geq_fit(Cam,plot): threshold, hence the upper bound approach """ def f(params): - m,c = params + m, c = params a = gdiff - (m*g0+c) """ asymmetric square error returns: @@ -49,29 +49,29 @@ def geq_fit(Cam,plot): """ return(np.sum(a**2+0.95*np.abs(a)*a)) - initial_guess = [0.01,500] + initial_guess = [0.01, 500] """ Nelder-Mead is usually not the most desirable optimisation method but has been chosen here due to its robustness to undifferentiability (is that a word?) """ - result = optimize.minimize(f,initial_guess,method='Nelder-Mead') + result = optimize.minimize(f, initial_guess, method='Nelder-Mead') """ need to check if the fit worked correectly """ if result.success: - slope,offset = result.x + slope, offset = result.x Cam.log += '\nFit result: slope = {:.5f} '.format(slope) Cam.log += 'offset = {}'.format(int(offset)) """ optional plotting code """ if plot: - x = np.linspace(max(g0)*1.1,100) + x = np.linspace(max(g0)*1.1, 100) y = slope*x + offset plt.title('GEQ Asymmetric \'Upper Bound\' Fit') - plt.plot(x,y,color='red',ls='--',label='fit') - plt.scatter(g0,gdiff,color='b',label='data') + plt.plot(x, y, color='red', ls='--', label='fit') + plt.scatter(g0, gdiff, color='b', label='data') plt.ylabel('Difference in green channels') plt.xlabel('Green value') @@ -103,7 +103,7 @@ def geq_fit(Cam,plot): """ if plot: y2 = slope*x + offset - plt.plot(x,y2,color='green',ls='--',label='scaled fit') + plt.plot(x, y2, color='green', ls='--', label='scaled fit') plt.grid() plt.legend() plt.show() @@ -122,19 +122,19 @@ def geq_fit(Cam,plot): print(result.message) Cam.log += '\nWARNING: Asymmetric least squares fit failed! ' Cam.log += 'Standard fit used could possibly lead to worse results' - fit = np.polyfit(gdiff,g0,1) - offset,slope = -fit[1]/fit[0],1/fit[0] + fit = np.polyfit(gdiff, g0, 1) + offset, slope = -fit[1]/fit[0], 1/fit[0] Cam.log += '\nFit result: slope = {:.5f} '.format(slope) Cam.log += 'offset = {}'.format(int(offset)) """ optional plotting code """ if plot: - x = np.linspace(max(g0)*1.1,100) + x = np.linspace(max(g0)*1.1, 100) y = slope*x + offset plt.title('GEQ Linear Fit') - plt.plot(x,y,color='red',ls='--',label='fit') - plt.scatter(g0,gdiff,color='b',label='data') + plt.plot(x, y, color='red', ls='--', label='fit') + plt.scatter(g0, gdiff, color='b', label='data') plt.ylabel('Difference in green channels') plt.xlabel('Green value') """ @@ -158,22 +158,22 @@ def geq_fit(Cam,plot): """ if plot: y2 = slope*x + offset - plt.plot(x,y2,color='green',ls='--',label='scaled fit') + plt.plot(x, y2, color='green', ls='--', label='scaled fit') plt.legend() plt.grid() plt.show() - return round(slope,5),int(offset) + return round(slope, 5), int(offset) """" Return green channels of macbeth patches -returns g0,g1 where +returns g0, g1 where > g0 is green next to red > g1 is green next to blue """ -def geq(Cam,Img): +def geq(Cam, Img): Cam.log += '\nProcessing image {}'.format(Img.name) patches = [Img.patches[i] for i in Img.order][1:3] - g_patches = np.array([(np.mean(patches[0][i]),np.mean(patches[1][i])) for i in range(24)]) + g_patches = np.array([(np.mean(patches[0][i]), np.mean(patches[1][i])) for i in range(24)]) Cam.log += '\n' return(g_patches) |