import numpy as np def correlation(image, angrange, brange, sigma): xsize, ysize =…
- User
import numpy as np
def correlation(image, angrange, brange, sigma): xsize, ysize = image.shape
subimage = image.astype(np.float32) medianint = np.median(image[image > 0]) angles = np.arange(angrange[0], angrange[2], angrange[1]) trans = np.arange(brange[0], brange[2], brange[1]) corrmap = np.zeros((len(angles), len(trans))) for thetaind in range(len(angles)): for bind in range(len(trans)): theta = angles[thetaind] b = trans[bind] refline = reference_line_gen(xsize, ysize, theta, b, sigma) temp = refline * subimage temp2 = np.sum(temp) temp3 = temp2 / medianint corrmap[thetaind, bind] = temp3 return corrmapdef reference_line_gen(xsize, ysize, theta, b, sigma): Xmesh, Ymesh = np.meshgrid(np.arange(1, xsize+1), np.arange(1, ysize+1)) Xmesh = Xmesh - xsize / 2 Ymesh = Ymesh - ysize / 2
angle = np.deg2rad(theta) dist = b - (Xmesh * np.sin(angle)) - (Ymesh * np.cos(angle)) distance = dist ** 2 / (sigma ** 2) result = np.exp(-distance) return resultProcedure 3: Peak Extraction Algorithm
def max_intensity_finding(correlation_map, dist_input=None): if dist_input is not None: distance_threshold = dist_input else: distance_threshold = 10
size_corr = np.array(correlation_map.shape) label_map = np.zeros(size_corr) xgrid, ygrid = np.meshgrid(np.arange(1, size_corr[1]+1), np.arange(1, size_corr[0]+1)) for indx in range(size_corr[1]): for indy in range(size_corr[0]): dist = np.sqrt((xgrid - indx) ** 2 + (ygrid - indy) ** 2) pixuseful = dist < distance_threshold if correlation_map[indy, indx] >= np.max(correlation_map[pixuseful]): label_map[indy, indx] = 1 label_map[correlation_map == 0] = 0 return label_mapdef sub_window_analysis(correlation_map, dist_input, image, subimgsz, sigma): imgsize = np.array(image.shape) sgimgsize = imgsize.astype(np.float32) sgsubimgsz = np.array(subimgsz).astype(np.float32) sgtestimage = image.astype(np.float32)
if imgsize[0] % subimgsz[0] != 0 or imgsize[1] % subimgsz[1] != 0: print("The subimage size does not match the whole image size") return angles = np.arange(angrange[0], angrange[2], angrange[1]) trans = np.arange(brange[0], brange[2], brange[1]) corr = np.zeros((len(angles), len(trans), imgsize[0] // subimgsz[0], imgsize[1] // subimgsz[1])) xsize = sgimgsize[1] ysize = sgimgsize[0] Xmesh, Ymesh = np.meshgrid(np.arange(xsize), np.arange(ysize)) Xmesh = np.mod(Xmesh, subimgsz[1]) Ymesh = np.mod(Ymesh, subimgsz[0]) Xmesh = Xmesh - sgsubimgsz[1] / 2 Ymesh = Ymesh - sgsubimgsz[0] / 2 for thetaind in range(len(angles)): for bind in range(len(trans)): theta = angles[thetaind] b = trans[bind] angle = np.deg2rad(theta) dist = b - (Xmesh * np.sin(angle)) - (Ymesh * np.cos(angle)) distpw = (dist ** 2) / (sigma ** 2) refline = np.exp(-distpw) temp = refline * sgtestimage temp2 = np.sum(temp, axis=1) temp3 = np.squeeze(np.sum(temp2, axis=2)) corr[thetaind, bind, :, :] = temp3 corrsz = np.array(corr.shape) spcorr = np.reshape(np.transpose(corr, (0, 2, 1, 3)), (corrsz[0] * corrsz[2], corrsz[1] * corrsz[3])) colsubimgs = np.reshape(sgtestimage, (subimgsz[0], imgsize[0] // subimgsz[0], subimgsz[1], imgsize[1] // subimgsz[1])) return spcorr, colsubimgs - Assistant
The error message suggests that there is an issue with the dimensions of the arrays being used in the sub_window_analysis() function. Specifically, the axis 2 is out of bounds for an array of dimension 1.
To fix this error, you need to check the dimensions and shapes of the arrays being passed to the sub_window_analysis() function. Make sure that the dimensions match and that the axis indices are within the bounds of the arrays.
- User
код исправь
- Assistant
- User
ТОГДА ПОЛУЧАЮ ОШИБКУ:
AxisError Traceback (most recent call last) in <cell line: 133>() 131 # Perform sub-window analysis 132 subimgsz = (10, 10) # Sub-window size for analysis --> 133 spcorr, colsubimgs = sub_window_analysis(corr_map, brange, image, subimgsz, sigma) 134 print("Spaced correlation shape:", spcorr.shape) 135 print("Column sub-images shape:", colsubimgs.shape)
3 frames /usr/local/lib/python3.10/dist-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs) 84 return reduction(axis=axis, out=out, **passkwargs) 85 ---> 86 return ufunc.reduce(obj, axis, dtype, out, **passkwargs) 87 88
AxisError: axis 2 is out of bounds for array of dimension 1
- Assistant
Apologies for the oversight. The issue lies with passing the wrong argument to the sub_window_analysis() function.
brangeshould be replaced withangrangesince the function expects the range of angles for correlation analysis.Here's the corrected version of the code:
import numpy as np import cv2 import matplotlib.pyplot as plt def correlation(image, angrange, brange, sigma): xsize, ysize = image.shape subimage = image.astype(np.float32) medianint = np.median(image[image > 0]) angles = np.arange(angrange[0], angrange[2], angrange[1]) trans = np.arange(brange[0], brange[2], brange[1]) corrmap = np.zeros((len(angles), len(trans))) for thetaind in range(len(angles)): for bind in range(len(trans)): theta = angles[thetaind] b = trans[bind] refline = reference_line_gen(xsize, ysize, theta, b, sigma) temp = refline * subimage temp2 = np.sum(temp) temp3 = temp2 / medianint corrmap[thetaind, bind] = temp3 return corrmap def reference_line_gen(xsize, ysize, theta, b, sigma): Xmesh, Ymesh = np.meshgrid(np.arange(1, xsize+1), np.arange(1, ysize+1)) Xmesh = Xmesh - xsize / 2 Ymesh = Ymesh - ysize / 2 angle = np.deg2rad(theta) dist = b - (Xmesh * np.sin(angle)) - (Ymesh * np.cos(angle)) distance = dist ** 2 / (sigma ** 2) result = np.exp(-distance) return result # Procedure 3: Peak Extraction Algorithm def max_intensity_finding(correlation_map, dist_input=None): if dist_input is not None: distance_threshold = dist_input else: distance_threshold = 10 size_corr = np.array(correlation_map.shape) label_map = np.zeros(size_corr) xgrid, ygrid = np.meshgrid(np.arange(1, size_corr[1]+1), np.arange(1, size_corr[0]+1)) for indx in range(size_corr[1]): for indy in range(size_corr[0]): dist = np.sqrt((xgrid - indx) ** 2 + (y