@Override public void getF(int index, float[] value) { value[0] = sb.getF(index); }
@Override public float getF(int index) { return sb.getF(index); }
/** * Computes the angle of each pixel and its gradient magnitude */ void precomputeAngles(D image) { int savecIndex = 0; for (int y = 0; y < image.height; y++) { int pixelIndex = y*image.stride + image.startIndex; for (int x = 0; x < image.width; x++, pixelIndex++, savecIndex++ ) { float spacialDX = imageDerivX.getF(pixelIndex); float spacialDY = imageDerivY.getF(pixelIndex); savedAngle.data[savecIndex] = UtilAngle.domain2PI(Math.atan2(spacialDY,spacialDX)); savedMagnitude.data[savecIndex] = (float)Math.sqrt(spacialDX*spacialDX + spacialDY*spacialDY); } } }
@Override public void getF(int index, float[] value) { for( int i = 0; i < image.getNumBands(); i++ ) { value[i] = bandWrappers[i].getF(index); } }
@Override public void segment(T frame, GrayU8 segmented) { if( background.width == 1 ) { ImageMiscOps.fill(segmented,unknownValue); return; } InputSanityCheck.checkSameShape(background,frame,segmented); inputWrapper.wrap(frame); float thresholdSq = threshold*threshold; int indexBG = 0; for (int y = 0; y < frame.height; y++) { int indexInput = frame.startIndex + y*frame.stride; int indexSegmented = segmented.startIndex + y*segmented.stride; int end = indexInput + frame.width; while( indexInput < end ) { float bg = background.data[indexBG]; float pixelFrame = inputWrapper.getF(indexInput); float diff = bg - pixelFrame; if (diff * diff <= thresholdSq) { segmented.data[indexSegmented] = 0; } else { segmented.data[indexSegmented] = 1; } indexInput++; indexSegmented++; indexBG++; } } }
@Override public void updateBackground( T frame) { if( background.width == 1 ) { background.reshape(frame.width, frame.height); GConvertImage.convert(frame, background); return; } else { InputSanityCheck.checkSameShape(background,frame); } inputWrapper.wrap(frame); float minusLearn = 1.0f - learnRate; int indexBG = 0; for (int y = 0; y < frame.height; y++) { int indexInput = frame.startIndex + y*frame.stride; int end = indexInput + frame.width; while( indexInput < end ) { float value = inputWrapper.getF(indexInput++); float bg = background.data[indexBG]; background.data[indexBG++] = minusLearn*bg + learnRate*value; } } }
@Override protected void _segment(Motion currentToWorld, T frame, GrayU8 segmented) { transform.setModel(currentToWorld); inputWrapper.wrap(frame); float thresholdSq = threshold*threshold; for (int y = 0; y < frame.height; y++) { int indexFrame = frame.startIndex + y*frame.stride; int indexSegmented = segmented.startIndex + y*segmented.stride; for (int x = 0; x < frame.width; x++, indexFrame++ , indexSegmented++ ) { transform.compute(x,y,work); if( work.x >= 0 && work.x < background.width && work.y >= 0 && work.y < background.height) { float bg = interpolationBG.get(work.x,work.y); float pixelFrame = inputWrapper.getF(indexFrame); if( bg == Float.MAX_VALUE ) { segmented.data[indexSegmented] = unknownValue; } else { float diff = bg - pixelFrame; if (diff * diff <= thresholdSq) { segmented.data[indexSegmented] = 0; } else { segmented.data[indexSegmented] = 1; } } } else { // there is no background here. Just mark it as not moving to avoid false positives segmented.data[indexSegmented] = unknownValue; } } } }
float pixelFrame = inputWrapper.getF(indexFrame);
float pixelFrame = inputWrapper.getF(indexInput);
@Override public void updateBackground( T frame) { if( background.width == 1 ) { background.reshape(frame.width, frame.height); GConvertImage.convert(frame, background.getBand(0)); GImageMiscOps.fill(background.getBand(1),initialVariance); return; } else { InputSanityCheck.checkSameShape(background, frame); } inputWrapper.wrap(frame); float minusLearn = 1.0f - learnRate; GrayF32 backgroundMean = background.getBand(0); GrayF32 backgroundVar = background.getBand(1); int indexBG = 0; for (int y = 0; y < background.height; y++) { int indexInput = frame.startIndex + y*frame.stride; int end = indexInput + frame.width; while( indexInput < end ) { float inputValue = inputWrapper.getF(indexInput); float meanBG = backgroundMean.data[indexBG]; float varianceBG = backgroundVar.data[indexBG]; float diff = meanBG-inputValue; backgroundMean.data[indexBG] = minusLearn*meanBG + learnRate*inputValue; backgroundVar.data[indexBG] = minusLearn*varianceBG + learnRate*diff*diff; indexBG++; indexInput++; } } }