Tuesday 3 July 2012

Histograms and Gaussian functions

Introduction:


For my major project, I have to write a classifier that would classify the movements of an actor. I am planning to use Gaussian Mixture Models. I do not really need to the Histograms for my project, but I thought it will help me develop my skills in using Graphical User Interface and Qt libraries. For that reason I wrote a simple Histogram Generator that program takes as input a file that defines 1D values. 


An example of input file is the following: 
11.6197996 12.408 13.4334002 14.8782005 17.3017998 18.1406994 20.0049992 17.1154003 16.7425003 16.5283012 16.6030006 16.5545006 17.1154003 16.7425003 16.5283012 16.6030006 16.5545006 11.6620998 7.58859968 3.36449957 2.99299955 2.99299955 11.6620998 7.58859968 3.36449957 2.99299955 2.99299955 11.6283998 12.4166002 13.4420004 14.8868008 17.3104 18.1492996 20.0135994 17.1240005 16.7511005 16.5369015 16.6116009 16.5631008 17.1240005 16.7511005 16.5369015 16.6116009 16.5631008 11.6707001 7.59719992 3.3730998 3.00159979 3.00159979 11.6707001 7.59719992 3.3730998 3.00159979 3.00159979 11.6350002 12.4232006 13.4486008 14.8934011 17.3170013 18.155901 20.0202007 17.1306019 16.7577019 16.5435028 16.6182022 16.5697021 17.1306019 16.7577019 16.5435028 16.6182022 16.5697021 11.6773005 7.6038003 3.37970018 3.00820017 3.00820017 11.6773005 7.6038003 3.37970018 3.00820017 3.00820017 11.6842003 12.4724007 13.4978008 14.9426012 17.3662014 18.205101 20.0694008 17.1798019 16.8069019 16.5927029 16.6674023 16.6189022 17.1798019 
16.8069019 16.5927029 16.6674023 16.6189022 11.7265005 7.65300035 3.42890024 3.05740023 3.05740023 11.7265005 7.65300035 3.42890024 3.05740023 3.05740023 11.8397999 12.6280003 13.6534004 15.0982008 17.521801 18.3607006 20.2250004 17.3354015 16.9625015 16.7483025 16.8230019 16.7745018 17.3354015 16.9625015 16.7483025 16.8230019 16.7745018 11.8821001 7.80859995 3.58449984 3.21299982 3.21299982 11.8821001 7.80859995 3.58449984 3.21299982 3.21299982 11.3535004 12.1417007 13.1671009 14.6119013 17.0355015 17.8744011 19.7387009 16.849102 16.476202 16.2620029 16.3367023 16.2882023 16.849102 16.476202 16.2620029 16.3367023 16.2882023 11.3958006 7.32230043 3.09820032 2.72670031 2.72670031 11.3958006 7.32230043 3.09820032 2.72670031 2.72670031 11.4074001 12.1956005 13.2210007 14.665801 17.0894012 17.9283009 19.7926006 16.9030018 16.5301018 16.3159027 16.3906021 16.3421021 16.9030018 16.5301018 16.3159027 16.3906021 16.3421021 11.4497004 7.3762002 3.15210009 2.78060007 2.78060007 11.4497004 7.3762002 3.15210009 
2.78060007 2.78060007 11.5637999 12.3520002 13.3774004 14.8222008 17.2458 18.0846996 19.9489994 17.0594006 16.6865005 16.4723015 16.5470009 16.4985008 17.0594006 16.6865005 16.4723015 16.5470009 16.4985008 11.6061001 7.53259993 3.30849981 2.9369998 2.9369998 11.6061001 7.53259993 3.30849981 2.9369998 2.9369998 11.5896997 12.3779001 13.4033003 14.8481007 17.2716999 18.1105995 19.9748993 17.0853004 16.7124004 16.4982014 16.5729008 16.5244007 17.0853004 16.7124004 16.4982014 16.5729008 16.5244007 11.632 7.55849981 3.3343997 2.96289968 2.96289968 11.632 7.55849981 3.3343997 2.96289968 2.96289968 11.651 12.4392004 13.4646006 14.9094009 17.3330002 18.1718998 20.0361996 17.1466007 16.7737007 16.5595016 16.634201 16.585701 17.1466007 16.7737007 16.5595016 16.634201 16.585701 11.6933002 7.61980009 3.39569998 3.02419996 3.02419996 11.6933002 7.61980009 3.39569998 3.02419996 3.02419996 11.7145996 12.5028 13.5282001 14.9730005 17.3966007 18.2355003 20.0998001 17.2102013 16.8373013 16.6231022 16.6978016 16.6493015 
17.2102013 16.8373013 16.6231022 16.6978016 16.6493015 11.7568998 7.68339968 3.45929956 3.08779955 3.08779955 11.7568998 7.68339968 3.45929956 3.08779955 3.08779955 11.7909002 12.5791006 13.6045008 15.0493011 17.4729004 18.3118 20.1760998 17.2865009 16.9136009 16.6994019 16.7741013 16.7256012 17.2865009 16.9136009 16.6994019 16.7741013 16.7256012 11.8332005 7.7597003 3.53560019 3.16410017 3.16410017 11.8332005 7.7597003 3.53560019 3.16410017 3.16410017 11.8065004 12.5947008 13.620101 15.0649014 17.4885006 18.3274002 20.1917 17.3021011 16.9292011 16.7150021 16.7897015 16.7412014 17.3021011 16.9292011 16.7150021 16.7897015 16.7412014 11.8488007 7.7753005 3.55120039 3.17970037 3.17970037 11.8488007 7.7753005 3.55120039 3.17970037 3.17970037 11.8034 12.5916004 13.6170006 15.061801 17.4854012 18.3243008 20.1886005 17.2990017 16.9261017 16.7119026 16.786602 16.738102 17.2990017 16.9261017 16.7119026 16.786602 16.738102 11.8457003 7.77220011 3.54809999 3.17659998 3.17659998 11.8457003 7.77220011 3.54809999 
3.17659998 3.17659998 10.9685001 11.7567005 12.7821007 14.2269011 16.6505013 17.4894009 19.3537006 16.4641018 16.0912018 15.8770018 15.9517021 15.9032021 16.4641018 16.0912018 15.8770018 15.9517021 15.9032021 11.0108004 6.93730021 2.71320009 2.34170008 2.34170008 11.0108004 6.93730021 2.71320009 2.34170008 2.34170008 11.5951996 12.3834 13.4088001 14.8536005 17.2772007 18.1161003 19.9804001 17.0908012 16.7179012 16.5037022 16.5784016 16.5299015 17.0908012 16.7179012 16.5037022 16.5784016 16.5299015 11.6374998 7.56399965 3.33989954 2.96839952 2.96839952 11.6374998 7.56399965 3.33989954 2.96839952 2.96839952 11.5797997 12.368 13.3934002 14.8382006 17.2618008 18.1007004 19.9650002 17.0754013 16.7025013 16.4883022 16.5630016 16.5145016 17.0754013 16.7025013 16.4883022 16.5630016 16.5145016 11.6220999 7.54859972 3.32449961 2.95299959 2.95299959 11.6220999 7.54859972 3.32449961 2.95299959 2.95299959 11.5881004 12.3763008 13.401701 14.8465014 17.2701015 18.1090012 19.9733009 17.0837021 16.7108021 16.496603 
16.5713024 16.5228024 17.0837021 16.7108021 16.496603 16.5713024 16.5228024 11.6304007 7.5569005 3.33280039 2.96130037 2.96130037 11.6304007 7.5569005 3.33280039 2.96130037 2.96130037 11.4809999 12.2692003 13.2946005 14.7394009 17.1630001 18.0018997 19.8661995 16.9766006 16.6037006 16.3895016 16.464201 16.4157009 16.9766006 16.6037006 16.3895016 16.464201 16.4157009 11.5233002 7.44980001 3.2256999 2.85419989 2.85419989 11.5233002 7.44980001 3.2256999 2.85419989 2.85419989 11.5675001 12.3557005 13.3811007 14.825901 17.2495003 18.0883999 19.9526997 17.0631008 16.6902008 16.4760017 16.5507011 16.5022011 17.0631008 16.6902008 16.4760017 16.5507011 16.5022011 11.6098003 7.53630018 3.31220007 2.94070005 2.94070005 11.6098003 7.53630018 3.31220007 2.94070005 2.94070005 11.6611004 12.4493008 13.4747009 14.9195013 17.3431015 18.1820011 20.0463009 17.156702 16.783802 16.569603 16.6443024 16.5958023 17.156702 16.783802 16.569603 16.6443024 16.5958023 11.7034006 7.62990046 3.40580034 3.03430033 3.03430033 11.7034006 
7.62990046 3.40580034 3.03430033 3.03430033 11.7503004 12.5385008 13.5639009 15.0087013 17.4323006 18.2712002 20.1355 17.2459011 16.8730011 16.658802 16.7335014 16.6850014 17.2459011 16.8730011 16.658802 16.7335014 16.6850014 11.7926006 7.71910048 3.49500036 3.12350035 3.12350035 11.7926006 7.71910048 3.49500036 3.12350035 3.12350035 11.6969004 12.4851007 13.5105009 14.9553013 17.3789005 18.2178001 20.0820999 17.1925011 16.8196011 16.605402 16.6801014 16.6316013 17.1925011 16.8196011 16.605402 16.6801014 16.6316013 11.7392006 7.66570044 3.44160032 3.07010031 3.07010031 11.7392006 7.66570044 3.44160032 3.07010031 3.07010031 11.8362999 12.6245003 13.6499004 15.0947008 17.518301 18.3572006 20.2215004 17.3319016 16.9590015 16.7448025 16.8195019 16.7710018 17.3319016 16.9590015 16.7448025 16.8195019 16.7710018 11.8786001 7.80509996 3.58099985 3.20949984 3.20949984 11.8786001 7.80509996 3.58099985 3.20949984 3.20949984 11.8722 12.6604004 13.6858006 15.1306009 17.5542011 18.3931007 20.2574005 17.3678017 16.9949017 
16.7807026 16.855402 16.8069019 17.3678017 16.9949017 16.7807026 16.855402 16.8069019 11.9145002 7.84100008 3.61689997 3.24539995 3.24539995 11.9145002 7.84100008 3.61689997 3.24539995 3.24539995 11.8768997 12.6651001 13.6905003 15.1353006 17.5589008 18.3978004 20.2621002 17.3725014 16.9996014 16.7854023 16.8601017 16.8116016 17.3725014 16.9996014 16.7854023 16.8601017 16.8116016 11.9191999 7.84569979 3.62159967 3.25009966 3.25009966 11.9191999 7.84569979 3.62159967 3.25009966 3.25009966 11.7931004 12.5813007 13.6067009 15.0515013 17.4751015 18.3140011 20.1783009 17.288702 16.915802 16.7016029 16.7763023 16.7278023 17.288702 16.915802 16.7016029 16.7763023 16.7278023 11.8354006 7.76190042 3.53780031 3.1663003 3.1663003 11.8354006 7.76190042 3.53780031 3.1663003 3.1663003 11.7256002 12.5138006 13.5392008 14.9840012 17.4076004 18.2465 20.1107998 17.2212009 16.8483009 16.6341019 16.7088013 16.6603012 17.2212009 16.8483009 16.6341019 16.7088013 16.6603012 11.7679005 7.69440031 3.4703002 3.09880018 3.09880018 
11.7679005 7.69440031 3.4703002 3.09880018 3.09880018 11.8715 12.6597004 13.6851006 15.1299009 17.5535011 18.3924007 20.2567005 17.3671017 16.9942017 16.7800026 16.854702 16.8062019 17.3671017 16.9942017 16.7800026 16.854702 16.8062019 11.9138002 7.84030008 3.61619997 3.24469995 3.24469995 11.9138002 7.84030008 3.61619997 3.24469995 3.24469995 11.8140001 12.6022005 13.6276007 15.072401 17.4960003 18.3348999 20.1991997 17.3096008 16.9367008 16.7225018 16.7972012 16.7487011 17.3096008 16.9367008 16.7225018 16.7972012 16.7487011 11.8563004 7.7828002 3.55870008 3.18720007 3.18720007 11.8563004 7.7828002 3.55870008 3.18720007 3.18720007 11.5502005 12.3384008 13.363801 14.8086014 17.2322006 18.0711002 19.9354 17.0458012 16.6729012 16.4587021 16.5334015 16.4849014 17.0458012 16.6729012 16.4587021 16.5334015 16.4849014 11.5925007 7.51900053 3.29490042 2.9234004 2.9234004 11.5925007 7.51900053 3.29490042 2.9234004 2.9234004 11.5425997 12.3308001 13.3562002 14.8010006 17.2245998 18.0634995 19.9277992 17.0382004 
16.6653004 16.4511013 16.5258007 16.4773006 17.0382004 16.6653004 16.4511013 16.5258007 16.4773006 11.5848999 7.51139975 3.28729963 2.91579962 2.91579962 11.5848999 7.51139975 3.28729963 2.91579962 2.91579962 11.7117004 12.4999008 13.525301 14.9701014 17.3937016 18.2326012 20.0969009 17.2073021 16.8344021 16.620203 16.6949024 16.6464024 17.2073021 16.8344021 16.620203 16.6949024 16.6464024 11.7540007 7.68050051 3.45640039 3.08490038 3.08490038 11.7540007 7.68050051 3.45640039 3.08490038 3.08490038 11.7428999 12.5311003 13.5565004 15.0013008 17.4249001 18.2637997 20.1280994 17.2385006 16.8656006 16.6514015 16.7261009 16.6776009 17.2385006 16.8656006 16.6514015 16.7261009 16.6776009 11.7852001 7.71169996 3.48759985 3.11609983 3.11609983 11.7852001 7.71169996 3.48759985 3.11609983 3.11609983 11.8252001 12.6134005 13.6388006 15.083601 17.5072002 18.3460999 20.2103996 17.3208008 16.9479008 16.7337017 16.8084011 16.759901 17.3208008 16.9479008 16.7337017 16.8084011 16.759901 11.8675003 7.79400015 3.56990004 
3.19840002 3.19840002 11.8675003 7.79400015 3.56990004 3.19840002 3.19840002 11.8252001 12.6134005 13.6388006 15.083601 17.5072002 18.3460999 20.2103996 17.3208008 16.9479008 16.7337017 16.8084011 16.759901 17.3208008 16.9479008 16.7337017 16.8084011 16.759901 11.8675003 7.79400015 3.56990004 3.19840002 3.19840002 11.8675003 7.79400015 3.56990004 3.19840002 3.19840002 11.8374996 12.6257 13.6511002 15.0959005 17.5195007 18.3584003 20.2227001 17.3331013 16.9602013 16.7460022 16.8207016 16.7722015 17.3331013 16.9602013 16.7460022 16.8207016 16.7722015 11.8797998 7.80629969 3.58219957 3.21069956 3.21069956 11.8797998 7.80629969 3.58219957 3.21069956 3.21069956 11.8374996 12.6257 13.6511002 15.0959005 17.5195007 18.3584003 20.2227001 17.3331013 16.9602013 16.7460022 16.8207016 16.7722015 17.3331013 16.9602013 16.7460022 16.8207016 16.7722015 11.8797998 7.80629969 3.58219957 3.21069956 3.21069956 11.8797998 7.80629969 3.58219957 3.21069956 3.21069956 11.8374996 12.6257 13.6511002 15.0959005 17.5195007 18.3584003 
20.2227001 17.3331013 16.9602013 16.7460022 16.8207016 16.7722015 17.3331013 16.9602013 16.7460022 16.8207016 16.7722015 11.8797998 7.80629969 3.58219957 3.21069956 3.21069956 11.8797998 7.80629969 3.58219957 3.21069956 3.21069956 11.8814001 12.6696005 13.6950006 15.139801 17.5634003 18.4022999 20.2665997 17.3770008 17.0041008 16.7899017 16.8646011 16.8161011 17.3770008 17.0041008 16.7899017 16.8646011 16.8161011 11.9237003 7.85020018 3.62610006 3.25460005 3.25460005 11.9237003 7.85020018 3.62610006 3.25460005 3.25460005 11.8814001 12.6696005 13.6950006 15.139801 17.5634003 18.4022999 20.2665997 17.3770008 17.0041008 16.7899017 16.8646011 16.8161011 17.3770008 17.0041008 16.7899017 16.8646011 16.8161011 11.9237003 7.85020018 3.62610006 3.25460005 3.25460005 11.9237003 7.85020018 3.62610006 3.25460005 3.25460005 11.8814001 12.6696005 13.6950006 15.139801 17.5634003 18.4022999 20.2665997 17.3770008 17.0041008 16.7899017 16.8646011 16.8161011 17.3770008 17.0041008 16.7899017 16.8646011 16.8161011 11.9237003 
7.85020018 3.62610006 3.25460005 3.25460005 11.9237003 7.85020018 3.62610006 3.25460005 3.25460005 11.9478998 12.7361002 13.7615004 15.2063007 17.6299 18.4687996 20.3330994 17.4435005 17.0706005 16.8564014 16.9311008 16.8826008 17.4435005 17.0706005 16.8564014 16.9311008 16.8826008 11.9902 7.91669989 3.69259977 3.32109976 3.32109976 11.9902 7.91669989 3.69259977 3.32109976 3.32109976 11.9478998 12.7361002 13.7615004 15.2063007 17.6299 18.4687996 20.3330994 17.4435005 17.0706005 16.8564014 16.9311008 16.8826008 17.4435005 17.0706005 16.8564014 16.9311008 16.8826008 11.9902 7.91669989 3.69259977 3.32109976 3.32109976 11.9902 7.91669989 3.69259977 3.32109976 3.32109976 11.9478998 12.7361002 13.7615004 15.2063007 17.6299 18.4687996 20.3330994 17.4435005 17.0706005 16.8564014 16.9311008 16.8826008 17.4435005 17.0706005 16.8564014 16.9311008 16.8826008 11.9902 7.91669989 3.69259977 3.32109976 3.32109976 11.9902 7.91669989 3.69259977 3.32109976 3.32109976 12.0300999 12.8183002 13.8437004 15.2885008 17.712101 
18.5510006 20.4153004 17.5257015 17.1528015 16.9386024 17.0133018 16.9648018 17.5257015 17.1528015 16.9386024 17.0133018 16.9648018 12.0724001 7.99889994 3.77479982 3.40329981 3.40329981 12.0724001 7.99889994 3.77479982 3.40329981 3.40329981 12.0300999 12.8183002 13.8437004 15.2885008 17.712101 18.5510006 20.4153004 17.5257015 17.1528015 16.9386024 17.0133018 16.9648018 17.5257015 17.1528015 16.9386024 17.0133018 16.9648018 12.0724001 7.99889994 3.77479982 3.40329981 3.40329981 12.0724001 7.99889994 3.77479982 3.40329981 3.40329981 12.0738001 12.8620005 13.8874006 15.332201 17.7558002 18.5946999 20.4589996 17.5694008 17.1965008 16.9823017 17.0570011 17.0085011 17.5694008 17.1965008 16.9823017 17.0570011 17.0085011 12.1161003 8.04260063 3.81850052 3.4470005 3.4470005 12.1161003 8.04260063 3.81850052 3.4470005 3.4470005 12.0065002 12.7947006 13.8201008 15.2649012 17.6885014 18.527401 20.3917007 17.5021019 17.1292019 16.9150028 16.9897022 16.9412022 17.5021019 17.1292019 16.9150028 16.9897022 16.9412022 
12.0488005 7.97530031 3.7512002 3.37970018 3.37970018 12.0488005 7.97530031 3.7512002 3.37970018 3.37970018 11.9668999 12.7551003 13.7805004 15.2253008 17.648901 18.4878006 20.3521004 17.4625015 17.0896015 16.8754025 16.9501019 16.9016018 17.4625015 17.0896015 16.8754025 16.9501019 16.9016018 12.0092001 7.93569994 3.71159983 3.34009981 3.34009981 12.0092001 7.93569994 3.71159983 3.34009981 3.34009981 11.9726 12.7608004 13.7862005 15.2310009 17.6546001 18.4934998 20.3577995 17.4682007 17.0953007 16.8811016 16.955801 16.9073009 17.4682007 17.0953007 16.8811016 16.955801 16.9073009 12.0149002 7.94140005 3.71729994 3.34579992 3.34579992 12.0149002 7.94140005 3.71729994 3.34579992 3.34579992 11.9532003 12.7414007 13.7668009 15.2116013 17.6352005 18.4741001 20.3383999 17.448801 17.075901 16.861702 16.9364014 16.8879013 17.448801 17.075901 16.861702 16.9364014 16.8879013 11.9955006 7.92200041 3.6979003 3.32640028 3.32640028 11.9955006 7.92200041 3.6979003 3.32640028 3.32640028 11.9421997 12.7304001 13.7558002 
15.2006006 17.6242008 18.4631004 20.3274002 17.4378014 17.0649014 16.8507023 16.9254017 16.8769016 17.4378014 17.0649014 16.8507023 16.9254017 16.8769016 11.9844999 7.91099977 3.68689966 3.31539965 3.31539965 11.9844999 7.91099977 3.68689966 3.31539965 3.31539965 11.8737001 12.6619005 13.6873007 15.1321011 17.5557003 18.3945999 20.2588997 17.3693008 16.9964008 16.7822018 16.8569012 16.8084011 17.3693008 16.9964008 16.7822018 16.8569012 16.8084011 11.9160004 7.84250021 3.6184001 3.24690008 3.24690008 11.9160004 7.84250021 3.6184001 3.24690008 3.24690008 11.8845997 12.6728001 13.6982002 15.1430006 17.5666008 18.4055004 20.2698002 17.3802013 17.0073013 16.7931023 16.8678017 16.8193016 17.3802013 17.0073013 16.7931023 16.8678017 16.8193016 11.9268999 7.85339975 3.62929964 3.25779963 3.25779963 11.9268999 7.85339975 3.62929964 3.25779963 3.25779963 11.9438 12.7320004 13.7574005 15.2022009 17.6258011 18.4647007 20.3290005 17.4394016 17.0665016 16.8523026 16.927002 16.8785019 17.4394016 17.0665016 16.8523026 
16.927002 16.8785019 11.9861002 7.91260004 3.68849993 3.31699991 3.31699991 11.9861002 7.91260004 3.68849993 3.31699991 3.31699991 11.9460001 12.7342005 13.7596006 15.204401 17.6280003 18.4668999 20.3311996 17.4416008 17.0687008 16.8545017 16.9292011 16.8807011 17.4416008 17.0687008 16.8545017 16.9292011 16.8807011 11.9883003 7.91480017 3.69070005 3.31920004 3.31920004 11.9883003 7.91480017 3.69070005 3.31920004 3.31920004 

