Compare commits

..

13 Commits

Author SHA1 Message Date
gauthiier
42d048ca04 ... 2023-09-08 08:20:39 +02:00
dviid
5c5581f16e mid-backed commit - master is dead 2012-02-21 18:06:58 +01:00
dviid
b5cebe9304 mid-commit AbstractAnalysis::saveimage 2012-02-20 14:10:20 +01:00
Tom Schofield
3649211943 merged with tom 2012-02-20 13:00:05 +00:00
Tom Schofield
bc19757de2 merge local master 2012-02-20 12:37:44 +00:00
Tom Schofield
d798394793 final check and commit before merge with master 2012-02-20 12:33:13 +00:00
Tom Schofield
bf8c630add make vector of meshes static since we only ever want to display one at a time 2012-02-20 12:00:18 +00:00
Tom Schofield
935d6414b5 tidy up 2012-02-20 11:33:26 +00:00
Tom Schofield
6b401b8424 added synthesise and display results to all analysis classes 2012-02-20 11:25:55 +00:00
Tom Schofield
3fd26c1eb4 fixed bug in color single 2012-02-19 18:51:25 +00:00
Tom Schofield
c53eb2eb6c corrected file confusion AGAIN 2012-02-19 18:25:37 +00:00
Tom Schofield
1d5d6abbde corrected file confusion 2012-02-19 18:17:45 +00:00
Tom Schofield
71e3a9a1db added image loading and display results stuff- changes all commented in code 2012-02-19 16:42:55 +00:00
40 changed files with 1745 additions and 4651 deletions

BIN
.DS_Store vendored

Binary file not shown.

3
.gitignore vendored
View File

@ -1 +1,2 @@
.DS_Store
src/.DS_Store

10
README
View File

@ -1,10 +0,0 @@
WARNING: WORK IN PROGRESS...
configuring OpenFrameworks under Xcode
---------------------------------------
these files are configured according to OpenFrameworks addons - http://ofxaddons.com/howto
(1) drag-drop "ReflectiveIndex" folder into your OpenFrameworks project
(2) Install depedencies: {ofxXmlSettings, ofxOpenCV, ofxFileHelper};
(3) Press Play!

2
README.md Normal file
View File

@ -0,0 +1,2 @@
<img src="https://davidgauthier.info/rfi/img-resize/rfi3.jpg" width="100%">
<img src="https://davidgauthier.info/rfi/img-resize/rfi4.jpg" width="100%">

View File

@ -1,115 +0,0 @@
<!-- THIS FILE NEEDS TO GO IN THE APPLICATION /data/ folder -->
<config>
<mode>analysing</mode>
<camera>
<id>1</id>
<width>640</width>
<height>480</height>
</camera>
<display>
<fps>30</fps>
</display>
<locale>
<name>FUTURE</name>
</locale>
<analysis_NUM_RUN>
<NUM_RUN_shadowscapes> 1 </NUM_RUN_shadowscapes>
<NUM_RUN_relaxrate> 1 </NUM_RUN_relaxrate>
<NUM_RUN_iresponse> 1 </NUM_RUN_iresponse>
<NUM_RUN_shapefromshading> 1 </NUM_RUN_shapefromshading>
<NUM_RUN_strobe> 1 </NUM_RUN_strobe>
<NUM_RUN_camnoise> 1 </NUM_RUN_camnoise>
<NUM_RUN_colorsingle> 1 </NUM_RUN_colorsingle>
<NUM_RUN_colormulti> 1 </NUM_RUN_colormulti>
<NUM_RUN_diffnoise> 1 </NUM_RUN_diffnoise>
</analysis_NUM_RUN>
<analysis_time>
<acquiretime_shadowscapes> 5 </acquiretime_shadowscapes>
<acquiretime_relaxrate> 5 </acquiretime_relaxrate>
<acquiretime_iresponse> 5 </acquiretime_iresponse>
<acquiretime_shapefromshading> 5 </acquiretime_shapefromshading>
<acquiretime_strobe> 5 </acquiretime_strobe>
<acquiretime_camnoise> 5 </acquiretime_camnoise>
<acquiretime_colorsingle> 5 </acquiretime_colorsingle>
<acquiretime_colormulti> 5 </acquiretime_colormulti>
<acquiretime_diffnoise> 5 </acquiretime_diffnoise>
</analysis_time>
<viewport>
<tx>0</tx>
<ty>-20</ty>
<tz>-400</tz>
<rx>-10</rx>
<ry>0</ry>
<rz>0</rz>
</viewport>
<algorithms>
<vertices_per_frame>1000</vertices_per_frame>
<pixel_per_vertex>4</pixel_per_vertex>
<colormulti>
<algo>2</algo>
<scale>1</scale>
<draw_style>2</draw_style>
<line_width>0.5</line_width>
</colormulti>
<colorsingle>
<algo>2</algo>
<scale>1</scale>
<draw_style>2</draw_style>
<line_width>0.5</line_width>
</colorsingle>
<relaxrate>
<algo>1</algo>
<scale>500</scale>
<draw_style>1</draw_style>
<line_width>2</line_width>
<point_size>2</point_size>
</relaxrate>
<diffnoise>
<algo>1</algo>
<scale>500</scale>
<draw_style>1</draw_style>
<line_width>2</line_width>
<point_size>2</point_size>
</diffnoise>
<shapeshade>
<algo>1</algo>
<scale>500</scale>
<draw_style>1</draw_style>
<line_width>2</line_width>
<point_size>2</point_size>
</shapeshade>
<strobe>
<algo>1</algo>
<scale>500</scale>
<draw_style>1</draw_style>
<line_width>2</line_width>
<point_size>2</point_size>
</strobe>
<camnoise>
<algo>1</algo>
<scale>500</scale>
<draw_style>1</draw_style>
<line_width>2</line_width>
<point_size>2</point_size>
</camnoise>
<shadowscapes>
<algo>4</algo>
<scale>500</scale>
<draw_style>2</draw_style>
<line_width>2</line_width>
</shadowscapes>
<iresponse>
<algo>1</algo>
<scale>500</scale>
<draw_style>1</draw_style>
<line_width>2</line_width>
<point_size>2</point_size>
</iresponse>
</algorithms>
</config>

View File

@ -1,31 +0,0 @@
<!-- THIS FILE NEEDS TO GO IN THE APPLICATION /data/ folder -->
<config>
<display>
<fps>30</fps>
</display>
<locale>
<name>DRAWING</name>
</locale>
<viewport>
<tx>0</tx>
<ty>-0</ty>
<tz>-0</tz>
<rx>0</rx>
<ry>0</ry>
<rz>0</rz>
</viewport>
<renderer>
<edit>true</edit>
<vertices_per_frame>1000</vertices_per_frame>
<pixel_per_vertex>1</pixel_per_vertex>
<algorithm>
<algo>3</algo>
<scale>0.71</scale>
<draw_style>3</draw_style>
<line_width>0.1</line_width>
</algorithm>
</renderer>
</config>

View File

