Example #1
0
/*----------------------- Reorient_Volume -------------------------------*/
int Reorient_Volume()
{
  int 	i, old_h, old_w, old_i, num_volumes;
  short 	*volume;
  int         vol_step;

  /* check that we have a volume */
  if ((num_images==1)||(!load_all_images)) {
    fprintf(stderr,"Only one image found!\n");
    return;
  }

  /* define new cursors */
  XDefineCursor(theDisp,mainW,waitCursor);
  XDefineCursor(theDisp,cmdW,waitCursor);
  XFlush(theDisp);
  if (Verbose) fprintf(stderr,"Volume reorientation in progress...\n");

  /* perform the reorientation */
  num_volumes = 1;
  for(i=0; i<ndimensions-3; i++){
    num_volumes *= slider_length[i];
  }
  vol_step = Width*Height*slider_length[ndimensions-3];
  for(i=0; i<num_volumes; i++){
    fprintf(stderr,"Reorienting volume: %d\r", i);
    volume = &(short_Image[i*vol_step]);
    _Reorient_Volume(volume, slider_length[ndimensions-3], 
		     Width, Height);
  }

  /* resize the color bar */
  Resize_ColorBar(color_bar_width,slider_length[ndimensions-3]);

  /* swap the width, height, and num_images variables */
  old_h=Height;
  old_w=Width;
  old_i=slider_length[ndimensions-3];
  zHeight=Height=old_i;
  zWidth=Width=old_h;
  slider_length[ndimensions-3]=old_w;
  num_images=old_w*num_volumes;
  theImage->width = theImage->bytes_per_line = zWidth;
  theImage->height = zHeight;

  /* Scale Image and Resize if necessary */
  Scale_Image(P_Min,P_Max);
  image_number=num_images/2;
  Resize(Width,Height);
  XResizeWindow(theDisp,mainW,
		Width+(color_bar?color_bar_width:0),
		Height+info_height);

  /* redraw image and clean up */
  DrawWindow(0,0,Width,Height);
  update_msgs();
  update_sliders();

  /* don't bother attempting to keep track of rotations */
  RW_valid = False;

  /* define new cursors */
  XDefineCursor(theDisp,mainW,mainCursor);
  XDefineCursor(theDisp,cmdW,cmdCursor);
  XFlush(theDisp);
}
inline
void
opt_feat::feature_video( std::string one_video, Struct_feat_lab &my_Struct_feat_lab )
{



    my_Struct_feat_lab.features_video_i.clear();
    my_Struct_feat_lab.labels_video_i.clear();

    int new_row = row;
    int new_col = col;

    cv::VideoCapture capVideo(one_video);
    int n_frames = capVideo.get(CV_CAP_PROP_FRAME_COUNT);


    if( !capVideo.isOpened() )
    {
        cout << "Video couldn't be opened" << endl;
        return;
    }

    cv::Mat prevgray, gray, flow, cflow, frame, prevflow;
    cv::Mat ixMat, iyMat, ixxMat, iyyMat;
    cv::Mat flow_xy[2], mag, ang;


    string text;


    for(int fr=0; fr<n_frames; fr++) {

        //cout << fr << " " ;

        bool bSuccess = capVideo.read(frame); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
            cout << "Cannot read the frame from video file" << endl;
            break;
        }


        if (shift!=0)
        {
            int shif_x = floor(col*shift/100);
            int shif_y = floor(row*shift/100);

            if (flag_shift) //horizontal shift
            {
                //cout << "Shift_Image_Horizontal" << endl;
                frame = Shift_Image_Horizontal( frame, shif_x);
            }

            if (!flag_shift)//vertical shift
            {
                frame = Shift_Image_Vertical( frame, shif_y);
            }

            //frame = Shift_Image( frame, shif_x, shif_y); //Shifting both

        }


        if (scale_factor!=1)
        {
//       new_row = row*scale_factor;
//       new_col = col*scale_factor;
//       cv::resize( frame, frame, cv::Size(new_row, new_col) );
//
            frame =  Scale_Image( frame,  scale_factor);
        }






        cv::cvtColor(frame, gray, CV_BGR2GRAY);



        if( prevgray.data )
        {
            //cout << t << " " ;
            cv::calcOpticalFlowFarneback(prevgray,
                                         gray,
                                         flow,
                                         0.5, //pyr_scale
                                         3,   //levels
                                         9,   //winsize
                                         1,   //iterations
                                         5,   //poly_n
                                         1.1, //poly_sigma
                                         0);  //flags


            //optical flow
            cv::split(flow, flow_xy);
            cv::cartToPolar(flow_xy[0], flow_xy[1], mag, ang, true);

            cv::Sobel(gray, ixMat, CV_32F, 1, 0, 1);
            cv::Sobel(gray, iyMat, CV_32F, 0, 1, 1);
            cv::Sobel(gray, ixxMat, CV_32F, 2, 0, 1);
            cv::Sobel(gray, iyyMat, CV_32F, 0, 2, 1);

            float  ux = 0, uy = 0, vx = 0,  vy = 0;
            float u, v;

            if( prevflow.data )
            {
                //cout << new_col << " " << new_row << endl;

                for (int x = 0 ; x < new_col ; ++x ) {
                    for (int y = 0 ; y < new_row ; ++y ) {

                        vec features_one_pixel(dim);
                        u = flow.at<cv::Vec2f>(y, x)[0];
                        v = flow.at<cv::Vec2f>(y, x)[1];

                        //cout << "x= " << x << " - y= " << y << endl;
                        // x grad
                        //cout << " x y grad" << endl;
                        float ix = ixMat.at<float>(y, x);
                        //cout << " y grad" << endl;
                        float iy = iyMat.at<float>(y, x);

                        // grad direction &  grad magnitude. (Edges)
                        //cout << "grad direction &  grad magnitude" << endl;
                        float gd = std::atan2(std::abs(iy), std::abs(ix));
                        float gm = std::sqrt(ix * ix + iy * iy);

                        // x second grad
                        //cout << "x y  second grad " << endl;
                        float ixx = ixxMat.at<float>(y, x);
                        // y second grad
                        float iyy = iyyMat.at<float>(y, x);

                        //du/dt
                        float ut = u - prevflow.at<cv::Vec2f>(y, x)[0];
                        // dv/dt
                        float vt = v - prevflow.at<cv::Vec2f>(y, x)[1];



                        if (x>0 && y>0 )
                        {
                            ux = u - flow.at<cv::Vec2f>(y, x - 1)[0];
                            uy = u - flow.at<cv::Vec2f>(y - 1, x)[0];
                            vx = v - flow.at<cv::Vec2f>(y, x - 1)[1];
                            vy = v - flow.at<cv::Vec2f>(y - 1, x)[1];
                        }


                        float Div = (ux + vy);
                        float Vor = (vx - uy);
                        //Adding more features
                        mat G (2,2);
                        mat S;
                        float gd_opflow = ang.at<float>(y,x);
                        float mg_opflow = mag.at<float>(y,x);
                        //Gradient Tensor
                        G   << ux << uy << endr
                            << vx << vy << endr;

                        //Rate of Stein Tensor
                        S = 0.5*(G + G.t());

                        float tr_G = trace(G);
                        float tr_G2 = trace( square(G) );
                        float tr_S = trace(S);
                        float tr_S2 = trace(square(S));

                        //Tensor Invariants  of the optical flow
                        float Gten = 0.5*( tr_G*tr_G - tr_G2 );
                        float Sten = 0.5*( tr_S*tr_S - tr_S2 );




                        if (dim ==12)
                        {
                            features_one_pixel  << x       << y       << fr   << gm  << u    << v
                                                << abs(ut) << abs(vt) << Div  << Vor << Gten << Sten;
                        }

                        if (dim ==14)
                        {

                            features_one_pixel  << x << y << abs(ix) << abs(iy) << abs(ixx)
                                                << abs(iyy) << gm << gd <<  u << v << abs(ut)
                                                << abs(vt) << (ux + vy)  << (vx - uy);
                        }




                        if (!is_finite( features_one_pixel ) )
                        {
                            cout << "It's not FINITE... continue???" << endl;
                            getchar();
                        }
                        // Plotting Moving pixels
                        //cout << " " << gm;

                        double is_zero = accu(abs(features_one_pixel));

                        if (gm>40 && is_finite( features_one_pixel ) && is_zero!=0 ) // Empirically set to 40
                        {

                            frame.at<cv::Vec3b>(y,x)[0] = 0;
                            frame.at<cv::Vec3b>(y,x)[1] = 0;
                            frame.at<cv::Vec3b>(y,x)[2] = 255;
                            my_Struct_feat_lab.features_video_i.push_back(features_one_pixel);
                            my_Struct_feat_lab.labels_video_i.push_back( fr );
                        }
                    }
                }
            }

            if(cv::waitKey(30)>=0)
                break;

        }

        std::swap(prevgray, gray);
        std::swap(prevflow, flow);

        //cv::imshow("color", frame);
        //cv::waitKey(3);


    }


}