Gaussian Function:


The program is able to calculate the mean (E[X], μ), the standard deviation (σ) and variance (Var[X]) of the cluster. The formulas for calculating those values are the following:






Mean is the centre of gravity of the cluster and Variance defines its spread. Once those values are calculated, a Gaussian curve function (f(x)) can be generated and used to find the probability of every point x to belong to that cluster:
$$f(x) = {{1 \over { \sqrt{2π{σ^2} } }} e^{-{{(x-μ)^2} \over {2σ^2}}}}$$


Histogram:


The program is also able to create and export an image with a histogram that illustrates the frequency of each value/point. The following histogram has been generated from the above data: 

Figure 2: Histogram


Graphical User Interface:


The GUI is the following:


Figure 1: Graphical User Interface

It is worth mentioning, that the min and max values define the interval the input data belongs to. I think it is better those values to be defined by the user and I will give a simple example to support my opinion. Let's assume that we have two classes: almonds and oranges. The weight of an almond is approximately 1.2g and the weight of an orange is approximately 200g. Therefore their weigh is a good measure to build a classifier that would distinguish oranges from almonds. We weight a number of almonds and a number of oranges and save that training data in two files. To visualise the clusters of that data, we generate two histograms. If the min and max values were relevant to the measurements, then the output would have looked similar. But if the user, for example, gives the values 0-250 then the almonds cluster will appear at the beginning of the histogram and the orange cluster at the end.


