问题描述
我有一个框架,并希望使用 openCL 类型 oclMat 在 openCV 中将其放在较大的图片上。但是下面的代码给出了黑框结果:
capture.read(fMat); // frame from camera or video
oclMat f; f.upload(fMat);
oclMat bf(f.rows * 2,f.cols * 2,f.ocltype()); //bf-big frame
oclMat bfRoi = bf(Rect(0,0,f.cols,f.rows));
f.copyTo(bfRoi); // something wrong here
// label 1
bf.download(fMat);
Mat bf2; bf.convertTo(bf2,fMat.type()); // this convert affect to nothing
imshow(big frame,bf2);
所以我必须在label 1地方添加oclMat-> Mat Mat-> oclMat:
Mat fTmp,bfTmp(Size(bf.cols,bf.rows),fMat.type ());
f.download(fTmp);
fTmp.convertTo(fTmp ,fMat.type()); //它是必要的,因为assert(channels()== CV_MAT_CN(dtype))
fTmp.copyTo(bfTmp(Rect(0,0,fTmp.cols,fTmp.rows)))
bf.upload(bfTmp);
它工作,但需要太多的时间,代码看起来很伤心。
好吧,我一直在搜索错误的地方:operations_on_matrices而不是image_filtering。所以至少有一种方法来做它在ocl存在 - copyMakeBorder(...)。所以我现在的方法是:
capture.read(fMat); // frame from camera or video
oclMat f; f.upload(fMat);
oclMat bf(f.rows * 2,f.cols * 2,f.ocltype()); //bf-big frame from ahere
//这里的新方法
oclMat bf2(bf.rows,bf,cols); //与大帧相同大小的临时帧
copyMakeBorder(f,bf2,0,bf2.rows-f.rows,0,bf2.cols-f.cols,BORDER_CONSTANT,Scalar(0,0,0 )); // 这里是!任何位置可能通过改变边框大小(记住关于掩码)
oclMat掩码(bf.rows,bf.cols,CV_8UC1); // bw mask保持大框架的一部分不变
mask = Scalar(0);掩码(Range(0,f.rows),Range(0,f.cols))=标量(1); //drawrectangle
bf2.copyTo(bf,mask);
// label 1
bf.download(fMat);
imshow(big frame,fMat);
我不知道这是否是最好的方法,
I have a frame and want to put it in on a bigger image in openCV using openCL type oclMat. But code below gives me black frame result:
capture.read(fMat); // frame from camera or video
oclMat f; f.upload(fMat);
oclMat bf(f.rows*2, f.cols*2, f.ocltype()); // "bf"-big frame
oclMat bfRoi = bf(Rect(0, 0, f.cols, f.rows));
f.copyTo(bfRoi); // something wrong here
// label 1
bf.download(fMat);
Mat bf2; bf.convertTo(bf2, fMat.type()); // this convert affects to nothing
imshow("big frame", bf2);
So I have to add at the "label 1" place "oclMat->Mat" conversion and back "Mat->oclMat":
Mat fTmp, bfTmp(Size(bf.cols, bf.rows), fMat.type());
f.download(fTmp);
fTmp.convertTo(fTmp, fMat.type()); // it is necessary due to assert(channels() == CV_MAT_CN(dtype))
fTmp.copyTo(bfTmp(Rect(0, 0, fTmp.cols, fTmp.rows)));
bf.upload(bfTmp);
It works but takes too much time and code looks sad. Is it possible to do the same thing staying in the term of oclMat only (without forward and back conversion)?
Well, I have been searching wrong place: operations_on_matrices instead of image_filtering. So at least one way to do it inside ocl exists -- copyMakeBorder(...). So my approach now is:
capture.read(fMat); // frame from camera or video
oclMat f; f.upload(fMat);
oclMat bf(f.rows*2, f.cols*2, f.ocltype()); // "bf"-big frame from somewhere
// new approach here
oclMat bf2(bf.rows, bf,cols); // temp frame of the same size as big frame
copyMakeBorder(f, bf2, 0, bf2.rows-f.rows, 0, bf2.cols-f.cols, BORDER_CONSTANT, Scalar(0,0,0)); // here it is! any position possible by changing border sizes (remember about mask)
oclMat mask(bf.rows, bf.cols, CV_8UC1); // bw mask to keep part of big frame unchanged
mask = Scalar(0); mask(Range(0, f.rows), Range(0, f.cols)) = Scalar(1); // "draw" rectangle
bf2.copyTo(bf, mask);
// label 1
bf.download(fMat);
imshow("big frame", fMat);
I am not sure if it is the best way, but at least it works.
这篇关于在openCV中为oclMat添加小图像到大图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!