Skip to content

Commit

Permalink
Merge pull request #2 from zhaorz/kroeger
Browse files Browse the repository at this point in the history
Add kroeger baseline
  • Loading branch information
Richard Zhao authored Apr 26, 2017
2 parents 641e53b + 5a23203 commit 0826e5f
Show file tree
Hide file tree
Showing 103 changed files with 8,649 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
# Install with
# $ git config --global core.excludesfile ~/.gitignore_global

# Output files
output
flow_ref

### C ###
# Prerequisites
*.d
Expand Down
2 changes: 2 additions & 0 deletions flow_code/C/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
color_flow
colortest
24 changes: 24 additions & 0 deletions flow_code/C/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Makefile for flow evaluation code

SRC = flowIO.cpp colorcode.cpp colortest.cpp color_flow.cpp
BIN = colortest color_flow

IMGLIB = imageLib

CC = g++
WARN = -W -Wall
OPT ?= -O3
CPPFLAGS = $(OPT) $(WARN) -I$(IMGLIB)
LDLIBS = -L$(IMGLIB) -lImg -lpng -lz
EXE = $(SRC:.cpp=.exe)

all: $(BIN)

colortest: colortest.cpp colorcode.cpp
color_flow: color_flow.cpp flowIO.cpp colorcode.cpp

clean:
rm -f core *.stackdump

allclean: clean
rm -f $(BIN) $(EXE)
40 changes: 40 additions & 0 deletions flow_code/C/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Some utilities for reading, writing, and color-coding .flo images

Daniel Scharstein, 7/2/07
updated 2/9/08 to fix bug in color_flow.cpp
updated 6/9/09 to make robust to NaN or constant 0 flow (thanks Jan Bouecke)

See flowIO.cpp for sample code for reading and writing .flo files.
Here's an excerpt from this file describing the flow file format:

// ".flo" file format used for optical flow evaluation
//
// Stores 2-band float image for horizontal (u) and vertical (v) flow components.
// Floats are stored in little-endian order.
// A flow value is considered "unknown" if either |u| or |v| is greater than 1e9.
//
// bytes contents
//
// 0-3 tag: "PIEH" in ASCII, which in little endian happens to be the float 202021.25
// (just a sanity check that floats are represented correctly)
// 4-7 width as an integer
// 8-11 height as an integer
// 12-end data (width*height*2*4 bytes total)
// the float values for u and v, interleaved, in row order, i.e.,
// u[row0,col0], v[row0,col0], u[row0,col1], v[row0,col1], ...
//


Once you have a .flo file, you can create a color coding of it using
color_flow

Use colortest to visualize the encoding


To compile

cd imageLib
make
cd ..
make
./colortest 10 colors.png
99 changes: 99 additions & 0 deletions flow_code/C/color_flow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// color_flow.cpp
// color-code motion field
// normalizes based on specified value, or on maximum motion present otherwise

// DS 2/9/08 fixed bug in MotionToColor concerning reallocation of colim (thanks Yunpeng!)

static char *usage = "\n usage: %s [-quiet] in.flo out.png [maxmotion]\n";

#include <stdio.h>
#include <math.h>
#include "imageLib.h"
#include "flowIO.h"
#include "colorcode.h"

int verbose = 1;

void MotionToColor(CFloatImage motim, CByteImage &colim, float maxmotion)
{
CShape sh = motim.Shape();
int width = sh.width, height = sh.height;
colim.ReAllocate(CShape(width, height, 3));
int x, y;
// determine motion range:
float maxx = -999, maxy = -999;
float minx = 999, miny = 999;
float maxrad = -1;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
float fx = motim.Pixel(x, y, 0);
float fy = motim.Pixel(x, y, 1);
if (unknown_flow(fx, fy))
continue;
maxx = __max(maxx, fx);
maxy = __max(maxy, fy);
minx = __min(minx, fx);
miny = __min(miny, fy);
float rad = sqrt(fx * fx + fy * fy);
maxrad = __max(maxrad, rad);
}
}
printf("max motion: %.4f motion range: u = %.3f .. %.3f; v = %.3f .. %.3f\n",
maxrad, minx, maxx, miny, maxy);


if (maxmotion > 0) // i.e., specified on commandline
maxrad = maxmotion;

if (maxrad == 0) // if flow == 0 everywhere
maxrad = 1;

if (verbose)
fprintf(stderr, "normalizing by %g\n", maxrad);

for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
float fx = motim.Pixel(x, y, 0);
float fy = motim.Pixel(x, y, 1);
uchar *pix = &colim.Pixel(x, y, 0);
if (unknown_flow(fx, fy)) {
pix[0] = pix[1] = pix[2] = 0;
} else {
computeColor(fx/maxrad, fy/maxrad, pix);
}
}
}
}