The step value is used to round the numbers. For example, if
a point (P) =  1.3345
min            =  0
max           =  4
step           = 0.1
then P belongs to the 13th interval, (1.3,1.4].


Code:

The Histogram Generator is a stand alone Qt Application. 

The source code is available here: https://sourceforge.net/projects/clusteringc/

The files written are the following:

#ifndef HISTOGRAM_H
#define HISTOGRAM_H
#include 
#include 
#include 

class Histogram
{
public:
    /// @brief default constructor
    Histogram();
    /// @brief method that loads the file
    void LoadImage(const std::string &_input);
    /// @brief method used to save the image
    void saveImage(const std::string &_name)const;
    /// @brief method that calculates and returns the mean value
    float getMean();
    /// @brief method that calculated the standard deviation of the clas
    /// @brief it should always be called after the getMean() function is called
    float getStandardDeviation();
    /// @brief method that creates the histogram for the given class
    void createHistogram(float _max, float _min, const float _step);
    /// @brief default destructor
    ~Histogram();

private:
    /// @brief method that return the number of the interval the point belongs to
    /// @brief _point the current point
    /// @brief _min the minimum value that appears on the histogram
    /// @brief _step the step value
    int getInterval(const float _point, float _min, const float _step)const;
    /// @brief method that draws the Histogram
    void drawHistogram();

