/* Median filter that only modifies a pixel when it significantly differs from the median of its 8-connected neighborhood that is weighted by the variance @param img: input image @param imgPara: struct with parameters @param original: current height map @param filterRadius: radius to define the 8-connected neighborhood @param threshold: threshold to identify pixels that need to be modified */ Array * LocalMedianFilter_InclVar(RasterizedImage & img, ParaSet & imgPara, Array * original, int filterRadius, double threshold){ uint32 *orig = AUINT32(original); Array *median = Make_Array_With_Shape(PLAIN_KIND, UINT32_TYPE, Coord2(original->dims[1], original->dims[0])); uint32 *med = AUINT32(median); Use_Extend_Boundary(); Frame * f = Make_Frame(original,Coord2(2*filterRadius+1,2*filterRadius+1),Coord2(filterRadius, filterRadius)); // Frame to compute the variance in intensity of the current grid element Frame * varF = Make_Frame(img.GetImage(),Coord3(1, imgPara.radius,imgPara.radius),Coord3(0, 0, 0)); Histogram * varH = Make_Histogram(UVAL,256,ValU(1),ValU(0)); uint32 * data; Place_Frame(f,0); Place_Frame(varF, 0); for (Dimn_Type y = 0; y <= img.GetHeight()-imgPara.radius; y += imgPara.radius){ for (Dimn_Type x = 0; x <= img.GetWidth()-imgPara.radius; x += imgPara.radius){ Indx_Type pHM = (y/imgPara.radius) * original->dims[0] + (x/imgPara.radius); Place_Frame(f, pHM); data = (uint32 *) Frame_Values(f); std::vector<double> depths; double sum = 0; double n = 0; for (int i = 0; i < AForm_Size(f); i++) { Indx_Type p = Coord2IdxA(img.GetImage(), Coord3(data[i],y, x)); Place_Frame(varF, p); Histagain_Array(varH,varF,0); sum += Histogram_Variance(varH) * data[i]; n += Histogram_Variance(varH); for (int j=0; j< floor(Histogram_Variance(varH)/10); j++) { // weight the current height with the variance depths.push_back(data[i]); } } double weightedMedian = VectorMedian(depths); if (abs(orig[pHM] - weightedMedian) > threshold ) { med[pHM] = floor(weightedMedian); } else { med[pHM] = orig[pHM]; } } } Kill_Histogram(varH); Kill_Frame(varF); Kill_Frame(f); return median; }
*/ REBSER *Make_Object(REBSER *parent, REBVAL *block) /* ** Create an object from a parent object and a spec block. ** The words within the resultant object are not bound. ** ***********************************************************************/ { REBSER *words; REBSER *object; PG_Reb_Stats->Objects++; if (!block || IS_END(block)) { object = parent ? Copy_Block_Values(parent, 0, SERIES_TAIL(parent), TS_CLONE) : Make_Frame(0); } else { words = Collect_Frame(BIND_ONLY, parent, block); // GC safe object = Create_Frame(words, 0); // GC safe if (parent) { if (Reb_Opts->watch_obj_copy) Debug_Fmt(BOOT_STR(RS_WATCH, 2), SERIES_TAIL(parent) - 1, FRM_WORD_SERIES(object)); // Copy parent values and deep copy blocks and strings: COPY_VALUES(FRM_VALUES(parent)+1, FRM_VALUES(object)+1, SERIES_TAIL(parent) - 1); Copy_Deep_Values(object, 1, SERIES_TAIL(object), TS_CLONE); } } //Dump_Frame(object); return object; }
static REBSER *Trim_Object(REBSER *obj) { REBVAL *val; REBINT cnt = 0; REBSER *nobj; REBVAL *nval; REBVAL *word; REBVAL *nwrd; word = FRM_WORDS(obj)+1; for (val = FRM_VALUES(obj)+1; NOT_END(val); val++, word++) { if (VAL_TYPE(val) > REB_NONE && !VAL_GET_OPT(word, OPTS_HIDE)) cnt++; } nobj = Make_Frame(cnt); nval = FRM_VALUES(nobj)+1; word = FRM_WORDS(obj)+1; nwrd = FRM_WORDS(nobj)+1; for (val = FRM_VALUES(obj)+1; NOT_END(val); val++, word++) { if (VAL_TYPE(val) > REB_NONE && !VAL_GET_OPT(word, OPTS_HIDE)) { *nval++ = *val; *nwrd++ = *word; } } SET_END(nval); SET_END(nwrd); SERIES_TAIL(nobj) = cnt+1; SERIES_TAIL(FRM_WORD_SERIES(nobj)) = cnt+1; return nobj; }
Array * LocalMedianFilter(RasterizedImage & img, ParaSet & imgPara, Array * original, int filterRadius, double threshold ){ uint32 *orig = AUINT32(original); Array *median = Make_Array_With_Shape(PLAIN_KIND, UINT32_TYPE, Coord2(original->dims[1], original->dims[0])); uint32 *med = AUINT32(median); Use_Extend_Boundary(); Frame * f = Make_Frame(original,Coord2(2*filterRadius+1,2*filterRadius+1),Coord2(filterRadius, filterRadius)); Histogram * h = Make_Histogram(UVAL,img.GetDepth(),ValU(1),ValU(0)); Place_Frame(f,0); for (Indx_Type p = 0; p < median->size; p++){ Empty_Histogram(h); Histagain_Array(h,f,0); h->counts[orig[p]]--; // excludes the height of p in the calculation of the median if (abs(static_cast<int>(orig[p]) - Percentile2Bin(h,.5)) > threshold) { // replaces a pixel only if the different between the pixel value and the median is greater than the threshold med[p] = Percentile2Bin(h,.5); } else { med[p] = orig[p]; } Move_Frame_Forward(f); } Kill_Histogram(h); Kill_Frame(f); if (imgPara.verbose){ std::cout << "Planes filtered" << std::endl; } return median; }
Array * LocalMedianFilter_Confidence(RasterizedImage & img, ParaSet & imgPara, Array * original, int filterRad, double threshold, Array * confidence){ uint32 *orig = AUINT32(original); Array *median = Make_Array_With_Shape(PLAIN_KIND, UINT32_TYPE, Coord2(original->dims[1], original->dims[0])); uint32 *med = AUINT32(median); Use_Extend_Boundary(); Frame * f = Make_Frame(original,Coord2(2*filterRad+1,2*filterRad+1),Coord2(filterRad, filterRad)); Frame * confF = Make_Frame(confidence,Coord2(2*filterRad+1,2*filterRad+1),Coord2(filterRad, filterRad)); Place_Frame(f,0); Place_Frame(confF, 0); uint8 * confVals; uint32 * data; for (Indx_Type p = 0; p < median->size; p++){ std::vector<double> heightVals; data = (uint32 *) Frame_Values(f); confVals = (uint8 *) Frame_Values(confF); for (int i = 0; i < AForm_Size(f); i++) { for (int j=0; j<confVals[i]; j++) { heightVals.push_back(data[i]); // weight the height value with the 'likelihood' from the confidence image } } if (heightVals.size() == 0) { med[p] = orig[p]; } else { med[p] = VectorMedian(heightVals); } Move_Frame_Forward(f); Move_Frame_Forward(confF); } Kill_Frame(f); Kill_Frame(confF); return median; }
*/ static REBSER *Init_Loop(REBVAL *spec, REBVAL *body_blk, REBSER **fram) /* ** Initialize standard for loops (copy block, make frame, bind). ** Spec: WORD or [WORD ...] ** ***********************************************************************/ { REBSER *frame; REBINT len; REBVAL *word; REBVAL *vals; REBSER *body; // For :WORD format, get the var's value: if (IS_GET_WORD(spec)) spec = Get_Var(spec); // Hand-make a FRAME (done for for speed): len = IS_BLOCK(spec) ? VAL_LEN(spec) : 1; if (len == 0) Trap_Arg(spec); frame = Make_Frame(len); SET_SELFLESS(frame); SERIES_TAIL(frame) = len+1; SERIES_TAIL(FRM_WORD_SERIES(frame)) = len+1; // Setup for loop: word = FRM_WORD(frame, 1); // skip SELF vals = BLK_SKIP(frame, 1); if (IS_BLOCK(spec)) spec = VAL_BLK_DATA(spec); // Optimally create the FOREACH frame: while (len-- > 0) { if (!IS_WORD(spec) && !IS_SET_WORD(spec)) { // Prevent inconsistent GC state: Free_Series(FRM_WORD_SERIES(frame)); Free_Series(frame); Trap_Arg(spec); } VAL_SET(word, VAL_TYPE(spec)); VAL_BIND_SYM(word) = VAL_WORD_SYM(spec); VAL_BIND_TYPESET(word) = ALL_64; word++; SET_NONE(vals); vals++; spec++; } SET_END(word); SET_END(vals); body = Clone_Block_Value(body_blk); Bind_Block(frame, BLK_HEAD(body), BIND_DEEP); *fram = frame; return body; }
*/ static REBSER *Init_Loop(const REBVAL *spec, REBVAL *body_blk, REBSER **fram) /* ** Initialize standard for loops (copy block, make frame, bind). ** Spec: WORD or [WORD ...] ** ***********************************************************************/ { REBSER *frame; REBINT len; REBVAL *word; REBVAL *vals; REBSER *body; // For :WORD format, get the var's value: if (IS_GET_WORD(spec)) spec = GET_VAR(spec); // Hand-make a FRAME (done for for speed): len = IS_BLOCK(spec) ? VAL_LEN(spec) : 1; if (len == 0) raise Error_Invalid_Arg(spec); frame = Make_Frame(len, FALSE); SERIES_TAIL(frame) = len+1; SERIES_TAIL(FRM_WORD_SERIES(frame)) = len+1; // Setup for loop: word = FRM_WORD(frame, 1); // skip SELF vals = BLK_SKIP(frame, 1); if (IS_BLOCK(spec)) spec = VAL_BLK_DATA(spec); // Optimally create the FOREACH frame: while (len-- > 0) { if (!IS_WORD(spec) && !IS_SET_WORD(spec)) { // Prevent inconsistent GC state: Free_Series(FRM_WORD_SERIES(frame)); Free_Series(frame); raise Error_Invalid_Arg(spec); } Val_Init_Word_Typed(word, VAL_TYPE(spec), VAL_WORD_SYM(spec), ALL_64); word++; SET_NONE(vals); vals++; spec++; } SET_END(word); SET_END(vals); body = Copy_Array_At_Deep_Managed( VAL_SERIES(body_blk), VAL_INDEX(body_blk) ); Bind_Values_Deep(BLK_HEAD(body), frame); *fram = frame; return body; }
//发送心跳数据 void Send_Heart_Beat(void) { INT8U Temp[20]; INT16U Len; INT8U *sendBuf; Len = Make_Frame(Temp + FDATA, 0, (INT8U *)&Screen_Para.COM_Para.Addr, C_HEART_BEAT, 0, 0, 0, (char *)Temp); sendBuf = udp_get_buf (Len); if(sendBuf) { memcpy(sendBuf, Temp, Len); udp_send (Screen_Status.UDP_Soc, (INT8U *)&Screen_Para.Net_Para.Serv_IP, Screen_Para.Net_Para.Serv_Port, sendBuf, Len); } }
Array * MedianFilter(RasterizedImage & img, ParaSet & imgPara, Array *original, int filterRadius){ // output image Array *median = Make_Array_With_Shape(PLAIN_KIND,UINT32_TYPE,Coord2(original->dims[1], original->dims[0])); uint32 *med = AUINT32(median); uint32 *orig = AUINT32(original); #ifdef DEVELOP Array *variance = Make_Array_With_Shape(PLAIN_KIND,UINT32_TYPE,Coord2(original->dims[1], original->dims[0])); uint32 *var = AUINT32(variance); #endif Use_Reflective_Boundary(); // frames defines the local neighborhood Frame * f = Make_Frame(original,Coord2(2*filterRadius+1,2*filterRadius+1),Coord2(filterRadius,filterRadius)); Histogram * h = Make_Histogram(UVAL,img.GetDepth(),ValU(1),ValU(0)); Place_Frame(f,0); for (Indx_Type p = 0; p < median->size; p++){ Empty_Histogram(h); Histagain_Array(h,f,0); h->counts[orig[p]]--; // excludes the height of p in the calculation of the median med[p] = Percentile2Bin(h,.5); // median is given at 50th percentile #ifdef DEVELOP var[p] = 10*Histogram_Sigma(h); #endif Move_Frame_Forward(f); } Kill_Histogram(h); Kill_Frame(f); #ifdef DEVELOP ShowArray(img, imgPara, "median.tif", median); ShowArray(img, imgPara, "variance.tif", variance); Free_Array(variance); #endif if (imgPara.verbose){ std::cout << "Planes filtered" << std::endl; } return (median); }
double ComputeSmoothness(Array * heightMap) { Array * distances = Make_Array_With_Shape(PLAIN_KIND,UINT32_TYPE,Coord2(heightMap->dims[1], heightMap->dims[0])); uint32 * distVals = AUINT32(distances); uint32 * hmVals = AUINT32(heightMap); Use_Reflective_Boundary(); HeightMapRange range = GetLevelRange(heightMap); // frame defines the local neighborhood Frame * f = Make_Frame(heightMap,Coord2(3,3),Coord2(1,1)); Histogram * h = Make_Histogram(UVAL,range.maxLayer,ValU(1),ValU(0)); Place_Frame(f,0); for (Indx_Type i=0; i< heightMap->size; i++) { Empty_Histogram(h); Histagain_Array(h, f, 0); int middleBin = Value2Bin(h, ValU(hmVals[i])); // To determine the median value of the pixel's neighborhood, we exlude the current pixel i from the histogram h->counts[middleBin]--; distVals[i] = std::abs(static_cast<double>(hmVals[i]) - static_cast<double>(Percentile2Bin(h, 0.5))); } #ifdef DEVELOP Write_Image("HeightMapSmoothness", distances, DONT_PRESS); #endif Empty_Histogram(h); Histagain_Array(h, distances, 0); double meanDistance = Histogram_Mean(h); std::cout << "Smoothness:\n Mean distance: " << meanDistance << "\n Sd: " << Histogram_Sigma(h) << std::endl; Free_Histogram(h); Free_Frame(f); Free_Array(distances); return meanDistance; }
*/ REBSER *Map_To_Object(REBSER *mapser) /* ***********************************************************************/ { REBVAL *val; REBCNT cnt = 0; REBSER *frame; REBVAL *key; REBVAL *mval; // Count number of set entries: for (mval = BLK_HEAD(mapser); NOT_END(mval) && NOT_END(mval+1); mval += 2) { if (ANY_WORD(mval) && !IS_NONE(mval+1)) cnt++; } // See Make_Frame() - cannot use it directly because no Collect_Words frame = Make_Frame(cnt, TRUE); key = FRM_KEY(frame, 1); val = FRM_VALUE(frame, 1); for (mval = BLK_HEAD(mapser); NOT_END(mval) && NOT_END(mval+1); mval += 2) { if (ANY_WORD(mval) && !IS_NONE(mval+1)) { // !!! Used to leave SET_WORD typed values here... but why? // (Objects did not make use of the set-word vs. other distinctions // that function specs did.) Val_Init_Typeset( key, // all types except END or UNSET ~((FLAGIT_64(REB_END) | FLAGIT_64(REB_UNSET))), VAL_WORD_SYM(mval) ); key++; *val++ = mval[1]; } } SET_END(key); SET_END(val); FRM_KEYLIST(frame)->tail = frame->tail = cnt + 1; return frame; }
*/ REBSER *Map_To_Object(REBSER *mapser) /* ***********************************************************************/ { REBVAL *val; REBCNT cnt = 0; REBSER *frame; REBVAL *word; REBVAL *mval; // Count number of set entries: for (mval = BLK_HEAD(mapser); NOT_END(mval) && NOT_END(mval+1); mval += 2) { if (ANY_WORD(mval) && !IS_NONE(mval+1)) cnt++; } // See Make_Frame() - cannot use it directly because no Collect_Words frame = Make_Frame(cnt, TRUE); word = FRM_WORD(frame, 1); val = FRM_VALUE(frame, 1); for (mval = BLK_HEAD(mapser); NOT_END(mval) && NOT_END(mval+1); mval += 2) { if (ANY_WORD(mval) && !IS_NONE(mval+1)) { Init_Unword( word, REB_SET_WORD, VAL_WORD_SYM(mval), // all types except END or UNSET ~((TYPESET(REB_END) | TYPESET(REB_UNSET))) ); word++; *val++ = mval[1]; } } SET_END(word); SET_END(val); FRM_WORD_SERIES(frame)->tail = frame->tail = cnt + 1; return frame; }
*/ REBSER *Make_Object(REBSER *parent, REBVAL value[]) /* ** Create an object from a parent object and a spec block. ** The words within the resultant object are not bound. ** ***********************************************************************/ { REBSER *words; REBSER *object; PG_Reb_Stats->Objects++; if (!value || IS_END(value)) { if (parent) { object = Copy_Array_Core_Managed( parent, 0, SERIES_TAIL(parent), TRUE, TS_CLONE ); } else { object = Make_Frame(0, TRUE); MANAGE_FRAME(object); } } else { words = Collect_Frame(parent, &value[0], BIND_ONLY); // GC safe object = Create_Frame(words, 0); // GC safe if (parent) { if (Reb_Opts->watch_obj_copy) Debug_Fmt(cs_cast(BOOT_STR(RS_WATCH, 2)), SERIES_TAIL(parent) - 1, FRM_WORD_SERIES(object)); // Bitwise copy parent values (will have bits fixed by Clonify) memcpy( FRM_VALUES(object) + 1, FRM_VALUES(parent) + 1, (SERIES_TAIL(parent) - 1) * sizeof(REBVAL) ); // For values we copied that were blocks and strings, replace // their series components with deep copies of themselves: Clonify_Values_Len_Managed( BLK_SKIP(object, 1), SERIES_TAIL(object) - 1, TRUE, TS_CLONE ); // The *word series* might have been reused from the parent, // based on whether any words were added, or we could have gotten // a fresh one back. Force our invariant here (as the screws // tighten...) ENSURE_SERIES_MANAGED(FRM_WORD_SERIES(object)); MANAGE_SERIES(object); } else { MANAGE_FRAME(object); } assert(words == FRM_WORD_SERIES(object)); } ASSERT_SERIES_MANAGED(object); ASSERT_SERIES_MANAGED(FRM_WORD_SERIES(object)); ASSERT_FRAME(object); return object; }