// Get accuracy of the predicted results float accuracy(const array& predicted, const array& target) { return 100 * count<float>(predicted == target) / target.elements(); }
array::array(const array& in) : arr(0), isRef(false) { AF_THROW(af_weak_copy(&arr, in.get())); }
constexpr bool operator<(array<T, M> a, array<U, N> b) { return M < N || detail::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()); }
inline void swap (array<T,N>& x, array<T,N>& y) { x.swap(y); }
void stack_push_back_value( array<as_value> & stack, T value ) { stack.push_back( value ); }
array rgb2ycbcr(const array& in, const YCCStd standard) { af_array temp = 0; AF_THROW(af_rgb2ycbcr(&temp, in.get(), standard)); return array(temp); }
bool operator== (const array<T,N>& x, const array<T,N>& y) { return std::equal(x.begin(), x.end(), y.begin()); }
template<> AFAPI af_cdouble var(const array& in, const array weights) { double real, imag; AF_THROW(af_var_all_weighted(&real, &imag, in.get(), weights.get())); return std::complex<double>(real, imag); }
mapped_type val() const { return ary_->get_val_at_id(id(), w_, o_, true, false); }
template<> AFAPI af_cfloat var(const array& in, bool isbiased) { double real, imag; AF_THROW(af_var_all(&real, &imag, in.get(), isbiased)); return std::complex<float>((float)real, (float)imag); }
template<> AFAPI af_cdouble var(const array& in, bool isbiased) { double real, imag; AF_THROW(af_var_all(&real, &imag, in.get(), isbiased)); return std::complex<double>(real, imag); }
array var(const array& in, const array weights, dim_type dim) { af_array temp = 0; AF_THROW(af_var_weighted(&temp, in.get(), weights.get(), getFNSD(dim, in.dims()))); return array(temp); }
array var(const array& in, bool isbiased, dim_type dim) { af_array temp = 0; AF_THROW(af_var(&temp, in.get(), isbiased, getFNSD(dim, in.dims()))); return array(temp); }
void print(const array<T,N>& a) { for (size_t i=0; i!=a.size(); ++i) cout << a[i] << '\n'; }
explicit array(const array<U, N>& that) { std::uninitialized_copy(that.begin(), that.end(), begin()); }
size_t pos() const { return key_.get_bits(0, ary_->lsize()); }
void swap(array<U, N>& that) { swap_ranges(begin(), end(), that.begin()); }
// Read compiled properties data from descent.pig void properties_read_cmp(PHYSFS_File * fp) { // bitmap_index is a short NumTextures = PHYSFSX_readInt(fp); bitmap_index_read_n(fp, Textures); range_for (tmap_info &ti, TmapInfo) tmap_info_read(ti, fp); PHYSFS_read(fp, Sounds, sizeof(Sounds[0]), Sounds.size()); PHYSFS_read(fp, AltSounds, sizeof(AltSounds[0]), AltSounds.size()); Num_vclips = PHYSFSX_readInt(fp); range_for (vclip &vc, Vclip) vclip_read(fp, vc); Num_effects = PHYSFSX_readInt(fp); range_for (eclip &ec, Effects) eclip_read(fp, ec); Num_wall_anims = PHYSFSX_readInt(fp); range_for (auto &w, WallAnims) wclip_read(fp, w); N_robot_types = PHYSFSX_readInt(fp); range_for (auto &r, Robot_info) robot_info_read(fp, r); N_robot_joints = PHYSFSX_readInt(fp); range_for (auto &r, Robot_joints) jointpos_read(fp, r); N_weapon_types = PHYSFSX_readInt(fp); weapon_info_read_n(Weapon_info, MAX_WEAPON_TYPES, fp, 0); N_powerup_types = PHYSFSX_readInt(fp); range_for (auto &p, Powerup_info) powerup_type_info_read(fp, p); N_polygon_models = PHYSFSX_readInt(fp); { const auto &&r = partial_range(Polygon_models, N_polygon_models); range_for (auto &p, r) polymodel_read(&p, fp); range_for (auto &p, r) polygon_model_data_read(&p, fp); } bitmap_index_read_n(fp, partial_range(Gauges, MAX_GAUGE_BMS)); range_for (auto &i, Dying_modelnums) i = PHYSFSX_readInt(fp); range_for (auto &i, Dead_modelnums) i = PHYSFSX_readInt(fp); bitmap_index_read_n(fp, ObjBitmaps); range_for (auto &i, ObjBitmapPtrs) i = PHYSFSX_readShort(fp); player_ship_read(&only_player_ship, fp); Num_cockpits = PHYSFSX_readInt(fp); bitmap_index_read_n(fp, cockpit_bitmap); PHYSFS_read(fp, Sounds, sizeof(Sounds[0]), Sounds.size()); PHYSFS_read(fp, AltSounds, sizeof(AltSounds[0]), AltSounds.size()); Num_total_object_types = PHYSFSX_readInt(fp); PHYSFS_read( fp, ObjType, sizeof(ubyte), MAX_OBJTYPE ); PHYSFS_read( fp, ObjId, sizeof(ubyte), MAX_OBJTYPE ); range_for (auto &i, ObjStrength) i = PHYSFSX_readFix(fp); First_multi_bitmap_num = PHYSFSX_readInt(fp); Reactors[0].n_guns = PHYSFSX_readInt(fp); range_for (auto &i, Reactors[0].gun_points) PHYSFSX_readVector(fp, i); range_for (auto &i, Reactors[0].gun_dirs) PHYSFSX_readVector(fp, i); exit_modelnum = PHYSFSX_readInt(fp); destroyed_exit_modelnum = PHYSFSX_readInt(fp); #if DXX_USE_EDITOR //Build tmaplist auto &&effect_range = partial_const_range(Effects, Num_effects); Num_tmaps = TextureEffects + std::count_if(effect_range.begin(), effect_range.end(), [](const eclip &e) { return e.changing_wall_texture >= 0; }); #endif }
float abserr(const array& predicted, const array& target) { return 100 * sum<float>(abs(predicted - target)) / predicted.elements(); }
//type==1 means 1.1, type==2 means 1.2 (with weapons) void bm_read_extra_robots(const char *fname, Mission::descent_version_type type) { int t,version; auto fp = PHYSFSX_openReadBuffered(fname); if (!fp) { Error("Failed to open HAM file \"%s\"", fname); return; } if (type == Mission::descent_version_type::descent2z) { int sig; sig = PHYSFSX_readInt(fp); if (sig != MAKE_SIG('X','H','A','M')) return; version = PHYSFSX_readInt(fp); } else version = 0; (void)version; // NOTE: we do not need it, but keep it for possible further use bm_free_extra_models(); bm_free_extra_objbitmaps(); //read extra weapons t = PHYSFSX_readInt(fp); N_weapon_types = N_D2_WEAPON_TYPES+t; weapon_info_read_n(Weapon_info, N_weapon_types, fp, 3, N_D2_WEAPON_TYPES); //now read robot info t = PHYSFSX_readInt(fp); N_robot_types = N_D2_ROBOT_TYPES+t; if (N_robot_types >= MAX_ROBOT_TYPES) Error("Too many robots (%d) in <%s>. Max is %d.",t,fname,MAX_ROBOT_TYPES-N_D2_ROBOT_TYPES); range_for (auto &r, partial_range(Robot_info, N_D2_ROBOT_TYPES.value, N_robot_types)) robot_info_read(fp, r); t = PHYSFSX_readInt(fp); N_robot_joints = N_D2_ROBOT_JOINTS+t; if (N_robot_joints >= MAX_ROBOT_JOINTS) Error("Too many robot joints (%d) in <%s>. Max is %d.",t,fname,MAX_ROBOT_JOINTS-N_D2_ROBOT_JOINTS); range_for (auto &r, partial_range(Robot_joints, N_D2_ROBOT_JOINTS.value, N_robot_joints)) jointpos_read(fp, r); unsigned u = PHYSFSX_readInt(fp); N_polygon_models = N_D2_POLYGON_MODELS+u; if (N_polygon_models >= MAX_POLYGON_MODELS) Error("Too many polygon models (%d) in <%s>. Max is %d.",u,fname,MAX_POLYGON_MODELS-N_D2_POLYGON_MODELS); { const auto &&r = partial_range(Polygon_models, N_D2_POLYGON_MODELS.value, N_polygon_models); range_for (auto &p, r) polymodel_read(&p, fp); range_for (auto &p, r) polygon_model_data_read(&p, fp); } range_for (auto &i, partial_range(Dying_modelnums, N_D2_POLYGON_MODELS.value, N_polygon_models)) i = PHYSFSX_readInt(fp); range_for (auto &i, partial_range(Dead_modelnums, N_D2_POLYGON_MODELS.value, N_polygon_models)) i = PHYSFSX_readInt(fp); t = PHYSFSX_readInt(fp); if (N_D2_OBJBITMAPS+t >= ObjBitmaps.size()) Error("Too many object bitmaps (%d) in <%s>. Max is %" DXX_PRI_size_type ".", t, fname, ObjBitmaps.size() - N_D2_OBJBITMAPS); bitmap_index_read_n(fp, partial_range(ObjBitmaps, N_D2_OBJBITMAPS.value, N_D2_OBJBITMAPS + t)); t = PHYSFSX_readInt(fp); if (N_D2_OBJBITMAPPTRS+t >= ObjBitmapPtrs.size()) Error("Too many object bitmap pointers (%d) in <%s>. Max is %" DXX_PRI_size_type ".", t, fname, ObjBitmapPtrs.size() - N_D2_OBJBITMAPPTRS); range_for (auto &i, partial_range(ObjBitmapPtrs, N_D2_OBJBITMAPPTRS.value, N_D2_OBJBITMAPPTRS + t)) i = PHYSFSX_readShort(fp); }
bool operator< (const array<T,N>& x, const array<T,N>& y) { return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end()); }
void load_robot_replacements(const d_fname &level_name) { int t,i,j; char ifile_name[FILENAME_LEN]; change_filename_extension(ifile_name, level_name, ".HXM" ); auto fp = PHYSFSX_openReadBuffered(ifile_name); if (!fp) //no robot replacement file return; t = PHYSFSX_readInt(fp); //read id "HXM!" if (t!= 0x21584d48) Error("ID of HXM! file incorrect"); t = PHYSFSX_readInt(fp); //read version if (t<1) Error("HXM! version too old (%d)",t); t = PHYSFSX_readInt(fp); //read number of robots for (j=0;j<t;j++) { i = PHYSFSX_readInt(fp); //read robot number if (i<0 || i>=N_robot_types) Error("Robots number (%d) out of range in (%s). Range = [0..%d].",i,static_cast<const char *>(level_name),N_robot_types-1); robot_info_read(fp, Robot_info[i]); } t = PHYSFSX_readInt(fp); //read number of joints for (j=0;j<t;j++) { i = PHYSFSX_readInt(fp); //read joint number if (i<0 || i>=N_robot_joints) Error("Robots joint (%d) out of range in (%s). Range = [0..%d].",i,static_cast<const char *>(level_name),N_robot_joints-1); jointpos_read(fp, Robot_joints[i]); } t = PHYSFSX_readInt(fp); //read number of polygon models for (j=0;j<t;j++) { i = PHYSFSX_readInt(fp); //read model number if (i<0 || i>=N_polygon_models) Error("Polygon model (%d) out of range in (%s). Range = [0..%d].",i,static_cast<const char *>(level_name),N_polygon_models-1); free_model(Polygon_models[i]); polymodel_read(&Polygon_models[i], fp); polygon_model_data_read(&Polygon_models[i], fp); Dying_modelnums[i] = PHYSFSX_readInt(fp); Dead_modelnums[i] = PHYSFSX_readInt(fp); } t = PHYSFSX_readInt(fp); //read number of objbitmaps for (j=0;j<t;j++) { i = PHYSFSX_readInt(fp); //read objbitmap number if (i < 0 || i >= ObjBitmaps.size()) Error("Object bitmap number (%d) out of range in (%s). Range = [0..%" DXX_PRI_size_type "].", i, static_cast<const char *>(level_name), ObjBitmaps.size() - 1); bitmap_index_read(fp, ObjBitmaps[i]); } t = PHYSFSX_readInt(fp); //read number of objbitmapptrs for (j=0;j<t;j++) { i = PHYSFSX_readInt(fp); //read objbitmapptr number if (i < 0 || i >= ObjBitmapPtrs.size()) Error("Object bitmap pointer (%d) out of range in (%s). Range = [0..%" DXX_PRI_size_type "].", i, static_cast<const char *>(level_name), ObjBitmapPtrs.size() - 1); ObjBitmapPtrs[i] = PHYSFSX_readShort(fp); } Robot_replacements_loaded = 1; }
std::size_t hash_value(const array<T,N>& arr) { return boost::hash_range(arr.begin(), arr.end()); }
bool operator!=(const array<T, N>& left, const array<U, N>& right) { return !equal(left.begin(), left.end(), right.begin()); }
void stack_push_back_ref( array<as_value> & stack, T & value ) { stack.push_back( value ); }
bool operator<(const array<T, M>& left, const array<U, N>& right) { return lexicographical_compare(left.begin(), left.end(), right.begin(), right.end()); }
constexpr bool operator==(array<T, M> a, array<U, N> b) { return M == N && detail::equal(a.begin(), a.end(), b.begin(), b.end()); }
array(const array& that) { std::uninitialized_copy(that.begin(), that.end(), begin()); }
void Pass::add(PassType type, array<Pass>& passes) { for(size_t i = 0; i < passes.size(); i++) if(passes[i].type == type) return; Pass pass; pass.type = type; pass.filter = true; pass.exposure = false; pass.divide_type = PASS_NONE; switch(type) { case PASS_NONE: pass.components = 0; break; case PASS_COMBINED: pass.components = 4; pass.exposure = true; break; case PASS_DEPTH: pass.components = 1; pass.filter = false; break; case PASS_MIST: pass.components = 1; break; case PASS_NORMAL: pass.components = 4; break; case PASS_UV: pass.components = 4; break; case PASS_MOTION: pass.components = 4; pass.divide_type = PASS_MOTION_WEIGHT; break; case PASS_MOTION_WEIGHT: pass.components = 1; break; case PASS_OBJECT_ID: case PASS_MATERIAL_ID: pass.components = 1; pass.filter = false; break; case PASS_EMISSION: case PASS_BACKGROUND: pass.components = 4; pass.exposure = true; break; case PASS_AO: pass.components = 4; break; case PASS_SHADOW: pass.components = 4; pass.exposure = false; break; case PASS_LIGHT: /* This isn't a real pass, used by baking to see whether * light data is needed or not. * * Set components to 0 so pass sort below happens in a * determined way. */ pass.components = 0; break; #ifdef WITH_CYCLES_DEBUG case PASS_BVH_TRAVERSED_NODES: case PASS_BVH_TRAVERSED_INSTANCES: case PASS_BVH_INTERSECTIONS: case PASS_RAY_BOUNCES: pass.components = 1; pass.exposure = false; break; #endif case PASS_RENDER_TIME: /* This pass is handled entirely on the host side. */ pass.components = 0; break; case PASS_DIFFUSE_COLOR: case PASS_GLOSSY_COLOR: case PASS_TRANSMISSION_COLOR: case PASS_SUBSURFACE_COLOR: pass.components = 4; break; case PASS_DIFFUSE_DIRECT: case PASS_DIFFUSE_INDIRECT: pass.components = 4; pass.exposure = true; pass.divide_type = PASS_DIFFUSE_COLOR; break; case PASS_GLOSSY_DIRECT: case PASS_GLOSSY_INDIRECT: pass.components = 4; pass.exposure = true; pass.divide_type = PASS_GLOSSY_COLOR; break; case PASS_TRANSMISSION_DIRECT: case PASS_TRANSMISSION_INDIRECT: pass.components = 4; pass.exposure = true; pass.divide_type = PASS_TRANSMISSION_COLOR; break; case PASS_SUBSURFACE_DIRECT: case PASS_SUBSURFACE_INDIRECT: pass.components = 4; pass.exposure = true; pass.divide_type = PASS_SUBSURFACE_COLOR; break; case PASS_VOLUME_DIRECT: case PASS_VOLUME_INDIRECT: pass.components = 4; pass.exposure = true; break; default: assert(false); break; } passes.push_back_slow(pass); /* order from by components, to ensure alignment so passes with size 4 * come first and then passes with size 1 */ sort(&passes[0], &passes[0] + passes.size(), compare_pass_order); if(pass.divide_type != PASS_NONE) Pass::add(pass.divide_type, passes); }
int main() { // Opening video and testing integrity ---------------------------/ VideoCapture capture(FILENAME); if (!capture.isOpened()) { cerr << "Nao conseguiu abrir o video.\n"; return -1; } //----------------------------------------------------------------/ for (int i = 0; i < samples.size(); i++) { samples[i] = Mat(Size(WIDTH, HEIGHT), CV_8UC3); } while (cont_n < SAMPLES) { bool bSuccess = capture.read(src); if (!bSuccess) { cout << "ERROR: could not read frame from file..." << endl; break; } img_8u3c = resizeFixed(src); if (cont_t % INTERVAL == 0) { for (int j = 0; j < HEIGHT; j++) { for (int i = 0; i < WIDTH; i++) { samples[cont_n].at<Vec3b>(j, i) = img_8u3c.at<Vec3b>(j, i); } } cont_n++; } cont_t++; } for (int j = 0; j < HEIGHT; j++) { for (int i = 0; i < WIDTH; i++) { for (int c = 0; c < SAMPLES; c++) { pixel_list[c] = samples[c].at<Vec3b>(j, i); } sort(begin(pixel_list), end(pixel_list), Compare_Vec3f); bg_8u3c.at<Vec3b>(j, i) = pixel_list[SAMPLES / 2 + 1]; } } // At this point we have an aproximation of the background // This is used to start the samples in the Vibe algorithm // imshow("Bg gerado", bg_8u3c); cvtColor(bg_8u3c, bg_lab_8u3c, CV_BGR2Lab); // Background gradients will be used in the texture patch test bg_8u3c.convertTo(bg_32fc3, CV_32FC3, 1 / 255.0); calcGradients(bg_32fc3, bg_dx_32f, bg_dy_32f, bg_mag_32f, bg_ori_32f); initBackground(bg_32fc3); Mat vibe_mask(Size(WIDTH, HEIGHT), CV_8U); Mat vibe_filtered(Size(WIDTH, HEIGHT), CV_8U); // Start of the real-time analysis (after initial bg generation) --------------------------/ while (true) { bool bSuccess = capture.read(src); if (!bSuccess) { cout << "ERROR: could not read frame from file..." << endl; return -1; } img_8u3c = resizeFixed(src); // In this point we have the Mat image holding // a smaller version of the actual frame. cvtColor(img_8u3c, img_lab_8u3c, CV_BGR2Lab); img_8u3c.convertTo(img_32fc3, CV_32FC3, 1 / 255.0); // Bloco para gerar mascara - usado no lugar do Vibe para debug -----------------------/ if (!VIBE) { Mat img_8u_gray, bg_8u_gray; cvtColor(img_8u3c, img_8u_gray, CV_BGR2GRAY); cvtColor(bg_8u3c, bg_8u_gray, CV_BGR2GRAY); absdiff(img_8u_gray, bg_8u_gray, diff); threshold(diff, mask_8u, 50, 10, CV_8U); morphologyEx(mask_8u, mask_8u, MORPH_CLOSE, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); //GaussianBlur(mask_8u, mask_8u, Size(3, 3), 0); filtered_mask_8u = media_binary(mask_8u, 3, 10); } else { // Vibe -------------------------------------------------------------------------------/ vibe(img_32fc3, vibe_mask); morphologyEx(vibe_mask, vibe_mask, MORPH_CLOSE, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); filtered_mask_8u = media_binary(vibe_mask, 3, 10); } // ------------------------------------------------------------------------------------/ //findConnectedComponents(filtered_mask_8u, components); //components.clear(); findConnectedComponents(filtered_mask_8u, components); // For all connected components: for (int i = 0; i < components.size(); i++) { Rect roi = components[i]; calcGradients(Mat(img_32fc3, roi), Mat(img_dx_32f, roi), Mat(img_dy_32f, roi), Mat(img_mag_32f, roi), Mat(img_ori_32f, roi)); /*imshow("ori_bg", bg_ori_32f); imshow("ori_fg", img_ori_32f); Mat diff_ori_ffs; absdiff(img_ori_32f, bg_ori_32f, diff_ori_ffs); threshold(diff_ori_ffs, diff_ori_ffs, 0.75, 255, CV_8U); imshow("ori_diff", Mat(diff_ori_ffs, roi)); waitKey(1000000);*/ // Detect shadows detectShadows(Mat(img_8u3c, roi), Mat(img_lab_8u3c, roi), Mat(bg_lab_8u3c, roi), Mat(filtered_mask_8u, roi), Mat(img_dx_32f, roi), Mat(img_dy_32f, roi), Mat(img_mag_32f, roi), Mat(img_ori_32f, roi), Mat(bg_dx_32f, roi), Mat(bg_dy_32f, roi), Mat(bg_mag_32f, roi), Mat(img_ori_32f, roi), roi); } components.clear(); //imshow("mask", filtered_mask_8u); //imshow("vibe", vibe_filtered); //imshow("img", img_8u3c); imshow("final", img_8u3c); waitKey(10000000); switch (waitKey(1)) { case ESC_KEY: return 0; } } capture.release(); return 0; }