    /// @brief the number of points per interval
    std::vector m_numOfPoints;
    /// @brief the input values
    std::vector m_values;
    /// @brief the standard Deviation of the class
    float m_standardDeviation;
    /// @brief the mean value of all the input values
    float m_mean;
    /// @brief the histogram image
    QImage *m_histogram;
    /// @brief the width of the image
    unsigned int m_width;
    /// @brief the height of the image
    unsigned int m_height;
};

#endif // HISTOGRAM_H

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include "Histogram.h"

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    /// @brief loads the values from a file
    void loadFile();
    /// @brief it is called when the save button is pushed to save the histogram
    void saveImage();
    /// @brief it is called when the close button is pushed to close the program
    void closeProgram();
    /// @brief method that calculates and shows on the screen the standard deviation and mean
    void calculate();
    /// @brief method that sets the maximum value of the graph
    /// @param[in] _max the new value of m_max
    void setMax(const double _max);
    /// @brief method that sets the minimum value of the histogram
    void setMin(const double _min);
    /// @brief method that sets the step value of the histogram
    void setStep(const double _step);

private:
    Ui::MainWindow *m_ui;
    /// @brief the histogram
    Histogram m_histogram;
    /// @brief the name of the input file
    std::string m_input;
    /// @brief the max value
    float m_max;
    /// @brief the min value
    float m_min;
    /// @brief the step value
    float m_step;
};

