把下面Matlab代码转成Python:function Result =…
- User
把下面Matlab代码转成Python:function Result = WaveletFilter(image,aMinRadius,aMaxRadius,scaleThreshold)
% Based on the code of Institut Pasteur
% Image : input image matrix, 8-bit or 16-bit grey % aMinRadius : minimal expected spot radius in pixel % aMaxRadius : maximal expected spot radius in pixel % scaleThreshold : N x Sigma of Standard Deviation (we can begin with 2 or 3 )
height = size(image,1); width = size(image,2); Result = zeros(height, width);
availableScale = [1, 3, 7, 13, 25]; % in pixel scalesList = logical([false, false, false, false, false]);
%Set scaleList accroding to min and max radius isEnable =false; for i = 1:5 if availableScale(i) >= aMinRadius * 2 && availableScale(i) <= aMaxRadius * 2 scalesList(i) = true; isEnable = true; end end
if ~isEnable for i = 1:5 if availableScale(i) >= aMinRadius * 2 scalesList(i - 1) = true; break; end end end
maxScale = 0; % minScale = 0; % find the max scale number in scalesList[] findMin = false; for i = 1:length(scalesList) if scalesList(i) if ~findMin findMin = true; % minScale = i + 1; end maxScale = i; end end if maxScale == 0 return; end
% check image dimensions minSize = 5 + (2^(maxScale - 1) - 1) * 4; if width < minSize || height < minSize return; end image = image'; pixels = image(:); % decompose the image scales = WaveletScales2D(pixels, width, height, maxScale); coefficients = b3WaveletCoefficients2D(scales, pixels, maxScale); % Apply threshold to coefficients but not last one (residual) for i = 1:(size(coefficients,1)-1) coefficients(i,:) = filter_wat(coefficients(i,:), i, maxScale, scalesList, scaleThreshold); end
% fill of 0 the residual image coefficients(end,:)= 0;
Arrayout = SpotConstruction2D(coefficients, maxScale, width * height, scalesList);
Result = reshape (Arrayout,width,height); Result = Result'; end
% Wavelet scale images for a 2D image (Copy from Institut Pasteur) function resArray = WaveletScales2D(dataIn, w, h, numScales)
% step between non zero coefficients of the convolution kernel stepS = 0; resArray = zeros(numScales, numel(dataIn)); prevArray = dataIn; % array to filter, original data for the first scale currentArray = []; % filtered array
% for each scale for s = 1:numScales stepS = 2^(s - 1); % compute the step between non zero coefficients of the convolution kernel = 2^(scale-1)
% convolve along the x direction and swap dimensions currentArray = filterAndSwap2D(prevArray, w, h, stepS); % swap current and previous array pointers if s == 1 prevArray = currentArray; % the filtered array becomes the array to filter currentArray = zeros(w * h, 1); % allocate memory for the next dimension filtering (preserve original data) else tmp = currentArray; currentArray = prevArray; % the filtered array becomes the array to filter prevArray = tmp; % the filtered array becomes the array to filter end % convolve along the y direction and swap dimensions currentArray = filterAndSwap2D(prevArray, h, w, stepS); % swap size of dimensions % swap current and previous array pointers tmp = currentArray; currentArray = prevArray; prevArray = tmp; resArray(s, :) = zeros(w * h, 1); % allocate memory to store the filtered array resArray(s, :) = prevArray; - Assistant
- User
把下面Java代码转换成python:private float[] filterAndSwap2D(float[] arrayIn, int w, int h, int stepS) { float[] arrayOut = new float[w * h]; // B3 spline wavelet configuration // the convolution kernel is {1/16, 1/4, 3/8, 1/4, 1/16} float w2 = ((float) 1) / 16; float w1 = ((float) 1) / 4; float w0 = ((float) 3) / 8;
int w0idx; int w1idx1; int w2idx1; int w1idx2; int w2idx2; int arrayOutIter; int cntX; w0idx = 0; for (int y = 0; y < h; y++) {// loop over the second dimension // manage the left border with mirror symmetry arrayOutIter = y;// output pointer initialization, we swap dimensions at this point // w0idx = arrayIn + y*width; w1idx1 = w0idx + stepS - 1; w2idx1 = w1idx1 + stepS; w1idx2 = w0idx + stepS; w2idx2 = w1idx2 + stepS; cntX = 0; while (cntX < stepS) { arrayOut[arrayOutIter] = w2 * ((arrayIn[w2idx1]) + (arrayIn[w2idx2])) + w1 * ((arrayIn[w1idx1]) + (arrayIn[w1idx2])) + w0 * (arrayIn[w0idx]); w1idx1--; w2idx1--; w1idx2++; w2idx2++; w0idx++; arrayOutIter += h; cntX++; } w1idx1++; while (cntX < 2 * stepS) { arrayOut[arrayOutIter] = w2 * ((arrayIn[w2idx1]) + (arrayIn[w2idx2])) + w1 * ((arrayIn[w1idx1]) + (arrayIn[w1idx2])) + w0 * (arrayIn[w0idx]); w1idx1++; w2idx1--; w1idx2++; w2idx2++; w0idx++; arrayOutIter += h; cntX++; } w2idx1++; // filter the center area of the image (no border issue) while (cntX < w - 2 * stepS) { arrayOut[arrayOutIter] = w2 * ((arrayIn[w2idx1]) + (arrayIn[w2idx2])) + w1 * ((arrayIn[w1idx1]) + (arrayIn[w1idx2])) + w0 * (arrayIn[w0idx]); w1idx1++; w2idx1++; w1idx2++; w2idx2++; w0idx++; arrayOutIter += h; cntX++; } w2idx2--; // manage the right border with mirror symmetry while (cntX < w - stepS) { arrayOut[arrayOutIter] = w2 * ((arrayIn[w2idx1]) + (arrayIn[w2idx2])) + w1 * ((arrayIn[w1idx1]) + (arrayIn[w1idx2])) + w0 * (arrayIn[w0idx]); w1idx1++; w2idx1++; w1idx2++; w2idx2--; w0idx++; arrayOutIter += h; cntX++; } w1idx2--; while (cntX < w) { arrayOut[arrayOutIter] = w2 * ((arrayIn[w2idx1]) + (arrayIn[w2idx2])) + w1 * ((arrayIn[w1idx1]) + (arrayIn[w1idx2])) + w0 * (arrayIn[w0idx]); w1idx1++; w2idx1++; w1idx2--; w2idx2--; w0idx++; arrayOutIter += h; cntX++; } } return arrayOut; } - Assistant
Here's the equivalent Python code for the given Java code:
import numpy as np def filterAndSwap2D(arrayIn, w, h, stepS): arrayOut = np.zeros(w * h) w2 = 1 / 16 w1 = 1 / 4 w0 = 3 / 8 w0idx = 0 for y in range(h): arrayOutIter = y w1idx1 = w0idx + stepS - 1 w2idx1 = w1idx1 + stepS w1idx2 = w0idx + stepS w2idx2 = w1idx2 + stepS cntX = 0 while cntX < stepS: arrayOut[arrayOutIter] = w2 * (arrayIn[w2idx1] + arrayIn[w2idx2]) + w1 * (arrayIn[w1idx1] + arrayIn[w1idx2]) + w0 * arrayIn[w0idx] w1idx1 -= 1 w2idx1 -= 1 w1idx2 += 1 w2idx2 += 1 w0idx += 1 arrayOutIter += h cntX += 1 w1idx1 += 1 while cntX < 2 * stepS: arrayOut[arrayOutIter] = w2 * (arrayIn[w2idx1] + arrayIn[w2idx2]) + w1 * (arrayIn[w1idx1] + arrayIn[w1idx2]) + w0 * arrayIn[w0idx] w1idx1 += 1 w2idx1 -= 1 w1idx2 += 1 w2idx2 += 1 w0idx += 1 arrayOutIter += h cntX += 1 w2idx1 += 1 while cntX < w - 2 * stepS: arrayOut[arrayOutIter] = w2 * (arrayIn[w2idx1] + arrayIn[w2idx2]) + w1 * (arrayIn[w1idx1] + arrayIn[w1idx2]) + w0 * arrayIn[w0idx] w1idx1 += 1 w2idx1 += 1 w1idx2 += 1 w2idx2 += 1 w0idx += 1 arrayOutIter += h cntX += 1 w2idx2 -= 1 while cntX < w - stepS: arrayOut[arrayOutIter] = w2 * (arrayIn[w2idx1] + arrayIn[w2idx2]) + w1 * (arrayIn[w1idx1] + arrayIn[w1idx2]) + w0 * arrayIn[w0idx] w1idx1 += 1 w2idx1 += 1 w1idx2 += 1 w2idx2 -= 1 w0idx += 1 arrayOutIter += h cntX += 1 w1idx2 -= 1 while cntX < w: arrayOut[arrayOutIter] = w2 * (arrayIn[w2idx1] + arrayIn[w2idx2]) + w1 * (arrayIn[w1idx1] + arrayIn[w1idx2]) + w0 * arrayIn[w0idx] w1idx1 += 1 w2idx1 += 1 w1idx2 -= 1 w2idx2 -= 1 w0idx += 1 arrayOutIter += h cntX += 1 return arrayOut