谁能建议我一个可以将vil_image_view容器转换为cv::Mat容器的函数?我对尺寸的表示法感到困惑,即行数和高度,列数和宽度
最佳答案
我没有在我的环境中安装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;
}
关于opencv - vil_image_view到cv::Mat转换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20309749/