#endif // MAINWINDOW_H;

#include "Histogram.h"
#include 
#include 
#include 

Histogram::Histogram():m_histogram(0)
{
    m_height = 576;
    m_width = 720;
}

Histogram::Histogram(const std::string &_input)
{
    m_height = 576;
    m_width = 720;
    LoadImage(_input);
}

void Histogram::LoadImage(const std::string &_input)
{
    std::ifstream inputFile(_input.c_str());
    if (!inputFile) {
        std::cout << "File \"" << _input << "\" not found.\n";
        exit(1);
    }
    std::istream_iterator strValues(inputFile);
    std::istream_iterator sentinel;
    std::vector lines(strValues,sentinel);
    unsigned int size = lines.size();
    m_values.resize(size);
    for(unsigned int i=0; isave(_name.c_str());
}

float Histogram::getMean()
{
    unsigned int size = m_values.size();
    float sum = 0;
    for(unsigned int i=0; i=start)
        {
            return currentInterval;
        }
        currentInterval++;
        start = end;
        end+=_step;
    }
    return -1; // the _point does not belog to the given interval
}

void Histogram::createHistogram(float _max, float _min, const float _step)
{
    if(_max<_min)
    {
        // max should have been greater than min - values of max and min will be swap
        std::swap(_max,_min);
    }
    // calculate m_width of histogram
    m_width=ceil((_max-_min)/_step);

    m_numOfPoints.resize(m_width);
    // initialisation
    for(unsigned int i=0; i= 0)
        {
           m_numOfPoints[interval]++;
        }
    }

    m_height = m_numOfPoints[0];
    for(unsigned int i=1; ifill(qRgb(255,255,255));

    unsigned int size = m_numOfPoints.size();
    for(unsigned int i=0; isetPixel(i,y,qRgb(0,0,0));
        }
    }
}

