Ejemplo n.º 1
0
Image* Frame::GetImage(int width, int height,
                       VideoMode::PixelFormat pixelFormat, int jpegQuality) {
  if (!m_impl) return nullptr;
  std::lock_guard<wpi::recursive_mutex> lock(m_impl->mutex);
  Image* cur = GetNearestImage(width, height, pixelFormat);
  if (!cur || cur->Is(width, height, pixelFormat)) return cur;

  DEBUG4("converting image from "
         << cur->width << "x" << cur->height << " type " << cur->pixelFormat
         << " to " << width << "x" << height << " type " << pixelFormat);

  // If the source image is a JPEG, we need to decode it before we can do
  // anything else with it.  Note that if the destination format is JPEG, we
  // still need to do this (unless the width/height were the same, in which
  // case we already returned the existing JPEG above).
  if (cur->pixelFormat == VideoMode::kMJPEG) cur = ConvertMJPEGToBGR(cur);

  // Resize
  if (!cur->Is(width, height)) {
    // Allocate an image.
    auto newImage = m_impl->source.AllocImage(
        cur->pixelFormat, width, height,
        width * height * (cur->size() / (cur->width * cur->height)));

    // Resize
    cv::Mat newMat = newImage->AsMat();
    cv::resize(cur->AsMat(), newMat, newMat.size(), 0, 0);

    // Save the result
    cur = newImage.release();
    m_impl->images.push_back(cur);
  }

  // Convert to output format
  return Convert(cur, pixelFormat, jpegQuality);
}
Ejemplo n.º 2
0
bool Frame::GetCv(cv::Mat& image, int width, int height) {
  Image* rawImage = GetImage(width, height, VideoMode::kBGR);
  if (!rawImage) return false;
  rawImage->AsMat().copyTo(image);
  return true;
}