double rgbdiff(double rgb1[], double rgb2[]) { double *lab1 = rgb2lab(rgb1); double *lab2 = rgb2lab(rgb2); double e = edistance(lab1, lab2); free(lab1); free(lab2); return e; }
double FeatureAverageColor::compare( FeatureValue a, FeatureValue b ) { if( (a.size() != 3) || (b.size() != 3) ) { cerr << "Wrong amount of parameters: should be 3, is: a(" << a.size() << ") | b(" << b.size() << ")" << endl; return 0; } double aLAB[3], bLAB[3]; rgb2lab( a.data(), aLAB ); rgb2lab( b.data(), bLAB ); return 1 - sqrt( pow( aLAB[0] - bLAB[0], 2 ) + pow( aLAB[1] - bLAB[1], 2 ) + pow( aLAB[2] - bLAB[2], 2 ) ) / 255.0; }
void GLView::process() { makeCurrent(); if (m_image.isNull()) { m_dst = texture_2d(); return; } int w = m_image.width(); int h = m_image.height(); glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo); glPushAttrib(GL_TEXTURE_BIT); texture_2d src(GL_RGB16F_ARB, m_image.width(), m_image.height(), GL_BGRA, GL_UNSIGNED_BYTE, m_image.bits()); texture_2d lab = rgb2lab(src); texture_2d tfm = tangent_flow_map(src, sst_sigma); texture_2d bfe = (bf_ne > 0)? orientation_aligned_bilateral_filter(lab, tfm, bf_ne, bf_sigma_d, bf_sigma_r) : lab; texture_2d bfa = (bf_na > 0)? orientation_aligned_bilateral_filter(lab, tfm, bf_na, bf_sigma_d, bf_sigma_r) : lab; texture_2d edges = (fdog_type == 0)? fdog_filter(bfe, tfm, fdog_n, fdog_sigma_e, fdog_sigma_r, fdog_tau, fdog_sigma_m, fdog_phi) : dog_filter(bfe, fdog_n, fdog_sigma_e, fdog_sigma_r, fdog_tau, fdog_phi); texture_2d cq = color_quantization(bfa, cq_nbins, cq_phi_q, cq_filter); texture_2d cq_rgb = lab2rgb(cq); texture_2d ov = mix_filter(edges, cq_rgb, fdog_color); texture_2d result = fs_type? smooth_filter(tfm, ov, fs_type, fs_sigma) : ov; switch (preview) { case 0: m_dst = result; break; case 1: m_dst = src; break; case 2: m_dst = lic_filter(tfm, m_noise, 5.0); break; case 3: m_dst = lab2rgb(bfe); break; case 4: m_dst = lab2rgb(bfa); break; case 5: m_dst = edges; break; case 6: m_dst = cq_rgb; break; } glUseProgram(0); glPopAttrib(); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); }
void work() { // convert from rgb to lab for(int i = 0; i < n; ++i) { rgb2lab(getcol(img, i/h, i%h), ori[i]); res[i] = delta[i] = 0; } // calculate delta HANDLE thd[8]; now = 0; for(int i = 0; i < 8; ++i) thd[i] = CreateThread(0, 0, calcDelta, 0, 0, 0); for(int i = 0; i < 8; ++i) WaitForSingleObject(thd[i], INFINITE); // calculate result for(int i = 1; i < n; ++i) res[i] = ( delta[i] - delta[i-1] + n * res[i-1]) / n; double sum = 0; for(int i = 0; i < n; i++) sum += res[i] - ori[i][0]; sum /= n; for(int i = 0; i < n; i++) res[i] = res[i] - sum; // output img for(int i = 0; i < n; ++i) { imgRes.at<uchar>(Point(i/h, i%h)) = lab2rgb(res[i]); } }
SeedFeature::SeedFeature( const ImageOverSegmentation & ios, const VectorXf & obj_param ) { Image rgb_im = ios.image(); const RMatrixXs & s = ios.s(); const int Ns = ios.Ns(), W = rgb_im.W(), H = rgb_im.H(); // Initialize various values VectorXf area = bin( s, 1, [&](int x, int y){ return 1.f; } ); VectorXf norm = (area.array()+1e-10).cwiseInverse(); pos_ = norm.asDiagonal() * bin( s, 6, [&](int i, int j){ float x=1.0*i/(W-1)-0.5,y=1.0*j/(H-1)-0.5; return makeArray<6>( x, y, x*x, y*y, fabs(x), fabs(y) ); } ); if (N_DYNAMIC_COL) { Image lab_im; rgb2lab( lab_im, rgb_im ); col_ = norm.asDiagonal() * bin( s, 6, [&](int x, int y){ return makeArray<6>( rgb_im(y,x, 0), rgb_im(y,x,1), rgb_im(y,x,2), lab_im(y,x,0), lab_im(y,x,1), lab_im(y,x,2) ); } ); } const int N_GEO = sizeof(EDGE_P)/sizeof(EDGE_P[0]); for( int i=0; i<N_GEO; i++ ) gdist_.push_back( GeodesicDistance(ios.edges(),ios.edgeWeights().array().pow(EDGE_P[i])+1e-3) ); // Compute the static features static_f_ = RMatrixXf::Zero( Ns, N_STATIC_F ); int o=0; // Add the position features static_f_.block( 0, o, Ns, N_STATIC_POS ) = pos_.leftCols( N_STATIC_POS ); o += N_STATIC_POS; // Add the geodesic features if( N_STATIC_GEO >= N_GEO ) { RMatrixXu8 bnd = findBoundary( s ); RMatrixXf mask = (bnd.array() == 0).cast<float>()*1e10; for( int i=0; i<N_GEO; i++ ) static_f_.col( o++ ) = gdist_[i].compute( mask ); for( int j=1; (j+1)*N_GEO<=N_STATIC_GEO; j++ ) { mask = (bnd.array() != j).cast<float>()*1e10; for( int i=0; i<N_GEO; i++ ) static_f_.col( o++ ) = gdist_[i].compute( mask ); } } if( N_STATIC_EDGE ) { RMatrixXf edge_map = DirectedSobel().detect( ios.image() ); for( int j=0; j<s.rows(); j++ ) for( int i=0; i<s.cols(); i++ ) { const int id = s(j,i); int bin = edge_map(j,i)*N_STATIC_EDGE; if ( bin < 0 ) bin = 0; if ( bin >= N_STATIC_EDGE ) bin = N_STATIC_EDGE-1; static_f_(id,o+bin) += norm[id]; } o += N_STATIC_EDGE; } if( N_OBJ_F>1 ) static_f_.col(o++) = (computeObjFeatures(ios)*obj_param).transpose(); // Initialize the dynamic features dynamic_f_ = RMatrixXf::Zero( Ns, N_DYNAMIC_F ); n_ = 0; min_dist_ = RMatrixXf::Ones(Ns,5)*10; var_ = RMatrixXf::Zero(Ns,6); }
// replace_luminance void replace_luminance(uint8& r1, uint8& g1, uint8& b1, uint8 r2, uint8 g2, uint8 b2) { float CIEL1, CIEa1, CIEb1, CIEL2;//, CIEa2, CIEb2; rgb2lab(r1, g1, b1, CIEL1, CIEa1, CIEb1); // rgb2lab(r2, g2, b2, CIEL2, CIEa2, CIEb2); CIEL2 = ((linearTable[r2] + linearTable[g2] + linearTable[b2]) / 3.0) * 100.0; lab2rgb(CIEL2, CIEa1, CIEb1, r1, g1, b1); }
// BANG - calculate and output void cs_bang(t_cs *x) { if(x->attr_mode == ps_rgb2hsl) rgb2hsl(x, x->val1, x->val2, x->val3); // first for speed else if(x->attr_mode == ps_hsl2rgb) hsl2rgb(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_no_transform) no_transform(x); else if(x->attr_mode == ps_rgb2cmy) rgb2cmy(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_cmy2rgb) cmy2rgb(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_rgb2hsv) rgb2hsv(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_hsv2rgb) hsv2rgb(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_rgb2xyz) rgb2xyz(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_xyz2rgb) xyz2rgb(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_rgb2uvw) rgb2uvw(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_uvw2rgb) uvw2rgb(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_rgb2retinalcone) rgb2cone(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_retinalcone2rgb) cone2rgb(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_rgb2lab) rgb2lab(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_lab2rgb) lab2rgb(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_rgb2yiq) rgb2yiq(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_yiq2rgb) yiq2rgb(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_rgb2hls) rgb2hls(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_hls2rgb) hls2rgb(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_rgb2rgbcie) rgb2rgbcie(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_rgbcie2rgb) rgbcie2rgb(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_rgb2rgbsmpte) rgb2rgbsmpte(x, x->val1, x->val2, x->val3); else if(x->attr_mode == ps_rgbsmpte2rgb) rgbsmpte2rgb(x, x->val1, x->val2, x->val3); if(x->attr_outputtype == ps_packed){ Atom temp_list[3]; atom_setlong(temp_list+0, x->calc1); atom_setlong(temp_list+1, x->calc2); atom_setlong(temp_list+2, x->calc3); outlet_list(x->out1, 0L, 3, temp_list); // output the result } else{ outlet_int(x->out3, x->calc3); // output the result outlet_int(x->out2, x->calc2); // output the result outlet_int(x->out1, x->calc1); // output the result } }
void compute( Ref<RMatrixXf> r, const ImageOverSegmentation & ios ) const { Image f_im; if( lab_ ) rgb2lab( f_im, ios.image() ); else f_im = ios.image(); const int N = ios.Ns(); std::vector<Vector3f> mean_color( N, Vector3f::Zero() ); std::vector<float> cnt( N, 1e-8 ); const RMatrixXs & s = ios.s(); for( int j=0; j<s.rows(); j++ ) for( int i=0; i<s.cols(); i++ ) if( s(j,i) >= 0 ){ mean_color[ s(j,i) ] += f_im.at<3>( j, i ); cnt[ s(j,i) ] += 1; } for( int i=0; i<N; i++ ) mean_color[i] /= cnt[i]; for( int i=0; i<(int)ios.edges().size(); i++ ) r.row(i) = (mean_color[ ios.edges()[i].a ]-mean_color[ ios.edges()[i].b ]).array().abs(); }
Image ColorConvert::apply(Image im, string from, string to) { // check for the trivial case assert(from != to, "color conversion from %s to %s is pointless\n", from.c_str(), to.c_str()); // unsupported destination color spaces if (to == "yuyv" || to == "uyvy") { panic("Unsupported destination color space: %s\n", to.c_str()); } // direct conversions that don't have to go via rgb if (from == "yuyv" && to == "yuv") { return yuyv2yuv(im); } else if (from == "uyvy" && to == "yuv") { return uyvy2yuv(im); } else if (from == "xyz" && to == "lab") { return xyz2lab(im); } else if (from == "lab" && to == "xyz") { return lab2xyz(im); } else if (from == "argb" && to == "xyz") { return argb2xyz(im); } else if (from == "xyz" && to == "argb") { return xyz2argb(im); } else if (from != "rgb" && to != "rgb") { // conversions that go through rgb Image halfway = apply(im, from, "rgb"); return apply(halfway, "rgb", to); } else if (from == "rgb") { // from rgb if (to == "hsv" || to == "hsl" || to == "hsb") { return rgb2hsv(im); } else if (to == "yuv") { return rgb2yuv(im); } else if (to == "xyz") { return rgb2xyz(im); } else if (to == "y" || to == "gray" || to == "grayscale" || to == "luminance") { return rgb2y(im); } else if (to == "lab") { return rgb2lab(im); } else if (to == "argb") { return rgb2argb(im); } else { panic("Unknown color space %s\n", to.c_str()); } } else { //(to == "rgb") if (from == "hsv" || from == "hsl" || from == "hsb") { return hsv2rgb(im); } else if (from == "yuv") { return yuv2rgb(im); } else if (from == "xyz") { return xyz2rgb(im); } else if (from == "y" || from == "gray" || from == "grayscale" || from == "luminance") { return y2rgb(im); } else if (from == "lab") { return lab2rgb(im); } else if (from == "uyvy") { return uyvy2rgb(im); } else if (from == "yuyv") { return yuyv2rgb(im); } else if (from == "argb") { return argb2rgb(im); } else { panic("Unknown color space %s\n", from.c_str()); } } // keep the compiler happy return Image(); }
RMatrixXf SeedFeature::computeObjFeatures( const ImageOverSegmentation & ios ) { Image rgb_im = ios.image(); const RMatrixXs & s = ios.s(); const Edges & g = ios.edges(); const int Ns = ios.Ns(); RMatrixXf r = RMatrixXf::Zero( Ns, N_OBJ_F ); if( N_OBJ_F<=1 ) return r; VectorXf area = bin( s, 1, [&](int x, int y){ return 1.f; } ); VectorXf norm = (area.array()+1e-10).cwiseInverse(); r.col(0).setOnes(); int o = 1; if (N_OBJ_COL>=6) { Image lab_im; rgb2lab( lab_im, rgb_im ); r.middleCols(o,6) = norm.asDiagonal() * bin( s, 6, [&](int x, int y){ return makeArray<6>( lab_im(y,x,0), lab_im(y,x,1), lab_im(y,x,2), lab_im(y,x,0)*lab_im(y,x,0), lab_im(y,x,1)*lab_im(y,x,1), lab_im(y,x,2)*lab_im(y,x,2) ); } ); RMatrixXf col = r.middleCols(o,3); if( N_OBJ_COL >= 9) r.middleCols(o+6,3) = col.array().square(); o += N_OBJ_COL; // Add color difference features if( N_OBJ_COL_DIFF ) { RMatrixXf bcol = RMatrixXf::Ones( col.rows(), col.cols()+1 ); bcol.leftCols(3) = col; for( int it=0; it*3+2<N_OBJ_COL_DIFF; it++ ) { // Apply a box filter on the graph RMatrixXf tmp = bcol; for( const auto & e: g ) { tmp.row(e.a) += bcol.row(e.b); tmp.row(e.b) += bcol.row(e.a); } bcol = tmp.col(3).cwiseInverse().asDiagonal()*tmp; r.middleCols(o,3) = (bcol.leftCols(3)-col).array().abs(); o += 3; } } } if( N_OBJ_POS >= 2 ) { RMatrixXf xy = norm.asDiagonal() * bin( s, 2, [&](int x, int y){ return makeArray<2>( 1.0*x/(s.cols()-1)-0.5, 1.0*y/(s.rows()-1)-0.5 ); } ); r.middleCols(o,2) = xy; o+=2; if( N_OBJ_POS >=4 ) { r.middleCols(o,2) = xy.array().square(); o+=2; } } if( N_OBJ_EDGE ) { RMatrixXf edge_map = DirectedSobel().detect( rgb_im ); for( int j=0; j<s.rows(); j++ ) for( int i=0; i<s.cols(); i++ ) { const int id = s(j,i); int bin = edge_map(j,i)*N_OBJ_EDGE; if ( bin < 0 ) bin = 0; if ( bin >= N_OBJ_EDGE ) bin = N_OBJ_EDGE-1; r(id,o+bin) += norm[id]; } o += N_OBJ_EDGE; } const int N_BASIC = o-1; // Add in context features for( int i=0; i<N_OBJ_CONTEXT; i++ ) { const int o0 = o - N_BASIC; // Box filter the edges RMatrixXf f = RMatrixXf::Ones( Ns, N_BASIC+1 ), bf = RMatrixXf::Zero( Ns, N_BASIC+1 ); f.rightCols( N_BASIC ) = r.middleCols(o0,N_BASIC); for( Edge e: g ) { bf.row(e.a) += f.row(e.b); bf.row(e.b) += f.row(e.a); } r.middleCols(o,N_BASIC) = bf.col(0).array().max(1e-10f).inverse().matrix().asDiagonal() * bf.rightCols(N_BASIC); o += N_BASIC; } return r; }