Histogram::~Histogram()
{
    if(m_histogram!=0)
    {
        delete m_histogram;
    }
};

#include "MainWindow.h"
#include "ui_MainWindow.h"
#include 

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    m_ui(new Ui::MainWindow)
{
    m_ui->setupUi(this);
    this->setWindowTitle(QString("Histogram Generator"));
    m_input = "";

    m_max = 10.0;
    m_min = -10.0;
    m_step = 1.0;

    connect(m_ui->m_pbLoad,SIGNAL(clicked()),this,SLOT(loadFile()));
    connect(m_ui->m_pbSave,SIGNAL(clicked()),this,SLOT(saveImage()));
    connect(m_ui->m_pbClose,SIGNAL(clicked()),this,SLOT(closeProgram()));
    connect(m_ui->m_pbCalculate,SIGNAL(clicked()),this,SLOT(calculate()));

    connect(m_ui->m_maxValue,SIGNAL(valueChanged(double)),this,SLOT(setMax(double)));
    connect(m_ui->m_minValue,SIGNAL(valueChanged(double)),this,SLOT(setMin(double)));
    connect(m_ui->m_step,SIGNAL(valueChanged(double)),this,SLOT(setStep(double)));
}


void MainWindow::loadFile()
{
    QString selectedFilter;
    QString fileName = QFileDialog::getOpenFileName(0, "Open", "",
    QString("All Files (*);;Text Files (*.txt)"),&selectedFilter);
    m_input = fileName.toStdString();
    m_histogram.LoadImage(m_input);
}