@ -1,372 +0,0 @@
/*
~ copyright (c) 2011 dviid
~ contact: dviid@labs.ciid.dk
+ redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ > redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ > redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
const sampler_t smp = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
const sampler_t smp_adrs = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;
float box_integral(read_only image2d_t src, int width, int height, int row, int col, int nbrrows, int nbrcols)
{
float A = 0.0f;
float B = 0.0f;
float C = 0.0f;
float D = 0.0f;
int r0 = min(row, height) - 1;
int c0 = min(col, width) - 1;
int r1 = min(row + nbrrows, height) - 1;
int c1 = min(col + nbrcols, width) - 1;
A = read_imagef(src, smp, (int2)(c0, r0)).x;
B = read_imagef(src, smp, (int2)(c1, r0)).x;
C = read_imagef(src, smp, (int2)(c0, r1)).x;
D = read_imagef(src, smp, (int2)(c1, r1)).x;
return max(0.0f, A - B - C + D);
}
__kernel void hessian_det(
read_only image2d_t src,
int width,
int height,
write_only image2d_t determinant,
write_only image2d_t laplacians,
int layer_width,
int layer_height,
int step,
int filter)
{
int l, w, b;
float Dxx, Dxy, Dyy, inverse;
int idx = get_global_id(0);
int idy = get_global_id(1);
w = filter;
l = w / 3;
b = (w - 1) / 2 + 1
inverse = 1.0f / (w * w);
int c = idx * step;
int r = idy * step;
if(r >= height || c >= width) return;
Dxx = box_integral(src, width, height, r - l + 1, c - b, 2 * l - 1, w) -
box_integral(src, width, height, r - l + 1, c - l / 2, 2 * l - 1, l) * 3;
Dxy = box_integral(src, width, height, r - l, c + 1, l, l) +
box_integral(src, width, height, r + 1, c - l, l, l) -
box_integral(src, width, height, r - 1, c - l, l, l) -
box_integral(src, widht, height, r + 1, c + 1, l, l);
DYY = box_integral(src, width, height, r - b, c - l + 1, w, 2 * l - 1) -
box_integral(src, width, height, r - l / 2, c - l + 1, l, 2 * l -1) * 3;
Dxx += inverse;
Dxy += inverse;
Dyy += inverse;
float4 det = {0.0f, 0.0f, 0.0f, 0.0f};
det.x = (Dxx * Dyy - 0.81f * Dxy * Dxy);
int4 lap = {0, 0, 0, 0};
lap.x = (Dxx + Dyy >= 0 ? 1 : 0);
write_imagef(determinant, (int2)(idx, idy), det);
write_imagef(laplacians, (int2)(idx, idy), lap);
}
int pop_laplacian(read_only image2d_t layer, int c, int r, int width)
{
int lap;
lap = read_imagei(layer, smp_adrs, (int2)(c,r)).x;
return lap;
}
float pop_response(read_only image2d_t layer, int c, int r, int width, int scale)
{
float resp;
resp = read_imagef(layer, smp_adrs, (int2)(c*scale, r*scale)).x;
return resp;
}
bool interpolate_extremum(
int r,
int c,
__global int* pts_cnt;
float2* pos,
float* det_scale
int* laplacian,
read_only image2d_t t,
int t_width,
int t_height,
int t_step,
read_only image2d_t m,
read_only image2d_t mlaplacian,
int m_width,
int m_height,
int m_filter,
read_only image2d_t b,
int b_width,
int b_height,
int b_filter
)
{
// 3D derivatives
int mscale = (m_width / m_height);
int bscale = (b_width / b_height);
float Dx, Dy, Dz;
Dx = (pop_response(m, c+1, r, m_width, mscale) -
pop_response(m, c-1, r, m_width, mscale)) / 2.0f;
Dy = (pop_response(m, c, r+1, m_width, mscale) -
pop_response(m, c, r-1, m_width, mscale)) / 2.0f;
Dz = (pop_response(t, c, r, t_width, 1) -
pop_response(b, c, r, b_width, bscale)) / 2.0f;
// inverse hessian
float v, Dxx, Dyy, Dzz, Dxy, Dxz, Dyz;
v = pop_response(m, r, c, m_width, mscale);
Dxx = pop_response(m, c+1, r, m_width, mscale) +
pop_response(m, c-1, r, m_width, mscale) - 2.0f * v;
Dyy = pop_response(m, c, r+1, m_width, mscale) +
pop_response(m, c, r-1, m_width, mscale) - 2.0f * v;
Dxy = (pop_response(m, c+1, r+1, m_width, mscale) -
pop_response(m, c-1, r+1, m_width, mscale) -
pop_response(m, c+1, r-1, m_width, mscale) +
pop_response(m, c-1, r-1, m_width, mscale)) / 4.0f;
Dzz = pop_response(t, c, r, t_width, 1) -
pop_response(b, c, r, b_width, bscale) - 2.0f * v;
Dxz = (pop_response(t, c+1, r, t_width, 1) -
pop_response(t, c-1, r, t_width, 1) -
pop_response(b, c+1, r, b_width, bscale) +
pop_response(b, c-1, r, b_width, bscale)) / 4.0f;
Dyz = (pop_response(t, c, r+1, t_width, 1) -
pop_response(t, c, r-1, t_width, 1) -
pop_response(b, c, r+1, b_width, bscale) +
pop_response(b, c, r-1, b_width, bscale)) / 4.0f;
float det = Dxx * (Dyy*Dzz - Dyz*Dyz) -
Dxy * (Dxy*Dzz - Dyz*Dxz) +
Dxz * (Dxy*Dyz - Dyy*Dxz);
float invdet = 1.0f / det;
float invDxx = (Dyy*Dzz-Dyz*Dyz) * invdet;
float invDxy = -(Dxy*Dzz-Dyz*Dxz) * invdet;
float invDxz = (Dxy*Dyz-Dyy*Dxz) * invdet;
float invDyx = -(Dxy*Dzz-Dxz*Dyz) * invdet;
float invDyy = (Dxx*Dzz-Dxz*Dxz) * invdet;
float invDyz = -(Dxx*Dyz-Dxy*Dxz) * invdet;
float invDzx = (Dxy*Dyz-Dxz*Dyy) * invdet;
float invDzy = -(Dxx*Dyz-Dxz*Dxy) * invdet;
float invDzz = (Dxx*Dyy-Dxy*Dxy) * invdet;
// derivative * hessian
float xi = 0.0f, xr = 0.0f, xc = 0.0f;
xc -= invDxx * Dx;
xc -= invDxy * Dy;
xc -= invDxz * Dz;
xr -= invDyx * Dx;
xr -= invDyy * Dy;
xr -= invDyz * Dz;
xc -= invDzx * Dx;
xc -= invDzy * Dy;
xc -= invDzz * Dz;
// extremum??
if(fabs(xi) < 0.5f && fabs(xr) < 0.5f && fabs(xc) < 0.5f) {
int fstep = m_filter - b_filter;
(*pos).x = (float)((c + xc) * fstep);
(*pos).y = (float)((c + xr) * fstep);
*det_scale = (float)(0.1333f) * (m_filter + (xi * fstep));
int s = m_width / t_width;
*laplacian = pop_laplacian(mlaplacian, c * s, r * s, m_width);
return true;
}
return false;
}
bool is_extremum(
int r,
int c,
read_only image2d_t t,
int t_width,
int t_height,
int t_step,
int t_filter,
read_only image2d_t m,
int m_width,
int m_height,
read_only image2d_t b,
int b_width,
int b_height,
float tresh
)
{
int border = (t_filter + 1) / (2 * t_step);
if(r <= border || r >= t_height - border || c <= border || c >= t_width - border) {
return false;
}
int mscale = m_width / t_width;
float candidate = pop_response(m, c, r, m_width, mscale);
if(candidate < tresh) {
return false;
}
// If any response in 3x3x3 is greater candidate not maximum
float localMax = getResponse(t, c-1, r-1, t_width, 1);
localMax = fmax(localMax, getResponse(t, c, r-1, t_width, 1));
localMax = fmax(localMax, getResponse(t, c+1, r-1, t_width, 1));
localMax = fmax(localMax, getResponse(t, c-1, r, t_width, 1));
localMax = fmax(localMax, getResponse(t, c, r, t_width, 1));
localMax = fmax(localMax, getResponse(t, c+1, r, t_width, 1));
localMax = fmax(localMax, getResponse(t, c-1, r+1, t_width, 1));
localMax = fmax(localMax, getResponse(t, c, r+1, t_width, 1));
localMax = fmax(localMax, getResponse(t, c+1, r+1, t_width, 1));
int bScale = b_width/t_width;
localMax = fmax(localMax, getResponse(b, c-1, r-1, b_width, bScale));
localMax = fmax(localMax, getResponse(b, c, r-1, b_width, bScale));
localMax = fmax(localMax, getResponse(b, c+1, r-1, b_width, bScale));
localMax = fmax(localMax, getResponse(b, c-1, r, b_width, bScale));
localMax = fmax(localMax, getResponse(b, c, r, b_width, bScale));
localMax = fmax(localMax, getResponse(b, c+1, r, b_width, bScale));
localMax = fmax(localMax, getResponse(b, c-1, r+1, b_width, bScale));
localMax = fmax(localMax, getResponse(b, c, r+1, b_width, bScale));
localMax = fmax(localMax, getResponse(b, c+1, r+1, b_width, bScale));
//int mScale = m_width/t_width;
localMax = fmax(localMax, getResponse(m, c-1, r-1, m_width, mScale));
localMax = fmax(localMax, getResponse(m, c, r-1, m_width, mScale));
localMax = fmax(localMax, getResponse(m, c+1, r-1, m_width, mScale));
localMax = fmax(localMax, getResponse(m, c-1, r, m_width, mScale));
// This is the candidate pixel
localMax = fmax(localMax, getResponse(m, c+1, r, m_width, mScale));
localMax = fmax(localMax, getResponse(m, c-1, r+1, m_width, mScale));
localMax = fmax(localMax, getResponse(m, c, r+1, m_width, mScale));
localMax = fmax(localMax, getResponse(m, c+1, r+1, m_width, mScale));
// If localMax > candidate, candidate is not the local maxima
if(localMax > candidate) {
return false;
}
return true;
}
__kernel void suppress_non_max(
read_only image2d_t tResponse,
int t_width,
int t_height,
int t_filter,
int t_step,
read_only image2d_t mResponse,
read_only image2d_t mLaplacian,
int m_width,
int m_height,
int m_filter,
read_only image2d_t bResponse,
int b_width;
int b_height,
int b_filter,
__global int* pts_cnt,
__global float2* pix_pos,
__global float* scale,
__global int* laplacian,
int max_pts,
float tresh
)
{
int r = get_global_id(0);
int c = get_global_id(1);
float2 pixpos;
float s;
int lap;
if(is_extremum(r, c, tResponse, t_width, t_height, t_step, t_filter, mResponse, m_width, m_height, bResponse, b_width, b_height, tresh)) {
if(interpolate_extremum(r, c, pts_cnt, &pixpos, &s, &lap, tResponse, t_width, t_height, t_step, mResponse, mLaplacian, m_width, m_height, m_filter, bResponse, b_width, b_height, b_filter)) {
int indx = atom_add(&pts_cnt[0],1);
if(indx < max_pts) {
pix_pos[indx] = pix_pos;
scale[indx] = s;
laplacian[indx] = lap;
}
}
}
}

View File

@ -1,148 +0,0 @@
/*
~ copyright (c) 2011 dviid
~ contact: dviid@labs.ciid.dk
+ redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ > redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ > redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
const sampler_t smp = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
__kernel void sobel(read_only image2d_t src, write_only image2d_t dst)
{
int2 coords = (int2) (get_global_id(0), get_global_id(1));
int2 dx = (int2)(1,0);
int2 dy = (int2)(0,1);
float Gx, Gy, G, theta, p;
if(coords.x > get_image_width(dst) || coords.y > get_image_height(dst))
return;
p = read_imagef(src, smp, coords - dx + dy).s0;
Gx = p;
Gy = p;
p = read_imagef(src, smp, coords + dy).s0;
Gy += 2 * p;
p = read_imagef(src, smp, coords + dx + dy).s0;
Gx -= p;
Gy += p;
p = read_imagef(src, smp, coords - dx).s0;
Gx += 2 * p;
p = read_imagef(src, smp, coords + dx).s0;
Gx -= 2 * p;
p = read_imagef(src, smp, coords - dx - dy).s0;
Gx += p;
Gy -= p;
p = read_imagef(src, smp, coords - dy).s0;
Gy -= 2 * p;
p = read_imagef(src, smp, coords + dx - dy).s0;
Gx -= p;
Gy -= p;
G = sqrt(Gx * Gx + Gy * Gy);
theta = atan(Gx / Gy);
write_imagef(dst, coords, G);
}
__kernel void hgauss(read_only image2d_t src, write_only image2d_t dst,
global read_only float* weights, global float* offsets, const int nbr_weights)
{
int2 coords = (int2) (get_global_id(0), get_global_id(1));
float2 src_coords = (float2) (get_global_id(0), get_global_id(1));
float4 pix = (float4)(0,0,0,0);
int i;
for(i = 0; i < nbr_weights; i++) {
pix += read_imagef(src, smp, src_coords + (float2) (offsets[i], 0.0f)) * weights[i];
}
write_imagef(dst, coords, clamp(pix, 0.0f, 1.0f));
}
__kernel void vgauss(read_only image2d_t src, write_only image2d_t dst,
global read_only float* weights, global float* offsets, const int nbr_weights)
{
int2 coords = (int2) (get_global_id(0), get_global_id(1));
float2 src_coords = (float2) (get_global_id(0), get_global_id(1));
float4 pix = (float4)(0,0,0,0);
int i;
for(i = 0; i < nbr_weights; i++) {
pix += read_imagef(src, smp, src_coords + (float2) (0.0f, offsets[i])) * weights[i];
}
write_imagef(dst, coords, clamp(pix, 0.0f, 1.0f));
}
__kernel void grey(read_only image2d_t src, write_only image2d_t dst)
{
int2 coords = (int2) (get_global_id(0), get_global_id(1));
float4 color = read_imagef(src, smp, coords);
float luminance = 0.3f * color.x + 0.59 * color.y + 0.11 * color.z;
color = (float4)(luminance, luminance, luminance, 1.0f);
write_imagef(dst, coords, color);
}
__kernel void brightness(read_only image2d_t src, write_only image2d_t dst)
{
float max = 0;
int2 coords = (int2) (get_global_id(0), get_global_id(1));
float4 color = read_imagef(src, smp, coords);
max = color.x;
if(color.y > max) { max = color.y; }
if(color.z > max) { max = color.z; }
write_imagef(dst, coords, max);
}
__kernel void lightness(read_only image2d_t src, write_only image2d_t dst)
{
float l = 0;
int2 coords = (int2) (get_global_id(0), get_global_id(1));
float4 color = read_imagef(src, smp, coords);
l = (color.x + color.y + color.z) / 3.0f;
write_imagef(dst, coords, l);
}
__kernel void hist()
{
}

View File

@ -1,130 +0,0 @@
/*
~ copyright (c) 2011 dviid
~ contact: dviid@labs.ciid.dk
+ redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ > redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ > redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#define WORK_G_SIZE 64
#define HALF_WORK_G_SIZE (WORK_G_SIZE / 2)
const sampler_t smp = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
__kernel void scan(read_only image2d_t src, write_only image2d_t dst, int rows, int cols)
{
__local float data0[WORK_G_SIZE + HALF_WORK_G_SIZE];
__local float data1[WORK_G_SIZE + HALF_WORK_G_SIZE];
int2 coords = (int2) (get_global_id(0), get_global_id(1));
int X = coords.x;
int Y = coords.x;
if(coords.x < HALF_WORK_G_SIZE) {
data0[coords.x] = 0.0f;
data1[coords.x] = 0.0f;
}
X += HALF_WORK_G_SIZE;
float max_val = 0.0f;
int it = cols / WORK_G_SIZE;
if(cols % WORK_G_SIZE != 0) {
it++;
}
for(int i = 0; i < it; i++) {
int col_offset = i * WORK_G_SIZE + coords.x;
data0[X] = read_imagef(src, smp, (int2)(col_offset, Y)).x;
barrier(CLK_LOCAL_MEM_FENCE);
// 1
data1[X] = data0[X] + data0[X-1];
barrier(CLK_LOCAL_MEM_FENCE);
// 2
data0[X] = data1[X] + data1[X-2];
barrier(CLK_LOCAL_MEM_FENCE);
// 4
data1[X] = data0[X] + data0[X-4];
barrier(CLK_LOCAL_MEM_FENCE);
// 8
data0[X] = data1[X] + data1[X-8];
barrier(CLK_LOCAL_MEM_FENCE);
// 16
data1[X] = data0[X] + data0[X-16];
barrier(CLK_LOCAL_MEM_FENCE);
// 32
data0[X] = data1[X] + data1[X-32];
barrier(CLK_LOCAL_MEM_FENCE);
if(col_offset < cols) {
write_imagef(dst, (int2)(col_offset, Y), (float4)(data0[X] + max_val, 0.0f, 0.0f, 0.0f));
}
max_val += data0[WORK_G_SIZE + HALF_WORK_G_SIZE - 1];
}
}
__kernel void transpose(read_only image2d_t src, write_only image2d_t dst, int rows, int cols)
{
__local float buff[256];
int2 coords = (int2) (get_global_id(0), get_global_id(1));
int inX = coords.x;
int inY = coords.y;
int lX = coords.x;
int lY = coords.y;
int ginX = coords.x * 16 + lX;
int ginY = coords.y * 16 + lY;
buff[lY * 16 + lX] = read_imagef(src, smp, (int2)(ginX, ginY)).x;
barrier(CLK_LOCAL_MEM_FENCE);
int outRows = rows;
int outCols = cols;
int outX = inX;
int outY = inY;
int goutX = coords.x * 16 + lX;
int goutY = coords.y * 16 + lY;
if(goutX >= 0 && goutX < outCols && goutY >=0 && goutY < outRows) {
write_imagef(dst, (int2)(goutX, goutY), (float4)(buff[lX * 16 + lY], 0.0f, 0.0f, 0.0f));
}
}

View File

@ -1,18 +0,0 @@
#version 120
uniform int algo;
uniform sampler2DRect tex0;
void main() {
if(algo == 4) {
vec4 color = texture2DRect(tex0, gl_TexCoord[0].st);
float luminance = 0.3f * color.x + 0.59 * color.y + 0.11 * color.z;
//gl_FragColor = vec4(luminance, luminance, luminance, 1.0f);
gl_FragColor = vec4(1.0f, 1.0f, 1.0f, 0.5f);
return;
}
gl_FragColor = texture2DRect(tex0, gl_TexCoord[0].st);
}

View File

@ -1,127 +0,0 @@
#version 120
#extension GL_EXT_geometry_shader4 : enable
#define ALGO_1 1
#define ALGO_2 2
#define ALGO_3 3
#define ALGO_4 4
uniform int algo;
uniform float scale;
uniform sampler2DRect tex0;
float max_color(vec3 rgb)
{
float max = rgb.r;
if(rgb.g > max) { max = rgb.g; }
if(rgb.b > max) { max = rgb.b; }
return max;
}
float min_color(vec3 rgb)
{
float min = rgb.r;
if(rgb.g < min) { min = rgb.g; }
if(rgb.b < min) { min = rgb.b; }
return min;
}
void hue(in vec4 color, out float h)
{
float max = max_color(color.rgb);
float min = min_color(color.rgb);
if(max == min) { h = 0.f; }
float hueSixth;
if(color.r == max) {
hueSixth = (color.g - color.b) / (max - min);
if(hueSixth < 0.f)
hueSixth += 6.f;
} else if (color.g == max) {
hueSixth = 2.f + (color.b - color.r) / (max - min);
} else {
hueSixth = 4.f + (color.r - color.g) / (max - min);
}
h = 255.f * hueSixth / 6.f;
}
void saturation(in vec4 color, out float s)
{
float max = max_color(color.rgb);
float min = min_color(color.rgb);
if(max == min) { s = 0.f; }
s = 255.f * (max - min) / max;
}
void brightness(in vec4 color, out float b)
{
b = max_color(color.rgb);
}
void toHSB(in vec3 rgb, out vec3 hsb)
{
float max = max_color(rgb);
float min = min_color(rgb);
if(max == min) {
hsb.x = 0.f;
hsb.y = 0.f;
hsb.z = 255.f * max;
return;
}
float hueSixth;
if(rgb.r == max) {
hueSixth = (rgb.g - rgb.b) / (max - min);
if(hueSixth < 0.f)
hueSixth += 6.f;
} else if (rgb.g == max) {
hueSixth = 2.f + (rgb.b - rgb.r) / (max - min);
} else {
hueSixth = 4.f + (rgb.r - rgb.g) / (max - min);
}
hsb.x = 255.f * hueSixth / 6.f;
hsb.y = 255.f * (max - min) / max;
hsb.z = max;
}
void main()
{
if(algo == ALGO_4 && mod(gl_PositionIn[0].y, 2.f) != 0) return;
for (int i = 0; i < gl_VerticesIn; i++)
{
gl_Position = gl_PositionIn[i];
vec4 color0 = texture2DRect(tex0, gl_TexCoordIn[i][0].st);
float depth = 0.f;
if(algo == ALGO_1) {
brightness(color0, depth);
}
else if(algo == ALGO_2) {
hue(color0, depth);
}
else if(algo == ALGO_3) {
saturation(color0, depth);
}
else if(algo == ALGO_4) {
brightness(color0, depth);
}
else {
brightness(color0, depth);
}
gl_Position = gl_ModelViewProjectionMatrix * vec4(gl_Position.x, gl_Position.y, depth * scale, 1.0);
gl_TexCoord[0] = gl_TexCoordIn[i][0];
EmitVertex();
}
}

View File

@ -1,11 +0,0 @@
#version 120
void main()
{
gl_FrontColor = gl_Color;
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = gl_Vertex;
}

View File

@ -1,5 +1,12 @@
/*
////also the new shit//// todo:
(1) Look at warinings about the #define which get over written
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "RefractiveIndex.h" #include "RefractiveIndex.h"
@ -15,12 +22,7 @@
#include "ofxXmlSettings.h" #include "ofxXmlSettings.h"
#include "ofxArcBall.h" #define CAMERA_ID 1
#include <stdio.h>
#include <stdlib.h>
#define CAMERA_ID 0
#define CAMERA_ACQU_WIDTH 640 #define CAMERA_ACQU_WIDTH 640
#define CAMERA_ACQU_HEIGHT 480 #define CAMERA_ACQU_HEIGHT 480
@ -32,26 +34,8 @@
#define ISTATE_TRANSITION 0xCCCC #define ISTATE_TRANSITION 0xCCCC
#define ISTATE_END 0xDDDD #define ISTATE_END 0xDDDD
static const int subdiv_pixels = 4;
static const int VID_W = 640, VID_H = 480;
static const int VERTICES_X = VID_W / subdiv_pixels, VERTICES_Y = VID_W / subdiv_pixels;
static const int TRI_W = 1000;
int draw_style = FACE;
#define ALGO_1 1
#define ALGO_2 2
#define ALGO_3 3
#define ALGO_4 4
int algo = ALGO_2;
float scale = 1.f;
int _state = ISTATE_UNDEF; int _state = ISTATE_UNDEF;
int RefractiveIndex::_mode;
ofPixels RefractiveIndex::_pixels; ofPixels RefractiveIndex::_pixels;
ofVideoGrabber RefractiveIndex::_vidGrabber; ofVideoGrabber RefractiveIndex::_vidGrabber;
int RefractiveIndex::_vid_w, RefractiveIndex::_vid_h, RefractiveIndex::_vid_id; int RefractiveIndex::_vid_w, RefractiveIndex::_vid_h, RefractiveIndex::_vid_id;
@ -59,84 +43,20 @@ bool RefractiveIndex::_vid_stream_open;
bool RefractiveIndex::_vid_toggle_on; bool RefractiveIndex::_vid_toggle_on;
string RefractiveIndex::_location; string RefractiveIndex::_location;
ofxXmlSettings RefractiveIndex::XML; ofxXmlSettings XML;
ofShader RefractiveIndex::_shader;
ofVboMesh RefractiveIndex::_mesh_vbo;
ofxArcBall RefractiveIndex::cam;
string msg;
void RefractiveIndex::setup_shader_vbo()
{
int vertices_per_frame = XML.getValue("config:algorithms:vertices_per_frame", TRI_W);
int pixel_per_vertex = XML.getValue("config:algorithms:pixel_per_vertex", subdiv_pixels);
int vertices_X = _vid_w / pixel_per_vertex, vertices_Y = _vid_h / pixel_per_vertex;
// VBO
for(int i = 0; i < vertices_X; i++){
for(int j = 0; j < vertices_Y; j++) {
_verts.push_back(ofVec3f((i / (float)vertices_X) * vertices_per_frame, (j / (float) vertices_Y) * vertices_per_frame, 0.0f));
_tex.push_back(ofVec2f(i / (float)vertices_X * _vid_w, j / (float) vertices_Y * _vid_h));
if( ( i + 1 < vertices_X ) && ( j + 1 < vertices_Y ) ) {
//triangle #1
_ind.push_back( (i+0) * vertices_Y + (j+0) );
_ind.push_back( (i+1) * vertices_Y + (j+0) );
_ind.push_back( (i+1) * vertices_Y + (j+1) );
//triangle #2
_ind.push_back( (i+1) * vertices_Y + (j+1) );
_ind.push_back( (i+0) * vertices_Y + (j+1) );
_ind.push_back( (i+0) * vertices_Y + (j+0) );
}
}
}
//ofEnableNormalizedTexCoords();
_mesh_vbo.addVertices(_verts);
_mesh_vbo.addTexCoords(_tex);
_mesh_vbo.addIndices(_ind);
_mesh_vbo.setMode(OF_PRIMITIVE_TRIANGLES);
// geometry shader
_shader.setGeometryInputType(GL_TRIANGLES);
_shader.setGeometryOutputType(GL_TRIANGLES);
_shader.setGeometryOutputCount(3);
_shader.load("dviid/rfi.vert.glsl", "dviid/rfi.frag.glsl", "dviid/rfi.geom.glsl");
printf("Maximum number of output vertices support is: %i\n", _shader.getGeometryMaxOutputCount());
}
void RefractiveIndex::setup() void RefractiveIndex::setup()
{ {
ofSetLogLevel(OF_LOG_VERBOSE);
ofHideCursor();
bool save_config = false; bool save_config = false;
cout << "Loading configuration..." << endl; cout << "Loading configuration..." << endl;
if(XML.loadFile("config.refindx") == false) { if(!XML.loadFile("../data/config.refindx")) {
ofLog(OF_LOG_ERROR) << "error loading config - using default."; ofLog(OF_LOG_ERROR) << "error loading config - using default.";
save_config = true; save_config = true;
} else {
XML.loadFile("config.refindx");
} }
// <mode>
string m = XML.getValue("config:mode", "analysing");
_mode = (m == "analysing" ? MODE_ANALYSING : (m == "drawing" ? MODE_DRAWING : MODE_ANALYSING));
// <camera> // <camera>
_vid_id = XML.getValue("config:camera:id", CAMERA_ID); _vid_id = XML.getValue("config:camera:id", CAMERA_ID);
cout << "_vid_id: " << _vid_id << endl;
_vid_w = XML.getValue("config:camera:width", CAMERA_ACQU_WIDTH); _vid_w = XML.getValue("config:camera:width", CAMERA_ACQU_WIDTH);
_vid_h = XML.getValue("config:camera:height", CAMERA_ACQU_HEIGHT); _vid_h = XML.getValue("config:camera:height", CAMERA_ACQU_HEIGHT);
@ -151,7 +71,6 @@ void RefractiveIndex::setup()
// display // display
cout << "> display" << endl; cout << "> display" << endl;
ofSetFrameRate(fps); ofSetFrameRate(fps);
if(fps > 30) { if(fps > 30) {
ofSetVerticalSync(FALSE); ofSetVerticalSync(FALSE);
} else { } else {
@ -165,40 +84,44 @@ void RefractiveIndex::setup()
cout << "* cam width = " << _vid_w << endl; cout << "* cam width = " << _vid_w << endl;
cout << "* cam height = " << _vid_h << endl; cout << "* cam height = " << _vid_h << endl;
if(_mode == MODE_ANALYSING) {
_vid_stream_open = false; _vid_stream_open = false;
setup_camera(); setup_camera();
}
cout << "RRRRRREADY!" << endl; cout << "RRRRRREADY!" << endl;
_analysisAdapator = NULL; _analysisAdapator = NULL;
_pixels.allocate(_vid_w, _vid_h, OF_IMAGE_COLOR_ALPHA); //getting a warning from the OFlog that the pixels aren't allocated
//void ofPixels::allocate(int w, int h, ofImageType type)
setup_shader_vbo();
//TODO: whichever one of these is first - it always runs twice ? //TODO: whichever one of these is first - it always runs twice ?
_analysisVector.push_back(new ShadowScapesAnalysis(V)); //1 //_analysisVector.push_back(new ShadowScapesAnalysis(V));
_analysisVector.push_back(new ShadowScapesAnalysis(H)); //2 //_analysisVector.push_back(new ShadowScapesAnalysis(H));
_analysisVector.push_back(new ShadowScapesAnalysis(D)); //3 //_analysisVector.push_back(new ShadowScapesAnalysis(D));
_analysisVector.push_back(new RelaxRateAnalysis()); //4
_analysisVector.push_back(new IResponseAnalysis()); //5
_analysisVector.push_back(new ShapeFromShadingAnalysis()); //6
_analysisVector.push_back(new StrobeAnalysis()); //7
_analysisVector.push_back(new CamNoiseAnalysis()); //8
_analysisVector.push_back(new ColorSingleAnalysis()); //9
_analysisVector.push_back(new ColorMultiAnalysis()); //10
_analysisVector.push_back(new DiffNoiseAnalysis()); //11
_currentAnalysis = NULL; _analysisVector.push_back(new RelaxRateAnalysis());
_state = ISTATE_UNDEF;
// disbale <ESC> ? //_analysisVector.push_back(new IResponseAnalysis());
//ofSetEscapeQuitsApp(false);
//_analysisVector.push_back(new ShapeFromShadingAnalysis());
//_analysisVector.push_back(new StrobeAnalysis());
//_analysisVector.push_back(new CamNoiseAnalysis());
//_analysisVector.push_back(new ColorSingleAnalysis());
//_analysisVector.push_back(new ColorMultiAnalysis());
//_analysisVector.push_back(new DiffNoiseAnalysis());
_currentAnalysisIndx = 0;
_currentAnalysis = _analysisVector.at(_currentAnalysisIndx++);
_state = ISTATE_START;
} }
@ -238,8 +161,7 @@ void RefractiveIndex::state_analysis()
case ISTATE_TRANSITION: case ISTATE_TRANSITION:
if(_currentAnalysisIndx >= _analysisVector.size()) { if(_currentAnalysisIndx >= _analysisVector.size()) {
_currentAnalysisIndx = 0; _currentAnalysisIndx = 0;
_currentAnalysis = _analysisVector.at(_currentAnalysisIndx++); _state = ISTATE_END;
_state = ISTATE_START;
} else { } else {
_currentAnalysis = _analysisVector.at(_currentAnalysisIndx++); _currentAnalysis = _analysisVector.at(_currentAnalysisIndx++);
_state = ISTATE_START; _state = ISTATE_START;
@ -247,13 +169,9 @@ void RefractiveIndex::state_analysis()
break; break;
case ISTATE_STOP: case ISTATE_STOP:
stop_analysis(); // blocking stop_analysis(); // blocking
if(_mode == MODE_DRAWING)
_state = ISTATE_UNDEF;
else
_state = ISTATE_TRANSITION; _state = ISTATE_TRANSITION;
break; break;
case ISTATE_END: case ISTATE_END:
if(_mode == MODE_ANALYSING)
stop_camera(); stop_camera();
::exit(1); ::exit(1);
break; break;
@ -267,13 +185,18 @@ void RefractiveIndex::state_analysis()
void RefractiveIndex::update() void RefractiveIndex::update()
{ {
state_analysis(); state_analysis();
RefractiveIndex::_vidGrabber.grabFrame(); // get a new frame from the camera
if (_vidGrabber.isFrameNew())
{
_pixels = _vidGrabber.getPixelsRef(); //get ofPixels from the camera
}
} }
void RefractiveIndex::draw() void RefractiveIndex::draw()
{ {
// black
ofBackground(0, 0, 0); ofBackground(0, 0, 0);
if(_currentAnalysis) if(_currentAnalysis)
_currentAnalysis->draw(); _currentAnalysis->draw();
} }
@ -282,20 +205,15 @@ void RefractiveIndex::setup_camera()
{ {
stop_camera(); stop_camera();
// THIS IS LOADED IN FROM THE XML FILE SETTINGS
_vidGrabber.setDeviceID(_vid_id);
_vidGrabber.listDevices();
if(!_vidGrabber.initGrabber(_vid_w, _vid_h)) { if(!_vidGrabber.initGrabber(_vid_w, _vid_h)) {
ofLog(OF_LOG_ERROR) << "RefractiveIndex::setup_camera - could not initialise grabber"; ofLog(OF_LOG_ERROR) << "RefractiveIndex::setup_camera - could not initialise grabber";
return; return;
} }
_vidGrabber.setVerbose(true);
_vidGrabber.setUseTexture(false); _vidGrabber.setUseTexture(false);
_vidGrabber.listDevices();
_vidGrabber.setVerbose(true);
_vid_stream_open = true; _vid_stream_open = true;
cout << "CAMERA SETUP " << endl; _vidGrabber.setDeviceID(_vid_id);
return;
} }
@ -309,122 +227,11 @@ void RefractiveIndex::stop_camera()
void RefractiveIndex::keyPressed (int key) void RefractiveIndex::keyPressed (int key)
{ {
if( key =='f') if( key =='f')
ofToggleFullscreen(); ofToggleFullscreen();
if(key == 'x')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysis = NULL;
_state = ISTATE_UNDEF;
}
else if(key == '1')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 0;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == '2')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 1;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == '3')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 2;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == '4')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 3;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == '5')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 4;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == '6')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 5;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == '7')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 6;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == '8')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 7;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == '9')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 8;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == '0')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 9;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == 'q')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 10;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
} }
void RefractiveIndex::exit() void RefractiveIndex::exit()
{ {
if(_currentAnalysis)
_analysisAdapator->stop();
stop_camera(); stop_camera();
} }

View File

@ -1,3 +1,8 @@
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#pragma once #pragma once
@ -8,17 +13,6 @@
#include "AbstractAnalysis.h" #include "AbstractAnalysis.h"
#include "AnalysisAdaptor.h" #include "AnalysisAdaptor.h"
#include "ofxOpenCv.h"
#include "ofxXmlSettings.h"
#include "ofxArcBall.h"
#define MODE_DRAWING 0xEEFF
#define MODE_ANALYSING 0xFFEE
#define VERTS 1
#define WIRE 2
#define FACE 3
class RefractiveIndex : public ofBaseApp class RefractiveIndex : public ofBaseApp
{ {
@ -38,8 +32,6 @@ public:
void stop_analysis(); void stop_analysis();
void state_analysis(); void state_analysis();
void setup_shader_vbo();
// ofx // ofx
void keyPressed (int key); void keyPressed (int key);
void keyReleased(int key){;} void keyReleased(int key){;}
@ -66,8 +58,6 @@ public:
// acquisition // acquisition
static ofPixels _pixels; static ofPixels _pixels;
static ofVideoGrabber _vidGrabber; static ofVideoGrabber _vidGrabber;
static int _mode;
vector<string> videoSourceList; vector<string> videoSourceList;
static int _vid_w, _vid_h, _vid_id; static int _vid_w, _vid_h, _vid_id;
static bool _vid_stream_open; static bool _vid_stream_open;
@ -75,18 +65,6 @@ public:
// this should be in xml // this should be in xml
static string _location; static string _location;
static ofxXmlSettings XML; // made this static so we can access RUN_NUM in the analyses
static ofVboMesh _mesh_vbo;
vector<ofVec3f> _verts;
vector<ofVec2f> _tex;
vector<unsigned int> _ind;
static ofxArcBall cam;
static ofShader _shader;
}; };

View File

@ -1,320 +0,0 @@
/*
RefractiveIndexRenderer.cpp - RefractiveIndexRenderer library
Copyright (c) 2013 Copenhagen Institute of Interaction Design.
All right reserved.
This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser Public License for more details.
You should have received a copy of the GNU Lesser Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ author: dviid
+ contact: dviid@labs.ciid.dk
*/
#include "RefractiveIndexRenderer.h"
// of
#include "ofSystemUtils.h"
// of addons
#include "ofxFileHelper.h"
const int VPF = 1000;
const int PPV = 4;
int W = 1280, INPUT_W = 0, H = 800, INPUT_H = 0, SIZE = 0;
bool killnow = false;
bool go = false;
bool edit = false;
ofTrueTypeFont font;
ofImage image;
const int algo_default = 1;
const float scale_default = 1;
const int draw_style_default = 3;
const int line_width_default = 0.5f;
const float point_size_default = 0.5f;
//viewport
float tx, ty, tz, rx, ry, rz;
void RefractiveIndexRenderer::setup(){
ofSetLogLevel(OF_LOG_VERBOSE);
cout << "Loading configuration..." << endl;
if(!XML.loadFile("config.renderer.refindx")) {
ofLog(OF_LOG_ERROR) << "error loading config - using defaults.";
}
string e = XML.getValue("config:renderer:edit", "false");
edit = (e == "true");
if(!setup_dirs())
exit();
if(_images_directory_filenames.size() == 0)
exit();
SIZE = _images_directory_filenames.size();
go = true;
// we assume all files from the images dir are the same size
string sample = _images_directory_filenames[(int)ofRandom(0, _images_directory_filenames.size() -1)];
if(!image.loadImage(sample))
exit();
INPUT_W = image.getWidth();
INPUT_H = image.getHeight();
// allocate pixels data
_pixels.allocate(W, H, OF_IMAGE_COLOR);
// allocate FBO
_fbo.allocate(W, H, GL_RGB);
// allocate VBO
setup_vbo(W, H);
// create shader
setup_shader();
// set index count (progess in rendering + saving images from FBO to drawing/ files)
_indx = 0;
// set load font
font.loadFont("/System/Library/Fonts/Geneva.dfont", 16);
algo = XML.getValue("config:renderer:algorithm:algo", algo_default);
scale = XML.getValue("config:renderer:algorithm:scale", scale_default);
draw_style = XML.getValue("config:renderer:algorithm:draw_style", draw_style_default);
line_width = XML.getValue("config:renderer:algorithm:line_width", line_width_default);
point_size = XML.getValue("config:renderer:algorithm:point_size", point_size_default);
}
void RefractiveIndexRenderer::update(){
if(killnow)
exit();
}
void RefractiveIndexRenderer::draw(){
static bool done = false;
if(!go) return;
if(!edit) {
char str[255];
sprintf(str, "%s \n input: %s\n output: %s \n at: %i \n total: %i", (done ? "Done!" : "Rendering"),_images_directory.c_str(), _draw_directory.c_str(), _indx, SIZE);
font.drawString(str, 10, 50);
}
if(done) return;
if(_indx >= _images_directory_filenames.size()) {
if(!edit) {
done = true;
image.clear();
return;
} else {
_indx = 0;
}
}
// clear previous image
image.clear();
// load new image to render
image.loadImage(_images_directory_filenames[_indx++]);
image.resize(W, H);
// bind FBO
_fbo.begin();
// camera
_cam.begin();
// GL
//ofEnableAlphaBlending();
glShadeModel(GL_SMOOTH);
glLineWidth(line_width);
glPointSize(point_size);
glEnable(GL_POINT_SMOOTH);
ofTranslate(tx, ty, tz);
ofRotateX(rx+1.5*cos(ofGetElapsedTimef())); ofRotateY(ry+1.5*sin(ofGetElapsedTimef())); ofRotateZ(rz);
//ofRotateX(rx); ofRotateY(ry); ofRotateZ(rz);
glScalef(1.5, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
// bind texture
image.bind();
// bind shader
_shader.begin();
_shader.setUniform1i("algo", algo);
_shader.setUniform1f("scale", scale);
_shader.setUniform1i("tex0", 0);
// VBO draw
switch (draw_style) {
case VERTS:
_mesh_vbo.drawVertices();
break;
case WIRE:
_mesh_vbo.drawWireframe();
break;
case FACE:
_mesh_vbo.drawFaces();
break;
}
// unbind shader
_shader.end();
// unbind texture
image.unbind();
_cam.end();
// unbind FBO
_fbo.end();
if(edit) {
_fbo.draw(0, 0);
} else {
std::stringstream s;
s << _indx;
save(_draw_directory + "/" + s.str());
}
}
void RefractiveIndexRenderer::exit(){
ofLog(OF_LOG_ERROR) << "exit...";
}
void RefractiveIndexRenderer::keyPressed (int key){
if( key =='e'){
edit = !edit;
_indx = 0;
}
}
bool file_cmp(string f0, string f1)
{
int v0 = atoi(f0.substr(0, f0.find("_")).c_str());
int v1 = atoi(f1.substr(0, f1.find("_")).c_str());
return v0 < v1;
}
bool RefractiveIndexRenderer::setup_dirs(){
// ask for a directory
ofFileDialogResult r = ofSystemLoadDialog("choooose da folda", true);
if(!r.bSuccess) {
ofSystemAlertDialog("OOOOPS.... ERROR...");
return;
}
// copy names
_images_directory = r.filePath;
_draw_directory = r.filePath + "/darwings";
// list all images in the directory and save into vector
File dir(_images_directory);
if(dir.exists() && dir.isDirectory()) {
vector<string> list;
dir.list(list);
std::sort(list.begin(), list.end(), file_cmp);
for(int i = 0; i < list.size(); i++) {
string filepath = _images_directory + "/" + list[i];
_images_directory_filenames.push_back(filepath);
}
}
// create drawing directory
if(!ofxFileHelper::doesDirectoryExist(_draw_directory))
ofxFileHelper::makeDirectory(_draw_directory);
}
void RefractiveIndexRenderer::setup_vbo(int img_width, int img_height){
int vertices_per_frame = XML.getValue("config:renderer:vertices_per_frame", VPF);
int pixel_per_vertex = XML.getValue("config:renderer:pixel_per_vertex", PPV);
int vertices_X = img_width / pixel_per_vertex, vertices_Y = img_height / pixel_per_vertex;
// VBO
for(int i = 0; i < vertices_X; i++){
for(int j = 0; j < vertices_Y; j++) {
_verts.push_back(ofVec3f((i / (float)vertices_X) * vertices_per_frame, (j / (float) vertices_Y) * vertices_per_frame, 0.0f));
_tex.push_back(ofVec2f(i / (float)vertices_X * img_width, j / (float) vertices_Y * img_height));
//_tex.push_back(ofVec2f(i * pixel_per_vertex, j * pixel_per_vertex));
if( ( i + 1 < vertices_X ) && ( j + 1 < vertices_Y ) ) {
//triangle #1
_ind.push_back( (i+0) * vertices_Y + (j+0) );
_ind.push_back( (i+1) * vertices_Y + (j+0) );
_ind.push_back( (i+1) * vertices_Y + (j+1) );
//triangle #2
_ind.push_back( (i+1) * vertices_Y + (j+1) );
_ind.push_back( (i+0) * vertices_Y + (j+1) );
_ind.push_back( (i+0) * vertices_Y + (j+0) );
}
}
}
//ofEnableNormalizedTexCoords();
_mesh_vbo.addVertices(_verts);
_mesh_vbo.addTexCoords(_tex);
_mesh_vbo.addIndices(_ind);
_mesh_vbo.setMode(OF_PRIMITIVE_TRIANGLES);
}
void RefractiveIndexRenderer::setup_shader(){
// TODO: INTEGRATE JAMIE CODE
/*
_shader.setGeometryOutputCount(3);
_shader.load("rfi.vert", "rfi.frag", "rfi.geom");
_shader.setGeometryInputType(GL_TRIANGLES);
_shader.setGeometryOutputType(GL_TRIANGLES);
*/
_shader.setGeometryOutputCount(3);
_shader.load("dviid/rfi.vert.glsl", "dviid/rfi.frag.glsl", "dviid/rfi.geom.glsl");
_shader.setGeometryInputType(GL_TRIANGLES);
_shader.setGeometryOutputType(GL_TRIANGLES);
printf("Maximum number of output vertices support is: %i\n", _shader.getGeometryMaxOutputCount());
}
void RefractiveIndexRenderer::save(string filename){
// clear pixels
_pixels.clear();
// copy fbo pixels to of pixels
_fbo.readToPixels(_pixels);
// save pixels
ofSaveImage(_pixels, filename+".jpg", OF_IMAGE_QUALITY_BEST);
}

View File

@ -1,81 +0,0 @@
/*
RefractiveIndexRenderer.h - RefractiveIndexRenderer library
Copyright (c) 2012 Copenhagen Institute of Interaction Design.
All right reserved.
This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser Public License for more details.
You should have received a copy of the GNU Lesser Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ author: dviid
+ contact: dviid@labs.ciid.dk
*/
#pragma once
// of
#include "ofMain.h"
#include "ofEvents.h"
// of addons
#include "ofxXmlSettings.h"
#include "ofxArcBall.h"
#define VERTS 1
#define WIRE 2
#define FACE 3
class RefractiveIndexRenderer : public ofBaseApp
{
public:
// of
void setup();
void update();
void draw();
void exit();
void keyPressed (int key);
bool setup_dirs();
void setup_vbo(int img_width, int img_height);
void setup_shader();
void save(string filename);
public:
ofVboMesh _mesh_vbo;
vector<ofVec3f> _verts;
vector<ofVec2f> _tex;
vector<unsigned int> _ind;
ofShader _shader;
ofPixels _pixels;
ofFbo _fbo;
int _indx;
ofxXmlSettings XML;
string _images_directory;
string _draw_directory;
vector<string> _images_directory_filenames;
int algo;
float scale;
int draw_style;
float line_width;
float point_size;
ofxArcBall _cam;
};

View File

@ -1,30 +1,35 @@
#include "ofAppGlutWindow.h" #include "ofAppGlutWindow.h"
#include "RefractiveIndex.h" #include "RefractiveIndex.h"
#include "RefractiveIndexRenderer.h" #include "ofxXmlSettings.h"
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
/////////////////////////dis is the new shit///////////
/////////////////////////dis is the new shit///////////
/////////////////////////dis is the new shit///////////
/////////////////////////dis is the new shit///////////
#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 800
int main() { int main() {
ofAppGlutWindow window; ofAppGlutWindow window;
bool fullscreen; ofxXmlSettings XML;
XML.loadFile("../data/config.refindx");
//fullscreen = true; bool fullscreen = (XML.getValue("config:display:fullscreen", "false") == "true" ? true : false);
fullscreen = false; int screen_w = XML.getValue("config:display:width", SCREEN_WIDTH);
int screen_h = XML.getValue("config:display:height", SCREEN_HEIGHT);
cout << "> display configuration" << endl; cout << "> display configuration" << endl;
cout << "* fullscreen: " << (fullscreen ? "yes" : "no") << endl; cout << "* fullscreen: " << (fullscreen ? "yes" : "no") << endl;
if(!fullscreen) { if(!fullscreen) {
cout << "* screen width: " << SCREEN_WIDTH << endl; cout << "* screen width: " << screen_w << endl;
cout << "* screen height: " << SCREEN_HEIGHT << endl; cout << "* screen height: " << screen_h << endl;
} }
ofSetupOpenGL(&window, SCREEN_WIDTH, SCREEN_HEIGHT, (fullscreen ? OF_FULLSCREEN : OF_WINDOW)); ofSetupOpenGL(&window, screen_w, screen_h, (fullscreen ? OF_FULLSCREEN : OF_WINDOW));
//ofRunApp(new RefractiveIndex()); ofRunApp(new RefractiveIndex());
ofRunApp(new RefractiveIndexRenderer());
} }

View File

@ -1,90 +1,54 @@
/* */ /*
- copyright (c) 2011 Copenhagen Institute of Interaction Design (CIID)
- all rights reserved.
+ redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ > redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ > redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "AbstractAnalysis.h" #include "AbstractAnalysis.h"
#include "RefractiveIndex.h" #include "RefractiveIndex.h"
#include "ofxFileHelper.h" #include "ofxFileHelper.h"
#include "ofSystemUtils.h"
void AbstractAnalysis::setup(int camWidth, int camHeight) {
_cam_w = camWidth; _cam_h = camHeight; vector<ofMesh> AbstractAnalysis::meshes;
if(RefractiveIndex::_mode == MODE_DRAWING) {
ofFileDialogResult r = ofSystemLoadDialog("choooose da folda", true);
if(!r.bSuccess) {
ofSystemAlertDialog("OOOOPS.... ERROR...");
return;
}
_whole_file_path_analysis = r.filePath;
_whole_file_path_synthesis = r.filePath + "/darwings";
}
// viewport
tx = RefractiveIndex::XML.getValue("config:viewport:tx", 200.0);
ty = RefractiveIndex::XML.getValue("config:viewport:ty", 50.0);
tz = RefractiveIndex::XML.getValue("config:viewport:tz", -500.0);
rx = RefractiveIndex::XML.getValue("config:viewport:rx", 0.0);
ry = RefractiveIndex::XML.getValue("config:viewport:ry", 0.0);
rz = RefractiveIndex::XML.getValue("config:viewport:rz", 0.0);
}
// this is the main threaded loop for a given analysis // this is the main threaded loop for a given analysis
void AbstractAnalysis::do_synthesize() { void AbstractAnalysis::do_synthesize() {
switch(RefractiveIndex::_mode)
{
case MODE_ANALYSING:
{
for(int i = 0; i < NUM_RUN; i++) {
cout << "NUM_RUN: " << i << endl;
_saved_filenames_analysis.clear();
_saved_filenames_synthesis.clear();
_state = STATE_ACQUIRING; _state = STATE_ACQUIRING;
acquire(); acquire();
if(_state == STATE_STOP) goto exit;
_state = STATE_SYNTHESISING; _state = STATE_SYNTHESISING;
synthesise(); synthesise();
if(_state == STATE_STOP) goto exit;
_state = STATE_DISPLAY_RESULTS; _state = STATE_DISPLAY_RESULTS;
displayresults(); display_results();
}
break;
}
case MODE_DRAWING:
{
ofxFileHelper fileHelperDrawing;
if(!fileHelperDrawing.doesDirectoryExist(_whole_file_path_synthesis)){
fileHelperDrawing.makeDirectory(_whole_file_path_synthesis);
}
read_dir_create_list(_whole_file_path_analysis);
_state = STATE_SYNTHESISING;
synthesise();
if(_state == STATE_STOP) goto exit;
_state = STATE_DISPLAY_RESULTS;
displayresults();
break;
}
}
exit:
cleanup();
ofxFileHelper::deleteFolder(_whole_file_path_analysis);
ofNotifyEvent(_synthesize_cb, _name); ofNotifyEvent(_synthesize_cb, _name);
} }
void AbstractAnalysis::create_dir_allocate_images() void AbstractAnalysis::create_dir()
{ {
// HERE IS WHERE WE SETUP THE DIRECTORY FOR ALL THE SAVED IMAGES // HERE IS WHERE WE SETUP THE DIRECTORY FOR ALL THE SAVED IMAGES
@ -107,161 +71,266 @@ void AbstractAnalysis::create_dir_allocate_images()
} }
} }
ofxFileHelper fileHelperAnalysis; ofxFileHelper fileHelper;
ofxFileHelper fileHelperSynthesis; _whole_file_path = ofToDataPath("") + ANALYSIS_PATH + RefractiveIndex::_location + "/" + _name + "/"+replaceTime ;
cout << "_whole_file_path = " << _whole_file_path << endl;
_whole_file_path_analysis = ANALYSIS_PATH + RefractiveIndex::_location + "/" + _name + "/"+replaceTime ;
//cout << "_whole_file_path_analysis = " << _whole_file_path_analysis << endl;
if(!fileHelperAnalysis.doesDirectoryExist(_whole_file_path_analysis)){
if(!fileHelperAnalysis.doesDirectoryExist(ANALYSIS_PATH))
fileHelperAnalysis.makeDirectory(ANALYSIS_PATH);
if(!fileHelperAnalysis.doesDirectoryExist(ANALYSIS_PATH+RefractiveIndex::_location))
fileHelperAnalysis.makeDirectory(ANALYSIS_PATH+RefractiveIndex::_location);
if(!fileHelperAnalysis.doesDirectoryExist(ANALYSIS_PATH+RefractiveIndex::_location+"/"+_name))
fileHelperAnalysis.makeDirectory(ANALYSIS_PATH+RefractiveIndex::_location+"/"+_name);
if(!fileHelperAnalysis.doesDirectoryExist(ANALYSIS_PATH+RefractiveIndex::_location+"/"+_name+"/"+replaceTime))
fileHelperAnalysis.makeDirectory(ANALYSIS_PATH+RefractiveIndex::_location+"/"+_name+"/"+replaceTime);
}
_whole_file_path_synthesis = SYNTHESIS_PATH + RefractiveIndex::_location + "/" + _name + "/"+replaceTime;
if(!fileHelperSynthesis.doesDirectoryExist(_whole_file_path_synthesis)){
if(!fileHelperAnalysis.doesDirectoryExist(SYNTHESIS_PATH))
fileHelperSynthesis.makeDirectory(SYNTHESIS_PATH);
if(!fileHelperAnalysis.doesDirectoryExist(SYNTHESIS_PATH+RefractiveIndex::_location))
fileHelperSynthesis.makeDirectory(SYNTHESIS_PATH+RefractiveIndex::_location);
if(!fileHelperAnalysis.doesDirectoryExist(SYNTHESIS_PATH+RefractiveIndex::_location+"/"+_name))
fileHelperSynthesis.makeDirectory(SYNTHESIS_PATH+RefractiveIndex::_location+"/"+_name);
if(!fileHelperAnalysis.doesDirectoryExist(SYNTHESIS_PATH+RefractiveIndex::_location+"/"+_name+"/"+replaceTime))
fileHelperSynthesis.makeDirectory(SYNTHESIS_PATH+RefractiveIndex::_location+"/"+_name+"/"+replaceTime);
if(!fileHelper.doesDirectoryExist(_whole_file_path)){
fileHelper.makeDirectory(ANALYSIS_PATH);
fileHelper.makeDirectory(ANALYSIS_PATH+RefractiveIndex::_location);
fileHelper.makeDirectory(ANALYSIS_PATH+RefractiveIndex::_location+"/"+_name);
fileHelper.makeDirectory(ANALYSIS_PATH+RefractiveIndex::_location+"/"+_name+"/"+replaceTime);
} }
//////////////////////////////END DIRECTORY CREATION ////////////////////////////////////////////////// //////////////////////////////END DIRECTORY CREATION //////////////////////////////////////////////////
//////////////////////////////ALLOCATE IMAGES //////////////////////////////////////////////////
myColorImage1.clear();
myColorImage1.setUseTexture(false);
myColorImage1.allocate(RefractiveIndex::_vid_w, RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
myColorImage2.clear();
myColorImage2.setUseTexture(false);
myColorImage2.allocate(RefractiveIndex::_vid_w, RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
myGrayImage1.clear();
myGrayImage1.setUseTexture(false);
myGrayImage1.allocate(RefractiveIndex::_vid_w, RefractiveIndex::_vid_h, OF_IMAGE_GRAYSCALE);
//////////////////////////////END ALLOCATE IMAGES //////////////////////////////////////////////////
} }
ofPixels AbstractAnalysis::calculateListOfZValues(ofImage image1, ofImage image2, int whichComparison){
//zScale is the mapping factor from pixel difference to shift on the zPlane
int zScale=200;
bool cmp_file(string f0, string f1) ofPixels imagePixels1 = image1.getPixelsRef();
{ ofPixels imagePixels2 = image2.getPixelsRef();
int v0 = atoi(f0.substr(0, f0.find("_")).c_str());
int v1 = atoi(f1.substr(0, f1.find("_")).c_str());
return v0 < v1;
}
void AbstractAnalysis::read_dir_create_list(string folder_path) ofPixels difference;
{ //this unsigned char should be unnecessary - I would have thought - can't you just address the pixel locations in ofPixels directly?
File dir(folder_path); unsigned char * thesePixels = new unsigned char[ imagePixels1.getWidth()*imagePixels1.getHeight()*3];
if(dir.exists() && dir.isDirectory()) { //where are we in the image pixel array
vector<string> list; int x=0;
dir.list(list); int y=0;
std::sort(list.begin(), list.end(), cmp_file); //for each pixel...
for(int i=0;i<imagePixels1.size();i+=3){
//get the colour of each image at this x y location - we will use these colours for comparison according to the below criteria
ofColor colourImage1 = imagePixels1.getColor(x, y);
ofColor colourImage2 = imagePixels2.getColor(x, y);
//COMPARE THIS PIXEL'S VALUES with the first image in the sequence
int thisDiff;
//compare Red
if (whichComparison==1) {
thisDiff=ofMap((colourImage1.r-colourImage2.r),-255,255,0,zScale);
}
//compare blue
if (whichComparison==2) {
thisDiff=ofMap((colourImage1.g-colourImage2.g),-255,255,0,zScale);
}
//compare green
if (whichComparison==3) {
thisDiff=ofMap((colourImage1.b-colourImage2.b),-255,255,0,zScale);
}
//compare hue
if (whichComparison==4) {
thisDiff=ofMap((colourImage1.getHue()-colourImage2.getHue()),-255,255,0,zScale);
}
//compare brightness
if (whichComparison==5) {
thisDiff=ofMap((colourImage1.getBrightness()-colourImage2.getBrightness()),-255,255,0,zScale);
}
thesePixels[i]=thisDiff;
thesePixels[i+1]=thisDiff;
thesePixels[i+2]=thisDiff;
x++;
//new line
if(x>imagePixels1.getWidth()){
x=0;
y++;
for(int i = 0; i < list.size(); i++) {
string filepath = folder_path + "/" + list[i];
_saved_filenames_analysis.push_back(filepath);
} }
} }
difference.setFromPixels(thesePixels,imagePixels1.getWidth(),imagePixels1.getHeight(), 3);
return difference;
} }
void AbstractAnalysis::saveImageAnalysis(string filename) ofPixels AbstractAnalysis::calculateListOfZValues(ofImage image1, ofImage image2, int whichComparison, int colourValue){
{ //zScale is the mapping factor from pixel difference to shift on the zPlane
int zScale=200;
RefractiveIndex::_vidGrabber.grabFrame(); // get a new frame from the camera ofPixels imagePixels1 = image1.getPixelsRef();
ofPixels imagePixels2 = image2.getPixelsRef();
if (RefractiveIndex::_vidGrabber.isFrameNew()) ofPixels difference;
//this unsigned char should be unnecessary - I would have thought - can't you just address the pixel locations in ofPixels directly?
unsigned char * thesePixels = new unsigned char[ imagePixels1.getWidth()*imagePixels1.getHeight()*3];
//where are we in the image pixel array
int x=0;
int y=0;
//for each pixel...
//for(int i=0;i<imagePixels1.size();i+=3){
for(int i=0;i<20;i+=3){
//get the colour of each image at this x y location - we will use these colours for comparison according to the below criteria
ofColor colourImage1 = imagePixels1.getColor(x, y);
ofColor colourImage2 = imagePixels2.getColor(x, y);
//COMPARE THIS PIXEL'S VALUES with the first image in the sequence
int thisDiff;
//compare Red
if (whichComparison==1) {
thisDiff=ofMap((colourImage1.r-colourImage2.r),-255,255,0,zScale);
}
//compare blue
if (whichComparison==2) {
thisDiff=ofMap((colourImage1.g-colourImage2.g),-255,255,0,zScale);
}
//compare green
if (whichComparison==3) {
thisDiff=ofMap((colourImage1.b-colourImage2.b),-255,255,0,zScale);
}
//compare hue
if (whichComparison==4) {
thisDiff=ofMap((colourImage1.getHue()-colourImage2.getHue()),-255,255,0,zScale);
}
//compare brightness
if (whichComparison==5) {
thisDiff=ofMap((colourImage1.getBrightness()-colourImage2.getBrightness()),-255,255,0,zScale);
}
thesePixels[i]=thisDiff;
thesePixels[i+1]=thisDiff;
thesePixels[i+2]=thisDiff;
x++;
//new line
if(x>imagePixels1.getWidth()){
x=0;
y++;
}
}
difference.setFromPixels(thesePixels,imagePixels1.getWidth(),imagePixels1.getHeight(), 3);
return difference;
}
void AbstractAnalysis::setMeshFromPixels(ofPixels somePixels, ofImage currentSecondImage, ofMesh * someMesh){
int x=0;
int y=0;
//get rid of all previous vectors and colours - uncomment if re-setting the mesh on the fly - ie live rather than saving it first
//someMesh->clear();
unsigned char * thesePixels =currentSecondImage.getPixels();
for(int i=0;i<somePixels.size();i+=3){
someMesh->addVertex(ofVec3f(x,y,- somePixels.getColor(x, y).getBrightness() ));
// add colour from current second image of two
someMesh->addColor( currentSecondImage.getColor(x, y) );
x++;
if(x>somePixels.getWidth()){
x=0;
y++;
}
}
}
vector<string>AbstractAnalysis:: getListOfImageFilePaths(string location, string whichAnalysis){
string path = ofToDataPath("")+"debug_analysis/"+location+"/"+whichAnalysis;
//ofxDirList dirList;
ofDirectory dirList;
int numDirs = dirList.listDir(path);
vector<string>directoryNames;
//get the last folder alphabetically - this should probably change to do something fancy with date to find most recent but don't want to code that until we are sure
string dirName=dirList.getName(numDirs-1);
const char *results=dirName.c_str();
ofFile file=ofFile(path+"/"+dirName);
vector<string>fileNamesToReturn;
// get
if(file.isDirectory()){
dirList.listDir(path+"/"+dirName);
//if there are no files, exit here
if(dirList.size()==0){
//if it's empty return an error warning
fileNamesToReturn.push_back("NO FILE HERE!");
cout<<"NO FILE HERE!";
return fileNamesToReturn;
}
for (int i=0; i<dirList.size(); i++) {
string fname=dirList.getName(i);
const char *results=fname.c_str();
//full path is what actually gets written into the vector
string fullPath=path+"/"+dirName+"/"+fname;
fileNamesToReturn.push_back(fullPath);
}
}
else{
cout<<"WARNING, DIRECTORY NOT FOUND";
fileNamesToReturn.push_back("NO FILE HERE!");
}
return fileNamesToReturn;
}
int AbstractAnalysis::getRecordedValueFromFileName(string str){
//split filename by underscore - there HAS to be a quicker way of doing things - its ONE LINE in java :(
char * cstr, *p;
vector<char *>tokens;
//string str ("Please split this phrase into tokens");
//make char pointer array
cstr = new char [str.size()+1];
//copy string to char pointer array
strcpy (cstr, str.c_str());
//tokenise char p array and put first results into pointer?
p=strtok (cstr,"_");
while (p!=NULL)
{ {
RefractiveIndex::_pixels = RefractiveIndex::_vidGrabber.getPixelsRef(); //get ofPixels from the camera p=strtok(NULL,"_");
//push tokenised char into vector
tokens.push_back(p);
}
delete[] cstr;
char *p1;
//cstr = new char [str.size()+1];
//strcpy (cstr, str.c_str());
p1=strtok (tokens[tokens.size()-2],".");
return ofToInt(p1);
}
void AbstractAnalysis::saveimage(string filename)
{
if(RefractiveIndex::_pixels.isAllocated()) {
string fname = _whole_file_path + "/" + filename;
cout << "saving - " << fname << endl;
ofSaveImage(RefractiveIndex::_pixels, fname, OF_IMAGE_QUALITY_BEST);
_saved_filenames.push_back(fname);
} else { } else {
return; ofLog(OF_LOG_ERROR) << "RefractiveIndex::_pixels NOT allocated...";
} }
#ifdef TARGET_OSX
ofSaveImage(RefractiveIndex::_pixels, _whole_file_path_analysis+"/"+filename, OF_IMAGE_QUALITY_BEST);
#elif defined(TARGET_WIN32)
//<---- NEW SAVING - seems to fix WINDOWS saving out BLACK FRAMES PROBLEM ---->
unsigned char * somePixels;
ofPixels appPix = RefractiveIndex::_pixels;
somePixels = appPix.getPixels();
myColorImage1.setUseTexture(false);
myColorImage1.setFromPixels(somePixels,appPix.getWidth(),appPix.getHeight(), OF_IMAGE_COLOR);
myColorImage1.saveImage(ofToDataPath("")+ _whole_file_path_analysis+"/"+filename);
myColorImage1.clear();
#endif
_saved_filenames_analysis.push_back(_whole_file_path_analysis+"/"+filename);
}
void AbstractAnalysis::saveImageSynthesis(string filename, ofxCvImage* newPixels, ofImageType newType)
{
#ifdef TARGET_OSX
ofSaveImage(newPixels->getPixelsRef(), _whole_file_path_synthesis+"/"+filename, OF_IMAGE_QUALITY_BEST);
#elif defined(TARGET_WIN32)
if (newType == OF_IMAGE_COLOR){
myColorImage2.setUseTexture(false);
myColorImage2.setFromPixels(newPixels->getPixels(), newPixels->getWidth(), newPixels->getHeight(), OF_IMAGE_COLOR);
myColorImage2.saveImage(_whole_file_path_synthesis+"/"+filename);
}
if (newType == OF_IMAGE_GRAYSCALE){
myGrayImage1.setUseTexture(false);
// THIS IS HOW YOU HAVE TO SAVE OUT THE GREYSCALE IMAGES on WINDOWS FOR SOME REASON --> i.e.: as an OF_IMAGE_COLOR
// But they don't save properly - they're spatially translated and generally f'd up
myGrayImage1.setFromPixels(newPixels->getPixels(), newPixels->getWidth(), newPixels->getHeight(), OF_IMAGE_COLOR);
myGrayImage1.setImageType(OF_IMAGE_COLOR);
myGrayImage1.saveImage(_whole_file_path_synthesis+"/"+filename);
//myGrayImage1.clear();
}
#endif
_saved_filenames_synthesis.push_back(_whole_file_path_synthesis+"/"+filename);
} }

View File

@ -7,17 +7,21 @@
#include "ofMain.h" #include "ofMain.h"
#include "ofEvents.h" #include "ofEvents.h"
#include "ofxOpenCv.h"
#include <string> #include <string>
#define ANALYSIS_PATH "analysis/" #define ANALYSIS_PATH "analysis/"
#define SYNTHESIS_PATH "synthesis/"
#define STATE_ACQUIRING 0x1111 #define STATE_ACQUIRING 0x1111
#define STATE_SYNTHESISING 0x2222 #define STATE_SYNTHESISING 0x2222
#define STATE_DISPLAY_RESULTS 0x3333 #define STATE_DISPLAY_RESULTS 0x3333
#define STATE_STOP 0xDEADBEEF #define STATE_STOP 0xDEADBEEF
#define COMPARE_RED 1
#define COMPARE_BLUE 2
#define COMPARE_GREEN 3
#define COMPARE_HUE 4
#define COMPARE_BRIGHTNESS 5
class AbstractAnalysis { class AbstractAnalysis {
public: public:
@ -25,22 +29,17 @@ public:
virtual ~AbstractAnalysis(){;} virtual ~AbstractAnalysis(){;}
// generic function to set up the camera // generic function to set up the camera
virtual void setup(int camWidth, int camHeight); virtual void setup(int camWidth, int camHeight){_cam_w = camWidth; _cam_h = camHeight;}
// this is the main threaded loop for a given analysis // this is the main threaded loop for a given analysis
void do_synthesize(); void do_synthesize();
// show the results to the screen // ofx
virtual void draw() = 0; virtual void draw() = 0;
protected: protected:
virtual void create_dir_allocate_images(); virtual void create_dir();
virtual void saveimage(string filename);
virtual void read_dir_create_list(string folder_path);
virtual void saveImageAnalysis(string filename);
virtual void saveImageSynthesis(string filename, ofxCvImage* newPixels, ofImageType newType);
// acquire images - all the children (see - do_synthesize) // acquire images - all the children (see - do_synthesize)
virtual void acquire() = 0; virtual void acquire() = 0;
@ -48,42 +47,51 @@ protected:
// analysis + synthesize images - all the children (see - do_synthesize) // analysis + synthesize images - all the children (see - do_synthesize)
virtual void synthesise() = 0; virtual void synthesise() = 0;
// display the results from disk //added tom s 19/2 function runs a call back exactly like acquire.
virtual void displayresults() = 0; virtual void display_results() = 0;
// display the results from disk //returns ofPixels which contain the color differences between the two images. Is overloaded to include comparison with values written in to file names for some analyses
virtual void cleanup() {;} virtual ofPixels calculateListOfZValues(ofImage image1, ofImage image2, int whichComparison);
virtual ofPixels calculateListOfZValues(ofImage image1, ofImage image2, int whichComparison, int colourValue);
//uses the returned pixels from make3DZmap to make a mesh of points whose Z positions are set by the brightness values in ofPixels - ofPixels is being used as a convenient container for a bunch of z coordinates
virtual void setMeshFromPixels(ofPixels somePixels, ofImage currentSecondImage, ofMesh * someMesh);
//HELPER FUNCTIONS
//this is purely for debug/viewing purposes and loads old images from middlesborough test
virtual vector<string> getListOfImageFilePaths(string location, string whichAnalysis);
//splits up the filename and returns the recorded value eg brightness
//EG FILENAME : DIFF_NOISE_7_85.7322.jpg RETURNS : 85.7322
virtual int getRecordedValueFromFileName(string str);
static vector<ofMesh>meshes;
public: public:
string _name; string _name;
string _draw_directory;
// event // event
ofEvent<string> _synthesize_cb; ofEvent<string> _synthesize_cb;
protected: protected:
int _cam_w, _cam_h; int _cam_w, _cam_h;
string _whole_file_path_analysis, _whole_file_path_synthesis; string _whole_file_path;
vector<string> _saved_filenames_analysis; vector<string> _saved_filenames;
vector<string> _saved_filenames_synthesis;
int _state; int _state;
ofImage myColorImage1;
ofImage myColorImage2;
ofImage myGrayImage1;
//int _run_cnt;
float DELTA_T_SAVE; float DELTA_T_SAVE;
int NUM_PHASE; int NUM_PHASE;
int NUM_RUN; int NUM_RUN;
int NUM_SAVE_PER_RUN; int NUM_SAVE_PER_RUN;
//viewport //added Tom S 19/2/12
//each mesh in the vector is a seperate 3D point cloud which is coloured with pixel data and shifted in the z plane according to the specified type of colour difference eg red value or hue
float tx, ty, tz, rx, ry, rz; //make this vector were static
//how fast to move from one mesh to the next
float speed;
//the index (inside the vector of meshes) of the current mesh being displayed
float whichMesh;
friend class AnalysisAdaptor; friend class AnalysisAdaptor;
}; };

View File

@ -21,15 +21,12 @@ public:
void start() void start()
{ {
_stopping = false;
_runnable = new RunnableAdapter<AbstractAnalysis>(*_analysis, &AbstractAnalysis::do_synthesize); _runnable = new RunnableAdapter<AbstractAnalysis>(*_analysis, &AbstractAnalysis::do_synthesize);
_worker.start(*_runnable); _worker.start(*_runnable);
} }
void stop() void stop()
{ {
if(_stopping) return;
_stopping = true;
_analysis->_state = STATE_STOP; _analysis->_state = STATE_STOP;
_worker.join(); _worker.join();
} }
@ -38,6 +35,5 @@ protected:
AbstractAnalysis* _analysis; AbstractAnalysis* _analysis;
Thread _worker; // Thread _worker; //
RunnableAdapter<AbstractAnalysis>* _runnable; RunnableAdapter<AbstractAnalysis>* _runnable;
bool _stopping;
}; };

View File

@ -1,3 +1,8 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "CamNoiseAnalysis.h" #include "CamNoiseAnalysis.h"
#include "ofMain.h" #include "ofMain.h"
@ -10,70 +15,19 @@ using Poco::Timer;
using Poco::TimerCallback; using Poco::TimerCallback;
using Poco::Thread; using Poco::Thread;
#define NUMBER_RUNS 1
#define ACQUIRE_TIME 20
const int algo_default = 1;
const int scale_default = 500;
const int draw_style_default = 3;
const int line_width_default = 0.5f;
const float point_size_default = 0.5f;
void CamNoiseAnalysis::setup(int camWidth, int camHeight) void CamNoiseAnalysis::setup(int camWidth, int camHeight)
{ {
AbstractAnalysis::setup(camWidth, camHeight); DELTA_T_SAVE = 200;
NUM_PHASE = 1;
NUM_RUN = 1;
NUM_SAVE_PER_RUN = 100;
NUM_RUN = RefractiveIndex::XML.getValue("config:analysis_NUM_RUN:NUM_RUN_camnoise", NUMBER_RUNS); create_dir();
cout << "NUM_RUN CamNoiseAnalysis " << NUM_RUN << endl;
//NUM_RUN = 5;
int acq_run_time; // 10 seconds of acquiring per run
acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_camnoise", ACQUIRE_TIME);
cout << "ACQUIRE_TIME CamNoiseAnalysis " << acq_run_time << endl;
//int acq_run_time = 20; // 20 seconds of acquiring per run
DELTA_T_SAVE = 2*(10*acq_run_time/2); // for 20 seconds, we want this to be around 200 files
// or 5 times per second = every 200 ms
_frame_cnt_max = acq_run_time*ofGetFrameRate(); // e.g.: 30 frames per second * 20 seconds = 600 frames
_frame_cnt = 0; _frame_cnt = 0;
_run_cnt = 0; _frame_cnt_max = ofGetFrameRate() * ((DELTA_T_SAVE * NUM_SAVE_PER_RUN) / 1000);
_synth_save_cnt = 0;
c = 0; c = 0;
int anim_time = 10; // 10 seconds
_anim_cnt_max = anim_time*ofGetFrameRate(); // e.g.: 30 frames per second = 150 frames
//create_dir_allocate_images();
_show_image = false;
_image_shown = false;
image1.clear();
image2.clear();
// images use for drawing the synthesized files to the screen ///
image1.setUseTexture(false); // the non texture image that is needed to first load the image
image2.setUseTexture(true); // the image that needs to get written to the screen which takes the content of image1
image1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
////---------
algo = RefractiveIndex::XML.getValue("config:algorithms:camnoise:algo", algo_default);
scale = RefractiveIndex::XML.getValue("config:algorithms:camnoise:scale", scale_default);
draw_style = RefractiveIndex::XML.getValue("config:algorithms:camnoise:draw_style", draw_style_default);
line_width = RefractiveIndex::XML.getValue("config:algorithms:camnoise:line_width", line_width_default);
point_size = RefractiveIndex::XML.getValue("config:algorithms:camnoise:point_size", point_size_default);
} }
@ -81,77 +35,91 @@ void CamNoiseAnalysis::acquire()
{ {
Timer* save_timer; Timer* save_timer;
TimerCallback<CamNoiseAnalysis> save_callback(*this, &CamNoiseAnalysis::save_cb); TimerCallback<CamNoiseAnalysis> save_callback(*this, &CamNoiseAnalysis::save_cb);
_frame_cnt = 0; _save_cnt = 0; _anim_cnt = 0, _synth_save_cnt = 0;
_run_cnt++;
_RUN_DONE = false;
create_dir_allocate_images();
// RUN ROUTINE // RUN ROUTINE
// for(int i = 0; i < NUM_RUN; i++) { for(int i = 0; i < NUM_RUN; i++) {
// _run_cnt = i;
//cout << "RUN NUM = " << i; _run_cnt = i;
cout << "RUN NUM = " << i;
save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
save_timer->start(save_callback); save_timer->start(save_callback);
_RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
while(!_RUN_DONE && _state != STATE_STOP) while(!_RUN_DONE)
Thread::sleep(3); Thread::sleep(3);
save_timer->stop(); save_timer->stop();
}
// }
} }
void CamNoiseAnalysis::synthesise() void CamNoiseAnalysis::synthesise()
{ {
// we don't need to synthesise //incrementer to whichMesh
return; speed=0.2;
//whichMesh is the index in the vector of meshes
whichMesh=0;
/* int index=0;
float iterator=1;
//if you want to see what this looks like with real data ignore the new filenames and load teh old ones.
bool debug=true;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
//cout << "IResponseAnalysis::saving synthesis...\n"; //hack to limit number of meshes.
if(_state == STATE_STOP) return; if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100;
}
_RUN_DONE = false; }
//clear vector so we don't add to it on successive runs
meshes.clear();
// _saved_filenames_synthesis has processed all the files in the analysis images folder for(float i=0;i<_saved_filenames.size()-1;i+=iterator){
while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3);
*/
ofImage image1;
ofImage image2;
//there is a known issue with using loadImage inside classes in other directories. the fix is to call setUseTExture(false)
image1.setUseTexture(false);
image2.setUseTexture(false);
//some of the textures are not loading correctly so only make mesh if both the images load
if(image1.loadImage(_saved_filenames[i]) && image2.loadImage(_saved_filenames[i+1])){
meshes.push_back(ofMesh());
cout<<"setting mesh"<<endl;
int _recorded_hue_value=getRecordedValueFromFileName(_saved_filenames[i]);
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_HUE,_recorded_hue_value), image2, &meshes[index]);
index++;
}
}
} }
void CamNoiseAnalysis::displayresults() void CamNoiseAnalysis::display_results(){
{
for(float i=1;i<_saved_filenames_analysis.size();i++){
if(_state == STATE_STOP) return; Timer* display_results_timer;
//cout << "_saved_filenames_analysis[i] - " << _saved_filenames_synthesis[i] << endl; TimerCallback<CamNoiseAnalysis> display_results_callback(*this, &CamNoiseAnalysis::display_results_cb);
// display results of the synthesis
while(!_image_shown){ display_results_timer = new Timer(0, 20); // timing interval for saving files
Thread::sleep(2); display_results_timer->start(display_results_callback);
//cout << "!_image_shown" << endl; _RUN_DONE = false;
} _results_cnt=0;
_results_cnt_max=300;
_show_image = false; while(!_RUN_DONE)
Thread::sleep(3);
display_results_timer->stop();
if(!image1.loadImage(_saved_filenames_analysis[i])){
//couldn't load image
cout << "didn't load image" << endl;
}
if(image1.loadImage(_saved_filenames_analysis[i])){
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
}
}
} }
@ -172,48 +140,44 @@ void CamNoiseAnalysis::draw()
ofColor aColour; ofColor aColour;
int _fade_in_frames = _frame_cnt_max/10; int _fade_in_frames = _frame_cnt_max/10;
float _number_of_grey_levels=5; float _number_of_grey_levels=10;
float _frames_per_level = _frame_cnt_max / _number_of_grey_levels; float _frames_per_level = _frame_cnt_max / _number_of_grey_levels;
ofColor someColor; ofColor someColor;
/*
if (_frame_cnt < _fade_in_frames) { if (_frame_cnt < _fade_in_frames) {
aColour.set(255, 255, 255, ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255)); aColour.set(255, 255, 255, ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255));
ofSetColor(aColour); ofSetColor(aColour);
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
//cout << "FADE IN STROBE TIME " << endl; //cout << "FADE IN STROBE TIME " << endl;
} }
*/
//if (_frame_cnt >= _fade_in_frames && _frame_cnt < (_frame_cnt_max)){ if (_frame_cnt >= _fade_in_frames && _frame_cnt < (_frame_cnt_max-_fade_in_frames)){
for(int i=1;i<_number_of_grey_levels;i++){ for(int i=0;i<_number_of_grey_levels;i++){
if ( _frame_cnt >= _frames_per_level*(i-1) && +_frame_cnt < _frames_per_level * (i+1) ) { if (_frame_cnt>= _frames_per_level *( i-1) && +_frame_cnt < _frames_per_level * (i) ) {
//set colour to current grey level //set colour to current grey level
c=255-( 255.0 * ( i /_number_of_grey_levels)); c=255-( 255.0 * ( i /_number_of_grey_levels));
//cout << "c: " << c << endl;
someColor.set(c); someColor.set(c);
} }
ofSetColor(someColor); ofSetColor(someColor);
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
} }
//} }
/*
if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max) { if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max) {
aColour.set(0, 0, 0, ofMap(_frame_cnt-(_frame_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255)); aColour.set(0, 0, 0, 255-ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255));
ofSetColor(aColour); ofSetColor(aColour);
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
// cout << "FADE OUT STROBE TIME " << endl; // cout << "FADE OUT STROBE TIME " << endl;
} }
*/
ofDisableAlphaBlending(); ofDisableAlphaBlending();
} else { } else {
// _state = STATE_SYNTHESISING;
_RUN_DONE = true; _RUN_DONE = true;
} }
@ -225,141 +189,36 @@ void CamNoiseAnalysis::draw()
case STATE_SYNTHESISING: case STATE_SYNTHESISING:
{ {
// display animation of something while the synthesis in on-going... // display animation of something while the synthesis in on-going...
//cout << "CamNoiseAnalysis = STATE_SYNTHESISING...\n";
// display animation of something while the synthesis in on-going...
ofEnableAlphaBlending();
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
if(_anim_cnt < _anim_cnt_max){
ofColor aColour;
int rectSizeW = ofGetWidth()/4;
int rectSizeH = ofGetHeight()/4;
int _fade_in_frames = _anim_cnt_max/2;
int c_anim = 10;
int fade;
//ofRotate(ofMap(_anim_cnt/2.0, 0, _anim_cnt_max, 0, 360));
if (_anim_cnt < _fade_in_frames) {
//cout << "CamNoiseAnalysis STATE_SYNTHESIZING = FADING IN ANIMATION...\n";
fade = ofMap(_anim_cnt, 0, _fade_in_frames, 0, 255);
for (int i=0; i <= 15; i++){
c_anim = 0+17*i;
aColour.set(c_anim, c_anim, c_anim, fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt >= _fade_in_frames && _anim_cnt <= (_anim_cnt_max-_fade_in_frames)){
for (int i=0; i <= 15; i++){
c_anim = 255;
aColour.set(c_anim, c_anim, c_anim, 255);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt > (_anim_cnt_max-_fade_in_frames) && _anim_cnt <= _anim_cnt_max) {
//cout << "_anim_cnt = " << _anim_cnt-(_anim_cnt_max-_fade_in_frames) << endl;
fade = ofMap(_anim_cnt-(_anim_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255);
//cout << "fade down = " << fade << endl;
for (int i=0; i <= 15; i++){
c_anim = (17*i);
aColour.set(c_anim, c_anim, c_anim, 255-fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
_anim_cnt++;
} else {
_RUN_DONE = true;
//_state = STATE_DISPLAY_RESULTS;
_anim_cnt=0;
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDisableAlphaBlending();
break; break;
} }
case STATE_DISPLAY_RESULTS: case STATE_DISPLAY_RESULTS:
{ {
// display results of the synthesis
// display results of the synthesis
int imageWidth=640;
int imageHeight =480;
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotateY(_results_cnt*0.3);
//ofRotateX(90);
//ofRotateZ(whichMesh);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2),-400;
ofTranslate((ofGetWidth()/2)-(imageWidth/2),0,0 );
meshes[whichMesh].drawVertices();
ofPopMatrix();
whichMesh+=speed;
cout<<whichMesh<<" size of meshes "<<meshes.size()<<endl;
if(whichMesh>meshes.size() -1 || whichMesh<0){
speed*=-1;
whichMesh+=speed;
if (_frame_cnt > 2)
{
_image_shown = true;
_frame_cnt=0;
} }
_frame_cnt++;
ofEnableAlphaBlending();
glShadeModel(GL_SMOOTH);
glLineWidth(line_width);
glPointSize(point_size);
glEnable(GL_POINT_SMOOTH);
RefractiveIndex::cam.begin();
ofTranslate(tx, ty, tz);
ofRotateX(rx); ofRotateY(ry); ofRotateZ(rz);
glScalef(1.5, 1, 1);
if (_show_image)
image2.setFromPixels(image1.getPixels(), image1.width, image1.height, OF_IMAGE_COLOR);
image2.bind();
RefractiveIndex::_shader.begin();
RefractiveIndex::_shader.setUniform1i("algo", algo);
RefractiveIndex::_shader.setUniform1f("scale", scale);
RefractiveIndex::_shader.setUniform1i("tex0", 0);
switch (draw_style) {
case VERTS:
RefractiveIndex::_mesh_vbo.drawVertices();
break;
case WIRE:
RefractiveIndex::_mesh_vbo.drawWireframe();
break;
case FACE:
RefractiveIndex::_mesh_vbo.drawFaces();
break; break;
} }
RefractiveIndex::_shader.end();
image2.unbind();
RefractiveIndex::cam.end();
break;
}
default: default:
break; break;
@ -374,6 +233,15 @@ void CamNoiseAnalysis::save_cb(Timer& timer)
_save_cnt++; _save_cnt++;
string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg"; string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg";
saveimage(file_name);
//if(_save_cnt >= NUM_SAVE_PER_RUN)
// _RUN_DONE = true;
saveImageAnalysis(file_name); }
void CamNoiseAnalysis::display_results_cb(Timer& timer){
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
} }

View File

@ -9,9 +9,6 @@
#include "Poco/Timer.h" #include "Poco/Timer.h"
#include "ofxOpenCv.h"
class CamNoiseAnalysis : public AbstractAnalysis class CamNoiseAnalysis : public AbstractAnalysis
{ {
public: public:
@ -23,27 +20,17 @@ public:
void setup(int camWidth, int camHeight); void setup(int camWidth, int camHeight);
void acquire(); void acquire();
void synthesise(); void synthesise();
void displayresults(); void display_results();
void draw(); void draw();
void save_cb(Poco::Timer& timer); void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected: protected:
bool _RUN_DONE; bool _RUN_DONE;
int _run_cnt, _save_cnt;
float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max;
int _run_cnt, _save_cnt, _synth_save_cnt,_anim_cnt;
float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max;
bool _show_image, _image_shown;
ofImage image1;
ofImage image2;
int algo;
int scale;
int draw_style;
float line_width;
float point_size;
}; };

View File

@ -1,3 +1,35 @@
/*
- copyright (c) 2011 Copenhagen Institute of Interaction Design (CIID)
- all rights reserved.
+ redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ > redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ > redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "ColorMultiAnalysis.h" #include "ColorMultiAnalysis.h"
#include "ofMain.h" #include "ofMain.h"
@ -9,173 +41,136 @@ using Poco::Timer;
using Poco::TimerCallback; using Poco::TimerCallback;
using Poco::Thread; using Poco::Thread;
#define NUMBER_RUNS 1
#define ACQUIRE_TIME 20
const int algo_default = 1;
const int scale_default = 500;
const int draw_style_default = 3;
const float line_width_default = 0.5f;
void ColorMultiAnalysis::setup(int camWidth, int camHeight) void ColorMultiAnalysis::setup(int camWidth, int camHeight)
{ {
AbstractAnalysis::setup(camWidth, camHeight); DELTA_T_SAVE = 100;//150; // the right number is about 300
NUM_PHASE = 1;
NUM_RUN = RefractiveIndex::XML.getValue("config:analysis_NUM_RUN:NUM_RUN_colormulti", NUMBER_RUNS); NUM_RUN = 1;
cout << "NUM_RUN ColorMultiAnalysis " << NUM_RUN << endl; NUM_SAVE_PER_RUN = 100;//;
//NUM_RUN = 5;
int acq_run_time; // 10 seconds of acquiring per run
acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_colormulti", ACQUIRE_TIME);
cout << "ACQUIRE_TIME ColorMultiAnalysis " << acq_run_time << endl;
//int acq_run_time = 35;
DELTA_T_SAVE = 1*(10*acq_run_time/2); // for 20 seconds, we want this to be around 200 files
// or 10 times per second = every 100 ms
_frame_cnt_max = acq_run_time*ofGetFrameRate(); // e.g.: 30 frames per second * 20 seconds = 600 frames
//create_dir_allocate_images();
create_dir();
_frame_cnt = 0; _frame_cnt = 0;
_run_cnt = 0; _fade_cnt=0;
_synth_save_cnt = 0; _frame_cnt_max = ofGetFrameRate() * ((DELTA_T_SAVE * NUM_SAVE_PER_RUN) / 1000);
c = 0; c = 0;
int anim_time = 10; // 10 seconds
_anim_cnt_max = anim_time*ofGetFrameRate(); // e.g.: 30 frames per second = 150 frames
_show_image = false;
_image_shown = false;
image1.clear();
image2.clear();
// images use for drawing the synthesized files to the screen ///
image1.setUseTexture(false); // the non texture image that is needed to first load the image
image2.setUseTexture(true); // the image that needs to get written to the screen which takes the content of image1
image1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
////---------
algo = RefractiveIndex::XML.getValue("config:algorithms:colormulti:algo", algo_default);
scale = RefractiveIndex::XML.getValue("config:algorithms:colormulti:scale", scale_default);
draw_style = RefractiveIndex::XML.getValue("config:algorithms:colormulti:draw_style", draw_style_default);
line_width = RefractiveIndex::XML.getValue("config:algorithms:colormulti:line_width", line_width_default);
} }
void ColorMultiAnalysis::acquire() void ColorMultiAnalysis::acquire()
{ {
Timer* save_timer; Timer* save_timer;
TimerCallback<ColorMultiAnalysis> save_callback(*this, &ColorMultiAnalysis::save_cb); TimerCallback<ColorMultiAnalysis> save_callback(*this, &ColorMultiAnalysis::save_cb);
_run_cnt++;
_frame_cnt = 0; _save_cnt = 0; _anim_cnt = 0, _synth_save_cnt = 0;
_RUN_DONE = false;
create_dir_allocate_images();
// RUN ROUTINE // RUN ROUTINE
//for(int i = 0; i < NUM_RUN; i++) { for(int i = 0; i < NUM_RUN; i++) {
// _run_cnt = i; _run_cnt = i;
//cout << "RUN NUM = " << i; cout << "RUN NUM = " << i;
save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
save_timer->start(save_callback); save_timer->start(save_callback);
_RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
while(!_RUN_DONE && _state != STATE_STOP) while(!_RUN_DONE)
Thread::sleep(3); Thread::sleep(3);
save_timer->stop(); save_timer->stop();
}
//}
} }
void ColorMultiAnalysis::synthesise() void ColorMultiAnalysis::synthesise()
{ {
//incrementer to whichMesh
speed=0.2;
//whichMesh is the index in the vector of meshes
whichMesh=0;
// we don't need to synthesise int index=0;
return; float iterator=1;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
/* //hack to limit number of meshes.
if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100;
}
//cout << "IResponseAnalysis::saving synthesis...\n"; }
if(_state == STATE_STOP) return; //clear vector so we don't add to it on successive runs
meshes.clear();
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){
ofImage image1;
ofImage image2;
//there is a known issue with using loadImage inside classes in other directories. the fix is to call setUseTExture(false)
image1.setUseTexture(false);
image2.setUseTexture(false);
//some of the textures are not loading correctly so only make mesh if both the images load
if(image1.loadImage(_saved_filenames[0]) && image2.loadImage(_saved_filenames[i+1])){
cout<<"setting mesh"<<endl;
meshes.push_back(ofMesh());
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_HUE), image2, &meshes[index]);
index++;
}
}
}
void ColorMultiAnalysis::display_results(){
Timer* display_results_timer;
TimerCallback<ColorMultiAnalysis> display_results_callback(*this, &ColorMultiAnalysis::display_results_cb);
// display results of the synthesis
display_results_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
display_results_timer->start(display_results_callback);
_RUN_DONE = false; _RUN_DONE = false;
_results_cnt=0;
_results_cnt_max=500;
// _saved_filenames_synthesis has processed all the files in the analysis images folder while(!_RUN_DONE)
while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3); Thread::sleep(3);
*/
display_results_timer->stop();
} }
void ColorMultiAnalysis::displayresults()
{
for(float i=1;i<_saved_filenames_analysis.size();i++){
if(_state == STATE_STOP) return;
//cout << "_saved_filenames_analysis[i] - " << _saved_filenames_synthesis[i] << endl;
while(!_image_shown){
Thread::sleep(2);
//cout << "!_image_shown" << endl;
}
_show_image = false;
if(!image1.loadImage(_saved_filenames_analysis[i])){
//couldn't load image
cout << "didn't load image" << endl;
}
if(image1.loadImage(_saved_filenames_analysis[i])){
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
}
}
}
void ColorMultiAnalysis::draw() void ColorMultiAnalysis::draw()
{ {
switch (_state) { switch (_state) {
case STATE_ACQUIRING: case STATE_ACQUIRING:
{ {
ofEnableAlphaBlending();
if (_frame_cnt < _frame_cnt_max) if (_frame_cnt < _frame_cnt_max)
{ {
int _fade_in_frames = _frame_cnt_max/50; int _fade_in_frames = _frame_cnt_max/50;
ofColor aColor;
if (_frame_cnt < _fade_in_frames) { if (_frame_cnt < _fade_in_frames) {
ofColor aColor;
aColor.setHsb(c, ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255), ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255)); aColor.setHsb(c, ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255), ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255));
ofSetColor(aColor); ofSetColor(aColor);
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
//cout << "FADING IN..." << endl; cout << "FADING IN..." << endl;
} }
if (_frame_cnt >= _fade_in_frames && _frame_cnt < _frame_cnt_max-_fade_in_frames){ if (_frame_cnt >= _fade_in_frames && _frame_cnt < _frame_cnt_max-_fade_in_frames){
ofColor aColor;
aColor.setHsb(c, 255, 255); aColor.setHsb(c, 255, 255);
ofSetColor(aColor); ofSetColor(aColor);
@ -185,26 +180,28 @@ void ColorMultiAnalysis::draw()
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
} }
if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt <= _frame_cnt_max) { if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max) {
aColor.set(c, c, c, 255-int(ofMap(_frame_cnt-(_frame_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255))); ofColor aColor;
//aColor.setHsb(c, 255-ofMap(_fade_cnt- (_frame_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255), 255-(ofMap(_fade_cnt-(_frame_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255))); aColor.setHsb(c, 255-ofMap(_fade_cnt, 0, _fade_in_frames, 0, 255), 255-ofMap(_fade_cnt, 0, _fade_in_frames, 0, 255));
ofSetColor(aColor); ofSetColor(aColor);
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
//cout << "FADING OUT..." << endl;
_fade_cnt++;
cout << "FADING OUT..." << endl;
} }
} else { } else {
//_state = STATE_SYNTHESISING;
_RUN_DONE = true; _RUN_DONE = true;
} }
_frame_cnt++; _frame_cnt++;
ofDisableAlphaBlending();
break; break;
} }
@ -212,141 +209,31 @@ void ColorMultiAnalysis::draw()
{ {
// display animation of something while the synthesis in on-going... // display animation of something while the synthesis in on-going...
//cout << "ColorMultiAnalysis = STATE_SYNTHESISING...\n";
// display animation of something while the synthesis in on-going...
ofEnableAlphaBlending();
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
if(_anim_cnt < _anim_cnt_max){
ofColor aColour;
int rectSizeW = ofGetWidth()/4;
int rectSizeH = ofGetHeight()/4;
int _fade_in_frames = _anim_cnt_max/2;
int c_anim = 10;
int fade;
//ofRotate(ofMap(_anim_cnt/2.0, 0, _anim_cnt_max, 0, 360));
if (_anim_cnt < _fade_in_frames) {
//cout << "ColorMultiAnalysis STATE_SYNTHESIZING = FADING IN ANIMATION...\n";
fade = ofMap(_anim_cnt, 0, _fade_in_frames, 0, 255);
for (int i=0; i <= 15; i++){
c_anim = 0+17*i;
aColour.set(c_anim, c_anim, c_anim, fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt >= _fade_in_frames && _anim_cnt <= (_anim_cnt_max-_fade_in_frames)){
for (int i=0; i <= 15; i++){
c_anim = 255;
aColour.set(c_anim, c_anim, c_anim, 255);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt > (_anim_cnt_max-_fade_in_frames) && _anim_cnt <= _anim_cnt_max) {
//cout << "_anim_cnt = " << _anim_cnt-(_anim_cnt_max-_fade_in_frames) << endl;
fade = ofMap(_anim_cnt-(_anim_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255);
//cout << "fade down = " << fade << endl;
for (int i=0; i <= 15; i++){
c_anim = (17*i);
aColour.set(c_anim, c_anim, c_anim, 255-fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
_anim_cnt++;
} else {
//_state = STATE_DISPLAY_RESULTS;
_anim_cnt=0;
_RUN_DONE = true;
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDisableAlphaBlending();
break; break;
} }
case STATE_DISPLAY_RESULTS: case STATE_DISPLAY_RESULTS:
{ {
int imageWidth=640;
int imageHeight =480;
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotateY(_results_cnt*0.3);
//ofRotateX(90);
//ofRotateZ(whichMesh);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2),-400;
ofTranslate((ofGetWidth()/2)-(imageWidth/2),0,0 );
//cout << "STATE_DISPLAY_RESULTS...\n" << endl; meshes[whichMesh].drawVertices();
ofPopMatrix();
whichMesh+=speed;
if (_frame_cnt > 2) if(whichMesh>meshes.size() -1 || whichMesh<0){
{ speed*=-1;
_image_shown = true; whichMesh+=speed;
_frame_cnt=0;
} }
_frame_cnt++;
ofEnableAlphaBlending();
glShadeModel(GL_SMOOTH);
glLineWidth(line_width);
RefractiveIndex::cam.begin();
ofTranslate(tx, ty, tz);
ofRotateX(rx); ofRotateY(ry); ofRotateZ(rz);
glScalef(1.5, 1, 1);
if (_show_image)
image2.setFromPixels(image1.getPixels(), image1.width, image1.height, OF_IMAGE_COLOR);
image2.bind();
RefractiveIndex::_shader.begin();
RefractiveIndex::_shader.setUniform1i("algo", algo);
RefractiveIndex::_shader.setUniform1f("scale", scale);
RefractiveIndex::_shader.setUniform1i("tex0", 0);
switch (draw_style) {
case VERTS:
RefractiveIndex::_mesh_vbo.drawVertices();
break; break;
case WIRE:
RefractiveIndex::_mesh_vbo.drawWireframe();
break;
case FACE:
RefractiveIndex::_mesh_vbo.drawFaces();
break;
}
RefractiveIndex::_shader.end();
image2.unbind();
RefractiveIndex::cam.end();
_RUN_DONE = true;
break;
} }
@ -360,9 +247,20 @@ void ColorMultiAnalysis::draw()
// this runs at save_cb timer rate = DELTA_T_SAVE // this runs at save_cb timer rate = DELTA_T_SAVE
void ColorMultiAnalysis::save_cb(Timer& timer) void ColorMultiAnalysis::save_cb(Timer& timer)
{ {
_save_cnt++;
// cout << "COLORMULTIANALYSIS::saving...\n";
string file_name = ofToString(_save_cnt,2)+"_"+ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg"; string file_name = ofToString(_save_cnt,2)+"_"+ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg";
saveimage(file_name);
_save_cnt++;
saveImageAnalysis(file_name);
} }
void ColorMultiAnalysis::display_results_cb(Timer& timer){
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
}

View File

@ -4,9 +4,6 @@
#include "Poco/Timer.h" #include "Poco/Timer.h"
#include "ofxOpenCv.h"
class ColorMultiAnalysis : public AbstractAnalysis class ColorMultiAnalysis : public AbstractAnalysis
{ {
public: public:
@ -18,27 +15,15 @@ public:
void setup(int camWidth, int camHeight); void setup(int camWidth, int camHeight);
void acquire(); void acquire();
void synthesise(); void synthesise();
void displayresults(); void display_results();
void draw(); void draw();
void save_cb(Poco::Timer& timer); void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected: protected:
bool _RUN_DONE; bool _RUN_DONE;
int _run_cnt, _save_cnt, _fade_cnt;
int _run_cnt, _save_cnt, _fade_cnt, _synth_save_cnt, _anim_cnt; float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max;
float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max;
bool _show_image, _image_shown;
ofImage image1;
ofImage image2;
int algo;
int scale;
int draw_style;
float line_width;
}; };

View File

@ -1,3 +1,8 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "ColorSingleAnalysis.h" #include "ColorSingleAnalysis.h"
#include "ofMain.h" #include "ofMain.h"
@ -10,70 +15,23 @@ using Poco::Timer;
using Poco::TimerCallback; using Poco::TimerCallback;
using Poco::Thread; using Poco::Thread;
#define NUMBER_RUNS 1
#define ACQUIRE_TIME 20
const int algo_default = 1;
const int scale_default = 500;
const int draw_style_default = 3;
const float line_width_default = 0.5f;
void ColorSingleAnalysis::setup(int camWidth, int camHeight) void ColorSingleAnalysis::setup(int camWidth, int camHeight)
{ {
AbstractAnalysis::setup(camWidth, camHeight); DELTA_T_SAVE = 100;//300;
NUM_PHASE = 1;
NUM_RUN = 1;
NUM_SAVE_PER_RUN = 100;
NUM_RUN = RefractiveIndex::XML.getValue("config:analysis_NUM_RUN:NUM_RUN_colorsingle", NUMBER_RUNS); create_dir();
cout << "NUM_RUN ColorSingleAnalysis " << NUM_RUN << endl;
//NUM_RUN = 5;
int acq_run_time; // 10 seconds of acquiring per run
acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_colorsingle", ACQUIRE_TIME);
cout << "ACQUIRE_TIME ColorSingleAnalysis " << acq_run_time << endl;
//int acq_run_time = 25; // 20 seconds of acquiring per run
DELTA_T_SAVE = 1*(10*acq_run_time/2); // for 20 seconds, we want this to be around 200 files
// or 10 times per second = every 100 ms
_frame_cnt_max = acq_run_time*ofGetFrameRate(); // e.g.: 30 frames per second * 20 seconds = 600 frames
//create_dir_allocate_images();
_run_cnt = 0;
_frame_cnt = 0; _frame_cnt = 0;
_synth_save_cnt = 0; _frame_cnt_max = ofGetFrameRate() * ((DELTA_T_SAVE * NUM_SAVE_PER_RUN) / 1000);
c = 0;
int anim_time = 10; // 10 seconds
_anim_cnt_max = anim_time*ofGetFrameRate(); // e.g.: 30 frames per second = 150 frames
r = 0; r = 0;
g = 0; g = 0;
b = 0; b = 0;
_fade_cnt=0;
fileNameTag = ""; fileNameTag = "";
_show_image = false;
_image_shown = false;
image1.clear();
image2.clear();
// images use for drawing the synthesized files to the screen ///
image1.setUseTexture(false); // the non texture image that is needed to first load the image
image2.setUseTexture(true); // the image that needs to get written to the screen which takes the content of image1
image1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
////---------
algo = RefractiveIndex::XML.getValue("config:algorithms:colorsingle:algo", algo_default);
scale = RefractiveIndex::XML.getValue("config:algorithms:colorsingle:scale", scale_default);
draw_style = RefractiveIndex::XML.getValue("config:algorithms:colorsingle:draw_style", draw_style_default);
line_width = RefractiveIndex::XML.getValue("config:algorithms:colorsingle:line_width", line_width_default);
} }
@ -84,79 +42,94 @@ void ColorSingleAnalysis::acquire()
TimerCallback<ColorSingleAnalysis> save_callback(*this, &ColorSingleAnalysis::save_cb); TimerCallback<ColorSingleAnalysis> save_callback(*this, &ColorSingleAnalysis::save_cb);
_run_cnt++;
_frame_cnt = 0; _save_cnt = 0; _anim_cnt = 0, _synth_save_cnt = 0;
_RUN_DONE = false;
create_dir_allocate_images();
// RUN ROUTINE // RUN ROUTINE
//for(int i = 0; i < NUM_RUN; i++) { for(int i = 0; i < NUM_RUN; i++) {
// _run_cnt = i;
//cout << "RUN NUM = " << i; _run_cnt = i;
cout << "RUN NUM = " << i;
save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
save_timer->start(save_callback); save_timer->start(save_callback);
_RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
while(!_RUN_DONE && _state != STATE_STOP) while(!_RUN_DONE)
Thread::sleep(3); Thread::sleep(3);
save_timer->stop(); save_timer->stop();
}
// }
} }
void ColorSingleAnalysis::synthesise() void ColorSingleAnalysis::synthesise()
{ {
// _saved_filenames has all the file names of all the saved images
//incrementer to whichMesh
speed=0.2;
//whichMesh is the index in the vector of meshes
whichMesh=0;
// we don't need to synthesise int index=0;
return; float iterator=1;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
/* //hack to limit number of meshes.
if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100;
}
//cout << "IResponseAnalysis::saving synthesis...\n"; }
if(_state == STATE_STOP) return; //clear vector so we don't add to it on successive runs
meshes.clear();
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){
ofImage image1;
ofImage image2;
//there is a known issue with using loadImage inside classes in other directories. the fix is to call setUseTExture(false)
image1.setUseTexture(false);
image2.setUseTexture(false);
//some of the textures are not loading correctly so only make mesh if both the images load
if(image1.loadImage(_saved_filenames[i]) && image2.loadImage(_saved_filenames[i+1])){
meshes.push_back(ofMesh());
if(i<_saved_filenames.size()/3){
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_RED), image2, &meshes[index]);
}
if(i>=_saved_filenames.size()/3 && i<2* _saved_filenames.size()/3){
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_GREEN), image2, &meshes[index]);
}
if(i>= 2* _saved_filenames.size()/3 && i<_saved_filenames.size()){
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_BLUE), image2, &meshes[index]);
}
index++;
}
}
}
void ColorSingleAnalysis::display_results(){
Timer* display_results_timer;
TimerCallback<ColorSingleAnalysis> display_results_callback(*this, &ColorSingleAnalysis::display_results_cb);
// display results of the synthesis
display_results_timer = new Timer(0, 20); // timing interval for saving files
display_results_timer->start(display_results_callback);
_RUN_DONE = false; _RUN_DONE = false;
_results_cnt=0;
_results_cnt_max=500;
// _saved_filenames_synthesis has processed all the files in the analysis images folder while(!_RUN_DONE)
while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3); Thread::sleep(3);
*/
display_results_timer->stop();
} }
void ColorSingleAnalysis::displayresults()
{
for(float i=1;i<_saved_filenames_analysis.size();i++){
if(_state == STATE_STOP) return;
//cout << "_saved_filenames_analysis[i] - " << _saved_filenames_synthesis[i] << endl;
while(!_image_shown){
Thread::sleep(2);
//cout << "!_image_shown" << endl;
}
_show_image = false;
if(!image1.loadImage(_saved_filenames_analysis[i])){
//couldn't load image
cout << "didn't load image" << endl;
}
if(image1.loadImage(_saved_filenames_analysis[i])){
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
}
}
}
void ColorSingleAnalysis::draw() void ColorSingleAnalysis::draw()
{ {
@ -206,8 +179,8 @@ void ColorSingleAnalysis::draw()
if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max){ if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max){
int fade = ofMap(_fade_cnt, 0, _fade_in_frames, 0, 255); ofSetColor(0, 0, 255-ofMap(_fade_cnt, 0, _fade_in_frames, 0, 255));
ofSetColor(0, 0, 255-fade); cout << "255-ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255)"<< 255-ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255) << endl;
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
_fade_cnt++; _fade_cnt++;
@ -215,7 +188,6 @@ void ColorSingleAnalysis::draw()
} }
} else { } else {
//_state = STATE_SYNTHESISING;
_RUN_DONE = true; _RUN_DONE = true;
} }
@ -227,143 +199,32 @@ void ColorSingleAnalysis::draw()
case STATE_SYNTHESISING: case STATE_SYNTHESISING:
{ {
// display animation of something while the synthesis in on-going... // display animation of something while the synthesis in on-going...
//cout << "ColorSingleAnalysis = STATE_SYNTHESISING...\n";
// display animation of something while the synthesis in on-going...
ofEnableAlphaBlending();
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
if(_anim_cnt < _anim_cnt_max){
ofColor aColour;
int rectSizeW = ofGetWidth()/4;
int rectSizeH = ofGetHeight()/4;
int _fade_in_frames = _anim_cnt_max/2;
int c_anim = 10;
int fade;
//ofRotate(ofMap(_anim_cnt/2.0, 0, _anim_cnt_max, 0, 360));
if (_anim_cnt < _fade_in_frames) {
// cout << "ColorSingleAnalysis STATE_SYNTHESIZING = FADING IN ANIMATION...\n";
fade = ofMap(_anim_cnt, 0, _fade_in_frames, 0, 255);
for (int i=0; i <= 15; i++){
c_anim = 0+17*i;
aColour.set(c_anim, c_anim, c_anim, fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt >= _fade_in_frames && _anim_cnt <= (_anim_cnt_max-_fade_in_frames)){
for (int i=0; i <= 15; i++){
c_anim = 255;
aColour.set(c_anim, c_anim, c_anim, 255);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt > (_anim_cnt_max-_fade_in_frames) && _anim_cnt <= _anim_cnt_max) {
//cout << "_anim_cnt = " << _anim_cnt-(_anim_cnt_max-_fade_in_frames) << endl;
fade = ofMap(_anim_cnt-(_anim_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255);
//cout << "fade down = " << fade << endl;
for (int i=0; i <= 15; i++){
c_anim = (17*i);
aColour.set(c_anim, c_anim, c_anim, 255-fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
_anim_cnt++;
} else {
_RUN_DONE = true;
//_state = STATE_DISPLAY_RESULTS;
_anim_cnt=0;
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDisableAlphaBlending();
break; break;
} }
case STATE_DISPLAY_RESULTS: case STATE_DISPLAY_RESULTS:
{ {
// display results of the synthesis
int imageWidth=640;
int imageHeight =480;
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotateY(_results_cnt*0.3);
//ofRotateX(90);
//ofRotateZ(whichMesh);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2),-400;
ofTranslate((ofGetWidth()/2)-(imageWidth/2),0,0 );
//cout << "STATE_DISPLAY_RESULTS...\n" << endl; meshes[whichMesh].drawVertices();
ofPopMatrix();
whichMesh+=speed;
if (_frame_cnt > 2) if(whichMesh>meshes.size() -1 || whichMesh<0){
{ speed*=-1;
_image_shown = true; whichMesh+=speed;
_frame_cnt=0;
} }
_frame_cnt++;
ofEnableAlphaBlending();
glShadeModel(GL_SMOOTH);
glLineWidth(line_width);
RefractiveIndex::cam.begin();
ofTranslate(tx, ty, tz);
ofRotateX(rx); ofRotateY(ry); ofRotateZ(rz);
glScalef(1.5, 1, 1);
if (_show_image)
image2.setFromPixels(image1.getPixels(), image1.width, image1.height, OF_IMAGE_COLOR);
image2.bind();
RefractiveIndex::_shader.begin();
RefractiveIndex::_shader.setUniform1i("algo", algo);
RefractiveIndex::_shader.setUniform1f("scale", scale);
RefractiveIndex::_shader.setUniform1i("tex0", 0);
switch (draw_style) {
case VERTS:
RefractiveIndex::_mesh_vbo.drawVertices();
break; break;
case WIRE:
RefractiveIndex::_mesh_vbo.drawWireframe();
break;
case FACE:
RefractiveIndex::_mesh_vbo.drawFaces();
break;
}
RefractiveIndex::_shader.end();
image2.unbind();
RefractiveIndex::cam.end();
_RUN_DONE = true;
break;
} }
default: default:
@ -375,9 +236,22 @@ void ColorSingleAnalysis::draw()
// this runs at save_cb timer rate = DELTA_T_SAVE // this runs at save_cb timer rate = DELTA_T_SAVE
void ColorSingleAnalysis::save_cb(Timer& timer) void ColorSingleAnalysis::save_cb(Timer& timer)
{ {
_save_cnt++;
//cout << "ColorSingleAnalysis::saving...\n";
string file_name =ofToString(_save_cnt,2)+"_"+fileNameTag+"_"+ofToString(_run_cnt,2)+".jpg"; string file_name =ofToString(_save_cnt,2)+"_"+fileNameTag+"_"+ofToString(_run_cnt,2)+".jpg";
saveimage(file_name);
_save_cnt++;
//if(_save_cnt >= NUM_SAVE_PER_RUN)
// _RUN_DONE = true;
saveImageAnalysis(file_name);
} }
void ColorSingleAnalysis::display_results_cb(Timer& timer){
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
}

View File

@ -9,9 +9,6 @@
#include "Poco/Timer.h" #include "Poco/Timer.h"
#include "ofxOpenCv.h"
class ColorSingleAnalysis : public AbstractAnalysis class ColorSingleAnalysis : public AbstractAnalysis
{ {
public: public:
@ -23,28 +20,16 @@ public:
void setup(int camWidth, int camHeight); void setup(int camWidth, int camHeight);
void acquire(); void acquire();
void synthesise(); void synthesise();
void displayresults(); void display_results();
void draw(); void draw();
void save_cb(Poco::Timer& timer); void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected: protected:
bool _RUN_DONE; bool _RUN_DONE;
string fileNameTag; string fileNameTag;
float r,g,b; int _run_cnt, _save_cnt, _fade_cnt;
float r,g,b, _frame_cnt, _frame_cnt_max , _results_cnt, _results_cnt_max;
int _run_cnt, _save_cnt, _fade_cnt, _synth_save_cnt, _anim_cnt;
float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max;
bool _show_image, _image_shown;
ofImage image1;
ofImage image2;
int algo;
int scale;
int draw_style;
float line_width;
}; };

View File

@ -1,3 +1,8 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "DiffNoiseAnalysis.h" #include "DiffNoiseAnalysis.h"
#include "ofMain.h" #include "ofMain.h"
@ -10,150 +15,112 @@ using Poco::Timer;
using Poco::TimerCallback; using Poco::TimerCallback;
using Poco::Thread; using Poco::Thread;
#define NUMBER_RUNS 1
#define ACQUIRE_TIME 20
const int algo_default = 1;
const int scale_default = 500;
const int draw_style_default = 3;
const int line_width_default = 0.5f;
const float point_size_default = 0.5f;
void DiffNoiseAnalysis::setup(int camWidth, int camHeight) void DiffNoiseAnalysis::setup(int camWidth, int camHeight)
{ {
AbstractAnalysis::setup(camWidth, camHeight); DELTA_T_SAVE = 600; // right number is about 450
NUM_PHASE = 1;
NUM_RUN = 1;
NUM_SAVE_PER_RUN = 50;
NUM_RUN = RefractiveIndex::XML.getValue("config:analysis_NUM_RUN:NUM_RUN_diffnoise", NUMBER_RUNS); create_dir();
cout << "NUM_RUN DiffNoiseAnalysis " << NUM_RUN << endl; _fade_cnt=0;
//NUM_RUN = 5;
int acq_run_time; // 10 seconds of acquiring per run
acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_diffnoise", ACQUIRE_TIME);
cout << "ACQUIRE_TIME DiffNoiseAnalysis " << acq_run_time << endl;
//int acq_run_time = 20; // 20 seconds of acquiring per run
DELTA_T_SAVE = 1*(10*acq_run_time/2); // for 20 seconds, we want this to be around 200 files
// or 10 times per second = every 100 ms
_frame_cnt_max = acq_run_time*ofGetFrameRate(); // e.g.: 30 frames per second * 20 seconds = 600 frames
//create_dir_allocate_images();
_run_cnt = 0;
_frame_cnt = 0; _frame_cnt = 0;
_synth_save_cnt = 0; _frame_cnt_max = ofGetFrameRate() * ((DELTA_T_SAVE * NUM_SAVE_PER_RUN) / 1000);
c = 0; c = 0;
int anim_time = 10; // 10 seconds
_anim_cnt_max = anim_time*ofGetFrameRate(); // e.g.: 30 frames per second = 150 frames
_show_image = false;
_image_shown = false;
image1.clear();
image2.clear();
// images use for drawing the synthesized files to the screen ///
image1.setUseTexture(false); // the non texture image that is needed to first load the image
image2.setUseTexture(true); // the image that needs to get written to the screen which takes the content of image1
image1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
////---------
algo = RefractiveIndex::XML.getValue("config:algorithms:diffnoise:algo", algo_default);
scale = RefractiveIndex::XML.getValue("config:algorithms:diffnoise:scale", scale_default);
draw_style = RefractiveIndex::XML.getValue("config:algorithms:diffnoise:draw_style", draw_style_default);
line_width = RefractiveIndex::XML.getValue("config:algorithms:diffnoise:line_width", line_width_default);
point_size = RefractiveIndex::XML.getValue("config:algorithms:diffnoise:point_size", point_size_default);
} }
void DiffNoiseAnalysis::acquire() void DiffNoiseAnalysis::acquire()
{ {
Timer* save_timer; Timer* save_timer;
TimerCallback<DiffNoiseAnalysis> save_callback(*this, &DiffNoiseAnalysis::save_cb); TimerCallback<DiffNoiseAnalysis> save_callback(*this, &DiffNoiseAnalysis::save_cb);
_run_cnt++;
_frame_cnt = 0; _save_cnt = 0; _anim_cnt = 0, _synth_save_cnt = 0;
_RUN_DONE = false;
create_dir_allocate_images();
// RUN ROUTINE // RUN ROUTINE
//for(int i = 0; i < NUM_RUN; i++) { for(int i = 0; i < NUM_RUN; i++) {
// _run_cnt = i; _run_cnt = i;
//cout << "RUN NUM = " << i;
cout << "RUN NUM = " << i;
save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
save_timer->start(save_callback); save_timer->start(save_callback);
_RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
while(!_RUN_DONE && _state != STATE_STOP) while(!_RUN_DONE)
Thread::sleep(3); Thread::sleep(3);
save_timer->stop(); save_timer->stop();
}
//}
} }
void DiffNoiseAnalysis::synthesise() void DiffNoiseAnalysis::synthesise()
{ {
//incrementer to whichMesh
speed=0.2;
//whichMesh is the index in the vector of meshes
whichMesh=0;
// we don't need to synthesise int index=0;
return; float iterator=1;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
/* //hack to limit number of meshes.
if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100;
}
//cout << "IResponseAnalysis::saving synthesis...\n"; }
if(_state == STATE_STOP) return; //clear vector so we don't add to it on successive runs
meshes.clear();
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){
ofImage image1;
ofImage image2;
//there is a known issue with using loadImage inside classes in other directories. the fix is to call setUseTExture(false)
image1.setUseTexture(false);
image2.setUseTexture(false);
//some of the textures are not loading correctly so only make mesh if both the images load
if(image1.loadImage(_saved_filenames[i]) && image2.loadImage(_saved_filenames[i+1])){
meshes.push_back(ofMesh());
cout<<"setting mesh"<<endl;
int _recorded_brightness_value=getRecordedValueFromFileName(_saved_filenames[i]);
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_HUE,_recorded_brightness_value), image2, &meshes[index]);
index++;
}
}
}
void DiffNoiseAnalysis::display_results(){
Timer* display_results_timer;
TimerCallback<DiffNoiseAnalysis> display_results_callback(*this, &DiffNoiseAnalysis::display_results_cb);
// display results of the synthesis
display_results_timer = new Timer(0, 20); // timing interval for saving files
display_results_timer->start(display_results_callback);
_RUN_DONE = false; _RUN_DONE = false;
_results_cnt=0;
_results_cnt_max=300;
// _saved_filenames_synthesis has processed all the files in the analysis images folder while(!_RUN_DONE)
while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3); Thread::sleep(3);
*/
} display_results_timer->stop();
void DiffNoiseAnalysis::displayresults() }// this runs at frame rate = 33 ms for 30 FPS
{
for(float i=1;i<_saved_filenames_analysis.size();i++){
if(_state == STATE_STOP) return;
//cout << "_saved_filenames_analysis[i] - " << _saved_filenames_synthesis[i] << endl;
while(!_image_shown){
Thread::sleep(2);
//cout << "!_image_shown" << endl;
}
_show_image = false;
if(!image1.loadImage(_saved_filenames_analysis[i])){
//couldn't load image
cout << "didn't load image" << endl;
}
if(image1.loadImage(_saved_filenames_analysis[i])){
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
}
}
}
// this runs at frame rate = 33 ms for 30 FPS
void DiffNoiseAnalysis::draw() void DiffNoiseAnalysis::draw()
{ {
switch (_state) { switch (_state) {
@ -182,12 +149,12 @@ void DiffNoiseAnalysis::draw()
} }
} }
//cout << "FADING IN..." << endl; cout << "FADING IN..." << endl;
} }
if (_frame_cnt >= _fade_in_frames && _frame_cnt <= (_frame_cnt_max-_fade_in_frames)){ if (_frame_cnt >= _fade_in_frames && _frame_cnt < (_frame_cnt_max-_fade_in_frames)){
for (int i=1; i < ofGetHeight() ; i=i+rectSize) for (int i=1; i < ofGetHeight() ; i=i+rectSize)
{ {
@ -203,27 +170,29 @@ void DiffNoiseAnalysis::draw()
} }
if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt <= _frame_cnt_max) { if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max) {
for (int i=1; i < ofGetHeight() ; i=i+rectSize) for (int i=1; i < ofGetHeight() ; i=i+rectSize)
{ {
for (int j=1; j < ofGetWidth(); j=j+rectSize) for (int j=1; j < ofGetWidth(); j=j+rectSize)
{ {
c = ofRandom(0,255); c = ofRandom(0,255);
aColour.set(c, c, c, 255-ofMap(_frame_cnt-(_frame_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255)); aColour.set(c, c, c, 255-ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255));
ofSetColor(aColour); ofSetColor(aColour);
ofRect(j, i, rectSize, rectSize); ofRect(j, i, rectSize, rectSize);
} }
} }
//_fade_cnt++; _fade_cnt++;
//cout << "FADING OUT..." << endl; cout << "FADING OUT..." << endl;
} }
ofDisableAlphaBlending(); ofDisableAlphaBlending();
} else { } else {
// _state = STATE_SYNTHESISING;
_RUN_DONE = true; _RUN_DONE = true;
} }
@ -236,152 +205,73 @@ void DiffNoiseAnalysis::draw()
case STATE_SYNTHESISING: case STATE_SYNTHESISING:
{ {
// display animation of something while the synthesis in on-going... // display animation of something while the synthesis in on-going...
//cout << "DiffNoiseAnalysis = STATE_SYNTHESISING...\n";
// display animation of something while the synthesis in on-going...
ofEnableAlphaBlending();
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
if(_anim_cnt < _anim_cnt_max){
ofColor aColour;
int rectSizeW = ofGetWidth()/4;
int rectSizeH = ofGetHeight()/4;
int _fade_in_frames = _anim_cnt_max/2;
int c_anim = 10;
int fade;
//ofRotate(ofMap(_anim_cnt/2.0, 0, _anim_cnt_max, 0, 360));
if (_anim_cnt < _fade_in_frames) {
//cout << "DiffNoiseAnalysis STATE_SYNTHESIZING = FADING IN ANIMATION...\n";
fade = ofMap(_anim_cnt, 0, _fade_in_frames, 0, 255);
for (int i=0; i <= 15; i++){
c_anim = 0+17*i;
aColour.set(c_anim, c_anim, c_anim, fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt >= _fade_in_frames && _anim_cnt <= (_anim_cnt_max-_fade_in_frames)){
for (int i=0; i <= 15; i++){
c_anim = 255;
aColour.set(c_anim, c_anim, c_anim, 255);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt > (_anim_cnt_max-_fade_in_frames) && _anim_cnt <= _anim_cnt_max) {
//cout << "_anim_cnt = " << _anim_cnt-(_anim_cnt_max-_fade_in_frames) << endl;
fade = ofMap(_anim_cnt-(_anim_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255);
//cout << "fade down = " << fade << endl;
for (int i=0; i <= 15; i++){
c_anim = (17*i);
aColour.set(c_anim, c_anim, c_anim, 255-fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
_anim_cnt++;
} else {
_RUN_DONE = true;
//_state = STATE_DISPLAY_RESULTS;
_anim_cnt=0;
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDisableAlphaBlending();
break; break;
} }
case STATE_DISPLAY_RESULTS: case STATE_DISPLAY_RESULTS:
{ {
if (_frame_cnt > 2) // display results of the synthesis
{ // display results of the synthesis
_image_shown = true; int imageWidth=640;
_frame_cnt=0; int imageHeight =480;
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotateY(_results_cnt*0.3);
//ofRotateX(90);
//ofRotateZ(whichMesh);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2),-400;
ofTranslate((ofGetWidth()/2)-(imageWidth/2),0,0 );
meshes[whichMesh].drawVertices();
ofPopMatrix();
whichMesh+=speed;
cout<<whichMesh<<" size of meshes "<<meshes.size()<<endl;
if(whichMesh>meshes.size() -1 || whichMesh<0){
speed*=-1;
whichMesh+=speed;
} }
_frame_cnt++;
ofEnableAlphaBlending();
glShadeModel(GL_SMOOTH);
glLineWidth(line_width);
glPointSize(point_size);
glEnable(GL_POINT_SMOOTH);
RefractiveIndex::cam.begin();
ofTranslate(tx, ty, tz);
ofRotateX(rx); ofRotateY(ry); ofRotateZ(rz);
glScalef(1.5, 1, 1);
if (_show_image)
image2.setFromPixels(image1.getPixels(), image1.width, image1.height, OF_IMAGE_COLOR);
image2.bind();
RefractiveIndex::_shader.begin();
RefractiveIndex::_shader.setUniform1i("algo", algo);
RefractiveIndex::_shader.setUniform1f("scale", scale);
RefractiveIndex::_shader.setUniform1i("tex0", 0);
switch (draw_style) {
case VERTS:
RefractiveIndex::_mesh_vbo.drawVertices();
break;
case WIRE:
RefractiveIndex::_mesh_vbo.drawWireframe();
break;
case FACE:
RefractiveIndex::_mesh_vbo.drawFaces();
break; break;
} }
RefractiveIndex::_shader.end();
image2.unbind();
RefractiveIndex::cam.end();
break;
}
default: default:
break; break;
} }
} }
// this runs at save_cb timer rate = DELTA_T_SAVE // this runs at save_cb timer rate = DELTA_T_SAVE
void DiffNoiseAnalysis::save_cb(Timer& timer) void DiffNoiseAnalysis::save_cb(Timer& timer)
{ {
_save_cnt++;
float rand10 = ofRandom(0,10);
if (rand10 > 5.0) {
cout << "DiffNoiseAnalysis::saving...\n";
cout << "c_last... " << c << endl;
cout<<"rand10... " <<rand10<<endl;
string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg"; string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg";
saveimage(file_name);
}
_save_cnt++;
//if(_save_cnt >= NUM_SAVE_PER_RUN)
// _RUN_DONE = true;
saveImageAnalysis(file_name);
} }
void DiffNoiseAnalysis::display_results_cb(Timer& timer){
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
}

View File

@ -9,9 +9,6 @@
#include "Poco/Timer.h" #include "Poco/Timer.h"
#include "ofxOpenCv.h"
class DiffNoiseAnalysis : public AbstractAnalysis class DiffNoiseAnalysis : public AbstractAnalysis
{ {
public: public:
@ -23,27 +20,16 @@ public:
void setup(int camWidth, int camHeight); void setup(int camWidth, int camHeight);
void acquire(); void acquire();
void synthesise(); void synthesise();
void displayresults(); void display_results();
void draw(); void draw();
void save_cb(Poco::Timer& timer); void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected: protected:
bool _RUN_DONE; bool _RUN_DONE;
int _run_cnt, _save_cnt, _fade_cnt;
int _run_cnt, _save_cnt, _fade_cnt, _synth_save_cnt, _anim_cnt; float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max;
float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max;
bool _show_image, _image_shown;
ofImage image1;
ofImage image2;
int algo;
int scale;
int draw_style;
float line_width;
float point_size;
}; };

View File

@ -1,3 +1,8 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "IResponseAnalysis.h" #include "IResponseAnalysis.h"
#include "ofMain.h" #include "ofMain.h"
@ -10,66 +15,19 @@ using Poco::Timer;
using Poco::TimerCallback; using Poco::TimerCallback;
using Poco::Thread; using Poco::Thread;
#define NUMBER_RUNS 1
#define ACQUIRE_TIME 20
const int algo_default = 1;
const int scale_default = 500;
const int draw_style_default = 3;
const int line_width_default = 0.5f;
const float point_size_default = 0.5f;
void IResponseAnalysis::setup(int camWidth, int camHeight) void IResponseAnalysis::setup(int camWidth, int camHeight)
{ {
AbstractAnalysis::setup(camWidth, camHeight); DELTA_T_SAVE = 100;
NUM_PHASE = 1;
NUM_RUN = 1;
NUM_SAVE_PER_RUN = 100;
NUM_RUN = RefractiveIndex::XML.getValue("config:analysis_NUM_RUN:NUM_RUN_iresponse", NUMBER_RUNS); create_dir();
cout << "NUM_RUN IResponseAnalysis " << NUM_RUN << endl;
//NUM_RUN = 5;
int acq_run_time; // 10 seconds of acquiring per run
acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_iresponse", ACQUIRE_TIME);
cout << "ACQUIRE_TIME IResponseAnalysis " << acq_run_time << endl;
//int acq_run_time = 20; // 20 seconds of acquiring per run
DELTA_T_SAVE = 2*(10*acq_run_time/2); // for 20 seconds, we want this to be around 200 files
// or 10 times per second = every 100 ms
_frame_cnt_max = acq_run_time*ofGetFrameRate(); // e.g.: 30 frames per second * 20 seconds = 600 frames
//create_dir_allocate_images();
_synth_save_cnt = 0;
_run_cnt = 0;
_frame_cnt = 0; _frame_cnt = 0;
_frame_cnt_max = ofGetFrameRate() * ((DELTA_T_SAVE * NUM_SAVE_PER_RUN) / 1000);
c = 0; c = 0;
int anim_time = 10; // 10 seconds
_anim_cnt_max = anim_time*ofGetFrameRate(); // e.g.: 30 frames per second = 150 frames
_show_image = false;
_image_shown = false;
image1.clear();
image2.clear();
// images use for drawing the synthesized files to the screen ///
image1.setUseTexture(false); // the non texture image that is needed to first load the image
image2.setUseTexture(true); // the image that needs to get written to the screen which takes the content of image1
image1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
////---------
algo = RefractiveIndex::XML.getValue("config:algorithms:iresponse:algo", algo_default);
scale = RefractiveIndex::XML.getValue("config:algorithms:iresponse:scale", scale_default);
draw_style = RefractiveIndex::XML.getValue("config:algorithms:iresponse:draw_style", draw_style_default);
line_width = RefractiveIndex::XML.getValue("config:algorithms:iresponse:line_width", line_width_default);
point_size = RefractiveIndex::XML.getValue("config:algorithms:iresponse:point_size", point_size_default);
} }
@ -77,82 +35,94 @@ void IResponseAnalysis::acquire()
{ {
Timer* save_timer; Timer* save_timer;
TimerCallback<IResponseAnalysis> save_callback(*this, &IResponseAnalysis::save_cb); TimerCallback<IResponseAnalysis> save_callback(*this, &IResponseAnalysis::save_cb);
_run_cnt++;
_frame_cnt = 0; _save_cnt = 0; _anim_cnt = 0, _synth_save_cnt = 0;
_RUN_DONE = false;
create_dir_allocate_images();
// RUN ROUTINE // RUN ROUTINE
//for(int i = 0; i < NUM_RUN; i++) { for(int i = 0; i < NUM_RUN; i++) {
//_run_cnt = i;
//cout << "RUN NUM = " << i; _run_cnt = i;
cout << "RUN NUM = " << i;
save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
save_timer->start(save_callback); save_timer->start(save_callback);
_RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
while(!_RUN_DONE && _state != STATE_STOP) while(!_RUN_DONE)
Thread::sleep(3); Thread::sleep(3);
save_timer->stop(); save_timer->stop();
}
//}
} }
void IResponseAnalysis::synthesise() void IResponseAnalysis::synthesise()
{ {
//incrementer to whichMesh
speed=0.2;
//whichMesh is the index in the vector of meshes
whichMesh=0;
// we don't need to synthesise int index=0;
return; float iterator=1;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
/* //hack to limit number of meshes.
if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100;
}
//cout << "IResponseAnalysis::saving synthesis...\n"; }
if(_state == STATE_STOP) return; //clear vector so we don't add to it on successive runs
meshes.clear();
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){
ofImage image1;
ofImage image2;
//there is a known issue with using loadImage inside classes in other directories. the fix is to call setUseTExture(false)
image1.setUseTexture(false);
image2.setUseTexture(false);
//some of the textures are not loading correctly so only make mesh if both the images load
if(image1.loadImage(_saved_filenames[i]) && image2.loadImage(_saved_filenames[i+1])){
meshes.push_back(ofMesh());
cout<<"setting mesh"<<endl;
int _recorded_brightness_value=getRecordedValueFromFileName(_saved_filenames[i]);
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_BRIGHTNESS,_recorded_brightness_value), image2, &meshes[index]);
index++;
}
}
}
void IResponseAnalysis::display_results(){
Timer* display_results_timer;
TimerCallback<IResponseAnalysis> display_results_callback(*this, &IResponseAnalysis::display_results_cb);
// display results of the synthesis
display_results_timer = new Timer(0, 20); // timing interval for saving files
display_results_timer->start(display_results_callback);
_RUN_DONE = false; _RUN_DONE = false;
_results_cnt=0;
_results_cnt_max=300;
// _saved_filenames_synthesis has processed all the files in the analysis images folder while(!_RUN_DONE)
while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3); Thread::sleep(3);
*/
display_results_timer->stop();
} }
void IResponseAnalysis::displayresults()
{
for(float i=1;i<_saved_filenames_analysis.size();i++){
if(_state == STATE_STOP) return;
//cout << "_saved_filenames_analysis[i] - " << _saved_filenames_synthesis[i] << endl;
while(!_image_shown){
Thread::sleep(2);
//cout << "!_image_shown" << endl;
}
_show_image = false;
if(!image1.loadImage(_saved_filenames_analysis[i])){
//couldn't load image
cout << "didn't load image" << endl;
}
if(image1.loadImage(_saved_filenames_analysis[i])){
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
}
}
}
// this runs at frame rate = 33 ms for 30 FPS // this runs at frame rate = 33 ms for 30 FPS
void IResponseAnalysis::draw() void IResponseAnalysis::draw()
{ {
@ -161,18 +131,9 @@ void IResponseAnalysis::draw()
case STATE_ACQUIRING: case STATE_ACQUIRING:
{ {
ofEnableAlphaBlending(); /// *** TODO *** ///
ofColor aColour; // still need to deal with latency frames here - i.e.: there are frames
int _fade_in_frames = _frame_cnt_max/10; /// *** TODO *** ///
//cout<< "_fade_in_frames" << _fade_in_frames<< endl;
if (_frame_cnt < _fade_in_frames) {
aColour.set(255, 255, 255, ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255));
ofSetColor(aColour);
ofRect(0, 0, ofGetWidth(), ofGetHeight());
}
if (_frame_cnt < _frame_cnt_max) if (_frame_cnt < _frame_cnt_max)
{ {
@ -180,18 +141,9 @@ void IResponseAnalysis::draw()
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
c = 255.0 * (_frame_cnt_max*_frame_cnt_max - _frame_cnt*_frame_cnt)/(_frame_cnt_max*_frame_cnt_max); c = 255.0 * (_frame_cnt_max*_frame_cnt_max - _frame_cnt*_frame_cnt)/(_frame_cnt_max*_frame_cnt_max);
} else { } else {
_RUN_DONE = true; _RUN_DONE = true;
} }
if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max) {
aColour.set(0, 0, 0, ofMap(_frame_cnt-(_frame_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255));
ofSetColor(aColour);
ofRect(0, 0, ofGetWidth(), ofGetHeight());
// cout << "FADE OUT STROBE TIME " << endl;
}
ofDisableAlphaBlending();
_frame_cnt++; _frame_cnt++;
break; break;
@ -199,143 +151,37 @@ void IResponseAnalysis::draw()
case STATE_SYNTHESISING: case STATE_SYNTHESISING:
{ {
// display animation of something while the synthesis in on-going...
// cout << "IResponse = STATE_SYNTHESISING...\n";
// display animation of something while the synthesis in on-going... // display animation of something while the synthesis in on-going...
ofEnableAlphaBlending();
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
if(_anim_cnt < _anim_cnt_max){
ofColor aColour;
int rectSizeW = ofGetWidth()/4;
int rectSizeH = ofGetHeight()/4;
int _fade_in_frames = _anim_cnt_max/2;
int c_anim = 10;
int fade;
//ofRotate(ofMap(_anim_cnt/2.0, 0, _anim_cnt_max, 0, 360));
if (_anim_cnt < _fade_in_frames) {
//cout << "IResponse STATE_SYNTHESIZING = FADING IN ANIMATION...\n";
fade = ofMap(_anim_cnt, 0, _fade_in_frames, 0, 255);
for (int i=0; i <= 15; i++){
c_anim = 0+17*i;
aColour.set(c_anim, c_anim, c_anim, fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt >= _fade_in_frames && _anim_cnt <= (_anim_cnt_max-_fade_in_frames)){
for (int i=0; i <= 15; i++){
c_anim = 255;
aColour.set(c_anim, c_anim, c_anim, 255);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt > (_anim_cnt_max-_fade_in_frames) && _anim_cnt <= _anim_cnt_max) {
//cout << "_anim_cnt = " << _anim_cnt-(_anim_cnt_max-_fade_in_frames) << endl;
fade = ofMap(_anim_cnt-(_anim_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255);
//cout << "fade down = " << fade << endl;
for (int i=0; i <= 15; i++){
c_anim = (17*i);
aColour.set(c_anim, c_anim, c_anim, 255-fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
_anim_cnt++;
} else {
_RUN_DONE = true;
_anim_cnt=0;
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDisableAlphaBlending();
break; break;
} }
case STATE_DISPLAY_RESULTS: case STATE_DISPLAY_RESULTS:
{ {
if (_frame_cnt > 2) // display results of the synthesis
{ int imageWidth=640;
_image_shown = true; int imageHeight =480;
_frame_cnt=0; ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotateY(_results_cnt*0.3);
//ofRotateX(90);
//ofRotateZ(whichMesh);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2),-400;
ofTranslate((ofGetWidth()/2)-(imageWidth/2),0,0 );
meshes[whichMesh].drawVertices();
ofPopMatrix();
whichMesh+=speed;
cout<<whichMesh<<" size of meshes "<<meshes.size()<<endl;
if(whichMesh>meshes.size() -1 || whichMesh<0){
speed*=-1;
whichMesh+=speed;
} }
_frame_cnt++;
ofEnableAlphaBlending();
glShadeModel(GL_SMOOTH);
glLineWidth(line_width);
glPointSize(point_size);
glEnable(GL_POINT_SMOOTH);
RefractiveIndex::cam.begin();
ofTranslate(tx, ty, tz);
ofRotateX(rx); ofRotateY(ry); ofRotateZ(rz);
glScalef(1.5, 1, 1);
if (_show_image)
image2.setFromPixels(image1.getPixels(), image1.width, image1.height, OF_IMAGE_COLOR);
image2.bind();
RefractiveIndex::_shader.begin();
RefractiveIndex::_shader.setUniform1i("algo", algo);
RefractiveIndex::_shader.setUniform1f("scale", scale);
RefractiveIndex::_shader.setUniform1i("tex0", 0);
switch (draw_style) {
case VERTS:
RefractiveIndex::_mesh_vbo.drawVertices();
break;
case WIRE:
RefractiveIndex::_mesh_vbo.drawWireframe();
break;
case FACE:
RefractiveIndex::_mesh_vbo.drawFaces();
break; break;
} }
RefractiveIndex::_shader.end();
image2.unbind();
RefractiveIndex::cam.end();
break;
}
default: default:
break; break;
@ -346,9 +192,21 @@ void IResponseAnalysis::draw()
// this runs at save_cb timer rate = DELTA_T_SAVE // this runs at save_cb timer rate = DELTA_T_SAVE
void IResponseAnalysis::save_cb(Timer& timer) void IResponseAnalysis::save_cb(Timer& timer)
{ {
//cout << "IResponseAnalysis::saving...\n";
//cout << "c_last... " << c << endl;
string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg";
saveimage(file_name);
_save_cnt++; _save_cnt++;
string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg"; //if(_save_cnt >= NUM_SAVE_PER_RUN)
// _RUN_DONE = true;
saveImageAnalysis(file_name);
} }
void IResponseAnalysis::display_results_cb(Timer& timer){
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
}

View File

@ -9,8 +9,6 @@
#include "Poco/Timer.h" #include "Poco/Timer.h"
#include "ofxOpenCv.h"
class IResponseAnalysis : public AbstractAnalysis class IResponseAnalysis : public AbstractAnalysis
{ {
public: public:
@ -22,27 +20,17 @@ public:
void setup(int camWidth, int camHeight); void setup(int camWidth, int camHeight);
void acquire(); void acquire();
void synthesise(); void synthesise();
void displayresults(); void display_results();
void draw(); void draw();
void save_cb(Poco::Timer& timer); void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected: protected:
bool _RUN_DONE; bool _RUN_DONE;
int _run_cnt, _save_cnt, _synth_save_cnt, _anim_cnt; int _run_cnt, _save_cnt;
float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max; float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max;
bool _show_image, _image_shown;
ofImage image1;
ofImage image2;
int algo;
int scale;
int draw_style;
float line_width;
float point_size;
}; };

View File

@ -1,3 +1,8 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "RelaxRateAnalysis.h" #include "RelaxRateAnalysis.h"
#include "ofMain.h" #include "ofMain.h"
@ -10,145 +15,116 @@ using Poco::Timer;
using Poco::TimerCallback; using Poco::TimerCallback;
using Poco::Thread; using Poco::Thread;
#define NUMBER_RUNS 1
#define ACQUIRE_TIME 20
const int algo_default = 1;
const int scale_default = 500;
const int draw_style_default = 3;
const int line_width_default = 0.5f;
const float point_size_default = 0.5f;
void RelaxRateAnalysis::setup(int camWidth, int camHeight) void RelaxRateAnalysis::setup(int camWidth, int camHeight)
{ {
AbstractAnalysis::setup(camWidth, camHeight); DELTA_T_SAVE = 130;
NUM_PHASE = 1;
NUM_RUN = 3;
NUM_SAVE_PER_RUN = 100;
NUM_RUN = RefractiveIndex::XML.getValue("config:analysis_NUM_RUN:NUM_RUN_relaxrate", NUMBER_RUNS); create_dir();
cout << "NUM_RUN RelaxRateAnalysis " << NUM_RUN << endl;
//NUM_RUN = 5;
int acq_run_time; // 10 seconds of acquiring per run
acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_relaxrate", ACQUIRE_TIME);
cout << "ACQUIRE_TIME RelaxRateAnalysis " << acq_run_time << endl;
DELTA_T_SAVE = 2*(10*acq_run_time/2); // for 20 seconds, we want this to be around 200 files
// or 10 times per second = every 100 ms
_frame_cnt_max = acq_run_time*ofGetFrameRate(); // e.g.: 30 frames per second * 20 seconds = 600 frames
//create_dir_allocate_images();
_run_cnt = 0;
_level = 0; _level = 0;
_flip = 1; _flip = 1;
_frame_cnt = 0; _frame_cnt = 0;
_frame_cnt_max = ofGetFrameRate() * ((DELTA_T_SAVE * NUM_SAVE_PER_RUN) / 1000);
c = 0; c = 0;
_synth_save_cnt = 0;
int anim_time = 5; // 10 seconds
_anim_cnt_max = anim_time*ofGetFrameRate(); // e.g.: 30 frames per second = 150 frames
_show_image = false;
_image_shown = false;
image1.clear();
image2.clear();
// images use for drawing the synthesized files to the screen ///
image1.setUseTexture(false); // the non texture image that is needed to first load the image
image2.setUseTexture(true); // the image that needs to get written to the screen which takes the content of image1
image1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
////---------
algo = RefractiveIndex::XML.getValue("config:algorithms:relaxrate:algo", algo_default);
scale = RefractiveIndex::XML.getValue("config:algorithms:relaxrate:scale", scale_default);
draw_style = RefractiveIndex::XML.getValue("config:algorithms:relaxrate:draw_style", draw_style_default);
line_width = RefractiveIndex::XML.getValue("config:algorithms:relaxrate:line_width", line_width_default);
point_size = RefractiveIndex::XML.getValue("config:algorithms:relaxrate:point_size", point_size_default);
} }
void RelaxRateAnalysis::acquire() void RelaxRateAnalysis::acquire()
{ {
Timer* save_timer; Timer* save_timer;
TimerCallback<RelaxRateAnalysis> save_callback(*this, &RelaxRateAnalysis::save_cb); TimerCallback<RelaxRateAnalysis> save_callback(*this, &RelaxRateAnalysis::save_cb);
_run_cnt++;
_frame_cnt = 0; _save_cnt = 0; _anim_cnt = 0, _synth_save_cnt = 0;
_RUN_DONE = false;
create_dir_allocate_images();
// RUN ROUTINE // RUN ROUTINE
//for(int i = 0; i < NUM_RUN; i++) { for(int i = 0; i < NUM_RUN; i++) {
//_run_cnt = i;
//cout << "RUN NUM = " << i; _run_cnt = i;
cout << "RUN NUM = " << i;
save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
save_timer->start(save_callback); save_timer->start(save_callback);
_RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
while(!_RUN_DONE && _state != STATE_STOP) while(!_RUN_DONE)
Thread::sleep(3); Thread::sleep(3);
save_timer->stop(); save_timer->stop();
}
} }
void RelaxRateAnalysis::synthesise() void RelaxRateAnalysis::synthesise()
{ {
// we don't need to synthesise /*
return; //incrementer to whichMesh
speed=0.2;
//whichMesh is the index in the vector of meshes
whichMesh=0;
int index=0;
float iterator=1;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
//hack to limit number of meshes.
if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100;
}
}
//clear vector so we don't add to it on successive runs
meshes.clear();
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){
ofImage image1;
ofImage image2;
//there is a known issue with using loadImage inside classes in other directories. the fix is to call setUseTExture(false)
image1.setUseTexture(false);
image2.setUseTexture(false);
//some of the textures are not loading correctly so only make mesh if both the images load
if(image1.loadImage(_saved_filenames[i]) && image2.loadImage(_saved_filenames[i+1])){
meshes.push_back(ofMesh());
cout<<"setting mesh"<<endl;
int _recorded_brightness_value=getRecordedValueFromFileName(_saved_filenames[i]);
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_BRIGHTNESS,_recorded_brightness_value), image2, &meshes[index]);
index++;
}
}
*/
}
void RelaxRateAnalysis::display_results(){
/* /*
//cout << "IResponseAnalysis::saving synthesis...\n"; Timer* display_results_timer;
if(_state == STATE_STOP) return;
TimerCallback<RelaxRateAnalysis> display_results_callback(*this, &RelaxRateAnalysis::display_results_cb);
// display results of the synthesis
display_results_timer = new Timer(0, 20); // timing interval for saving files
display_results_timer->start(display_results_callback);
_RUN_DONE = false; _RUN_DONE = false;
_results_cnt=0;
_results_cnt_max=300;
// _saved_filenames_synthesis has processed all the files in the analysis images folder while(!_RUN_DONE)
while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3); Thread::sleep(3);
display_results_timer->stop();
*/ */
}
void RelaxRateAnalysis::displayresults()
{
for(float i=1;i<_saved_filenames_analysis.size();i++){
if(_state == STATE_STOP) return;
//cout << "_saved_filenames_analysis[i] - " << _saved_filenames_synthesis[i] << endl;
while(!_image_shown){
Thread::sleep(2);
//cout << "!_image_shown" << endl;
}
_show_image = false;
if(!image1.loadImage(_saved_filenames_analysis[i])){
//couldn't load image
cout << "didn't load image" << endl;
}
if(image1.loadImage(_saved_filenames_analysis[i])){
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
}
}
} }
@ -159,7 +135,9 @@ void RelaxRateAnalysis::draw()
switch (_state) { switch (_state) {
case STATE_ACQUIRING: case STATE_ACQUIRING:
{ {
/// *** TODO *** ///
// still need to deal with latency frames here - i.e.: there are frames
/// *** TODO *** ///
if (_frame_cnt < _frame_cnt_max) if (_frame_cnt < _frame_cnt_max)
{ {
@ -178,175 +156,81 @@ void RelaxRateAnalysis::draw()
} }
} else { } else {
cout << "RELAXRATE RUN COMPLETED" << endl;
//_state = STATE_SYNTHESISING;
_RUN_DONE = true; _RUN_DONE = true;
} }
_frame_cnt++; _frame_cnt++;
//cout << "_frame_cnt:" << _frame_cnt << endl; cout << "_frame_cnt:" << _frame_cnt << endl;
break; break;
} }
case STATE_SYNTHESISING: case STATE_SYNTHESISING:
{ {
// display animation of something while the synthesis in on-going... // display animation of something while the synthesis in on-going...
//cout << "RelaxRateAnalysis = STATE_SYNTHESISING...\n";
// display animation of something while the synthesis in on-going...
ofEnableAlphaBlending();
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
if(_anim_cnt < _anim_cnt_max){
ofColor aColour;
int rectSizeW = ofGetWidth()/4;
int rectSizeH = ofGetHeight()/4;
int _fade_in_frames = _anim_cnt_max/2;
int c_anim = 10;
int fade;
//ofRotate(ofMap(_anim_cnt/2.0, 0, _anim_cnt_max, 0, 360));
if (_anim_cnt < _fade_in_frames) {
//cout << "ShadowScapesAnalysis STATE_SYNTHESIZING = FADING IN ANIMATION...\n";
fade = ofMap(_anim_cnt, 0, _fade_in_frames, 0, 255);
for (int i=0; i <= 15; i++){
c_anim = 0+17*i;
aColour.set(c_anim, c_anim, c_anim, fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt >= _fade_in_frames && _anim_cnt <= (_anim_cnt_max-_fade_in_frames)){
//cout << "ShadowScapesAnalysis STATE_SYNTHESIZING = MIDDLE OF ANIMATION...\n";
for (int i=0; i <= 15; i++){
c_anim = 255;
aColour.set(c_anim, c_anim, c_anim, 255);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
//cout << "_anim_cnt = " << _anim_cnt << endl;
if (_anim_cnt > (_anim_cnt_max-_fade_in_frames) && _anim_cnt <= _anim_cnt_max) {
//cout << "ShadowScapesAnalysis STATE_SYNTHESIZING = FADE OUT OF ANIMATION...\n";
//cout << "_anim_cnt = " << _anim_cnt-(_anim_cnt_max-_fade_in_frames) << endl;
fade = ofMap(_anim_cnt-(_anim_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255);
//cout << "fade down = " << fade << endl;
for (int i=0; i <= 15; i++){
c_anim = (17*i);
//cout << "c_anim = " << c_anim << endl;
aColour.set(c_anim, c_anim, c_anim, 255-fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
_anim_cnt++;
} else {
_RUN_DONE = true;
_anim_cnt=0;
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDisableAlphaBlending();
break; break;
} }
case STATE_DISPLAY_RESULTS: case STATE_DISPLAY_RESULTS:
{ {
/*
// display results of the synthesis
// display results of the synthesis
int imageWidth=640;
int imageHeight =480;
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotateY(_results_cnt*0.3);
//ofRotateX(90);
//ofRotateZ(whichMesh);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2),-400;
ofTranslate((ofGetWidth()/2)-(imageWidth/2),0,0 );
meshes[whichMesh].drawVertices();
ofPopMatrix();
whichMesh+=speed;
cout<<whichMesh<<" size of meshes "<<meshes.size()<<endl;
if(whichMesh>meshes.size() -1 || whichMesh<0){
speed*=-1;
whichMesh+=speed;
if (_frame_cnt > 2)
{
_image_shown = true;
_frame_cnt=0;
} }
*/
_frame_cnt++;
ofEnableAlphaBlending();
glShadeModel(GL_SMOOTH);
glLineWidth(line_width);
glPointSize(point_size);
glEnable(GL_POINT_SMOOTH);
RefractiveIndex::cam.begin();
ofTranslate(tx, ty, tz);
ofRotateX(rx); ofRotateY(ry); ofRotateZ(rz);
glScalef(1.5, 1, 1);
if (_show_image)
image2.setFromPixels(image1.getPixels(), image1.width, image1.height, OF_IMAGE_COLOR);
image2.bind();
RefractiveIndex::_shader.begin();
RefractiveIndex::_shader.setUniform1i("algo", algo);
RefractiveIndex::_shader.setUniform1f("scale", scale);
RefractiveIndex::_shader.setUniform1i("tex0", 0);
switch (draw_style) {
case VERTS:
RefractiveIndex::_mesh_vbo.drawVertices();
break;
case WIRE:
RefractiveIndex::_mesh_vbo.drawWireframe();
break;
case FACE:
RefractiveIndex::_mesh_vbo.drawFaces();
break; break;
} }
RefractiveIndex::_shader.end();
image2.unbind();
RefractiveIndex::cam.end();
break;
}
default: default:
break; break;
} }
} }
// this runs at save_cb timer rate = DELTA_T_SAVE // this runs at save_cb timer rate = DELTA_T_SAVE
void RelaxRateAnalysis::save_cb(Timer& timer) void RelaxRateAnalysis::save_cb(Timer& timer)
{ {
_save_cnt++; //cout << "RelaxRateAnalysis::saving...\n";
//cout << "c_last... " << c << endl;
string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg"; string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg";
saveImageAnalysis(file_name); saveimage(file_name);
_save_cnt++;
//if(_save_cnt >= NUM_SAVE_PER_RUN)
// _RUN_DONE = true;
}
void RelaxRateAnalysis::display_results_cb(Timer& timer){
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
} }

View File

@ -1,3 +1,7 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#pragma once #pragma once
@ -5,11 +9,6 @@
#include "Poco/Timer.h" #include "Poco/Timer.h"
#include "rfiCvContourFinder.h"
#include "ofxOpenCv.h"
class RelaxRateAnalysis : public AbstractAnalysis class RelaxRateAnalysis : public AbstractAnalysis
{ {
@ -22,32 +21,17 @@ public:
void setup(int camWidth, int camHeight); void setup(int camWidth, int camHeight);
void acquire(); void acquire();
void synthesise(); void synthesise();
void displayresults(); void display_results();
void draw(); void draw();
void save_cb(Poco::Timer& timer); void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected: protected:
bool _RUN_DONE; bool _RUN_DONE;
float _flip, _level; float _flip, _level;
int _run_cnt, _save_cnt, _synth_save_cnt, _anim_cnt; int _run_cnt, _save_cnt;
float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max; float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max;
int _treshold;
int _maxblobs;
bool _show_image, _image_shown;
ofImage image1;
ofImage image2;
int algo;
int scale;
int draw_style;
float line_width;
float point_size;
}; };

View File

@ -1,3 +1,35 @@
/*
- copyright (c) 2011 Copenhagen Institute of Interaction Design (CIID)
- all rights reserved.
+ redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ > redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ > redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "ShadowScapesAnalysis.h" #include "ShadowScapesAnalysis.h"
#include "ofMain.h" #include "ofMain.h"
@ -5,150 +37,130 @@
#include "Poco/Thread.h" #include "Poco/Thread.h"
#include "RefractiveIndex.h" #include "RefractiveIndex.h"
//#include "ofxXmlSettings.h"
#include "ofxOpenCv.h"
using Poco::Timer; using Poco::Timer;
using Poco::TimerCallback; using Poco::TimerCallback;
using Poco::Thread; using Poco::Thread;
#define STATE_SCAN 0 #define STATE_SCAN 0
#define STATE_ANALYSIS 1 #define STATE_ANALYSIS 1
#define NUMBER_RUNS 1
#define ACQUIRE_TIME 20
const int algo_default = 1;
const int scale_default = 500;
const int draw_style_default = 3;
const double line_width_default = 0.5;
void ShadowScapesAnalysis::setup(int camWidth, int camHeight) void ShadowScapesAnalysis::setup(int camWidth, int camHeight)
{ {
AbstractAnalysis::setup(camWidth, camHeight); DELTA_T_SAVE = 100;
NUM_PHASE = 1;
NUM_RUN = 1;
NUM_SAVE_PER_RUN = 100;
NUM_RUN = RefractiveIndex::XML.getValue("config:analysis_NUM_RUN:NUM_RUN_shadowscapes", NUMBER_RUNS); create_dir();
cout << "NUM_RUN ShadowScapesAnalysis " << NUM_RUN << endl; _speed = 900.0; // 900.0 is the correct number
//NUM_RUN = 5; _scanLineWidth = 100.0;
int acq_run_time; // 10 seconds of acquiring per run
acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_shadowscapes", ACQUIRE_TIME);
cout << "ACQUIRE_TIME ShadowScapesAnalysis " << acq_run_time << endl;
int screenSpan;
if (_dir == V) screenSpan = ofGetHeight();
if (_dir == H) screenSpan = ofGetWidth();
if (_dir == D) screenSpan = ofGetHeight();
_step = (screenSpan/acq_run_time)/(ofGetFrameRate());
// pixel per frame = (pixels / sec) / (frame / sec)
// 40 pixels per second should give us a 20 second scan at 800 pixels wide
DELTA_T_SAVE = 3*(10*acq_run_time/2); // for 20 seconds, we want this to be around 100 files
// or 5 times per second = every 200 ms
_scanLineWidth = 300.0;
_run_cnt = 0; _run_cnt = 0;
_save_cnt = 0; _save_cnt = 0;
_synth_save_cnt = 0;
int anim_time = 10; // 10 seconds
_anim_cnt_max = anim_time*ofGetFrameRate(); // e.g.: 30 frames per second = 150 frames
_show_image = false;
_image_shown = false;
image1.clear();
image2.clear();
// images use for drawing the synthesized files to the screen ///
image1.setUseTexture(false); // the non texture image that is needed to first load the image
image2.setUseTexture(true); // the image that needs to get written to the screen which takes the content of image1
image1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
////---------
algo = RefractiveIndex::XML.getValue("config:algorithms:shadowscapes:algo", algo_default);
scale = RefractiveIndex::XML.getValue("config:algorithms:shadowscapes:scale", scale_default);
draw_style = RefractiveIndex::XML.getValue("config:algorithms:shadowscapes:draw_style", draw_style_default);
line_width = RefractiveIndex::XML.getValue("config:algorithms:shadowscapes:line_width", line_width_default);
} }
void ShadowScapesAnalysis::acquire() void ShadowScapesAnalysis::acquire()
{ {
int w;
if (_dir == V) w = ofGetHeight();
if (_dir == H) w = ofGetWidth();
if (_dir == D) w = ofGetHeight();
_step = ((w/_speed) * 1000.0) / 500.0;
_line = 0;
// RUN ROUTINE
for(int i = 0; i < NUM_RUN; i++) {
Timer save_timer(0, DELTA_T_SAVE); Timer save_timer(0, DELTA_T_SAVE);
TimerCallback<ShadowScapesAnalysis> save_callback(*this, &ShadowScapesAnalysis::save_cb); TimerCallback<ShadowScapesAnalysis> save_callback(*this, &ShadowScapesAnalysis::save_cb);
_run_cnt++;
_frame_cnt = 0; _save_cnt = 0; _anim_cnt = 0, _synth_save_cnt = 0;
_line = 0;
_RUN_DONE = false; _RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
create_dir_allocate_images();
save_timer.start(save_callback); save_timer.start(save_callback);
while(!_RUN_DONE && _state != STATE_STOP) while(!_RUN_DONE)
Thread::sleep(3); Thread::sleep(3);
save_timer.stop(); save_timer.stop();
}
} }
void ShadowScapesAnalysis::synthesise() void ShadowScapesAnalysis::synthesise()
{ {
// we don't need to synthesise //incrementer to whichMesh
return; speed=0.2;
//whichMesh is the index in the vector of meshes
whichMesh=0;
/* int index=0;
float iterator=1;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
//cout << "IResponseAnalysis::saving synthesis...\n"; //hack to limit number of meshes.
if(_state == STATE_STOP) return; if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100;
}
}
//clear vector so we don't add to it on successive runs
meshes.clear();
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){
ofImage image1;
ofImage image2;
//there is a known issue with using loadImage inside classes in other directories. the fix is to call setUseTExture(false)
image1.setUseTexture(false);
image2.setUseTexture(false);
string fnamei = _saved_filenames[i];
string fnameii = _saved_filenames[i + 1];
bool l1 = image1.loadImage(_saved_filenames[i]);
bool l2 = image2.loadImage(_saved_filenames[i+1]);
//some of the textures are not loading correctly so only make mesh if both the images load
if(image1.loadImage(_saved_filenames[i]) && image2.loadImage(_saved_filenames[i+1])){
meshes.push_back(ofMesh());
cout<<"setting mesh"<<endl;
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_BRIGHTNESS), image2, &meshes[index]);
index++;
}
image1.clear();
image2.clear();
}
}
void ShadowScapesAnalysis::display_results(){
Timer* display_results_timer;
TimerCallback<ShadowScapesAnalysis> display_results_callback(*this, &ShadowScapesAnalysis::display_results_cb);
// display results of the synthesis
display_results_timer = new Timer(0, 20); // timing interval for saving files
display_results_timer->start(display_results_callback);
_RUN_DONE = false; _RUN_DONE = false;
_results_cnt=0;
_results_cnt_max=300;
// _saved_filenames_synthesis has processed all the files in the analysis images folder while(!_RUN_DONE)
while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3); Thread::sleep(3);
*/
display_results_timer->stop();
} }
void ShadowScapesAnalysis::displayresults()
{
for(float i=1;i<_saved_filenames_analysis.size();i++){
if(_state == STATE_STOP) return;
//cout << "_saved_filenames_analysis[i] - " << _saved_filenames_synthesis[i] << endl;
while(!_image_shown){
Thread::sleep(2);
//cout << "!_image_shown" << endl;
}
_show_image = false;
if(!image1.loadImage(_saved_filenames_analysis[i])){
//couldn't load image
cout << "didn't load image" << endl;
}
if(image1.loadImage(_saved_filenames_analysis[i])){
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
}
}
}
// the animation draw - and the output draw // the animation draw - and the output draw
void ShadowScapesAnalysis::draw() void ShadowScapesAnalysis::draw()
{ {
@ -219,22 +231,24 @@ void ShadowScapesAnalysis::draw()
} }
if(_dir == V && int(_line) >= (ofGetHeight()+4*_scanLineWidth)){
//cout << "VERTICAL IS DONE - _line >= (ofGetHeight()+4*_scanLineWidth) is TRUE" << endl;
_RUN_DONE = true;
if(_dir == V && int(_line) >= (ofGetHeight()+4*_scanLineWidth)){
cout << "VERTICAL IS DONE - _line >= (ofGetHeight()+4*_scanLineWidth) is TRUE" << endl;
//_state = STATE_SYNTHESISING;
_RUN_DONE = true;
} }
if(_dir == H && int(_line) >= (ofGetWidth()+4*_scanLineWidth)) { if(_dir == H && int(_line) >= (ofGetWidth()+4*_scanLineWidth)) {
//cout << "HORIZONTAL IS DONE - _line >= (ofGetWidth()+4*_scanLineWidth)) is TRUE" << endl; //cout << "HORIZONTAL IS DONE - _line >= (ofGetWidth()+4*_scanLineWidth)) is TRUE" << endl;
//_state = STATE_SYNTHESISING;
_RUN_DONE = true; _RUN_DONE = true;
} }
if(_dir == D && int(_line) >= (1.5*ofGetHeight()+4*_scanLineWidth)) { if(_dir == D && int(_line) >= (1.5*ofGetHeight()+4*_scanLineWidth)) {
//cout << "DIAGONAL IS DONE - _line >= (1.5*ofGetHeight()+4*_scanLineWidth)) is TRUE" << endl; //cout << "DIAGONAL IS DONE - _line >= (1.5*ofGetHeight()+4*_scanLineWidth)) is TRUE" << endl;
//_state = STATE_SYNTHESISING;
_RUN_DONE = true; _RUN_DONE = true;
} }
@ -243,137 +257,35 @@ void ShadowScapesAnalysis::draw()
case STATE_SYNTHESISING: case STATE_SYNTHESISING:
{ {
// cout << "ShadowScapesAnalysis = STATE_SYNTHESISING...\n";
// display animation of something while the synthesis in on-going... // display animation of something while the synthesis in on-going...
ofEnableAlphaBlending();
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
if(_anim_cnt < _anim_cnt_max){
ofColor aColour;
int rectSizeW = ofGetWidth()/4;
int rectSizeH = ofGetHeight()/4;
int _fade_in_frames = _anim_cnt_max/2;
int c_anim = 10;
int fade;
//ofRotate(ofMap(_anim_cnt/2.0, 0, _anim_cnt_max, 0, 360));
if (_anim_cnt < _fade_in_frames) {
// cout << "ShadowScapesAnalysis STATE_SYNTHESIZING = FADING IN ANIMATION...\n";
fade = ofMap(_anim_cnt, 0, _fade_in_frames, 0, 255);
for (int i=0; i <= 15; i++){
c_anim = 0+17*i;
aColour.set(c_anim, c_anim, c_anim, fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt >= _fade_in_frames && _anim_cnt <= (_anim_cnt_max-_fade_in_frames)){
for (int i=0; i <= 15; i++){
c_anim = 255;
aColour.set(c_anim, c_anim, c_anim, 255);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt > (_anim_cnt_max-_fade_in_frames) && _anim_cnt <= _anim_cnt_max) {
//cout << "_anim_cnt = " << _anim_cnt-(_anim_cnt_max-_fade_in_frames) << endl;
fade = ofMap(_anim_cnt-(_anim_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255);
//cout << "fade down = " << fade << endl;
for (int i=0; i <= 15; i++){
c_anim = (17*i);
aColour.set(c_anim, c_anim, c_anim, 255-fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
_anim_cnt++;
} else {
_RUN_DONE = true;
_anim_cnt=0;
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDisableAlphaBlending();
break; break;
} }
case STATE_DISPLAY_RESULTS: case STATE_DISPLAY_RESULTS:
{ {
//cout << "STATE_DISPLAY_RESULTS...\n" << endl; // display results of the synthesis
// display results of the synthesis
int imageWidth=640;
int imageHeight =480;
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotateY(_results_cnt*0.3);
//ofRotateX(90);
//ofRotateZ(whichMesh);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2),-400;
ofTranslate((ofGetWidth()/2)-(imageWidth/2),0,0 );
meshes[whichMesh].drawVertices();
ofPopMatrix();
whichMesh+=speed;
cout<<whichMesh<<" size of meshes "<<meshes.size()<<endl;
if(whichMesh>meshes.size() -1 || whichMesh<0){
speed*=-1;
whichMesh+=speed;
if (_frame_cnt > 2)
{
_image_shown = true;
_frame_cnt=0;
} }
_frame_cnt++;
ofEnableAlphaBlending();
glShadeModel(GL_SMOOTH);
glLineWidth(line_width);
RefractiveIndex::cam.begin();
ofTranslate(tx, ty, tz);
ofRotateX(rx); ofRotateY(ry); ofRotateZ(rz);
glScalef(1.5, 1, 1);
if (_show_image)
image2.setFromPixels(image1.getPixels(), image1.width, image1.height, OF_IMAGE_COLOR);
image2.bind();
RefractiveIndex::_shader.begin();
RefractiveIndex::_shader.setUniform1i("algo", algo);
RefractiveIndex::_shader.setUniform1f("scale", scale);
RefractiveIndex::_shader.setUniform1i("tex0", 0);
switch (draw_style) {
case VERTS:
RefractiveIndex::_mesh_vbo.drawVertices();
break;
case WIRE:
RefractiveIndex::_mesh_vbo.drawWireframe();
break;
case FACE:
RefractiveIndex::_mesh_vbo.drawFaces();
break;
}
RefractiveIndex::_shader.end();
image2.unbind();
RefractiveIndex::cam.end();
_RUN_DONE = true;
break; break;
} }
@ -387,8 +299,15 @@ void ShadowScapesAnalysis::draw()
void ShadowScapesAnalysis::save_cb(Timer& timer) void ShadowScapesAnalysis::save_cb(Timer& timer)
{ {
_save_cnt++; RefractiveIndex::_vidGrabber.grabFrame(); // get a new frame from the camera
if (RefractiveIndex::_vidGrabber.isFrameNew())
{
RefractiveIndex::_pixels = RefractiveIndex::_vidGrabber.getPixelsRef(); //get ofPixels from the camera
}
cout << "ShadowScapesAnalysis::saving...\n";
string file_name; string file_name;
if(_dir == H) { if(_dir == H) {
@ -403,6 +322,15 @@ void ShadowScapesAnalysis::save_cb(Timer& timer)
file_name = ofToString(_save_cnt, 2)+"_D_"+ofToString(_line, 2)+"_"+ofToString(_run_cnt,2)+".jpg"; file_name = ofToString(_save_cnt, 2)+"_D_"+ofToString(_line, 2)+"_"+ofToString(_run_cnt,2)+".jpg";
} }
saveImageAnalysis(file_name); saveimage(file_name);
_save_cnt++;
} }
void ShadowScapesAnalysis::display_results_cb(Timer& timer){
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
}

View File

@ -1,4 +1,34 @@
/* */ /*
- copyright (c) 2011 Copenhagen Institute of Interaction Design (CIID)
- all rights reserved.
+ redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ > redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ > redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#pragma once #pragma once
@ -6,8 +36,6 @@
#include "Poco/Timer.h" #include "Poco/Timer.h"
#include "ofxOpenCv.h"
enum shadow_type { enum shadow_type {
H, V, D, H, V, D,
}; };
@ -24,11 +52,11 @@ public:
void setup(int camWidth, int camHeight); void setup(int camWidth, int camHeight);
void acquire(); void acquire();
void synthesise(); void synthesise();
void displayresults(); void display_results();
void draw(); void draw();
void save_cb(Poco::Timer& timer); void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected: protected:
@ -38,17 +66,7 @@ protected:
float _scanLineWidth; // pix per second float _scanLineWidth; // pix per second
float _step; float _step;
shadow_type _dir; shadow_type _dir;
int _run_cnt, _save_cnt, _synth_save_cnt, _anim_cnt; int _run_cnt, _save_cnt;
float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max; float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max;
bool _show_image, _image_shown;
ofImage image1;
ofImage image2;
int algo;
int scale;
int draw_style;
double line_width;
}; };

View File

@ -1,3 +1,8 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "ShapeFromShadingAnalysis.h" #include "ShapeFromShadingAnalysis.h"
#include "ofMain.h" #include "ofMain.h"
@ -10,81 +15,43 @@ using Poco::Timer;
using Poco::TimerCallback; using Poco::TimerCallback;
using Poco::Thread; using Poco::Thread;
#define NUMBER_RUNS 1
#define ACQUIRE_TIME 20
const int algo_default = 1;
const int scale_default = 500;
const int draw_style_default = 3;
const int line_width_default = 0.5f;
const float point_size_default = 0.5f;
void ShapeFromShadingAnalysis::setup(int camWidth, int camHeight) void ShapeFromShadingAnalysis::setup(int camWidth, int camHeight)
{ {
AbstractAnalysis::setup(camWidth, camHeight); DELTA_T_SAVE = 300;
NUM_PHASE = 1;
NUM_RUN = 1;
NUM_SAVE_PER_RUN = 100;
NUM_RUN = RefractiveIndex::XML.getValue("config:analysis_NUM_RUN:NUM_RUN_shapefromshading", NUMBER_RUNS); create_dir();
cout << "NUM_RUN ShapeFromShadingAnalysis " << NUM_RUN << endl;
//NUM_RUN = 5;
int acq_run_time; // 10 seconds of acquiring per run _level = 0;
acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_shapefromshading", ACQUIRE_TIME); _flip = 1;
cout << "ACQUIRE_TIME ShapeFromShadingAnalysis " << acq_run_time << endl;
//int acq_run_time = 20; // 20 seconds of acquiring per run
DELTA_T_SAVE = 2*(10*acq_run_time/2); // for 20 seconds, we want this to be around 200 files
// or 10 times per second = every 100 ms
_frame_cnt_max = acq_run_time*ofGetFrameRate(); // e.g.: 30 frames per second * 20 seconds = 600 frames
//create_dir_allocate_images();
_synth_save_cnt = 0;
_run_cnt = 0;
_frame_cnt = 0; _frame_cnt = 0;
_frame_cnt_max = ofGetFrameRate() * ((DELTA_T_SAVE * NUM_SAVE_PER_RUN) / 1000);
c = 0; c = 0;
int anim_time = 5; // 10 seconds
_anim_cnt_max = anim_time*ofGetFrameRate(); // e.g.: 30 frames per second = 150 frames
_show_image = false;
_image_shown = false;
image1.clear();
image2.clear();
image1.setUseTexture(false);
image2.setUseTexture(true);
image1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
////---------
algo = RefractiveIndex::XML.getValue("config:algorithms:shapeshade:algo", algo_default);
scale = RefractiveIndex::XML.getValue("config:algorithms:shapeshade:scale", scale_default);
draw_style = RefractiveIndex::XML.getValue("config:algorithms:shapeshade:draw_style", draw_style_default);
line_width = RefractiveIndex::XML.getValue("config:algorithms:shapeshade:line_width", line_width_default);
point_size = RefractiveIndex::XML.getValue("config:algorithms:shapeshade:point_size", point_size_default);
} }
void ShapeFromShadingAnalysis::acquire() void ShapeFromShadingAnalysis::acquire()
{ {
Timer* save_timer; Timer* save_timer;
TimerCallback<ShapeFromShadingAnalysis> save_callback(*this, &ShapeFromShadingAnalysis::save_cb); TimerCallback<ShapeFromShadingAnalysis> save_callback(*this, &ShapeFromShadingAnalysis::save_cb);
_run_cnt++; // RUN ROUTINE
_frame_cnt = 0; _save_cnt = 0; _anim_cnt = 0, _synth_save_cnt = 0; for(int i = 0; i < NUM_RUN; i++) {
_RUN_DONE = false;
create_dir_allocate_images();
_run_cnt = i;
cout << "RUN NUM = " << i;
save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
save_timer->start(save_callback);
_RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
_animation_cnt1 = 0; _animation_cnt1 = 0;
_animation_cnt2 = 0; _animation_cnt2 = 0;
_animation_cnt3 = 0; _animation_cnt3 = 0;
@ -103,72 +70,76 @@ void ShapeFromShadingAnalysis::acquire()
_animation_cnt16 = 0; _animation_cnt16 = 0;
_animation_reset = false; // coundn't get this to work - so using seperate counters - shitty! _animation_reset = false; // coundn't get this to work - so using seperate counters - shitty!
// RUN ROUTINE while(!_RUN_DONE)
//for(int i = 0; i < NUM_RUN; i++) {
// _run_cnt = i;
//cout << "RUN NUM = " << i;
save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
save_timer->start(save_callback);
while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3); Thread::sleep(3);
save_timer->stop(); save_timer->stop();
}
// }
} }
void ShapeFromShadingAnalysis::synthesise() void ShapeFromShadingAnalysis::synthesise()
{ {
// we don't need to synthesise //incrementer to whichMesh
return; speed=0.2;
//whichMesh is the index in the vector of meshes
whichMesh=0;
/* int index=0;
float iterator=1;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
//cout << "IResponseAnalysis::saving synthesis...\n"; //hack to limit number of meshes.
if(_state == STATE_STOP) return; if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100;
}
_RUN_DONE = false; }
//clear vector so we don't add to it on successive runs
meshes.clear();
// _saved_filenames_synthesis has processed all the files in the analysis images folder for(float i=0;i<_saved_filenames.size()-1;i+=iterator){
while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3);
*/
ofImage image1;
ofImage image2;
//there is a known issue with using loadImage inside classes in other directories. the fix is to call setUseTExture(false)
image1.setUseTexture(false);
image2.setUseTexture(false);
//some of the textures are not loading correctly so only make mesh if both the images load
if(image1.loadImage(_saved_filenames[i]) && image2.loadImage(_saved_filenames[i+1])){
meshes.push_back(ofMesh());
cout<<"setting mesh"<<endl;
int _recorded_brightness_value=getRecordedValueFromFileName(_saved_filenames[i]);
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_BRIGHTNESS,_recorded_brightness_value), image2, &meshes[index]);
index++;
}
}
} }
void ShapeFromShadingAnalysis::displayresults() void ShapeFromShadingAnalysis::display_results(){
{
for(float i=1;i<_saved_filenames_analysis.size();i++){
if(_state == STATE_STOP) return; Timer* display_results_timer;
//cout << "_saved_filenames_analysis[i] - " << _saved_filenames_synthesis[i] << endl; TimerCallback<ShapeFromShadingAnalysis> display_results_callback(*this, &ShapeFromShadingAnalysis::display_results_cb);
// display results of the synthesis
while(!_image_shown){ display_results_timer = new Timer(0, 20); // timing interval for saving files
Thread::sleep(2); display_results_timer->start(display_results_callback);
//cout << "!_image_shown" << endl; _RUN_DONE = false;
} _results_cnt=0;
_results_cnt_max=300;
_show_image = false; while(!_RUN_DONE)
Thread::sleep(3);
display_results_timer->stop();
if(!image1.loadImage(_saved_filenames_analysis[i])){
//couldn't load image
cout << "didn't load image" << endl;
}
if(image1.loadImage(_saved_filenames_analysis[i])){
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
}
}
} }
// this runs at frame rate = 33 ms for 30 FPS // this runs at frame rate = 33 ms for 30 FPS
@ -183,10 +154,23 @@ void ShapeFromShadingAnalysis::draw()
ofEnableAlphaBlending(); ofEnableAlphaBlending();
int _quarter_frame_cnt_max = _frame_cnt_max/4; int _quarter_frame_cnt_max = _frame_cnt_max/4;
int _half_frame_cnt_max = _frame_cnt_max/2; int _half_frame_cnt_max = _frame_cnt_max/2;
int _threequarters_frame_cnt_max = 3*_frame_cnt_max/4; int _threequarters_frame_cnt_max = 3*_frame_cnt_max/4;
//TODO: put in CROSS FADES, ETC§E
/*
if (_animation_reset == true)
{
_animation_cnt1 = 0;
_animation_cnt2 = 0;
_animation_cnt3 = 0;
_animation_cnt4 = 0;
}
*/
if(_frame_cnt < _quarter_frame_cnt_max) { if(_frame_cnt < _quarter_frame_cnt_max) {
quad = "QUAD1"; quad = "QUAD1";
@ -327,6 +311,10 @@ void ShapeFromShadingAnalysis::draw()
_animation_cnt12++; _animation_cnt12++;
} }
} }
@ -378,10 +366,6 @@ void ShapeFromShadingAnalysis::draw()
} }
ofDisableAlphaBlending(); ofDisableAlphaBlending();
} else {
//_state = STATE_SYNTHESISING;
_frame_cnt = 0;
_RUN_DONE = true;
} }
_frame_cnt++; _frame_cnt++;
@ -393,142 +377,33 @@ void ShapeFromShadingAnalysis::draw()
case STATE_SYNTHESISING: case STATE_SYNTHESISING:
{ {
// display animation of something while the synthesis in on-going... // display animation of something while the synthesis in on-going...
// cout << "RelaxRateAnalysis = STATE_SYNTHESISING...\n";
// display animation of something while the synthesis in on-going...
ofEnableAlphaBlending();
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
if(_anim_cnt < _anim_cnt_max){
ofColor aColour;
int rectSizeW = ofGetWidth()/4;
int rectSizeH = ofGetHeight()/4;
int _fade_in_frames = _anim_cnt_max/2;
int c_anim = 10;
int fade;
//ofRotate(ofMap(_anim_cnt/2.0, 0, _anim_cnt_max, 0, 360));
if (_anim_cnt < _fade_in_frames) {
//cout << "ShadowScapesAnalysis STATE_SYNTHESIZING = FADING IN ANIMATION...\n";
fade = ofMap(_anim_cnt, 0, _fade_in_frames, 0, 255);
for (int i=0; i <= 15; i++){
c_anim = 0+17*i;
aColour.set(c_anim, c_anim, c_anim, fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt >= _fade_in_frames && _anim_cnt <= (_anim_cnt_max-_fade_in_frames)){
for (int i=0; i <= 15; i++){
c_anim = 255;
aColour.set(c_anim, c_anim, c_anim, 255);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt > (_anim_cnt_max-_fade_in_frames) && _anim_cnt <= _anim_cnt_max) {
//cout << "_anim_cnt = " << _anim_cnt-(_anim_cnt_max-_fade_in_frames) << endl;
fade = ofMap(_anim_cnt-(_anim_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255);
//cout << "fade down = " << fade << endl;
for (int i=0; i <= 15; i++){
c_anim = (17*i);
aColour.set(c_anim, c_anim, c_anim, 255-fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
_anim_cnt++;
} else {
_RUN_DONE = true;
//_state = STATE_DISPLAY_RESULTS;
_anim_cnt=0;
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDisableAlphaBlending();
break; break;
} }
case STATE_DISPLAY_RESULTS: case STATE_DISPLAY_RESULTS:
{ {
if (_frame_cnt > 2) // display results of the synthesis
{ int imageWidth=640;
_image_shown = true; int imageHeight =480;
_frame_cnt=0; ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotateY(_results_cnt*0.3);
//ofRotateX(90);
//ofRotateZ(whichMesh);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2),-400;
ofTranslate((ofGetWidth()/2)-(imageWidth/2),0,0 );
meshes[whichMesh].drawVertices();
ofPopMatrix();
whichMesh+=speed;
cout<<whichMesh<<" size of meshes "<<meshes.size()<<endl;
if(whichMesh>meshes.size() -1 || whichMesh<0){
speed*=-1;
whichMesh+=speed;
} }
_frame_cnt++;
ofEnableAlphaBlending();
glShadeModel(GL_SMOOTH);
glLineWidth(line_width);
glPointSize(point_size);
glEnable(GL_POINT_SMOOTH);
RefractiveIndex::cam.begin();
ofTranslate(tx, ty, tz);
ofRotateX(rx); ofRotateY(ry); ofRotateZ(rz);
glScalef(1.5, 1, 1);
if (_show_image)
image2.setFromPixels(image1.getPixels(), image1.width, image1.height, OF_IMAGE_COLOR);
image2.bind();
RefractiveIndex::_shader.begin();
RefractiveIndex::_shader.setUniform1i("algo", algo);
RefractiveIndex::_shader.setUniform1f("scale", scale);
RefractiveIndex::_shader.setUniform1i("tex0", 0);
switch (draw_style) {
case VERTS:
RefractiveIndex::_mesh_vbo.drawVertices();
break; break;
case WIRE:
RefractiveIndex::_mesh_vbo.drawWireframe();
break;
case FACE:
RefractiveIndex::_mesh_vbo.drawFaces();
break;
}
RefractiveIndex::_shader.end();
image2.unbind();
RefractiveIndex::cam.end();
break;
} }
@ -542,9 +417,23 @@ void ShapeFromShadingAnalysis::draw()
// this runs at save_cb timer rate = DELTA_T_SAVE // this runs at save_cb timer rate = DELTA_T_SAVE
void ShapeFromShadingAnalysis::save_cb(Timer& timer) void ShapeFromShadingAnalysis::save_cb(Timer& timer)
{ {
_save_cnt++; //cout << "ShapeFromShadingAnalysis::saving...\n";
string file_name = ofToString(_save_cnt,2)+"_"+ quad +"_"+ofToString(_run_cnt,2)+".jpg"; string file_name = ofToString(_save_cnt,2)+"_"+ quad +"_"+ofToString(_run_cnt,2)+".jpg";
saveImageAnalysis(file_name); saveimage(file_name);
_save_cnt++;
//if(_save_cnt >= NUM_SAVE_PER_RUN)
// _RUN_DONE = true;
} }
void ShapeFromShadingAnalysis::display_results_cb(Timer& timer){
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
}

View File

@ -9,8 +9,6 @@
#include "Poco/Timer.h" #include "Poco/Timer.h"
#include "ofxOpenCv.h"
class ShapeFromShadingAnalysis : public AbstractAnalysis class ShapeFromShadingAnalysis : public AbstractAnalysis
{ {
@ -23,16 +21,18 @@ public:
void setup(int camWidth, int camHeight); void setup(int camWidth, int camHeight);
void acquire(); void acquire();
void synthesise(); void synthesise();
void displayresults(); void display_results();
void draw(); void draw();
void save_cb(Poco::Timer& timer); void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected: protected:
string quad; string quad;
bool _RUN_DONE; bool _RUN_DONE;
float _flip, _level; float _flip, _level;
int _run_cnt, _save_cnt;
int _animation_cnt1; int _animation_cnt1;
int _animation_cnt2; int _animation_cnt2;
int _animation_cnt3; int _animation_cnt3;
@ -50,19 +50,6 @@ protected:
int _animation_cnt15; int _animation_cnt15;
int _animation_cnt16; int _animation_cnt16;
int _animation_reset; // this reset part didn't get working - so using 16 different counters! yay! int _animation_reset; // this reset part didn't get working - so using 16 different counters! yay!
float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max;
int _run_cnt, _save_cnt, _synth_save_cnt, _anim_cnt;
float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max;
bool _show_image, _image_shown;
ofImage image1;
ofImage image2;
int algo;
int scale;
int draw_style;
float line_width;
float point_size;
}; };

View File

@ -1,3 +1,8 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "StrobeAnalysis.h" #include "StrobeAnalysis.h"
#include "ofMain.h" #include "ofMain.h"
@ -10,69 +15,22 @@ using Poco::Timer;
using Poco::TimerCallback; using Poco::TimerCallback;
using Poco::Thread; using Poco::Thread;
#define NUMBER_RUNS 1
#define ACQUIRE_TIME 30
const int algo_default = 1;
const int scale_default = 500;
const int draw_style_default = 3;
const int line_width_default = 0.5f;
const float point_size_default = 0.5f;
void StrobeAnalysis::setup(int camWidth, int camHeight) void StrobeAnalysis::setup(int camWidth, int camHeight)
{ {
AbstractAnalysis::setup(camWidth, camHeight); DELTA_T_SAVE = 100;
NUM_RUN = 1;
NUM_RUN = RefractiveIndex::XML.getValue("config:analysis_NUM_RUN:NUM_RUN_strobe", NUMBER_RUNS); _strobe_cnt = 0;
cout << "NUM_RUN StrobeAnalysis " << NUM_RUN << endl; _strobe_cnt_max = 20; // 40 x 500 ms = 20000 ms = 20 seconds run time
//NUM_RUN = 5; _strobe_interval = 1000; //every 0.5seconds = 15 frames
_frame_cnt_max = _strobe_cnt_max * _strobe_interval * ofGetFrameRate()/1000;
int acq_run_time; // 10 seconds of acquiring per run
acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_strobe", ACQUIRE_TIME);
cout << "ACQUIRE_TIME StrobeAnalysis " << acq_run_time << endl;
//int acq_run_time = 25; // 20 seconds of acquiring per run
DELTA_T_SAVE = 2*(10*acq_run_time/2); // for 20 seconds, we want this to be around 100 files
// or 5 times per second = every 200 ms
_frame_cnt_max = acq_run_time*ofGetFrameRate(); // e.g.: 30 frames per second * 20 seconds = 600 frames
_strobe_interval = 2000; //every 1 seconds, or every thirty frames 30 frames
// The British Health and Safety Executive recommend that a net flash rate for a bank of strobe lights does not exceed 5 flashes per second, at which only 5% of photosensitive epileptics are at risk. It also recommends that no strobing effect continue for more than 30 seconds, due to the potential for discomfort and disorientation. // The British Health and Safety Executive recommend that a net flash rate for a bank of strobe lights does not exceed 5 flashes per second, at which only 5% of photosensitive epileptics are at risk. It also recommends that no strobing effect continue for more than 30 seconds, due to the potential for discomfort and disorientation.
//create_dir_allocate_images(); //or 20 times, every one second...
_synth_save_cnt = 0; _save_cnt_max = _strobe_cnt_max*_strobe_interval/DELTA_T_SAVE;
_run_cnt = 0;
int anim_time = 5; // 5 seconds for the animation
_anim_cnt_max = anim_time*ofGetFrameRate(); // e.g.: 30 frames per second = 150 frames
_show_image = false;
_image_shown = false;
image1.clear();
image2.clear();
image1.clear();
image2.clear();
// images use for drawing the synthesized files to the screen ///
image1.setUseTexture(false); // the non texture image that is needed to first load the image
image2.setUseTexture(true); // the image that needs to get written to the screen which takes the content of image1
image1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
////---------
algo = RefractiveIndex::XML.getValue("config:algorithms:strobe:algo", algo_default);
scale = RefractiveIndex::XML.getValue("config:algorithms:strobe:scale", scale_default);
draw_style = RefractiveIndex::XML.getValue("config:algorithms:strobe:draw_style", draw_style_default);
line_width = RefractiveIndex::XML.getValue("config:algorithms:strobe:line_width", line_width_default);
point_size = RefractiveIndex::XML.getValue("config:algorithms:strobe:point_size", point_size_default);
create_dir();
} }
@ -80,82 +38,94 @@ void StrobeAnalysis::acquire()
{ {
Timer* save_timer; Timer* save_timer;
TimerCallback<StrobeAnalysis> save_callback(*this, &StrobeAnalysis::save_cb); TimerCallback<StrobeAnalysis> save_callback(*this, &StrobeAnalysis::save_cb);
_frame_cnt = 0; _save_cnt = 0; _anim_cnt = 0; _strobe_cnt = 0, _synth_save_cnt = 0;
_run_cnt++;
_RUN_DONE = false;
create_dir_allocate_images();
// RUN ROUTINE // RUN ROUTINE
//for(int i = 0; i < NUM_RUN; i++) { for(int i = 0; i < NUM_RUN; i++) {
//_run_cnt = i; _run_cnt = i;
_save_cnt = 0;
_frame_cnt = 0;
//cout << "RUN NUM = " << i; cout << "RUN NUM = " << i;
save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
save_timer->start(save_callback); save_timer->start(save_callback);
_RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
while(!_RUN_DONE && _state != STATE_STOP) while(!_RUN_DONE)
Thread::sleep(3); Thread::sleep(3);
save_timer->stop(); save_timer->stop();
}
//}
} }
void StrobeAnalysis::synthesise() void StrobeAnalysis::synthesise()
{ {
// we don't need to synthesise //incrementer to whichMesh
return; speed=0.2;
//whichMesh is the index in the vector of meshes
whichMesh=0;
/* int index=0;
float iterator=1;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
//cout << "IResponseAnalysis::saving synthesis...\n"; //hack to limit number of meshes.
if(_state == STATE_STOP) return; if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100;
}
}
//clear vector so we don't add to it on successive runs
meshes.clear();
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){
ofImage image1;
ofImage image2;
//there is a known issue with using loadImage inside classes in other directories. the fix is to call setUseTExture(false)
image1.setUseTexture(false);
image2.setUseTexture(false);
//some of the textures are not loading correctly so only make mesh if both the images load
if(image1.loadImage(_saved_filenames[i]) && image2.loadImage(_saved_filenames[i+1])){
meshes.push_back(ofMesh());
cout<<"setting mesh"<<endl;
int _recorded_brightness_value=getRecordedValueFromFileName(_saved_filenames[i]);
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_BRIGHTNESS,_recorded_brightness_value), image2, &meshes[index]);
index++;
}
}
}
void StrobeAnalysis::display_results(){
Timer* display_results_timer;
TimerCallback<StrobeAnalysis> display_results_callback(*this, &StrobeAnalysis::display_results_cb);
// display results of the synthesis
display_results_timer = new Timer(0, 20); // timing interval for saving files
display_results_timer->start(display_results_callback);
_RUN_DONE = false; _RUN_DONE = false;
_results_cnt=0;
_results_cnt_max=300;
// _saved_filenames_synthesis has processed all the files in the analysis images folder while(!_RUN_DONE)
while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3); Thread::sleep(3);
*/
}
void StrobeAnalysis::displayresults() display_results_timer->stop();
{
for(float i=1;i<_saved_filenames_analysis.size();i++){
if(_state == STATE_STOP) return;
//cout << "_saved_filenames_analysis[i] - " << _saved_filenames_synthesis[i] << endl;
while(!_image_shown){
Thread::sleep(2);
//cout << "!_image_shown" << endl;
}
_show_image = false;
if(!image1.loadImage(_saved_filenames_analysis[i])){
//couldn't load image
cout << "didn't load image" << endl;
}
if(image1.loadImage(_saved_filenames_analysis[i])){
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
}
}
} }
// this runs at frame rate = 33 ms for 30 FPS // this runs at frame rate = 33 ms for 30 FPS
void StrobeAnalysis::draw() void StrobeAnalysis::draw()
{ {
@ -169,7 +139,7 @@ void StrobeAnalysis::draw()
ofEnableAlphaBlending(); ofEnableAlphaBlending();
ofColor aColour; ofColor aColour;
int _fade_in_frames = _frame_cnt_max/10; int _fade_in_frames = _frame_cnt_max/10;
// cout<< "_fade_in_frames" << _fade_in_frames<< endl; cout<< "_fade_in_frames" << _fade_in_frames<< endl;
if (_frame_cnt < _fade_in_frames) { if (_frame_cnt < _fade_in_frames) {
@ -187,13 +157,13 @@ void StrobeAnalysis::draw()
//cout << "frame_cnt % 15: " << _frame_cnt%15 << endl; //cout << "frame_cnt % 15: " << _frame_cnt%15 << endl;
//cout << "MAIN STROBE TIME " << endl; //cout << "MAIN STROBE TIME " << endl;
if (int(_frame_cnt)%int(ofGetFrameRate()*_strobe_interval/1000) < (ofGetFrameRate()*_strobe_interval/1000)/2) if (_frame_cnt%int(ofGetFrameRate()*_strobe_interval/1000) < (ofGetFrameRate()*_strobe_interval/1000)/2)
{ {
ofSetColor(255, 255, 255); ofSetColor(255, 255, 255);
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
_strobe_cnt++; _strobe_cnt++;
_strobe_on = 1; _strobe_on = 1;
} else if (int(_frame_cnt)%int(ofGetFrameRate()*_strobe_interval/1000) >= (ofGetFrameRate()*_strobe_interval/1000)/2) } else if (_frame_cnt%int(ofGetFrameRate()*_strobe_interval/1000) >= (ofGetFrameRate()*_strobe_interval/1000)/2)
{ {
ofSetColor(0, 0, 0); ofSetColor(0, 0, 0);
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
@ -203,7 +173,7 @@ void StrobeAnalysis::draw()
} }
if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max) { if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max) {
aColour.set(255, 255, 255, 255-ofMap(_frame_cnt-(_frame_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255)); aColour.set(255, 255, 255, 255-ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255));
ofSetColor(aColour); ofSetColor(aColour);
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
// cout << "FADE OUT STROBE TIME " << endl; // cout << "FADE OUT STROBE TIME " << endl;
@ -211,7 +181,6 @@ void StrobeAnalysis::draw()
ofDisableAlphaBlending(); ofDisableAlphaBlending();
} else { } else {
//_state = STATE_SYNTHESISING;
_RUN_DONE = true; _RUN_DONE = true;
} }
@ -223,141 +192,33 @@ void StrobeAnalysis::draw()
case STATE_SYNTHESISING: case STATE_SYNTHESISING:
{ {
// display animation of something while the synthesis in on-going... // display animation of something while the synthesis in on-going...
// cout << "StrobeAnalysis = STATE_SYNTHESISING...\n";
// display animation of something while the synthesis in on-going...
ofEnableAlphaBlending();
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
if(_anim_cnt < _anim_cnt_max){
ofColor aColour;
int rectSizeW = ofGetWidth()/4;
int rectSizeH = ofGetHeight()/4;
int _fade_in_frames = _anim_cnt_max/2;
int c_anim = 10;
int fade;
//ofRotate(ofMap(_anim_cnt/2.0, 0, _anim_cnt_max, 0, 360));
if (_anim_cnt < _fade_in_frames) {
// cout << "StrobeAnalysis STATE_SYNTHESIZING = FADING IN ANIMATION...\n";
fade = ofMap(_anim_cnt, 0, _fade_in_frames, 0, 255);
for (int i=0; i <= 15; i++){
c_anim = 0+17*i;
aColour.set(c_anim, c_anim, c_anim, fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt >= _fade_in_frames && _anim_cnt <= (_anim_cnt_max-_fade_in_frames)){
for (int i=0; i <= 15; i++){
c_anim = 255;
aColour.set(c_anim, c_anim, c_anim, 255);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt > (_anim_cnt_max-_fade_in_frames) && _anim_cnt <= _anim_cnt_max) {
// cout << "_anim_cnt = " << _anim_cnt-(_anim_cnt_max-_fade_in_frames) << endl;
fade = ofMap(_anim_cnt-(_anim_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255);
// cout << "fade down = " << fade << endl;
for (int i=0; i <= 15; i++){
c_anim = (17*i);
aColour.set(c_anim, c_anim, c_anim, 255-fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
_anim_cnt++;
} else {
_RUN_DONE = true;
//_state = STATE_DISPLAY_RESULTS;
_anim_cnt=0;
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDisableAlphaBlending();
break; break;
} }
case STATE_DISPLAY_RESULTS: case STATE_DISPLAY_RESULTS:
{ {
// display results of the synthesis
int imageWidth=640;
int imageHeight =480;
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotateY(_results_cnt*0.3);
//ofRotateX(90);
//ofRotateZ(whichMesh);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2),-400;
ofTranslate((ofGetWidth()/2)-(imageWidth/2),0,0 );
meshes[whichMesh].drawVertices();
ofPopMatrix();
whichMesh+=speed;
cout<<whichMesh<<" size of meshes "<<meshes.size()<<endl;
if(whichMesh>meshes.size() -1 || whichMesh<0){
speed*=-1;
whichMesh+=speed;
if (_frame_cnt > 2)
{
_image_shown = true;
_frame_cnt=0;
} }
_frame_cnt++;
ofEnableAlphaBlending();
glShadeModel(GL_SMOOTH);
glLineWidth(line_width);
glPointSize(point_size);
glEnable(GL_POINT_SMOOTH);
RefractiveIndex::cam.begin();
ofTranslate(tx, ty, tz);
ofRotateX(rx); ofRotateY(ry); ofRotateZ(rz);
glScalef(1.5, 1, 1);
if (_show_image)
image2.setFromPixels(image1.getPixels(), image1.width, image1.height, OF_IMAGE_COLOR);
image2.bind();
RefractiveIndex::_shader.begin();
RefractiveIndex::_shader.setUniform1i("algo", algo);
RefractiveIndex::_shader.setUniform1f("scale", scale);
RefractiveIndex::_shader.setUniform1i("tex0", 0);
switch (draw_style) {
case VERTS:
RefractiveIndex::_mesh_vbo.drawVertices();
break; break;
case WIRE:
RefractiveIndex::_mesh_vbo.drawWireframe();
break;
case FACE:
RefractiveIndex::_mesh_vbo.drawFaces();
break;
}
RefractiveIndex::_shader.end();
image2.unbind();
RefractiveIndex::cam.end();
break;
} }
@ -370,9 +231,27 @@ void StrobeAnalysis::draw()
// this runs at save_cb timer rate = DELTA_T_SAVE // this runs at save_cb timer rate = DELTA_T_SAVE
void StrobeAnalysis::save_cb(Timer& timer) void StrobeAnalysis::save_cb(Timer& timer)
{ {
string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(_strobe_on) +"_"+ofToString(_run_cnt,2)+".jpg";
saveimage(file_name);
_save_cnt++; _save_cnt++;
string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(_strobe_on) +"_"+ofToString(_run_cnt,2)+".jpg"; cout << "_save_cnt" << _save_cnt << endl;
cout << "_save_cnt_max" << _save_cnt_max << endl;
//TODO: something fucked here with my calc of _save_cnt_max - new structure should fix it?
//if(_save_cnt >= _save_cnt_max-10) {
// _RUN_DONE = true;
//}
saveImageAnalysis(file_name);
} }
void StrobeAnalysis::display_results_cb(Timer& timer){
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
}

View File

@ -9,8 +9,6 @@
#include "Poco/Timer.h" #include "Poco/Timer.h"
#include "ofxOpenCv.h"
class StrobeAnalysis : public AbstractAnalysis class StrobeAnalysis : public AbstractAnalysis
{ {
@ -23,32 +21,21 @@ public:
void setup(int camWidth, int camHeight); void setup(int camWidth, int camHeight);
void acquire(); void acquire();
void synthesise(); void synthesise();
void displayresults(); void display_results();
void draw(); void draw();
void save_cb(Poco::Timer& timer); void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected: protected:
bool _RUN_DONE; bool _RUN_DONE;
int _strobe_cnt, _strobe_cnt_max; int _strobe_cnt, _run_cnt, _strobe_cnt_max;
int _save_cnt;
int _frame_cnt, _frame_cnt_max, _save_cnt_max ;
float _results_cnt, _results_cnt_max;
int _strobe_interval; int _strobe_interval;
bool _strobe_on; bool _strobe_on;
int _run_cnt, _save_cnt,_synth_save_cnt, _anim_cnt;
float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max;
bool _show_image, _image_shown;
ofImage image1;
ofImage image2;
int algo;
int scale;
int draw_style;
float line_width;
float point_size;
}; };

View File

@ -1,211 +0,0 @@
#include "rfiCvContourFinder.h"
//--------------------------------------------------------------------------------
static bool sort_carea_compare( const CvSeq* a, const CvSeq* b) {
// use opencv to calc size, then sort based on size
float areaa = fabs(cvContourArea(a, CV_WHOLE_SEQ));
float areab = fabs(cvContourArea(b, CV_WHOLE_SEQ));
//return 0;
return (areaa > areab);
}
//--------------------------------------------------------------------------------
rfiCvContourFinder::rfiCvContourFinder() {
_width = 0;
_height = 0;
myMoments = (CvMoments*)malloc( sizeof(CvMoments) );
reset();
}
//--------------------------------------------------------------------------------
rfiCvContourFinder::~rfiCvContourFinder() {
free( myMoments );
}
//--------------------------------------------------------------------------------
void rfiCvContourFinder::reset() {
cvSeqBlobs.clear();
blobs.clear();
nBlobs = 0;
}
//--------------------------------------------------------------------------------
int rfiCvContourFinder::findContours( ofxCvGrayscaleImage& input,
int minArea,
int maxArea,
int nConsidered,
bool bFindHoles,
bool bUseApproximation) {
// get width/height disregarding ROI
IplImage* ipltemp = input.getCvImage();
_width = ipltemp->width;
_height = ipltemp->height;
reset();
// opencv will clober the image it detects contours on, so we want to
// copy it into a copy before we detect contours. That copy is allocated
// if necessary (necessary = (a) not allocated or (b) wrong size)
// so be careful if you pass in different sized images to "findContours"
// there is a performance penalty, but we think there is not a memory leak
// to worry about better to create mutiple contour finders for different
// sizes, ie, if you are finding contours in a 640x480 image but also a
// 320x240 image better to make two rfiCvContourFinder objects then to use
// one, because you will get penalized less.
if( inputCopy.getWidth() == 0 ) {
inputCopy.setUseTexture(false);
inputCopy.allocate( _width, _height );
} else if( inputCopy.getWidth() != _width || inputCopy.getHeight() != _height ) {
// reallocate to new size
inputCopy.clear();
inputCopy.setUseTexture(false);
inputCopy.allocate( _width, _height );
}
inputCopy.setROI( input.getROI() );
inputCopy = input;
CvSeq* contour_list = NULL;
contour_storage = cvCreateMemStorage( 1000 );
storage = cvCreateMemStorage( 1000 );
CvContourRetrievalMode retrieve_mode
= (bFindHoles) ? CV_RETR_LIST : CV_RETR_EXTERNAL;
cvFindContours( inputCopy.getCvImage(), contour_storage, &contour_list,
sizeof(CvContour), retrieve_mode, bUseApproximation ? CV_CHAIN_APPROX_SIMPLE : CV_CHAIN_APPROX_NONE );
CvSeq* contour_ptr = contour_list;
// put the contours from the linked list, into an array for sorting
while( (contour_ptr != NULL) ) {
float area = fabs( cvContourArea(contour_ptr, CV_WHOLE_SEQ) );
if( (area > minArea) && (area < maxArea) ) {
cvSeqBlobs.push_back(contour_ptr);
}
contour_ptr = contour_ptr->h_next;
}
// sort the pointers based on size
if( cvSeqBlobs.size() > 1 ) {
sort( cvSeqBlobs.begin(), cvSeqBlobs.end(), sort_carea_compare );
}
// now, we have cvSeqBlobs.size() contours, sorted by size in the array
// cvSeqBlobs let's get the data out and into our structures that we like
for( int i = 0; i < MIN(nConsidered, (int)cvSeqBlobs.size()); i++ ) {
blobs.push_back( ofxCvBlob() );
float area = cvContourArea( cvSeqBlobs[i], CV_WHOLE_SEQ );
CvRect rect = cvBoundingRect( cvSeqBlobs[i], 0 );
cvMoments( cvSeqBlobs[i], myMoments );
blobs[i].area = fabs(area);
blobs[i].hole = area < 0 ? true : false;
blobs[i].length = cvArcLength(cvSeqBlobs[i]);
blobs[i].boundingRect.x = rect.x;
blobs[i].boundingRect.y = rect.y;
blobs[i].boundingRect.width = rect.width;
blobs[i].boundingRect.height = rect.height;
blobs[i].centroid.x = (myMoments->m10 / myMoments->m00);
blobs[i].centroid.y = (myMoments->m01 / myMoments->m00);
// get the points for the blob:
CvPoint pt;
CvSeqReader reader;
cvStartReadSeq( cvSeqBlobs[i], &reader, 0 );
for( int j=0; j < cvSeqBlobs[i]->total; j++ ) {
CV_READ_SEQ_ELEM( pt, reader );
blobs[i].pts.push_back( ofPoint((float)pt.x, (float)pt.y) );
}
blobs[i].nPts = blobs[i].pts.size();
}
nBlobs = blobs.size();
// Free the storage memory.
// Warning: do this inside this function otherwise a strange memory leak
if( contour_storage != NULL ) { cvReleaseMemStorage(&contour_storage); }
if( storage != NULL ) { cvReleaseMemStorage(&storage); }
return nBlobs;
}
//--------------------------------------------------------------------------------
void rfiCvContourFinder::draw( float x, float y, float w, float h ) {
float scalex = 0.0f;
float scaley = 0.0f;
if( _width != 0 ) { scalex = w/_width; } else { scalex = 1.0f; }
if( _height != 0 ) { scaley = h/_height; } else { scaley = 1.0f; }
if(bAnchorIsPct){
x -= anchor.x * w;
y -= anchor.y * h;
}else{
x -= anchor.x;
y -= anchor.y;
}
ofPushStyle();
glPushMatrix();
glTranslatef( x, y, 0.0 );
glScalef( scalex, scaley, 0.0 );
ofSetHexColor(0xFFFFFF);
for( int i=0; i<(int)blobs.size(); i++ ) {
ofNoFill();
ofBeginShape();
for( int j=0; j<blobs[i].nPts; j++ ) {
ofVertex( blobs[i].pts[j].x, blobs[i].pts[j].y );
}
ofEndShape();
}
glPopMatrix();
ofPopStyle();
}
//----------------------------------------------------------
void rfiCvContourFinder::draw(const ofPoint & point){
draw(point.x, point.y);
}
//----------------------------------------------------------
void rfiCvContourFinder::draw(const ofRectangle & rect){
draw(rect.x, rect.y, rect.width, rect.height);
}
//--------------------------------------------------------------------------------
void rfiCvContourFinder::setAnchorPercent( float xPct, float yPct ){
anchor.x = xPct;
anchor.y = yPct;
bAnchorIsPct = true;
}
//--------------------------------------------------------------------------------
void rfiCvContourFinder::setAnchorPoint( int x, int y ){
anchor.x = x;
anchor.y = y;
bAnchorIsPct = false;
}
//--------------------------------------------------------------------------------
void rfiCvContourFinder::resetAnchor(){
anchor.set(0,0);
bAnchorIsPct = false;
}

View File

@ -1,73 +0,0 @@
/*
* rfiCvContourFinder.h
*
* Finds white blobs in binary images and identifies
* centroid, bounding box, area, length and polygonal contour
* The result is placed in a vector of ofxCvBlob objects.
*
*/
#ifndef RFI_CV_CONTOUR_FINDER
#define RFI_CV_CONTOUR_FINDER
#include "ofxCvConstants.h"
#include "ofxCvBlob.h"
#include "ofxCvGrayscaleImage.h"
#include <algorithm>
class rfiCvContourFinder : public ofBaseDraws {
public:
vector<ofxCvBlob> blobs;
int nBlobs; // DEPRECATED: use blobs.size() instead
rfiCvContourFinder();
virtual ~rfiCvContourFinder();
virtual float getWidth() { return _width; }; //set after first findContours call
virtual float getHeight() { return _height; }; //set after first findContours call
virtual int findContours( ofxCvGrayscaleImage& input,
int minArea, int maxArea,
int nConsidered, bool bFindHoles,
bool bUseApproximation = true);
// approximation = don't do points for all points
// of the contour, if the contour runs
// along a straight line, for example...
virtual void draw() { draw(0,0, _width, _height); };
virtual void draw( float x, float y ) { draw(x,y, _width, _height); };
virtual void draw( float x, float y, float w, float h );
virtual void draw(const ofPoint & point);
virtual void draw(const ofRectangle & rect);
virtual void setAnchorPercent(float xPct, float yPct);
virtual void setAnchorPoint(int x, int y);
virtual void resetAnchor();
//virtual ofxCvBlob getBlob(int num);
protected:
int _width;
int _height;
ofxCvGrayscaleImage inputCopy;
CvMemStorage* contour_storage;
CvMemStorage* storage;
CvMoments* myMoments;
vector<CvSeq*> cvSeqBlobs; //these will become blobs
ofPoint anchor;
bool bAnchorIsPct;
virtual void reset();
};
#endif