OpenCV4.0 摄像头风格迁移

再按照 https://github.com/opencv/opencv/blob/3.4.0/samples/dnn/fast_neural_style.py 改为 C++ 版的摄像头风格迁移

//摄像头或视频风格转换
//按空格存一图
//OpenCV:使用VideoCapture类进行视频读取和显示

#include 
#include 
#include 

using namespace std;
using namespace cv;

//数字转字符串:用C++的streanstream:
#include 
#include 

string num2str(double i)
{
	stringstream ss;
	ss << i;
	return ss.str();
}

//----------------cv中取出	-----------------开始
const int CV_MAX_DIM = 32;
Mat getPlane(const Mat &m, int n, int cn)
{
    CV_Assert(m.dims > 2);
    int sz[CV_MAX_DIM];
    for(int i = 2; i < m.dims; i++)
    {
        sz[i-2] = m.size.p[i];
    }
    return Mat(m.dims - 2, sz, m.type(), (void*)m.ptr(n, cn));
}

//用于单图,如果多图还要再修改
void imagesFromBlob(const cv::Mat& blob_, OutputArrayOfArrays images_)
{
    //blob 是浮点精度的4维矩阵
    //blob_[0] = 批量大小 = 图像数
    //blob_[1] = 通道数
    //blob_[2] = 高度
    //blob_[3] = 宽度    
	CV_Assert(blob_.depth() == CV_32F);
    CV_Assert(blob_.dims == 4);

    //images_.create(cv::Size(1, blob_.size[0]),blob_.depth() );//多图,不明白为什么?
    images_.create(blob_.size[2],blob_.size[3],blob_.depth() );//创建一个图像
	

    std::vector vectorOfChannels(blob_.size[1]);
    //for (int n = 0; n <  blob_.size[0]; ++n) //多个图
    {int n = 0;                                //只有一个图
        //for (int c = 0; c < blob_.size[1]; ++c)
        //{
        //    vectorOfChannels[c] = getPlane(blob_, n, c);
        //}
    vectorOfChannels[0] = getPlane(blob_, n, 0)+103.939;
    vectorOfChannels[1] = getPlane(blob_, n, 1)+116.779;
    vectorOfChannels[2] = getPlane(blob_, n, 2)+123.68;

        //cv::merge(vectorOfChannels, images_.getMatRef(n));//这里会出错,是前面的create的原因?
        cv::merge(vectorOfChannels, images_);//通道合并
    }
}
//----------------cv中取出	-----------------结束

int main(int argc, char *argv[])
{

	String model[]={"mosaic.t7","candy.t7","la_muse.t7","the_scream.t7","feathers.t7","udnie.t7",//instance_norm 6个
		"the_wave.t7","starry_night.t7","la_muse_eccv16.t7","composition_vii.t7"};//eccv16 4个

	//随机取一个模型
	 cv::RNG rng((unsigned)time(NULL));
	 int num=rng.uniform(0, 1000 )% 10;
	 cout<

感觉速度有点慢

你可能感兴趣的:(opencv,风格转换)