void MainWindow::saveImage()
{
    // create Histogram
    m_histogram.createHistogram(m_max,m_min,m_step);
    // show dialog box
    QString selectedFilter;
    QString fileName = QFileDialog::getSaveFileName(0, "Save", "",
    QString("All Files (*);;Text Files (*.txt)"),&selectedFilter);
    std::string name = fileName.toStdString();
    // save image
    m_histogram.saveImage(name);
}

void MainWindow::calculate()
{
    if (m_input!="") // check if the file is loaded
    {
        m_ui->m_mean->setText(QString::number(m_histogram.getMean()));
        float standardDeviation = m_histogram.getStandardDeviation();
        m_ui->m_standardDeviation->setText(QString::number(standardDeviation));
        m_ui->m_variance->setText(QString::number(standardDeviation*standardDeviation));
    }
    else
    {
        // print an error message
        std::cout << "Please load a file first.\n";
    }
}

void MainWindow::closeProgram()
{
    exit(1);
}

void MainWindow::setMax(const double _max)
{
    m_max = _max;
}

void MainWindow::setMin(const double _min)
{
    m_min = _min;
}

void MainWindow::setStep(const double _step)
{
    m_step = _step;
}

MainWindow::~MainWindow()
{
    delete m_ui;
};


#include 
#include "MainWindow.h"

int main(int argc, char **argv)
{
  // make an instance of the QApplication
  QApplication a(argc, argv);
  // Create a new MainWindow
  MainWindow w;
  // show it
  w.show();
  // hand control over to Qt framework
  return a.exec();
};

QT+=	gui \
        core
INCLUDEPATH+=include \
           +=ui
