/** * clip this view so that outside of the visible bounds can be hidden. */ void ScrollView::onBeforeDraw() { if (_clippingToBounds) { _scissorRestored = false; Rect frame = getViewRect(); auto glview = Director::getInstance()->getOpenGLView(); if (glview->getVR() == nullptr) { if (glview->isScissorEnabled()) { _scissorRestored = true; _parentScissorRect = glview->getScissorRect(); //set the intersection of _parentScissorRect and frame as the new scissor rect if (frame.intersectsRect(_parentScissorRect)) { float x = MAX(frame.origin.x, _parentScissorRect.origin.x); float y = MAX(frame.origin.y, _parentScissorRect.origin.y); float xx = MIN(frame.origin.x + frame.size.width, _parentScissorRect.origin.x + _parentScissorRect.size.width); float yy = MIN(frame.origin.y + frame.size.height, _parentScissorRect.origin.y + _parentScissorRect.size.height); glview->setScissorInPoints(x, y, xx - x, yy - y); } } else { glEnable(GL_SCISSOR_TEST); glview->setScissorInPoints(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height); } } } }
VRTest1::VRTest1() { auto size = Director::getInstance()->getVisibleSize(); auto image = Sprite::create("Images/background.png"); image->setPosition(size/2); addChild(image); auto button = MenuItemFont::create("Enable / Disable VR", [](Ref* ref){ auto glview = Director::getInstance()->getOpenGLView(); auto vrimpl = glview->getVR(); if (vrimpl) { glview->setVR(nullptr); } else { auto genericvr = new VRGenericRenderer; glview->setVR(genericvr); } }); button->setFontSizeObj(16); auto menu = Menu::create(button, nullptr); addChild(menu); menu->setPosition(size / 6 ); }
/** * retract what's done in beforeDraw so that there's no side effect to * other nodes. */ void ScrollView::onAfterDraw() { if (_clippingToBounds) { auto glview = Director::getInstance()->getOpenGLView(); if (glview->getVR() == nullptr) { if (_scissorRestored) {//restore the parent's scissor rect glview->setScissorInPoints(_parentScissorRect.origin.x, _parentScissorRect.origin.y, _parentScissorRect.size.width, _parentScissorRect.size.height); } else { glDisable(GL_SCISSOR_TEST); } } } }
AppTemplate::AppTemplate(const Mat* frame_set, const Rect iniWin,int ID) :ID(ID)//bgr,hsv,lab { //get roi out of frame set Rect body_win=scaleWin(iniWin,1/TRACKING_TO_BODYSIZE_RATIO); Rect roi_win(body_win.x-body_win.width,body_win.y-body_win.width,3*body_win.width,2*body_win.width+body_win.height); body_win= body_win&Rect(0,0,frame_set[0].cols,frame_set[0].rows); roi_win=roi_win&Rect(0,0,frame_set[0].cols,frame_set[0].rows); Mat roi_set[]={Mat(frame_set[0],roi_win),Mat(frame_set[1],roi_win),Mat(frame_set[2],roi_win)}; Rect iniWin_roi=iniWin-Point(roi_win.x,roi_win.y); //scores for each channel list<ChannelScore> channel_score; Mat mask_roi(roi_set[0].rows,roi_set[0].cols,CV_8UC1,Scalar(0)); rectangle(mask_roi,iniWin_roi,Scalar(255),-1); Mat inv_mask_roi(roi_set[0].rows,roi_set[0].cols,CV_8UC1,Scalar(255)); rectangle(inv_mask_roi,body_win-Point(roi_win.x,roi_win.y),Scalar(0),-1); //calculate score for each channel Mat temp_hist; Mat temp_bp; int hist_size[]={BIN_NUMBER}; for (int i=0;i<9;i++) { float range1[]={0,255}; if (i==3) { range1[1]=179; } const float* hist_range[]={range1}; calcHist(roi_set,3,&i,inv_mask_roi,temp_hist,1,hist_size,hist_range); normalize(temp_hist,temp_hist,255,0.0,NORM_L1);//scale to 255 for display calcBackProject(roi_set,3,&i,temp_hist,temp_bp,hist_range); int c[]={0}; int hs[]={BIN_NUMBER}; float hr[]={0,255}; const float* hrr[]={hr}; Mat hist_fore; Mat hist_back; calcHist(&temp_bp,1,c,mask_roi,hist_fore,1,hs,hrr); calcHist(&temp_bp,1,c,inv_mask_roi,hist_back,1,hs,hrr); normalize(hist_fore,hist_fore,1.0,0.0,NORM_L1); normalize(hist_back,hist_back,1.0,0.0,NORM_L1); //deal with gray image to get rid of #IND double score=getVR(hist_back,hist_fore); score=score==score ? score:0; channel_score.push_back(ChannelScore(i,score)); } //choose the 2 highest scored channels channel_score.sort(compareChannel); channels[0]=channel_score.back().idx; channel_score.pop_back(); channels[1]=channel_score.back().idx; //using 2 best channel to calculate histogram for (int i=0;i<2;++i) { _hRang[i][0]=0; if (channels[i]==3) _hRang[i][1]=179; else _hRang[i][1]=255; hRange[i]=_hRang[i]; } calcHist(roi_set,3,channels,inv_mask_roi,temp_hist,2,hSize,hRange); normalize(temp_hist,temp_hist,255,0,NORM_L1); Mat final_mask;//mask for sampling calcBackProject(roi_set,3,channels,temp_hist,final_mask,hRange); threshold(final_mask,final_mask,5,255,CV_THRESH_BINARY_INV); final_mask=min(final_mask,mask_roi); //choose the best two feature space for foreground**************** Mat hist_fore,hist_back; channel_score.clear(); double sum_score=0; for (int i=0;i<9;i++) { float range1[]={0,255}; if (i==3) { range1[1]=179; } const float* hist_range[]={range1}; Mat temp_hist_neg; calcHist(roi_set,3,&i,final_mask,temp_hist,1,hist_size,hist_range); normalize(temp_hist,temp_hist,255,0,NORM_L1); calcHist(roi_set,3,&i,inv_mask_roi,temp_hist_neg,1,hist_size,hist_range); normalize(temp_hist_neg,temp_hist_neg,255,0,NORM_L1); log(temp_hist,temp_hist); log(temp_hist_neg,temp_hist_neg); temp_hist=temp_hist-temp_hist_neg; threshold(temp_hist,temp_hist,0,255,CV_THRESH_TOZERO); normalize(temp_hist,temp_hist,255,0.0,NORM_L1);//scale to 255 for display calcBackProject(roi_set,3,&i,temp_hist,temp_bp,hist_range); int c[]={0}; int hs[]={BIN_NUMBER}; float hr[]={0,255}; const float* hrr[]={hr}; calcHist(&temp_bp,1,c,final_mask,hist_fore,1,hs,hrr); calcHist(&temp_bp,1,c,inv_mask_roi,hist_back,1,hs,hrr); normalize(hist_fore,hist_fore,1.0,0.0,NORM_L1); normalize(hist_back,hist_back,1.0,0.0,NORM_L1); double score=getVR(hist_back,hist_fore); score=score==score ? score:0; channel_score.push_back(ChannelScore(i,score)); sum_score+=exp(score); } channel_score.sort(compareChannel); channels[0]=channel_score.back().idx; channel_score.pop_back(); channels[1]=channel_score.back().idx; for (int i=0;i<2;++i) { _hRang[i][0]=0; if (channels[i]==3) _hRang[i][1]=179; else _hRang[i][1]=255; hRange[i]=_hRang[i]; } calcHist(roi_set,3,channels,final_mask,hist,2,hSize,hRange);/////////////////// normalize(hist,hist,255,0,NORM_L1); //recover the shift_vector Mat backPro; calcBackProject(roi_set,3,channels,hist,backPro,hRange); iniWin_roi=iniWin-Point(roi_win.x,roi_win.y); Point2f origin_point_roi((float)(iniWin_roi.x+0.5*iniWin_roi.width),(float)(iniWin_roi.y+0.5*iniWin_roi.height)); meanShift(backPro,iniWin_roi,TermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 )); Point2f shift_point_roi((float)(iniWin_roi.x+0.5*iniWin_roi.width),(float)(iniWin_roi.y+0.5*iniWin_roi.height)); shift_vector=(shift_point_roi-origin_point_roi)*(1/(float)iniWin.width); }