Skip to content

Commit

Permalink
added patch abs diff stds 2D (test)
Browse files Browse the repository at this point in the history
  • Loading branch information
ammendes committed Sep 18, 2024
1 parent 41b33ec commit 63665f3
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 3 deletions.
73 changes: 72 additions & 1 deletion src/main/java/BlockRedundancy2D_.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ public void run(String s) {
}

// Define metric possibilities
String[] metrics = new String[4];
String[] metrics = new String[5];
metrics[0] = "Pearson's R";
metrics[1] = "Cosine similarity";
metrics[2] = "SSIM";
metrics[3] = "NRMSE (inverted)";
metrics[4] = "Abs. Diff. of StdDevs.";

// Initialize dialog box
NonBlockingGenericDialog gd = new NonBlockingGenericDialog("SReD: Block Repetition (2D)");
Expand Down Expand Up @@ -679,6 +680,76 @@ public void run(String s) {
}
}

if (metric == metrics[4]) { // Abs. Diff. of StdDevs
showStatus("Calculating Abs. Diff. of StdDevs...");

// Build OpenCL program
String programStringGetPatchDiffStd = getResourceAsString(BlockRedundancy2D_.class, "kernelGetPatchDiffStd2D.cl");
programStringGetPatchDiffStd = replaceFirst(programStringGetPatchDiffStd, "$WIDTH$", "" + w);
programStringGetPatchDiffStd = replaceFirst(programStringGetPatchDiffStd, "$HEIGHT$", "" + h);
programStringGetPatchDiffStd = replaceFirst(programStringGetPatchDiffStd, "$BRW$", "" + bRW);
programStringGetPatchDiffStd = replaceFirst(programStringGetPatchDiffStd, "$BRH$", "" + bRH);
programStringGetPatchDiffStd = replaceFirst(programStringGetPatchDiffStd, "$PATCH_STD$", "" + patchStdDev);
programStringGetPatchDiffStd = replaceFirst(programStringGetPatchDiffStd, "$EPSILON$", "" + EPSILON);
programGetPatchDiffStd = context.createProgram(programStringGetPatchDiffStd).build();

// Fill OpenCL buffers
clDiffStdMap = context.createFloatBuffer(wh, READ_WRITE);
fillBufferWithFloatArray(clDiffStdMap, repetitionMap);

// Create kernel and set args
kernelGetPatchDiffStd = programGetPatchDiffStd.createCLKernel("kernelGetPatchDiffStd2D");

argn = 0;
kernelGetPatchDiffStd.setArg(argn++, clLocalStds);
kernelGetPatchDiffStd.setArg(argn++, clDiffStdMap);

// Calculate absolute difference of StdDevs
queue.putWriteBuffer(clDiffStdMap, true);
queue.put2DRangeKernel(kernelGetPatchDiffStd, 0, 0, w, h, 0, 0);
queue.finish();

// Read results back from the OpenCL device
queue.putReadBuffer(clDiffStdMap, true);
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
repetitionMap[y * w + x] = clDiffStdMap.getBuffer().get(y * w + x);
queue.finish();
}
}
queue.finish();

// Release GPU resources
kernelGetPatchDiffStd.release();
clDiffStdMap.release();
programGetPatchDiffStd.release();

// Normalize between 0 and 1
float repMin = Float.MAX_VALUE;
float repMax = Float.MIN_VALUE;

for (int y = bRH; y < h-bRH; y++) {
for (int x = bRW; x < w-bRW; x++) {
repMin = Math.min(repMin, repetitionMap[y*w+x]);
repMax = Math.max(repMax, repetitionMap[y*w+x]);
}
}

for (int y = bRH; y < h-bRH; y++) {
for (int x = bRW; x < w-bRW; x++) {
repetitionMap[y*w+x] = (repetitionMap[y*w+x] - repMin) / (repMax - repMin + EPSILON);
}
}

// Convert dissimilarity into similarity
for (int y = bRH; y < h-bRH; y++) {
for (int x = bRW; x < w-bRW; x++) {
repetitionMap[y*w+x] = 1.0f - repetitionMap[y*w+x];
}
}

}


// --------------------------------------- //
// ---- Filter out irrelevant regions ---- //
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/RedundancyMap_.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void run(String s) {
metrics[3] = "mSSIM";

// Initialize dialog box
NonBlockingGenericDialog gd = new NonBlockingGenericDialog("SReD: Global Redundancy");
NonBlockingGenericDialog gd = new NonBlockingGenericDialog("SReD: Global Repetition Detector");
gd.addNumericField("Block width (px): ", 3, 2);
gd.addNumericField("Box height (px): ", 3, 2);
gd.addCheckbox("Time-lapse?", false);
Expand Down Expand Up @@ -219,7 +219,7 @@ public void run(String s) {
// ---- Calculate Redundancy Map ---- //
// ---------------------------------- //

IJ.log("Calculating redundancy...please wait...");
IJ.log("Calculating global repetition...please wait...");

int rounds = 5; // How many scale levels should be analyzed
if(multiScale == 0){
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/kernelGetPatchCosineSim2D.cl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define bRH $BRH$
#define ref_std $PATCH_STD$
#define EPSILON $EPSILON$

kernel void kernelGetPatchCosineSim2D(
global float* local_stds,
global float* diff_std_map
Expand Down
30 changes: 30 additions & 0 deletions src/main/resources/kernelGetPatchDiffStd2D.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//#pragma OPENCL EXTENSION cl_khr_fp64 : enable
#define w $WIDTH$
#define h $HEIGHT$
#define bRW $BRW$
#define bRH $BRH$
#define ref_std $PATCH_STD$
#define EPSILON $EPSILON$

kernel void kernelGetPatchCosineSim2D(
global float* local_stds,
global float* diff_std_map
){

int gx = get_global_id(0);
int gy = get_global_id(1);

// Bound check (avoids borders dynamically based on patch dimensions)
if(gx<bRW || gx>=w-bRW || gy<bRH || gy>=h-bRH){
return;
}


// -------------------------------------------------------------- //
// ---- Calculate absolute difference of standard deviations ---- //
// -------------------------------------------------------------- //

float test_std = local_stds[gy*w+gx];

diff_std_map[gy*w+gx] = fabs(ref_std - test_std);
}

0 comments on commit 63665f3

Please sign in to comment.