Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

请将下面的Java代码转换成Matlab代码://Wavelet scale images for a 2D image (Copy from Institut…

  1. User

    请将下面的Java代码转换成Matlab代码://Wavelet scale images for a 2D image (Copy from Institut Pasteur) private float[][] WaveletScales2D(float[] dataIn, int w, int h, int numScales) {

    	// step between non zero coefficients of the convolution kernel
    	int stepS;
    	float[][] resArray = new float[numScales][];
    	float[] prevArray = dataIn; // array to filter, original data for the first scale
    	float[] currentArray; // filtered array
    
    	// for each scale
    	for (int s = 1; s <= numScales; s++) {
    		stepS = (int) Math.pow(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 = new float[w * h]; // allocate memory for the next dimension filtering (preserve original
    												// data)
    		} else {
    			float[] tmp = currentArray;
    			currentArray = prevArray; // the filtered array becomes the array to filter
    			prevArray = tmp; // the filtered array becomes the array to filter
    		}
    		// convolve along the y direction and swap dimensions
    		currentArray = filterAndSwap2D(prevArray, h, w, stepS);// swap size of dimensions
    		// swap current and previous array pointers
    		float[] tmp = currentArray;
    		currentArray = prevArray;
    		prevArray = tmp;
    
    		resArray[s - 1] = new float[w * h]; // allocate memory to store the filtered array
    		System.arraycopy(prevArray, 0, resArray[s - 1], 0, w * h);
    	}
    
    	return resArray;
    }
    
  2. Assistant

    % 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, 1);
    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;
    end
    

    end

  3. User

    private float[][] b3WaveletCoefficients2D(float[][] scaleCoefficients, float[] originalImage, int numScales) { // maxScales wavelet images to store, + one image for the low pass residual float[][] waveletCoefficients = new float[numScales + 1][];

    	// compute wavelet coefficients as the difference between scale coefficients of
    	// subsequent scales
    	float[] iterPrev = originalImage;// the finest scale coefficient is the difference between the original image
    										// and the first scale.
    	float[] iterCurrent;
    
    	int j = 0;
    	while (j < numScales) {
    		iterCurrent = scaleCoefficients[j];
    		float[] wCoefficients = new float[originalImage.length];
    		for (int i = 0; i < originalImage.length; i++) {
    			wCoefficients[i] = iterPrev[i] - iterCurrent[i];
    		}
    		waveletCoefficients[j] = wCoefficients;
    		iterPrev = iterCurrent;
    		j++;
    	}
    	// residual low pass image is the last wavelet Scale
    	waveletCoefficients[numScales] = new float[originalImage.length];
    	System.arraycopy(scaleCoefficients[numScales - 1], 0, waveletCoefficients[numScales], 0, originalImage.length);
    	return waveletCoefficients;
    }
    
    /**
     * filter a 2D image with the B3 spline wavelet scale kernel in the x direction
     * when using the a trous algorithm, and swap dimensions (Copy from Institut Pasteur)
     */
    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 = 0 + 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;
    }
    
  4. Assistant

    function waveletCoefficients = b3WaveletCoefficients2D(scaleCoefficients, originalImage, numScales)

    % maxScales wavelet images to store, + one image for the low pass residual
    waveletCoefficients = zeros(numScales + 1, 1);
    
    % compute wavelet coefficients as the difference between scale coefficients of subsequent scales
    iterPrev = originalImage; % the finest scale coefficient is the difference between the original image and the first scale.
    iterCurrent = [];
    
    j = 1;
    while j <= numScales
        iterCurrent = scaleCoefficients{j};
        wCoefficients = zeros(size(originalImage));
        for i = 1:numel(originalImage)
            wCoefficients(i) = iterPrev(i) - iterCurrent(i);
        end
        waveletCoefficients{j} = wCoefficients;
        iterPrev = iterCurrent;
        j = j + 1;
    end
    
    % residual low pass image is the last wavelet Scale
    waveletCoefficients{numScales+1} = scaleCoefficients{numScales};
    

    end

    function arrayOut = filterAndSwap2D(arrayIn, w, h, stepS)

    arrayOut = zeros(w * h, 1);
    
    % B3 spline wavelet configuration
    % the convolution kernel is {1/16, 1/4, 3/8, 1/4, 1/16}
    w2 = 1/16;
    w1 = 1/4;
    w0 = 3/8;
    
    w0idx = 1;
    
    for y = 1:h % loop over the second dimension
        
        % manage the left border with mirror symmetry
        arrayOutIter = y; % output pointer initialization, we swap dimensions at this point
        w1idx1 = w0idx + stepS - 1;
        w2idx1 = w1idx1 + stepS;
        w1idx2 = w0idx + stepS;
        w2idx2 = w1idx2 + stepS;
    
        cntX = 1;
        
        while cntX <= stepS
            arrayOut(arrayOutIter) = w2 * (arrayIn(w2idx1) + arrayIn(w2idx2))...
                + w1 * (arrayIn(w1idx1) + arrayIn(w1idx2)) + w0 * arrayIn(w0idx);
            w1idx1 = w1idx1 - 1;
            w2idx1 = w2idx1 - 1;
            w1idx2 = w1idx2 + 1;
            w2idx2 = w2idx2 + 1;
            w0idx = w0idx + 1;
            arrayOutIter = arrayOutIter + h;
            cntX = cntX + 1;
        end
        
        w1idx1 = w1idx1 + 1;
        
        while cntX <= 2 * stepS
            arrayOut(arrayOutIter) = w2 * (arrayIn(w2idx1) + arrayIn(w2idx2))...
                + w1 * (arrayIn(w1idx1) + arrayIn(w1idx2)) + w0 * arrayIn(w0idx);
            w1idx1 = w1idx1 + 1;
            w2idx1 = w2idx1 - 1;
            w1idx2 = w1idx2 + 1;
            w2idx2 = w2idx2 + 1;
            w0idx = w0idx + 1;
            arrayOutIter = arrayOutIter + h;
            cntX = cntX + 1;
        end
        
        w2idx1 = w2idx1 + 1;
        
        % 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 = w1idx1 + 1;
            w2idx1 = w2idx1 + 1;
            w1idx2 = w1idx2 + 1;
            w2idx2 = w2idx2 + 1;
            w0idx = w0idx + 1;
            arrayOutIter = arrayOutIter + h;
            cntX = cntX + 1;
        end
        
        w2idx2 = w2idx2 - 1;
        
        % 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 = w1idx1 + 1;
            w2idx1 = w2idx1 + 1;
            w1idx2 = w1idx2 + 1;
            w2idx2 = w2idx2 - 1;
            w0idx = w0idx + 1;
            arrayOutIter = arrayOutIter + h;
            cntX = cntX + 1;
        end
        
        w1idx2 = w1idx2 - 1;
        
        while cntX <= w
            arrayOut(arrayOutIter) = w2 * (arrayIn(w2idx1) + arrayIn(w2idx2))...
                + w1 * (arrayIn(w1idx1) + arrayIn(w1idx2)) + w0 * arrayIn(w0idx);
            w1idx1 = w1idx1 + 1;
            w2idx1 = w2idx1 + 1;
            w1idx2 = w1idx2 - 1;
            w2idx2 = w2idx2 - 1;
            w0idx = w0idx + 1;
            arrayOutIter = arrayOutIter + h;
            cntX = cntX + 1;
        end
        
    end
    

    end

  5. User

    private void filter_wat(float[] data, int depth, int numPixels, int numScales, boolean[] aScalesList, double aScaleThreshold) {

    	if (!aScalesList[depth]) {
    		// if the scale is not selected, fill of 0
    		for (int i = 0; i < data.length; i++) {
    			data[i] = 0;
    		}
    		return;
    	}
    
    	// Init lambda value
    	double lambdac[] = new double[numScales + 2];
    
    	for (int i = 0; i < numScales + 2; i++) {
    		// ( 1 << (2*i) ) ) gives 1 , 4 , 16 , 64 , 256 , 1024 ...
    		lambdac[i] = Math.sqrt(2 * Math.log(numPixels / (1 << (2 * i))));
    	}
    	
    	
    
    

    /*** float mean = 0; float sum = 0; for (int i = 0; i < data.length; i++) { sum += data[i]; } mean = sum / data.length;

    	float a = 0;
    	float s;
    	for (int i = 0; i < data.length; i++) {
    		s = data[i] - mean;
    		a = a + Math.abs(s);
    	}
    	float mad = a / data.length;
    	***/
    	
    
    	float[] residuel = new float[data.length];
    	float median = ExocytosisAnalyzer.detection.FindMedian.findMedian(data);
    	
    	for (int i =0; i < data.length ; i++ ) {
    		residuel[i] =  Math.abs(data[i] - median);
    	}
    
    	float mad = ExocytosisAnalyzer.detection.FindMedian.findMedian(residuel) / 0.6745f;
    
    
    	
    	double coeffThr = (lambdac[depth + 1] * mad) * aScaleThreshold;
    
    	for (int i = 0; i < data.length; i++) {
    		if (data[i] >= coeffThr) {
    			// data[ i ] = 255 ;
    		} else {
    			data[i] = 0;
    		}
    	}
    }
    
  6. Assistant

    function filter_wat(data, depth, numPixels, numScales, aScalesList, aScaleThreshold)

    if aScalesList(depth) == 0
        data(:) = 0;
        return;
    end
    
    lambdac = zeros(numScales + 2, 1);
    
    for i = 1:numScales + 2
        lambdac(i) = sqrt(2 * log(numPixels / (2^(2 * i))));
    end
    
    resid