vil_image_view到cv :: Mat转换

时间:2013-12-01 07:21:01

标签: opencv image-processing computer-vision containers

任何人都可以建议我一个可以将vil_image_view容器转换为cv :: Mat容器的函数吗?我对尺寸的符号,即行数和行数的混淆感到困惑。高度,列数和数量宽度

1 个答案:

答案 0 :(得分:0)

我的环境中没有安装VXL,我认为在您的情况下最好的办法是通过反复试验找到正确的代码...根据我的经验,阅读文档只是其中的一部分解决问题。

话虽如此,我的第一个猜测是(请记住,我没有测试代码):

#include <vcl_iostream.h>
#include <vxl_config.h>
#include <vil/vil_rgb.h>
#include <vil/vil_load.h>
#include <vil/vil_image_view.h>
#include "opencv2/core/core.hpp"

int main()
{
  vil_image_view<vil_rgb<vxl_byte> > img_src;
  img_src = vil_load("test.png");

  int n_rows = img_src.ni();
  int n_cols = img_src.nj();

  // see http://docs.opencv.org/doc/user_guide/ug_mat.html#accessing-pixel-intensity-values
  const int OPENCV_RED_INDEX   = 2;
  const int OPENCV_GREEN_INDEX = 1;
  const int OPENCV_BLUE_INDEX  = 0;

  // see http://stackoverflow.com/a/7903042/15485
  cv::Mat img_dst(n_rows, n_cols, CV_8UC3);
  for ( int r = 0; r < n_rows; r++ ) {
    for ( int c = 0; c < n_cols; c++ ) {
        img_dst.at<cv::Vec3b>(r,c)[OPENCV_RED_INDEX  ] = img_src(c,r).r;
        img_dst.at<cv::Vec3b>(r,c)[OPENCV_GREEN_INDEX] = img_src(c,r).g;
        img_dst.at<cv::Vec3b>(r,c)[OPENCV_BLUE_INDEX ] = img_src(c,r).b;
    }
  }

  return 0;
}

我的第二个猜测是:

#include <vcl_iostream.h>
#include <vxl_config.h>
#include <vil/vil_rgb.h>
#include <vil/vil_load.h>
#include <vil/vil_image_view.h>
#include "opencv2/core/core.hpp"

int main()
{
  vil_image_view<vil_rgb<vxl_byte> > img_src;
  img_src = vil_load("test.png");

  int n_rows = img_src.ni();
  int n_cols = img_src.nj();

  // see http://docs.opencv.org/doc/user_guide/ug_mat.html#accessing-pixel-intensity-values
  const int OPENCV_RED_INDEX   = 2;
  const int OPENCV_GREEN_INDEX = 1;
  const int OPENCV_BLUE_INDEX  = 0;

  // see http://stackoverflow.com/a/7903042/15485
  cv::Mat img_dst(n_rows, n_cols, CV_8UC3);
  for ( int r = 0; r < n_rows; r++ ) {
    for ( int c = 0; c < n_cols; c++ ) {
        img_dst.at<cv::Vec3b>(r,c)[OPENCV_RED_INDEX  ] = img_src(r,c).r;
        img_dst.at<cv::Vec3b>(r,c)[OPENCV_GREEN_INDEX] = img_src(r,c).g;
        img_dst.at<cv::Vec3b>(r,c)[OPENCV_BLUE_INDEX ] = img_src(r,c).b;
    }
  }

  return 0;
}