nctionSUBWINDOWANALYSIS(correlationmap,distinput) 2: imgsize = size(testimage); 3:…
- User
nctionSUB_WINDOW_ANALYSIS(correlation_map,dist_input) 2: imgsize = size(testimage); 3: sgimgsize = single(imgsize); 4: sgsubimgsz = single(subimgsz); 5: sgtestimage = single(testimage); 6: sigma = single(sigma); 7: 8: if (length(imgsize)>2) then 9: testimage = rgb2gray(testimage); 10: end if 11: 12: if (rem(imgsize(1),subimgsz(1)) || rem(imgsize(1),subimgsz(1))) then 13: display(’the subimage size does not match the whole image size’); 14: return 15: end if 16: angles = angrange(1):angrange(2):angrange(3); trans = brange(1):brange(2):brange(3); corr = zeros(length(angles), length(trans),imgsize(1) / subimgsz(1), ... imgsize(2) / subimgsz(2)); xsize = sgimgsize(2); ysize = sgimgsize(1); [Xmesh, Ymesh] = meshgrid(0:(xsize-1), 0:(ysize-1)); Xmesh = mod(Xmesh,subimgsz(2)); Ymesh = mod(Ymesh,subimgsz(1)); Xmesh = Xmesh - sgsubimgsz(2)/2; Ymesh = Ymesh - sgsubimgsz(1)/2; for thetaind = 1:length(angles) do for bind = 1:length(trans) do theta = angles(thetaind); b = single(trans(bind)); angle = single((theta* pi) / 180); dist = b - (Xmeshsin(angle)) - (Ymeshcos(angle)); distpw = (dist2 )/ (sigma2 ); refline = exp(-distpw); temp = refline*sgtestimage; temp2 = reshape(temp,[subimgsz(1),imgsize(1)/subimgsz(1),... subimgsz(2),imgsize(2)/subimgsz(2)]); temp3 = sum(temp2,1); temp4 = squeeze(sum(temp3,3)); corr(thetaind, bind, :, :) = temp4; end for end for corrsz = size(corr); spcorr = reshape(permute(corr,[1,3,2,4]),corrsz(1)*corrsz(3),corrsz(2)*corrsz(4)); colsubimgs = reshape(sgtestimage,... [subimgsz(1),imgsize(1)/subimgsz(1), subimgsz(2),imgsize(2)/subimgsz(2)]); endfunction
Перепиши на python, можно использовать только numpy
- Assistant
import numpy as np
def sub_window_analysis(correlation_map, dist_input): imgsize = np.shape(testimage) sgimgsize = np.array(imgsize, dtype=np.float32) sgsubimgsz = np.array(subimgsz, dtype=np.float32) sgtestimage = np.array(testimage, dtype=np.float32) sigma = np.float32(sigma)
if len(imgsize) > 2: testimage = np.dot(testimage[...,:3], [0.2989, 0.5870, 0.1140]) 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[1], angrange[2]) trans = np.arange(brange[0], brange[1], brange[2]) 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(0,xsize), np.arange(0,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 = np.float32(trans[bind]) angle = (theta * np.pi) / 180 dist = b - (Xmesh * np.sin(angle)) - (Ymesh * np.cos(angle)) distpw = dist**2 / (sigma**2) refline = np.exp(-distpw) temp = refline * sgtestimage temp2 = np.reshape(temp, (subimgsz[0], imgsize[0]//subimgsz[0], subimgsz[1], imgsize[1]//subimgsz[1])) temp3 = np.sum(temp2, axis=1) temp4 = np.squeeze(np.sum(temp3, axis=2)) corr[thetaind, bind, :, :] = temp4 corrsz = np.shape(corr) 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 corrsz, spcorr, colsubimgs - User
Где табуляция?
- Assistant
К сожалению, я не могу передавать табуляции или форматирование кода в текстовом формате. Однако, вот обновленный код с правильным форматированием:
import numpy as np def sub_window_analysis(correlation_map, dist_input): imgsize = np.shape(testimage) sgimgsize = np.array(imgsize, dtype=np.float32) sgsubimgsz = np.array(subimgsz, dtype=np.float32) sgtestimage = np.array(testimage, dtype=np.float32) sigma = np.float32(sigma) if len(imgsize) > 2: testimage = np.dot(testimage[...,:3], [0.2989, 0.5870, 0.1140]) 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[1], angrange[2]) trans = np.arange(brange[0], brange[1], brange[2]) 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(0,xsize), np.arange(0,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 = np.float32(trans[bind]) angle = (theta * np.pi) / 180 dist = b - (Xmesh * np.sin(angle)) - (Ymesh * np.cos(angle)) distpw = dist**2 / (sigma**2) refline = np.exp(-distpw) temp = refline * sgtestimage temp2 = np.reshape(temp, (subimgsz[0], imgsize[0]//subimgsz[0], subimgsz[1], imgsize[1]//subimgsz[1])) temp3 = np.sum(temp2, axis=1) temp4 = np.squeeze(np.sum(temp3, axis=2)) corr[thetaind, bind, :, :] = temp4 corrsz = np.shape(corr) 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 corrsz, spcorr, colsubimgs - Assistant
Извините за предыдущую ошибку. Вот код с использованием отступов:
import numpy as np def sub_window_analysis(correlation_map, dist_input): imgsize = np.shape(testimage) sgimgsize = np.array(imgsize, dtype=np.float32) sgsubimgsz = np.array(subimgsz, dtype=np.float32) sgtestimage = np.array(testimage, dtype=np.float32) sigma = np.float32(sigma) if len(imgsize) > 2: testimage = np.dot(testimage[...,:3], [0.2989, 0.5870, 0.1140]) 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[1], angrange[2]) trans = np.arange(brange[0], brange[1], brange[2]) 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(0,xsize), np.arange(0,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 = np.float32(trans[bind]) angle = (theta * np.pi) / 180 dist = b - (Xmesh * np.sin(angle)) - (Ymesh * np.cos(angle)) distpw = dist**2 / (sigma**2) refline = np.exp(-distpw) temp = refline * sgtestimage temp2 = np.reshape(temp, (subimgsz[0], imgsize[0]//subimgsz[0], subimgsz[1], imgsize[1]//subimgsz[1])) temp3 = np.sum(temp2, axis=1) temp4 = np.squeeze(np.sum(temp3, axis=2)) corr[thetaind, bind, :, :] = temp4 corrsz = np.shape(corr) 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 corrsz, spcorr, colsubimgs