本文整理汇总了C++中OutputArray::getMat方法的典型用法代码示例。如果您正苦于以下问题:C++ OutputArray::getMat方法的具体用法?C++ OutputArray::getMat怎么用?C++ OutputArray::getMat使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OutputArray
的用法示例。
在下文中一共展示了OutputArray::getMat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: cvtColorInvariants
void cvtColorInvariants(InputArray _src, OutputArray _dst, int code, int dcn )
{
Mat src = _src.getMat(), dst;
Size sz = src.size();
int scn = src.channels(), depth = src.depth(), bidx;
int blueIdx;
CV_Assert( depth == CV_8U || depth == CV_16U || depth == CV_32F );
switch(code) {
case COLOR_INVARIANCE_PASSTHROUGH:
dst = src;
break;
case COLOR_INVARIANCE_BGR2GAUSSIAN_OPPONENT:
case COLOR_INVARIANCE_RGB2GAUSSIAN_OPPONENT:
CV_Assert( scn == 3 || scn == 4 );
dcn = 3;
blueIdx = (code == COLOR_INVARIANCE_BGR2GAUSSIAN_OPPONENT) ? 0 : 2;
_dst.create( sz, CV_MAKETYPE(depth, dcn));
dst = _dst.getMat();
if( depth == CV_8U )
CvtColorLoop(src, dst, GaussianOpponent<uchar>(scn, dcn, blueIdx,256));
else if( depth == CV_16U )
CvtColorLoop(src, dst, GaussianOpponent<ushort>(scn, dcn, blueIdx, 2<<16 ) );
else
CvtColorLoop(src, dst, GaussianOpponent<float>(scn, dcn, blueIdx, 1 ));
break;
case COLOR_INVARIANCE_BGR2HInvariant:
CV_Assert( scn == 3 || scn == 4 );
dcn = 1;
blueIdx = (code == COLOR_INVARIANCE_BGR2HInvariant) ? 0 : 2;
_dst.create( sz, CV_MAKETYPE(depth, dcn));
dst = _dst.getMat();
if( depth == CV_8U )
CvtColorLoop(src, dst, HInvariant<uchar>(scn, dcn, blueIdx, 256));
else if( depth == CV_16U )
CvtColorLoop(src, dst, HInvariant<ushort>(scn, dcn, blueIdx, 2<<16 ) );
else
CvtColorLoop(src, dst, HInvariant<float>(scn, dcn, blueIdx, 1 ));
break;
case COLOR_INVARIANCE_Gray2YB:
CV_Assert( scn == 1 );
dcn = 3;
_dst.create( sz, CV_MAKETYPE(CV_8U, 3));
dst = _dst.getMat();
if( depth == CV_8U )
CvtColorLoop(src, dst, Gray2YB<uchar>( 256 ) );
else if( depth == CV_16U )
CvtColorLoop(src, dst, Gray2YB<ushort>( 2<<16 ) );
else
CvtColorLoop(src, dst, Gray2YB<float>( 1 ) );
break;
case COLOR_INVARIANCE_Gray2RG:
CV_Assert( scn == 1 );
dcn = 3;
_dst.create( sz, CV_MAKETYPE(CV_8U, 3));
dst = _dst.getMat();
if( depth == CV_8U )
CvtColorLoop(src, dst, Gray2RG<uchar>( 256 ) );
else if( depth == CV_16U )
CvtColorLoop(src, dst, Gray2RG<ushort>( 2<<16 ) );
else
CvtColorLoop(src, dst, Gray2RG<float>( 1 ) );
break;
default:
CV_Error( CV_StsBadFlag, "Unknown/unsupported color conversion code" );
}
}
示例2: getPosteriorImage
void BackgroundSubtractorGMG::getPosteriorImage(OutputArray _img)
{
_img.create(Size(imWidth,imHeight),CV_32F);
Mat img = _img.getMat();
posteriorImage.copyTo(img);
}
示例3: decolor
void cv::decolor(InputArray _src, OutputArray _dst, OutputArray _color_boost)
{
CV_INSTRUMENT_REGION();
Mat I = _src.getMat();
_dst.create(I.size(), CV_8UC1);
Mat dst = _dst.getMat();
_color_boost.create(I.size(), CV_8UC3);
Mat color_boost = _color_boost.getMat();
CV_Assert(!I.empty() && (I.channels()==3));
// Parameter Setting
const int maxIter = 15;
const double tol = .0001;
int iterCount = 0;
double E = 0;
double pre_E = std::numeric_limits<double>::infinity();
Mat img;
I.convertTo(img, CV_32FC3, 1.0/255.0);
// Initialization
Decolor obj;
vector <double> Cg;
vector < vector <double> > polyGrad;
vector <Vec3i> comb;
vector <double> alf;
obj.grad_system(img,polyGrad,Cg,comb);
obj.weak_order(img,alf);
// Solver
Mat Mt = Mat(int(polyGrad.size()),int(polyGrad[0].size()), CV_32FC1);
obj.wei_update_matrix(polyGrad,Cg,Mt);
vector <double> wei;
obj.wei_inti(comb,wei);
//////////////////////////////// main loop starting ////////////////////////////////////////
vector <double> G_pos(alf.size());
vector <double> G_neg(alf.size());
vector <double> EXPsum(G_pos.size());
vector <double> EXPterm(G_pos.size());
vector <double> temp(polyGrad[0].size());
vector <double> temp1(polyGrad[0].size());
vector <double> temp2(EXPsum.size());
vector <double> wei1(polyGrad.size());
while(sqrt(pow(E-pre_E,2)) > tol)
{
iterCount +=1;
pre_E = E;
for(size_t i=0; i<polyGrad[0].size(); i++)
{
double val = 0.0;
for(size_t j=0; j<polyGrad.size(); j++)
val = val + (polyGrad[j][i] * wei[j]);
temp[i] = val - Cg[i];
temp1[i] = val + Cg[i];
}
for(size_t i=0; i<alf.size(); i++)
{
const double sqSigma = obj.sigma * obj.sigma;
const double pos = ((1 + alf[i])/2) * exp(-1.0 * 0.5 * (temp[i] * temp[i]) / sqSigma);
const double neg = ((1 - alf[i])/2) * exp(-1.0 * 0.5 * (temp1[i] * temp1[i]) / sqSigma);
G_pos[i] = pos;
G_neg[i] = neg;
}
for(size_t i=0; i<G_pos.size(); i++)
EXPsum[i] = G_pos[i]+G_neg[i];
for(size_t i=0; i<EXPsum.size(); i++)
temp2[i] = (EXPsum[i] == 0) ? 1.0 : 0.0;
for(size_t i=0; i<G_pos.size(); i++)
EXPterm[i] = (G_pos[i] - G_neg[i])/(EXPsum[i] + temp2[i]);
for(int i=0; i<int(polyGrad.size()); i++)
{
double val1 = 0.0;
for(int j=0; j<int(polyGrad[0].size()); j++)
{
val1 = val1 + (Mt.at<float>(i,j) * EXPterm[j]);
}
wei1[i] = val1;
}
for(size_t i=0; i<wei.size(); i++)
wei[i] = wei1[i];
E = obj.energyCalcu(Cg, polyGrad, wei);
if(iterCount > maxIter)
//.........这里部分代码省略.........
示例4: segmentMotion
void segmentMotion(InputArray _mhi, OutputArray _segmask,
vector<Rect>& boundingRects,
double timestamp, double segThresh)
{
Mat mhi = _mhi.getMat();
_segmask.create(mhi.size(), CV_32F);
Mat segmask = _segmask.getMat();
segmask = Scalar::all(0);
CV_Assert( mhi.type() == CV_32F );
CV_Assert( segThresh >= 0 );
Mat mask = Mat::zeros( mhi.rows + 2, mhi.cols + 2, CV_8UC1 );
int x, y;
// protect zero mhi pixels from floodfill.
for( y = 0; y < mhi.rows; y++ )
{
const float* mhiptr = mhi.ptr<float>(y);
uchar* maskptr = mask.ptr<uchar>(y+1) + 1;
for( x = 0; x < mhi.cols; x++ )
{
if( mhiptr[x] == 0 )
maskptr[x] = 1;
}
}
float ts = (float)timestamp;
float comp_idx = 1.f;
for( y = 0; y < mhi.rows; y++ )
{
float* mhiptr = mhi.ptr<float>(y);
uchar* maskptr = mask.ptr<uchar>(y+1) + 1;
for( x = 0; x < mhi.cols; x++ )
{
if( mhiptr[x] == ts && maskptr[x] == 0 )
{
Rect cc;
floodFill( mhi, mask, Point(x,y), Scalar::all(0),
&cc, Scalar::all(segThresh), Scalar::all(segThresh),
FLOODFILL_MASK_ONLY + 2*256 + 4 );
for( int y1 = 0; y1 < cc.height; y1++ )
{
float* segmaskptr = segmask.ptr<float>(cc.y + y1) + cc.x;
uchar* maskptr1 = mask.ptr<uchar>(cc.y + y1 + 1) + cc.x + 1;
for( int x1 = 0; x1 < cc.width; x1++ )
{
if( maskptr1[x1] > 1 )
{
maskptr1[x1] = 1;
segmaskptr[x1] = comp_idx;
}
}
}
comp_idx += 1.f;
boundingRects.push_back(cc);
}
}
}
}
示例5: guiWeightedModeUpsample
void guiWeightedModeUpsample(InputArray srcimage, OutputArray dest, int resizeFactor, InputArray ref)
{
string windowName = "weighted mode";
namedWindow(windowName);
Mat src = srcimage.getMat();
int alpha = 0; createTrackbar("a",windowName, &alpha, 100);
int sw = 0; createTrackbar("sw",windowName, &sw, 1);
int sw2 = 0; createTrackbar("sw2",windowName, &sw2, 1);
int r = 3; createTrackbar("r",windowName, &r, 30);
int sc = 40; createTrackbar("sigma_color",windowName, &sc, 255);
int ss = 30; createTrackbar("sigma_space",windowName, &ss, 255);
int sb = 10; createTrackbar("sigma_bin",windowName, &sb, 255);
int iter = 2; createTrackbar("iteration",windowName, &iter, 10);
int iter2 = 2; createTrackbar("iteration2",windowName, &iter2, 10);
int key = 0;
while(key!='q')
{
Mat srctemp;
{
Mat med;
medianBlur(srcimage, med,1);
src.copyTo(srctemp);
CalcTime t;
for(int i=0;i<iter;i++)
{
Mat tmp = srctemp.clone();
weightedModeFilter(srctemp, med, tmp, r, ss, sc, 2, sb);
tmp.copyTo(srctemp);
}
//upsampling function
if(sw==0) hqx(srctemp, dest, resizeFactor);
else SAI(srctemp, dest);
//else resize(srctemp, dest, Size(src.cols*resizeFactor, src.rows*resizeFactor), 0,0, INTER_LANCZOS4);
}
//shock filter
if(sw2!=0)
{
Mat a = dest.getMat();
//blurRemoveMinMax(a,a,2);
iterativeBackProjectionDeblurGaussian(a, a, Size(9,9), 3, 0.2, iter2);
a.copyTo(dest);
}
//blending referece image for debug
alphaBlend(ref, dest, alpha/100.0, dest);
imshow(windowName, dest);
key = waitKey(30);
if(key=='f')
{
alpha = (alpha != 0) ? 0:100;
setTrackbarPos("a", windowName, alpha);
}
}
destroyWindow(windowName);
}
示例6: if
void
triangulatePoints(InputArrayOfArrays _points2d, InputArrayOfArrays _projection_matrices,
OutputArray _points3d)
{
// check
size_t nviews = (unsigned) _points2d.total();
CV_Assert(nviews >= 2 && nviews == _projection_matrices.total());
// inputs
size_t n_points;
vector<Mat_<double> > points2d(nviews);
vector<Matx34d> projection_matrices(nviews);
{
vector<Mat> points2d_tmp;
_points2d.getMatVector(points2d_tmp);
n_points = points2d_tmp[0].cols;
vector<Mat> projection_matrices_tmp;
_projection_matrices.getMatVector(projection_matrices_tmp);
// Make sure the dimensions are right
for(size_t i=0; i<nviews; ++i) {
CV_Assert(points2d_tmp[i].rows == 2 && points2d_tmp[i].cols == n_points);
if (points2d_tmp[i].type() == CV_64F)
points2d[i] = points2d_tmp[i];
else
points2d_tmp[i].convertTo(points2d[i], CV_64F);
CV_Assert(projection_matrices_tmp[i].rows == 3 && projection_matrices_tmp[i].cols == 4);
if (projection_matrices_tmp[i].type() == CV_64F)
projection_matrices[i] = projection_matrices_tmp[i];
else
projection_matrices_tmp[i].convertTo(projection_matrices[i], CV_64F);
}
}
// output
_points3d.create(3, n_points, CV_64F);
cv::Mat points3d = _points3d.getMat();
// Two view
if( nviews == 2 )
{
const Mat_<double> &xl = points2d[0], &xr = points2d[1];
const Matx34d & Pl = projection_matrices[0]; // left matrix projection
const Matx34d & Pr = projection_matrices[1]; // right matrix projection
// triangulate
for( unsigned i = 0; i < n_points; ++i )
{
Vec3d point3d;
triangulateDLT( Vec2d(xl(0,i), xl(1,i)), Vec2d(xr(0,i), xr(1,i)), Pl, Pr, point3d );
for(char j=0; j<3; ++j)
points3d.at<double>(j, i) = point3d[j];
}
}
else if( nviews > 2 )
{
// triangulate
for( unsigned i=0; i < n_points; ++i )
{
// build x matrix (one point per view)
Mat_<double> x( 2, nviews );
for( unsigned k=0; k < nviews; ++k )
{
points2d.at(k).col(i).copyTo( x.col(k) );
}
Vec3d point3d;
nViewTriangulate( x, projection_matrices, point3d );
for(char j=0; j<3; ++j)
points3d.at<double>(j, i) = point3d[j];
}
}
}
示例7: compute
void compute(InputArray leftarr, InputArray rightarr, OutputArray disparr)
{
int dtype = disparr.fixedType() ? disparr.type() : params.dispType;
Size leftsize = leftarr.size();
if (leftarr.size() != rightarr.size())
CV_Error(Error::StsUnmatchedSizes, "All the images must have the same size");
if (leftarr.type() != CV_8UC1 || rightarr.type() != CV_8UC1)
CV_Error(Error::StsUnsupportedFormat, "Both input images must have CV_8UC1");
if (dtype != CV_16SC1 && dtype != CV_32FC1)
CV_Error(Error::StsUnsupportedFormat, "Disparity image must have CV_16SC1 or CV_32FC1 format");
if (params.preFilterType != PREFILTER_NORMALIZED_RESPONSE &&
params.preFilterType != PREFILTER_XSOBEL)
CV_Error(Error::StsOutOfRange, "preFilterType must be = CV_STEREO_BM_NORMALIZED_RESPONSE");
if (params.preFilterSize < 5 || params.preFilterSize > 255 || params.preFilterSize % 2 == 0)
CV_Error(Error::StsOutOfRange, "preFilterSize must be odd and be within 5..255");
if (params.preFilterCap < 1 || params.preFilterCap > 63)
CV_Error(Error::StsOutOfRange, "preFilterCap must be within 1..63");
if (params.kernelSize < 5 || params.kernelSize > 255 || params.kernelSize % 2 == 0 ||
params.kernelSize >= std::min(leftsize.width, leftsize.height))
CV_Error(Error::StsOutOfRange, "kernelSize must be odd, be within 5..255 and be not larger than image width or height");
if (params.numDisparities <= 0 || params.numDisparities % 16 != 0)
CV_Error(Error::StsOutOfRange, "numDisparities must be positive and divisble by 16");
if (params.textureThreshold < 0)
CV_Error(Error::StsOutOfRange, "texture threshold must be non-negative");
if (params.uniquenessRatio < 0)
CV_Error(Error::StsOutOfRange, "uniqueness ratio must be non-negative");
int FILTERED = (params.minDisparity - 1) << DISPARITY_SHIFT;
Mat left0 = leftarr.getMat(), right0 = rightarr.getMat();
Mat disp0 = disparr.getMat();
int width = left0.cols;
int height = left0.rows;
if(previous_size != width * height)
{
previous_size = width * height;
speckleX.create(height,width,CV_32SC4);
speckleY.create(height,width,CV_32SC4);
puss.create(height,width,CV_32SC4);
censusImage[0].create(left0.rows,left0.cols,CV_32SC4);
censusImage[1].create(left0.rows,left0.cols,CV_32SC4);
partialSumsLR.create(left0.rows + 1,(left0.cols + 1) * (params.numDisparities + 1),CV_16S);
agregatedHammingLRCost.create(left0.rows + 1,(left0.cols + 1) * (params.numDisparities + 1),CV_16S);
hammingDistance.create(left0.rows, left0.cols * (params.numDisparities + 1),CV_16S);
preFilteredImg0.create(left0.size(), CV_8U);
preFilteredImg1.create(left0.size(), CV_8U);
aux.create(height,width,CV_8UC1);
}
Mat left = preFilteredImg0, right = preFilteredImg1;
int ndisp = params.numDisparities;
int wsz = params.kernelSize;
int bufSize0 = (int)((ndisp + 2)*sizeof(int));
bufSize0 += (int)((height + wsz + 2)*ndisp*sizeof(int));
bufSize0 += (int)((height + wsz + 2)*sizeof(int));
bufSize0 += (int)((height + wsz + 2)*ndisp*(wsz + 2)*sizeof(uchar) + 256);
int bufSize1 = (int)((width + params.preFilterSize + 2) * sizeof(int) + 256);
if(params.usePrefilter == true)
{
uchar *_buf = slidingSumBuf.ptr();
parallel_for_(Range(0, 2), PrefilterInvoker(left0, right0, left, right, _buf, _buf + bufSize1, ¶ms), 1);
}
else if(params.usePrefilter == false)
{
left = left0;
right = right0;
}
if(params.kernelType == CV_SPARSE_CENSUS)
{
censusTransform(left,right,params.kernelSize,censusImage[0],censusImage[1],CV_SPARSE_CENSUS);
}
else if(params.kernelType == CV_DENSE_CENSUS)
{
censusTransform(left,right,params.kernelSize,censusImage[0],censusImage[1],CV_SPARSE_CENSUS);
}
else if(params.kernelType == CV_CS_CENSUS)
{
symetricCensusTransform(left,right,params.kernelSize,censusImage[0],censusImage[1],CV_CS_CENSUS);
}
else if(params.kernelType == CV_MODIFIED_CS_CENSUS)
{
//.........这里部分代码省略.........
示例8: threshold
/*******************************************************************************
* Function: subtractBGOpenDiagonal
* Description: BG subtraction via opening with diagonal structuring elements
* Arguments:
inImg - input image
bgsImg - BG subtracted image
threshVal - threshold value for converting to binary image
seLength - length of structuring elements
* Returns: void
* Comments:
* Revision:
*******************************************************************************/
int
FGExtraction::subtractBGOpenDiagonal(InputArray src, OutputArray dst, int threshVal, int seLength)
{
// generate binary image by thresholding
Mat bin;
double thresh = threshold(src, bin, threshVal, 255, THRESH_BINARY);
// opening by horizontal structuring element
//Mat structElemHorizontal = Mat::ones(1, seLength, CV_8U);
//morphologyEx(bin, dst, MORPH_OPEN, structElemHorizontal);
// opening by vertical structuring element
//Mat structElemVertical = Mat::ones(seLength, 1, CV_8U);
//morphologyEx(dst, dst, MORPH_OPEN, structElemVertical);
//imshow("src", src);
//imshow("bin", bin);
//waitKey(0);
// opening by first diagonal structuring element
Mat structElemBackSlash = Mat::eye(seLength, seLength, CV_8U);
morphologyEx(bin, dst, MORPH_OPEN, structElemBackSlash);
//imshow("dst1", dst);
//waitKey(0);
// opening by second diagonal structuring element
Mat structElemSlash;
flip(structElemBackSlash, structElemSlash, 0);
morphologyEx(dst, dst, MORPH_OPEN, structElemSlash);
//imshow("dst2", dst);
//waitKey(0);
// eliminate small noise
Mat structElemEllip = getStructuringElement(MORPH_ELLIPSE, Size(seLength, seLength));
morphologyEx(dst, dst, MORPH_OPEN, structElemEllip);
//imshow("dst3", dst);
//waitKey(0);
// get object size
Mat dstImg = dst.getMat();
vector<vector<Point>> contours = extractContours(dstImg);
if (contours.size()==0)
return 1;
Mat mask = Mat::zeros(_bgsImg.size(), CV_8U);
vector<int> areas(contours.size());
int cnt = 0;
int argMax = 0;
int max_area = 0;
for(vector<vector<Point> >::const_iterator it = contours.begin(); it != contours.end(); ++it){
Rect uprightBox = boundingRect(*it);
areas[cnt] = uprightBox.height*uprightBox.width;
if (areas[cnt]>max_area) {
max_area = areas[cnt];
argMax = cnt;
}
cnt++;
}
vector<Point> largestContour = contours[argMax]; //***** only use the largest contour
RotatedRect orientedBox = orientedBoundingBox(largestContour);
int updateSeL = int(min(orientedBox.size.width, orientedBox.size.height)/5.0+0.5);
// opening by first diagonal structuring element
structElemBackSlash = Mat::eye(updateSeL, updateSeL, CV_8U);
morphologyEx(bin, dst, MORPH_OPEN, structElemBackSlash);
//imshow("dst1", dst);
//waitKey(0);
// opening by second diagonal structuring element
flip(structElemBackSlash, structElemSlash, 0);
morphologyEx(dst, dst, MORPH_OPEN, structElemSlash);
//imshow("dst2", dst);
//waitKey(0);
// eliminate small noise
structElemEllip = getStructuringElement(MORPH_ELLIPSE, Size(updateSeL, updateSeL));
morphologyEx(dst, dst, MORPH_OPEN, structElemEllip);
//imshow("dst3", dst);
//waitKey(0);
//.........这里部分代码省略.........
示例9: matchTemplate
void cv::matchTemplate( InputArray _img, InputArray _templ, OutputArray _result, int method )
{
CV_Assert( CV_TM_SQDIFF <= method && method <= CV_TM_CCOEFF_NORMED );
int numType = method == CV_TM_CCORR || method == CV_TM_CCORR_NORMED ? 0 :
method == CV_TM_CCOEFF || method == CV_TM_CCOEFF_NORMED ? 1 : 2;
bool isNormed = method == CV_TM_CCORR_NORMED ||
method == CV_TM_SQDIFF_NORMED ||
method == CV_TM_CCOEFF_NORMED;
Mat img = _img.getMat(), templ = _templ.getMat();
if( img.rows < templ.rows || img.cols < templ.cols )
std::swap(img, templ);
CV_Assert( (img.depth() == CV_8U || img.depth() == CV_32F) &&
img.type() == templ.type() );
Size corrSize(img.cols - templ.cols + 1, img.rows - templ.rows + 1);
_result.create(corrSize, CV_32F);
Mat result = _result.getMat();
int cn = img.channels();
crossCorr( img, templ, result, result.size(), result.type(), Point(0,0), 0, 0);
if( method == CV_TM_CCORR )
return;
double invArea = 1./((double)templ.rows * templ.cols);
Mat sum, sqsum;
Scalar templMean, templSdv;
double *q0 = 0, *q1 = 0, *q2 = 0, *q3 = 0;
double templNorm = 0, templSum2 = 0;
if( method == CV_TM_CCOEFF )
{
integral(img, sum, CV_64F);
templMean = mean(templ);
}
else
{
integral(img, sum, sqsum, CV_64F);
meanStdDev( templ, templMean, templSdv );
templNorm = CV_SQR(templSdv[0]) + CV_SQR(templSdv[1]) +
CV_SQR(templSdv[2]) + CV_SQR(templSdv[3]);
if( templNorm < DBL_EPSILON && method == CV_TM_CCOEFF_NORMED )
{
result = Scalar::all(1);
return;
}
templSum2 = templNorm +
CV_SQR(templMean[0]) + CV_SQR(templMean[1]) +
CV_SQR(templMean[2]) + CV_SQR(templMean[3]);
if( numType != 1 )
{
templMean = Scalar::all(0);
templNorm = templSum2;
}
templSum2 /= invArea;
templNorm = sqrt(templNorm);
templNorm /= sqrt(invArea); // care of accuracy here
q0 = (double*)sqsum.data;
q1 = q0 + templ.cols*cn;
q2 = (double*)(sqsum.data + templ.rows*sqsum.step);
q3 = q2 + templ.cols*cn;
}
double* p0 = (double*)sum.data;
double* p1 = p0 + templ.cols*cn;
double* p2 = (double*)(sum.data + templ.rows*sum.step);
double* p3 = p2 + templ.cols*cn;
int sumstep = sum.data ? (int)(sum.step / sizeof(double)) : 0;
int sqstep = sqsum.data ? (int)(sqsum.step / sizeof(double)) : 0;
int i, j, k;
for( i = 0; i < result.rows; i++ )
{
float* rrow = (float*)(result.data + i*result.step);
int idx = i * sumstep;
int idx2 = i * sqstep;
for( j = 0; j < result.cols; j++, idx += cn, idx2 += cn )
{
double num = rrow[j], t;
double wndMean2 = 0, wndSum2 = 0;
if( numType == 1 )
{
for( k = 0; k < cn; k++ )
{
t = p0[idx+k] - p1[idx+k] - p2[idx+k] + p3[idx+k];
wndMean2 += CV_SQR(t);
//.........这里部分代码省略.........
示例10: ulbp_
template <typename _Tp> static
inline void ulbp_(InputArray _src, OutputArray _dst, int radius, int neighbors, vector<int> m_uniform)
{
if (neighbors != 8 || m_uniform.size() != 58)
{
cout << "neighbor must be 8! and uniform size be 58!\n";
system("pause");
exit(-1);
}
//get matrices
Mat src = _src.getMat();
// allocate memory for result
_dst.create(src.rows-2*radius, src.cols-2*radius, CV_32SC1);
Mat dst = _dst.getMat();
// zero
dst.setTo(0);
for(int n=0; n<neighbors; n++)
{
// sample points
float x = static_cast<float>(-radius) * sin(2.0*CV_PI*n/static_cast<float>(neighbors));
float y = static_cast<float>(radius) * cos(2.0*CV_PI*n/static_cast<float>(neighbors));
// relative indices
int fx = static_cast<int>(floor(x));
int fy = static_cast<int>(floor(y));
int cx = static_cast<int>(ceil(x));
int cy = static_cast<int>(ceil(y));
// fractional part
float ty = y - fy;
float tx = x - fx;
// set interpolation weights
float w1 = (1 - tx) * (1 - ty);
float w2 = tx * (1 - ty);
float w3 = (1 - tx) * ty;
float w4 = tx * ty;
// iterate through your data
for(int i=radius; i < src.rows-radius; i++) {
for(int j=radius; j < src.cols-radius; j++) {
// calculate interpolated value
float t = w1*src.at<_Tp>(i+fy,j+fx) + w2*src.at<_Tp>(i+fy,j+cx) + w3*src.at<_Tp>(i+cy,j+fx) + w4*src.at<_Tp>(i+cy,j+cx);
// floating point precision, so check some machine-dependent epsilon
dst.at<int>(i-radius,j-radius) += ((t > src.at<_Tp>(i,j)) ||
(std::abs(t-src.at<_Tp>(i,j)) < std::numeric_limits<float>::epsilon()))
<< n;
}
}
}
for (int i = 0; i < dst.rows; ++i)
{
for (int j = 0; j < dst.cols; ++j)
{
int data = dst.at<int>(i, j);
vector<int>::iterator iter = find(m_uniform.begin(), m_uniform.end(), data);
if (iter == m_uniform.end())
{
dst.at<int>(i, j) = 0;
}
else
{
int new_data = iter - m_uniform.begin() ;
dst.at<int>(i, j) = new_data + 1;
}
}
}
}
示例11: computeRawCornerMat
void FeatureShiCorner::computeRawCornerMat( InputArray _image, OutputArray _corner )
{
// TODO check: _corner must be CV_32SC1
const Mat image = _image.getMat();
const int height = image.rows;
const int width = image.cols;
const int radius = 1;
Mat derX( height, width, CV_32SC1, Scalar( 0 ) );
Mat derY( height, width, CV_32SC1, Scalar( 0 ) );
Mat Mx2( height, width, CV_32SC1, Scalar( 0 ) );
Mat My2( height, width, CV_32SC1, Scalar( 0 ) );
Mat Mxy( height, width, CV_32SC1, Scalar( 0 ) );
applyFilter< uchar, int32_t >( _image, derX, &filter_derX[0][0], 3, 1, 0, true );
applyFilter< uchar, int32_t >( _image, derY, &filter_derY[0][0], 1, 3, 0, true );
int normDivisor = 0;
const int * pGauss = &FeatureShiCorner::filter_gauss[0][0];
int const * pGaussE = pGauss + 9;
for(; pGauss != pGaussE; pGauss++ )
{
normDivisor += abs( *pGauss );
}
int32_t maxVal = 0;
for( int y = 0; y < height; y++ )
{
for( int x = 0; x < width; x++ )
{
for( int dy = -radius; dy <= radius; dy++ )
{
for( int dx = -radius; dx <= radius; dx++ )
{
int fx = x + dx;
if( (fx < 0) || (fx >= width) ) { continue; }
int fy = y + dy;
if( (fy < 0) || (fy >= height) ) { continue; }
int f = FeatureShiCorner::filter_gauss[(radius + dx)][(radius + dy)];
Mx2.at< int32_t >( y, x ) += int32_t( f * pow( derX.at< int32_t >( fy, fx ), 2 ) );
My2.at< int32_t >( y, x ) += int32_t( f * pow( derY.at< int32_t >( fy, fx ), 2 ) );
Mxy.at< int32_t >( y, x ) += int32_t( f * derX.at< int32_t >( fy, fx ) * derY.at< int >( fy, fx ) );
}
}
Mx2.at< int32_t >( y, x ) /= normDivisor;
My2.at< int32_t >( y, x ) /= normDivisor;
Mxy.at< int32_t >( y, x ) /= normDivisor;
maxVal = max( Mx2.at< int32_t >( y, x ), maxVal );
maxVal = max( My2.at< int32_t >( y, x ), maxVal );
maxVal = max( Mxy.at< int32_t >( y, x ), maxVal );
}
}
Mat corners = _corner.getMat();
const auto it_cE = corners.end< int32_t >();
auto it_cS = corners.begin< int32_t >();
auto it_Mx2S = Mx2.begin< int32_t >();
auto it_My2S = My2.begin< int32_t >();
auto it_MxyS = Mxy.begin< int32_t >();
// reduce to high values if necessary
// maxval: 0..1 * 255^2, maxval^2 should not overflow for the next step
// reduce to sqrt( 2^31-1 (signed int) ) -> 46340
const int maxValC = 46340;
if( maxVal > maxValC )
{
cout << "maxVal > maxValC | maxVal: " << maxVal << endl;
const double scaleFac = maxValC / (double) maxVal; // scaleFac = 0.xxxx
while( it_cS != it_cE )
{
*it_cS *= int32_t( scaleFac );
*it_Mx2S *= int32_t( scaleFac );
*it_My2S *= int32_t( scaleFac );
*it_MxyS *= int32_t( scaleFac );
it_cS++;
it_Mx2S++;
it_My2S++;
it_MxyS++;
}
// reset iterators
it_cS = corners.begin< int32_t >();
it_Mx2S = Mx2.begin< int32_t >();
it_My2S = My2.begin< int32_t >();
it_MxyS = Mxy.begin< int32_t >();
}
maxVal = 0;
// calc eigenvalues
int32_t trc, det;
double ev_sqrt, trc_halve, eigVal1, eigVal2;
//.........这里部分代码省略.........
示例12: decolor
void cv::decolor(InputArray _src, OutputArray _dst, OutputArray _color_boost)
{
Mat I = _src.getMat();
_dst.create(I.size(), CV_8UC1);
Mat dst = _dst.getMat();
_color_boost.create(I.size(), CV_8UC3);
Mat color_boost = _color_boost.getMat();
if(!I.data )
{
cout << "Could not open or find the image" << endl ;
return;
}
if(I.channels() !=3)
{
cout << "Input Color Image" << endl;
return;
}
// Parameter Setting
int maxIter = 15;
int iterCount = 0;
double tol = .0001;
double E = 0;
double pre_E = std::numeric_limits<double>::infinity();
Decolor obj;
Mat img;
img = Mat(I.size(),CV_32FC3);
I.convertTo(img,CV_32FC3,1.0/255.0);
// Initialization
obj.init();
vector <double> Cg;
vector < vector <double> > polyGrad;
vector < vector < int > > comb;
vector <double> alf;
obj.grad_system(img,polyGrad,Cg,comb);
obj.weak_order(img,alf);
// Solver
Mat Mt = Mat(polyGrad.size(),polyGrad[0].size(), CV_32FC1);
obj.wei_update_matrix(polyGrad,Cg,Mt);
vector <double> wei;
obj.wei_inti(comb,wei);
//////////////////////////////// main loop starting ////////////////////////////////////////
while(sqrt(pow(E-pre_E,2)) > tol)
{
iterCount +=1;
pre_E = E;
vector <double> G_pos;
vector <double> G_neg;
vector <double> temp;
vector <double> temp1;
double val = 0.0;
for(unsigned int i=0;i< polyGrad[0].size();i++)
{
val = 0.0;
for(unsigned int j =0;j<polyGrad.size();j++)
val = val + (polyGrad[j][i] * wei[j]);
temp.push_back(val - Cg[i]);
temp1.push_back(val + Cg[i]);
}
double pos = 0.0;
double neg = 0.0;
for(unsigned int i =0;i<alf.size();i++)
{
pos = ((1 + alf[i])/2) * exp((-1.0 * 0.5 * pow(temp[i],2))/pow(obj.sigma,2));
neg = ((1 - alf[i])/2) * exp((-1.0 * 0.5 * pow(temp1[i],2))/pow(obj.sigma,2));
G_pos.push_back(pos);
G_neg.push_back(neg);
}
vector <double> EXPsum;
vector <double> EXPterm;
for(unsigned int i = 0;i<G_pos.size();i++)
EXPsum.push_back(G_pos[i]+G_neg[i]);
vector <double> temp2;
for(unsigned int i=0;i<EXPsum.size();i++)
{
if(EXPsum[i] == 0)
temp2.push_back(1.0);
else
temp2.push_back(0.0);
//.........这里部分代码省略.........
示例13: spatialGradient
void spatialGradient( InputArray _src, OutputArray _dx, OutputArray _dy,
int ksize, int borderType )
{
CV_INSTRUMENT_REGION()
// Prepare InputArray src
Mat src = _src.getMat();
CV_Assert( !src.empty() );
CV_Assert( src.type() == CV_8UC1 );
CV_Assert( borderType == BORDER_DEFAULT || borderType == BORDER_REPLICATE );
// Prepare OutputArrays dx, dy
_dx.create( src.size(), CV_16SC1 );
_dy.create( src.size(), CV_16SC1 );
Mat dx = _dx.getMat(),
dy = _dy.getMat();
// TODO: Allow for other kernel sizes
CV_Assert(ksize == 3);
// Get dimensions
const int H = src.rows,
W = src.cols;
// Row, column indices
int i = 0,
j = 0;
// Handle border types
int i_top = 0, // Case for H == 1 && W == 1 && BORDER_REPLICATE
i_bottom = H - 1,
j_offl = 0, // j offset from 0th pixel to reach -1st pixel
j_offr = 0; // j offset from W-1th pixel to reach Wth pixel
if ( borderType == BORDER_DEFAULT ) // Equiv. to BORDER_REFLECT_101
{
if ( H > 1 )
{
i_top = 1;
i_bottom = H - 2;
}
if ( W > 1 )
{
j_offl = 1;
j_offr = -1;
}
}
// Pointer to row vectors
uchar *p_src, *c_src, *n_src; // previous, current, next row
short *c_dx, *c_dy;
int i_start = 0;
int j_start = 0;
#if CV_SIMD128 && CV_SSE2
if(hasSIMD128())
{
uchar *m_src;
short *n_dx, *n_dy;
// Characters in variable names have the following meanings:
// u: unsigned char
// s: signed int
//
// [row][column]
// m: offset -1
// n: offset 0
// p: offset 1
// Example: umn is offset -1 in row and offset 0 in column
for ( i = 0; i < H - 1; i += 2 )
{
if ( i == 0 ) p_src = src.ptr<uchar>(i_top);
else p_src = src.ptr<uchar>(i-1);
c_src = src.ptr<uchar>(i);
n_src = src.ptr<uchar>(i+1);
if ( i == H - 2 ) m_src = src.ptr<uchar>(i_bottom);
else m_src = src.ptr<uchar>(i+2);
c_dx = dx.ptr<short>(i);
c_dy = dy.ptr<short>(i);
n_dx = dx.ptr<short>(i+1);
n_dy = dy.ptr<short>(i+1);
v_uint8x16 v_select_m = v_uint8x16(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0xFF);
// Process rest of columns 16-column chunks at a time
for ( j = 1; j < W - 16; j += 16 )
{
// Load top row for 3x3 Sobel filter
v_uint8x16 v_um = v_load(&p_src[j-1]);
v_uint8x16 v_up = v_load(&p_src[j+1]);
// TODO: Replace _mm_slli_si128 with hal method
v_uint8x16 v_un = v_select(v_select_m, v_uint8x16(_mm_slli_si128(v_up.val, 1)),
v_uint8x16(_mm_srli_si128(v_um.val, 1)));
v_uint16x8 v_um1, v_um2, v_un1, v_un2, v_up1, v_up2;
v_expand(v_um, v_um1, v_um2);
v_expand(v_un, v_un1, v_un2);
//.........这里部分代码省略.........
示例14: solvePnPRansac
void cv::solvePnPRansac(InputArray _opoints, InputArray _ipoints,
InputArray _cameraMatrix, InputArray _distCoeffs,
OutputArray _rvec, OutputArray _tvec, bool useExtrinsicGuess,
int iterationsCount, float reprojectionError, int minInliersCount,
OutputArray _inliers, int flags)
{
Mat opoints = _opoints.getMat(), ipoints = _ipoints.getMat();
Mat cameraMatrix = _cameraMatrix.getMat(), distCoeffs = _distCoeffs.getMat();
CV_Assert(opoints.isContinuous());
CV_Assert(opoints.depth() == CV_32F);
CV_Assert((opoints.rows == 1 && opoints.channels() == 3) || opoints.cols*opoints.channels() == 3);
CV_Assert(ipoints.isContinuous());
CV_Assert(ipoints.depth() == CV_32F);
CV_Assert((ipoints.rows == 1 && ipoints.channels() == 2) || ipoints.cols*ipoints.channels() == 2);
_rvec.create(3, 1, CV_64FC1);
_tvec.create(3, 1, CV_64FC1);
Mat rvec = _rvec.getMat();
Mat tvec = _tvec.getMat();
Mat objectPoints = opoints.reshape(3, 1), imagePoints = ipoints.reshape(2, 1);
if (minInliersCount <= 0)
minInliersCount = objectPoints.cols;
cv::pnpransac::Parameters params;
params.iterationsCount = iterationsCount;
params.minInliersCount = minInliersCount;
params.reprojectionError = reprojectionError;
params.useExtrinsicGuess = useExtrinsicGuess;
params.camera.init(cameraMatrix, distCoeffs);
params.flags = flags;
vector<int> localInliers;
Mat localRvec, localTvec;
rvec.copyTo(localRvec);
tvec.copyTo(localTvec);
if (objectPoints.cols >= pnpransac::MIN_POINTS_COUNT)
{
parallel_for(BlockedRange(0,iterationsCount), cv::pnpransac::PnPSolver(objectPoints, imagePoints, params,
localRvec, localTvec, localInliers));
}
if (localInliers.size() >= (size_t)pnpransac::MIN_POINTS_COUNT)
{
if (flags != CV_P3P)
{
int i, pointsCount = (int)localInliers.size();
Mat inlierObjectPoints(1, pointsCount, CV_32FC3), inlierImagePoints(1, pointsCount, CV_32FC2);
for (i = 0; i < pointsCount; i++)
{
int index = localInliers[i];
Mat colInlierImagePoints = inlierImagePoints(Rect(i, 0, 1, 1));
imagePoints.col(index).copyTo(colInlierImagePoints);
Mat colInlierObjectPoints = inlierObjectPoints(Rect(i, 0, 1, 1));
objectPoints.col(index).copyTo(colInlierObjectPoints);
}
solvePnP(inlierObjectPoints, inlierImagePoints, params.camera.intrinsics, params.camera.distortion, localRvec, localTvec, true, flags);
}
localRvec.copyTo(rvec);
localTvec.copyTo(tvec);
if (_inliers.needed())
Mat(localInliers).copyTo(_inliers);
}
else
{
tvec.setTo(Scalar(0));
Mat R = Mat::eye(3, 3, CV_64F);
Rodrigues(R, rvec);
if( _inliers.needed() )
_inliers.release();
}
return;
}
示例15: ipp_Deriv
static bool ipp_Deriv(InputArray _src, OutputArray _dst, int dx, int dy, int ksize, double scale, double delta, int borderType)
{
#ifdef HAVE_IPP_IW
CV_INSTRUMENT_REGION_IPP()
::ipp::IwiSize size(_src.size().width, _src.size().height);
IppDataType srcType = ippiGetDataType(_src.depth());
IppDataType dstType = ippiGetDataType(_dst.depth());
int channels = _src.channels();
bool useScale = false;
bool useScharr = false;
if(channels != _dst.channels() || channels > 1)
return false;
if(fabs(delta) > FLT_EPSILON || fabs(scale-1) > FLT_EPSILON)
useScale = true;
if(ksize <= 0)
{
ksize = 3;
useScharr = true;
}
IppiMaskSize maskSize = ippiGetMaskSize(ksize, ksize);
if((int)maskSize < 0)
return false;
#if IPP_VERSION_X100 <= 201703
// Bug with mirror wrap
if(borderType == BORDER_REFLECT_101 && (ksize/2+1 > size.width || ksize/2+1 > size.height))
return false;
#endif
IwiDerivativeType derivType = ippiGetDerivType(dx, dy, (useScharr)?false:true);
if((int)derivType < 0)
return false;
// Acquire data and begin processing
try
{
Mat src = _src.getMat();
Mat dst = _dst.getMat();
::ipp::IwiImage iwSrc = ippiGetImage(src);
::ipp::IwiImage iwDst = ippiGetImage(dst);
::ipp::IwiImage iwSrcProc = iwSrc;
::ipp::IwiImage iwDstProc = iwDst;
::ipp::IwiBorderSize borderSize(maskSize);
::ipp::IwiBorderType ippBorder(ippiGetBorder(iwSrc, borderType, borderSize));
if(!ippBorder)
return false;
if(srcType == ipp8u && dstType == ipp8u)
{
iwDstProc.Alloc(iwDst.m_size, ipp16s, channels);
useScale = true;
}
else if(srcType == ipp8u && dstType == ipp32f)
{
iwSrc -= borderSize;
iwSrcProc.Alloc(iwSrc.m_size, ipp32f, channels);
CV_INSTRUMENT_FUN_IPP(::ipp::iwiScale, iwSrc, iwSrcProc, 1, 0, ::ipp::IwiScaleParams(ippAlgHintFast));
iwSrcProc += borderSize;
}
if(useScharr)
CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterScharr, iwSrcProc, iwDstProc, derivType, maskSize, ::ipp::IwDefault(), ippBorder);
else
CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterSobel, iwSrcProc, iwDstProc, derivType, maskSize, ::ipp::IwDefault(), ippBorder);
if(useScale)
CV_INSTRUMENT_FUN_IPP(::ipp::iwiScale, iwDstProc, iwDst, scale, delta, ::ipp::IwiScaleParams(ippAlgHintFast));
}
catch (::ipp::IwException)
{
return false;
}
return true;
#else
CV_UNUSED(_src); CV_UNUSED(_dst); CV_UNUSED(dx); CV_UNUSED(dy); CV_UNUSED(ksize); CV_UNUSED(scale); CV_UNUSED(delta); CV_UNUSED(borderType);
return false;
#endif
}