int main(int argc, char *argv[])
{
try {
int argn = 1;
if (argc > 1 && argv[1][0]=='-' && argv[1][1]=='q') {
verbose = 0;
argn++;
}
if (argn >= argc-3 && argn <= argc-2) {
char *flowname = argv[argn++];
char *outname = argv[argn++];
float maxmotion = argn < argc ? atof(argv[argn++]) : -1;
CFloatImage im, fband;
ReadFlowFile(im, flowname);
CByteImage band, outim;
CShape sh = im.Shape();
sh.nBands = 3;
outim.ReAllocate(sh);
outim.ClearPixels();
MotionToColor(im, outim, maxmotion);
WriteImageVerb(outim, outname, verbose);
} else
throw CError(usage, argv[0]);
}
catch (CError &err) {
fprintf(stderr, err.message);
fprintf(stderr, "\n");
return -1;
}

return 0;
}
74 changes: 74 additions & 0 deletions flow_code/C/colorcode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// colorcode.cpp
//
// Color encoding of flow vectors
// adapted from the color circle idea described at
// http://members.shaw.ca/quadibloc/other/colint.htm
//
// Daniel Scharstein, 4/2007
// added tick marks and out-of-range coding 6/05/07

#include <stdlib.h>
#include <math.h>
typedef unsigned char uchar;

int ncols = 0;
#define MAXCOLS 60
int colorwheel[MAXCOLS][3];


void setcols(int r, int g, int b, int k)
{
colorwheel[k][0] = r;
colorwheel[k][1] = g;
colorwheel[k][2] = b;
}

void makecolorwheel()
{
// relative lengths of color transitions:
// these are chosen based on perceptual similarity
// (e.g. one can distinguish more shades between red and yellow
// than between yellow and green)
int RY = 15;
int YG = 6;
int GC = 4;
int CB = 11;
int BM = 13;
int MR = 6;
ncols = RY + YG + GC + CB + BM + MR;
//printf("ncols = %d\n", ncols);
if (ncols > MAXCOLS)
exit(1);
int i;
int k = 0;
for (i = 0; i < RY; i++) setcols(255, 255*i/RY, 0, k++);
for (i = 0; i < YG; i++) setcols(255-255*i/YG, 255, 0, k++);
for (i = 0; i < GC; i++) setcols(0, 255, 255*i/GC, k++);
for (i = 0; i < CB; i++) setcols(0, 255-255*i/CB, 255, k++);
for (i = 0; i < BM; i++) setcols(255*i/BM, 0, 255, k++);
for (i = 0; i < MR; i++) setcols(255, 0, 255-255*i/MR, k++);
}

void computeColor(float fx, float fy, uchar *pix)
{
if (ncols == 0)
makecolorwheel();

float rad = sqrt(fx * fx + fy * fy);
float a = atan2(-fy, -fx) / M_PI;
float fk = (a + 1.0) / 2.0 * (ncols-1);
int k0 = (int)fk;
int k1 = (k0 + 1) % ncols;
float f = fk - k0;
//f = 0; // uncomment to see original color wheel
for (int b = 0; b < 3; b++) {
float col0 = colorwheel[k0][b] / 255.0;
float col1 = colorwheel[k1][b] / 255.0;
float col = (1 - f) * col0 + f * col1;
if (rad <= 1)
col = 1 - rad * (1 - col); // increase saturation with radius
else
col *= .75; // out of range
pix[2 - b] = (int)(255.0 * col);
}
}
1 change: 1 addition & 0 deletions flow_code/C/colorcode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void computeColor(float fx, float fy, uchar *pix);
61 changes: 61 additions & 0 deletions flow_code/C/colortest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// colortest.cpp

// create a test image showing the color encoding

static char usage[] = "usage: %s range outimage [size]\n";

# include <stdio.h>
# include <math.h>
#include "imageLib.h"
#include "colorcode.h"

int main(int argc, char **argv)
{
int verbose = 1;
if (argc < 3) {
fprintf(stderr, usage, argv[0]);
exit(1);
}
int optind = 1;
float truerange = atof(argv[optind++]);
char *outname = argv[optind++];
int size = optind < argc ? atoi(argv[optind++]) : 151;

float range = 1.04 * truerange; // make picture a bit bigger to show out-of-range coding
try {
CShape sh(size, size, 3);
CByteImage out(sh);

int s2 = size/2;
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
float fx = (float)x / (float)s2 * range - range;
float fy = (float)y / (float)s2 * range - range;
if (x == s2 || y == s2) // make black coordinate axes
continue;
uchar *pix = &out.Pixel(x, y, 0);
//fx = rintf(fx);
//fy = rintf(fy);
computeColor(fx/truerange, fy/truerange, pix);
}
}
int ir = (int)truerange;
int ticksize = size < 120 ? 1 : 2;
for (int k = -ir; k <= ir; k++) {
int ik = (int)(k / range * s2) + s2;
for (int t = -ticksize; t <= ticksize; t++) {
uchar *pix;
pix = &out.Pixel(ik, s2 + t, 0); pix[0] = pix[1] = pix[2] = 0;
pix = &out.Pixel(s2 + t, ik, 0); pix[0] = pix[1] = pix[2] = 0;
}
}

WriteImageVerb(out, outname, verbose);
}
catch (CError &err) {
fprintf(stderr, err.message);
fprintf(stderr, "\n");
exit(1);
}
return 0;
}
Loading

0 comments on commit 0826e5f

Please sign in to comment.