예제 #1
0
void VideoHandler::segmentThread()
{

  m_keepSegmentThreadAlive = true;  
  m_availableSegment = false;
  m_segmentThreadFinished = false;
  
  while (m_keepSegmentThreadAlive) {
    
    {
      Glib::Threads::Mutex::Lock lock(m_SegmentMutex);
      
      while ((m_availableSegment) && (m_keepSegmentThreadAlive)){
	m_emptySegmentCond.wait(m_SegmentMutex);
      }
      if (!m_keepSegmentThreadAlive)
	continue;

      // start new Segment computation
      {
	Glib::Threads::Mutex::Lock lock(m_NextFrameMutex);
	
	while ((!m_availableNextFrame) && (m_keepSegmentThreadAlive)
	       && (!m_nextFrameThreadFinished)) {
	  m_availableNextFrameCond.wait(m_NextFrameMutex);
	}
	
	if ((!m_keepSegmentThreadAlive) || (m_nextFrameThreadFinished))
	  continue;

	if ((!colorfeature) && (difffeature) && (!knnMethod))
	  m_NextDFrame.copyTo(m_Frame);
	else
	  m_NextFrame.copyTo(m_Frame);
	
	if (colorfeature) 
	  m_NextOFrame.copyTo(m_OFrame);
	else if ((difffeature) && (!knnMethod)) // not strictly needed..
	  m_NextFrame.copyTo(m_OFrame);
	else
	  m_OFrame = m_Frame;

	  
	m_NextBWImg.copyTo(m_BWImg);
	
	m_TimeStamp = m_NextTimeStamp;
	
	m_availableNextFrame = false; 
	m_emptyNextFrameCond.signal();
      }
      
      
      if (computeSegments) {
	  
#ifdef DEBUG
	Glib::Timer timer;
	timer.start();
#endif
	
	// finding contours
	vector<vector<cv::Point> > contours;
	findFishContours(m_BWImg,&contours);
	
	
#ifdef DEBUG 
	cout << "contours: " <<  timer.elapsed() << endl;
	timer.start();
#endif
	
	Segment segm;
	vector <Segment> tmp(0);
	m_NextSegments.clear();
	m_NextSegments = tmp;

	int ii=0;
	int ssize;
	ssize = contours.size();
	if (ssize>MAXCONTOUR)
	  ssize = MAXCONTOUR;
	
	for( int i = 0; i< ssize; i++ ) {

	  getSegment(&segm,contours[i],m_BWImg,m_Frame,m_OFrame);
	      
	  if (testValid(&segm)) {
	    ii++;
	    m_NextSegments.push_back(segm);
	  }
	  if (ii>=MAXVALIDCONTOUR)
	    break;
	}
	
	
#ifdef DEBUG 
	cout << m_NextSegments.size() << endl;
	cout << "segments: " <<  timer.elapsed() << endl;  
#endif
      }
      
      // signal stuff
      m_availableSegment = true; 
      m_availableSegmentCond.signal();
    }
  }
  m_segmentThreadFinished = true;
}
예제 #2
0
void 
ViewDrawingArea::draw_buffer()
{
#ifdef DEBUG
std::cerr << "VA->val:"<<_vadj->get_value() <<" VA->upper:"<<_vadj->get_upper() << std::endl << std::flush;
#endif

	Feed *feed = AppContext::get().get_feed();
	if (feed == NULL) 
		return;

	if (AppContext::get().get_display_type() == FULL)
	{
		render_full_article();
		return;
	}

	// Dimensions of drawing area
	Gtk::Allocation allocation = get_allocation();
	const int width = allocation.get_width();
	const int height = allocation.get_height();

	// First determine start item
	Cairo::RefPtr<Cairo::Context> cr = _pixmap->create_cairo_context();

#ifdef DEBUG
	/// This code was meant to investigate the 500ms delay with pango
	/// at start of rendering.
	if (!_layout_prepared)
	{
Glib::Timer sw;
sw.start();
		Glib::RefPtr<Pango::Layout> pl = Pango::Layout::create (cr);
		const char *fonts[] = {"Sans 10", "Sans 14"};
		for (unsigned i = 0; i < sizeof(fonts)/sizeof(const char*); ++i)
		{
			Pango::FontDescription fdesc (fonts[i]);
			pl->set_font_description (fdesc);
			pl->set_width (10 * Pango::SCALE); // force line break
			pl->set_markup ("<b>Show Us Your Freaky Geek Costumes</b>\nHitting the streets in a scary, tech-themed outfit this Halloween? We want to see it. Find out how your Sergey Brin costume (or is it David Duchovny?) could be featured on Wired News. test\n test");
			Pango::Rectangle irect, lrect;
			pl->get_extents (irect, lrect);
		}
		_layout_prepared = true;
sw.stop();
unsigned long l;
sw.elapsed(l);
std::cerr << "Time spent rendering: " << l << " us" << std::endl << std::flush;
	}
#endif

	const item_list_t items = feed->get_items();
	item_list_t::const_iterator start_item;
	const int n_items = items.size();
	double y = 0.0;

	if (n_items == 0)
		start_item = items.end();
	else
	{
		// Set start_item and h
		int start = static_cast<int> (_vadj->get_value());
		double h = 0.0;
		if (_vadj->get_value() > 0 && _vadj->get_value() == _vadj->get_upper())
		{
			// This will give a blank page when pulling the handle
			// full down. While unusual, it might make sense with
			// an ever-growing newslist.
			y = 0.0;
			start_item = items.end();
		}
		else
		{
			start_item = items.begin();
			for (int i = 0; i < start; ++i)
				++start_item;
			if (start < 0)
				start = 0;

			double prop = _vadj->get_value() - start;
			if (prop > 0.0)
			{
				(*start_item)->make_display_unit();
				ItemDisplayUnit *du = (*start_item)->get_display_unit();
				du->layout (cr);
				h = du->get_height();
			}
			y = - h * prop;
#ifdef DEBUG
std::cerr << "prop:"<<prop <<" y:"<<y << std::endl << std::flush;
#endif

		}
	}

	// clip to the area indicated by the expose event so that we only redraw
	// the portion of the window that needs to be redrawn
	cr->reset_clip();
	cr->rectangle (0.0, 0.0, width, height);
	cr->clip();

	// Black on white
	cr->set_source_rgb (1.0, 1.0, 1.0);
	cr->paint();
	cr->set_source_rgb (0.0, 0.0, 0.0);

	// Render text
	item_list_t::const_iterator it = start_item;
	_top_item = *start_item;
	_topitem_y = y;
	int count = 0;
	for (; it != items.end() && y < height; ++it, ++count)
	{
		(*it)->make_display_unit();
		ItemDisplayUnit *du = (*it)->get_display_unit();
		du->render (cr, 0, y);
		y += du->get_height();
	}

	cr->show_page();

	// Scrollbar settings can now be specified
	_vadj->set_upper (n_items);
	//_vadj->set_page_size (count - 1 + );
	_vadj->set_step_increment (1);
	_vadj->set_page_increment (count);
	_vadj->changed();

}
예제 #3
0
void VideoHandler::readNextFrameThread()
{

  m_keepNextFrameThreadAlive = true;  
  m_availableNextFrame = false;
  m_nextFrameThreadFinished = false;
  
  while (m_keepNextFrameThreadAlive) {

    {
      Glib::Threads::Mutex::Lock lock(m_NextFrameMutex);
      // wait for segment thread  handling
      while ((m_availableNextFrame) && (m_keepNextFrameThreadAlive)) {
	m_emptyNextFrameCond.wait(m_NextFrameMutex);
      }
      if (!m_keepNextFrameThreadAlive)
	continue;

#ifdef DEBUG
      Glib::Timer timer;
      timer.start();
#endif

      Mat oframe;
      if (m_camera) {

	int frameNumber;
	if (pVideoSaver->getFrame(&oframe,&m_NextTimeStamp,&frameNumber)!=0) {
	  break;
	} // returns RGB
#ifdef DEBUG
	cout << oframe.size() << "  " << frameNumber << " " <<  m_NextTimeStamp << endl;
#endif
      
      } else {

	m_NextTimeStamp = pVideoCapture->get(cv::CAP_PROP_POS_MSEC)*1e-3; 
	if (!pVideoCapture->read(oframe)) { // should return RGB instead of BGR. But seems not to work.. NOW BGR
		//cout <<  "ERROR: File read error." << endl;
	  break;
	}
	//cvtColor(oframe,oframe,cv::COLOR_BGR2RGB);

#ifdef DEBUG
	cout << oframe.size() << " Time: " <<  m_NextTimeStamp << endl;
#endif

	if (oframe.type()!=CV_8UC3) {
	  cout <<  "ERROR: Expect CV_8UC3 color movie." << endl;
	  break;
	}
      }
    
      if ((resizeif) && (resizescale!=1)) {
	cv::resize(oframe,oframe,Size(0,0),resizescale,resizescale);
      }
    
      Mat frame;
    
      if (scaled) {
	cv::Mat channel[3];
	cv::Mat floatframe;
	vector<int> order(3);
	split(oframe, channel);
	for (int ii=0; ii<3; ii++) {
	  channel[ii].convertTo(channel[ii], CV_32FC1);
	  if (m_camera)
	    order[ii] = ii; // rgb
	  else
	    order[ii] = 2-ii; // bgr
	}
	floatframe =  ((float) Scale[0]/255.)*channel[order[0]] + ((float) Scale[1]/255.)*channel[order[1]] + ((float) Scale[2]/255.)*channel[order[2]] + (Delta); 
      
	// subtract mean
	//cv::Scalar globalmean = cv::mean(floatframe) ; // one channel anyway
	//floatframe -= (globalmean[0]-0.5);// 0.5 because should be between 0..1
	//floatframe.convertTo(frame, CV_8UC1,255.,(-globalmean[0]+0.5)*255); // better convert it back because of boundaries
	floatframe.convertTo(frame, CV_8UC1,255.);
      }
      else {
	if (m_camera)
	  cvtColor(oframe,frame,cv::COLOR_RGB2GRAY);
	else
	  cvtColor(oframe,frame,cv::COLOR_BGR2GRAY); // seems not to work for video capture...
      }
    
    
#ifdef DEBUG
      cout << "reading frame: " <<  timer.elapsed() << endl;  
      timer.start();
#endif
    
      // background computation (THIS IS THE SLOWEST PART!)
      if (knnMethod) {
	if (inverted) {
	  Mat iframe;
	  iframe = 255-frame;
	  pBackgroundSubtractor->apply(iframe, m_NextBWImg);
	}  else {
	  pBackgroundSubtractor->apply(frame, m_NextBWImg);
	}
      } else {
	pBackgroundThresholder->apply(frame, &m_NextBWImg, &m_NextDFrame);

      }
      
    
#ifdef DEBUG
      cout << "background sub: " <<  timer.elapsed() << endl;  
#endif

      m_NextFrame = frame;
      if (colorfeature)
	m_NextOFrame = oframe;
      else
	m_NextOFrame = frame;
    
      // thread stuff signal the end
      m_availableNextFrame = true;
      m_availableNextFrameCond.signal();
    }
  }
  m_nextFrameThreadFinished = true;
};
예제 #4
0
void mySleep(float time)
{
	Glib::Timer tim ; 
	tim.start() ;
	while( tim.elapsed() < time ) ;
}