OBJECTS_DIR=obj/
MOC_DIR=moc/
TARGET=Histograms
CONFIG += console
CONFIG -= app_bundle
UI_HEADERS_DIR=ui
SOURCES += \
    src/main.cpp \
    src/MainWindow.cpp \
    src/Histogram.cpp
HEADERS += \
    include/MainWindow.h \
    include/Histogram.h

FORMS += \
    ui/MainWindow.ui

OTHER_FILES+= \

QMAKE_CXXFLAGS+= -msse -msse2 -msse3
macx:QMAKE_CXXFLAGS+= -arch x86_64
macx:INCLUDEPATH+=/usr/local/boost/
linux-g++:QMAKE_CXXFLAGS +=  -march=native
linux-g++-64:QMAKE_CXXFLAGS +=  -march=native
DEFINES +=NGL_DEBUG

LIBS += -L/usr/local/lib

Future Work:

I am planning to expand the program such that it will take as input 3D data and generate the covariance matrix for the input class. 


7 comments:

  1. Dear Milto,

    Using a gaussian mixture is a good idea. But you certainly know it is not the only one. Have you thought about Neuron networks, Support Vector Machine (SVM) or logistic regression ? I wonder if that works :)

    By the way, I am not sure to understand what is your input data. You work on actor movements, right ? What are those numbers ?

    ReplyDelete
    Replies
    1. Hello Alex,

      For my project, I am using a 3D Gaussian (not the simple one I explain above) and it works fine, but I still have to do a lot of testing! The reason, I choose Gaussian function is because it gives me the probability of a point to belong to a cluster. To be honest, I have heard about Neuron networks and SVM, but I have never read anything about them. Thanks for mentioning them, I will read about them. I may not use them on that project, but it is definitely worth knowing about them. =)

      The input files of my project are .bhv files. I have used the iPi Studio: http://www.ipisoft.com/downloads_index.php to get the mocap data. The good thing about it is that I used a studio for the shooting. It does worth using one during your studies.

      Then, I wrote a .bvh parser using the some parsing method as the one found here: http://sourceforge.net/projects/bioviewer/ and of course a .bhv viewer. For now I only have to count the speed of the actor in my calculation and write down my thesis.

      It is not a big project, but it is enough for 7-8 week, if you do not want to stress yourself or if you are away for a while. I did both, not stressed and being away for while. =)

      I am sure you will find a good project to work on, next year.

      Milto

      Delete
  2. 7-8 weeks are very short indeed. You do fine not choosing a too big subject, and this is interesting enough.

    "The reason, I choose Gaussian function is because it gives me the probability of a point to belong to a cluster." I guess you have more than two clusters (in that case regression is not a good idea). My question is : what ARE those clusters we are talking about. How do you classify your people ? May be the aim is to recognize people through their gestures or something like that ?

    SVM and Neuron Networks are not tough to understand, I will send you something that explains things clearly.

    I have read a bit about wang tiles. If I understand well, the goal of this method is to fill a space, right ? I am not sure how useful it is for computer animation.

    ReplyDelete
  3. "The reason, I choose Gaussian function is because it gives me the probability of a point to belong to a cluster." Every method gives you the probability if you wish. I think the good reason for using mixture is because you do unsupervised classification : you do not have groups and then ask for each individual : to what group does he belong ? You have individuals and then you build the best groups that contain them. :)

    ReplyDelete
  4. If you can send me information about SVM and Neuron Networks, you will help me a lot.

    Wang Tiles are used to construct complex signals like textures and poison Disk Distributions without repeating patterns in linear time.
    For example, let's assume that you want to create a large forest with many trees and no collision. You can use either Dart Throwing, Relaxation Dart Throwing or the Lloyds’s Relation Scheme to generate the location/points of the trees. But their time complexity grows exponentially.
    Wang Tiles allow you to generate arbitrary large sets of points in linear time, because that method uses precomputed data. Therefore it is a much faster.

    It is also be used for:

    1. Image/Texture Synthesis
    2. Anti-aliasing
    3. Non-photorealistic rendering
    4. Procedural texturing
    5. Geometry processing

    I hope this make more sense that my Wang Tiles related post. =)

    ReplyDelete
  5. By the way, I can send you my poster if you want. It is more clear than the post.

    ReplyDelete
  6. Hey. I wish I got a notice when someone comments back my posts. :)

    I got a lecture on SVM, the slides are here, but may be it is too long. If you want, I will explain the slides that are not clear.

    http://cbio.ensmp.fr/~jvert/talks/120302ensae/ensae.pdf

    What is a poster ? Ok for that if you can send it. Anyway, I understand better with your answer.

    ReplyDelete