Skip to content

Commit 0200571

Browse files
authored
attempt to add 0d/1d mat support (#3511)
* fixed BIMEF test failures and very slightly optimized (there is a _lot_ of room for the further optimization) * dummy commit to rerun tests * fixed contrib tests * seem to fixed one more opencv_face test * maybe adding some whitespaces will help? ) * hopefully fixed the remaining test failures
1 parent f23a567 commit 0200571

File tree

10 files changed

+64
-53
lines changed

10 files changed

+64
-53
lines changed

modules/cudaimgproc/test/test_histogram.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ CUDA_TEST_P(HistEven, Accuracy)
107107
Mat srcRoi = src(roi);
108108
cv::calcHist(&srcRoi, 1, channels, cv::Mat(), hist_gold, 1, histSize, ranges);
109109

110-
hist_gold = hist_gold.t();
111110
hist_gold.convertTo(hist_gold, CV_32S);
112111

113112
EXPECT_MAT_NEAR(hist_gold, hist, 0.0);

modules/cudev/test/test_nd.cu

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ public:
4141
else
4242
ret.push_back(Range::all());
4343

44-
if (dims == 1)
45-
{
46-
// Mat expects two ranges even in this case
47-
ret.push_back(Range::all());
48-
}
49-
5044
return ret;
5145
}
5246

modules/face/src/facemarkAAM.cpp

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ void FacemarkAAMImpl::training(void* parameters){
243243

244244
/* get PCA Projection vectors */
245245
Mat S;
246-
getProjection(M.t(),S,param_max_n);
246+
getProjection(M.t(), S, param_max_n);
247247
/* Create similarity eig*/
248248
Mat shape_S,shape_Q;
249249
calcSimilarityEig(AAM.s0,S,AAM.Q,AAM.S);
@@ -259,7 +259,7 @@ void FacemarkAAMImpl::training(void* parameters){
259259

260260
/*get the min and max of x and y coordinate*/
261261
double min_x, max_x, min_y, max_y;
262-
s0_scaled_m = s0_scaled_m.reshape(1);
262+
s0_scaled_m = s0_scaled_m.reshape(1, (int)s0_scaled_m.total());
263263
Mat s0_scaled_x = s0_scaled_m.col(0);
264264
Mat s0_scaled_y = s0_scaled_m.col(1);
265265
minMaxIdx(s0_scaled_x, &min_x, &max_x);
@@ -287,7 +287,7 @@ void FacemarkAAMImpl::training(void* parameters){
287287
if(params.verbose) printf("extract features from image #%i/%i\n", (int)(i+1), (int)images.size());
288288
warped = warpImage(images[i],base_shape, facePoints[i], AAM.triangles, AAM.textures[scale].resolution,AAM.textures[scale].textureIdx);
289289
feat = getFeature<uchar>(warped, AAM.textures[scale].ind1);
290-
texture_feats.push_back(feat.t());
290+
texture_feats.push_back(feat.reshape(feat.channels(), 1));
291291
}
292292
Mat T= texture_feats.t();
293293

@@ -307,7 +307,7 @@ void FacemarkAAMImpl::training(void* parameters){
307307
for(int i =0;i<AAM.textures[scale].A.cols;i++){
308308
Mat c = AAM.textures[scale].A.col(i);
309309
ud = getFeature<float>(c,fe_map);
310-
U_data.push_back(ud.t());
310+
U_data.push_back(ud.reshape(ud.channels(), 1));
311311
}
312312
Mat U = U_data.t();
313313
AAM.textures[scale].AA = orthonormal(U);
@@ -413,7 +413,7 @@ bool FacemarkAAMImpl::fitImpl( const Mat image, std::vector<Point2f>& landmarks,
413413
Mat Wx_dp, Wy_dp;
414414
createWarpJacobian(S, AAM.Q, AAM.triangles, AAM.textures[scl],Wx_dp, Wy_dp, Tp);
415415

416-
std::vector<Point2f> s0_init = Mat(Mat(R*scale*AAM.scales[scl]*Mat(Mat(s0).reshape(1)).t()).t()).reshape(2);
416+
std::vector<Point2f> s0_init = Mat(Mat(R*scale*AAM.scales[scl]*Mat(Mat(s0).reshape(1, (int)s0.size())).t()).t()).reshape(2);
417417
std::vector<Point2f> curr_shape = Mat(Mat(s0_init)+Scalar(T.x,T.y));
418418
curr_shape = Mat(1.0/scale*Mat(curr_shape)).reshape(2);
419419

@@ -444,8 +444,8 @@ bool FacemarkAAMImpl::fitImpl( const Mat image, std::vector<Point2f>& landmarks,
444444
AAM.textures[scl].resolution ,
445445
AAM.textures[scl].textureIdx);
446446

447-
I = getFeature<uchar>(warped, AAM.textures[scl].ind1);
448-
II = getFeature<uchar>(warped, AAM.textures[scl].ind2);
447+
I = getFeature<uchar>(warped, AAM.textures[scl].ind1).t();
448+
II = getFeature<uchar>(warped, AAM.textures[scl].ind2).t();
449449

450450
if(t==0){
451451
c = A.t()*(I-AAM.textures[scl].A0); //little bit different to matlab, probably due to datatype
@@ -480,8 +480,8 @@ bool FacemarkAAMImpl::fitImpl( const Mat image, std::vector<Point2f>& landmarks,
480480
invert(Hfsic, iHfsic);
481481

482482
/*compute dp dq and dc*/
483-
Mat dqp = iHfsic*Jfsic.t()*(II-AAM.textures[scl].AA0);
484-
dc = AA.t()*(II-Mat(Irec_vec)-J*dqp);
483+
Mat dqp = iHfsic*Jfsic.t()*(II-AAM.textures[scl].AA0.t());
484+
dc = AA.t()*(II-Mat(Irec_vec).t()-J*dqp);
485485
warpUpdate(curr_shape, dqp, s0,S, AAM.Q, AAM.triangles,Tp);
486486
}
487487
landmarks = Mat(scale*Mat(curr_shape)).reshape(2);
@@ -602,16 +602,17 @@ Mat FacemarkAAMImpl::procrustes(std::vector<Point2f> P, std::vector<Point2f> Q,
602602
reduce(Ys,sumYs, 0, REDUCE_SUM);
603603

604604
//calculate the normrnd
605-
double normX = sqrt(Mat(sumXs.reshape(1)).at<float>(0)+Mat(sumXs.reshape(1)).at<float>(1));
606-
double normY = sqrt(Mat(sumYs.reshape(1)).at<float>(0)+Mat(sumYs.reshape(1)).at<float>(1));
605+
Point2f sumXs0 = sumXs.at<Point2f>(0), sumYs0 = sumYs.at<Point2f>(0);
606+
double normX = sqrt(sumXs0.x + sumXs0.y);
607+
double normY = sqrt(sumYs0.x + sumYs0.y);
607608

608609
//normalization
609610
X0 = X0/normX;
610611
Y0 = Y0/normY;
611612

612613
//reshape, convert to 2D Matrix
613-
Mat Xn=X0.reshape(1);
614-
Mat Yn=Y0.reshape(1);
614+
Mat Xn=X0.reshape(1, (int)X0.total());
615+
Mat Yn=Y0.reshape(1, (int)Y0.total());
615616

616617
//calculate the covariance matrix
617618
Mat M = Xn.t()*Yn;
@@ -634,7 +635,7 @@ Mat FacemarkAAMImpl::procrustes(std::vector<Point2f> P, std::vector<Point2f> Q,
634635
trans[1] = t.at<float>(1);
635636

636637
// calculate the recovered form
637-
Mat Qmat = Mat(Q).reshape(1);
638+
Mat Qmat = Mat(Q).reshape(1, (int)Q.size());
638639

639640
return Mat(scale*Qmat*rot+trans).clone();
640641
}
@@ -777,7 +778,7 @@ Mat FacemarkAAMImpl::orthonormal(Mat Mo){
777778
return O.colRange(0,k).clone();
778779
}
779780

780-
void FacemarkAAMImpl::calcSimilarityEig(std::vector<Point2f> s0,Mat S, Mat & Q_orth, Mat & S_orth){
781+
void FacemarkAAMImpl::calcSimilarityEig(std::vector<Point2f> s0, Mat S, Mat & Q_orth, Mat & S_orth){
781782
int npts = (int)s0.size();
782783

783784
Mat Q = Mat::zeros(2*npts,4,CV_32FC1);
@@ -792,7 +793,7 @@ void FacemarkAAMImpl::calcSimilarityEig(std::vector<Point2f> s0,Mat S, Mat & Q_o
792793
w.copyTo(c0);
793794

794795
/*c1 = [-s0(npts:2*npts); s0(0:npts-1)]*/
795-
Mat s0_mat = Mat(s0).reshape(1);
796+
Mat s0_mat = Mat(s0).reshape(1, (int)s0.size());
796797
// s0_mat.convertTo(s0_mat, CV_64FC1);
797798
Mat swapper = Mat::zeros(2,npts,CV_32FC1);
798799
Mat s00 = s0_mat.col(0);
@@ -826,11 +827,15 @@ void FacemarkAAMImpl::calcSimilarityEig(std::vector<Point2f> s0,Mat S, Mat & Q_o
826827
Mat allOrth = orthonormal(all.t());
827828
Q_orth = allOrth.colRange(0,4).clone();
828829
S_orth = allOrth.colRange(4,allOrth.cols).clone();
829-
830830
}
831831

832832
inline Mat FacemarkAAMImpl::linearize(Mat s){ // all x values and then all y values
833-
return Mat(s.reshape(1).t()).reshape(1,2*s.rows);
833+
int npoints = s.rows;
834+
if (s.channels() > 1) {
835+
npoints = (int)s.total();
836+
s = s.reshape(1, npoints);
837+
}
838+
return Mat(s.t()).reshape(1, 2*npoints);
834839
}
835840
inline Mat FacemarkAAMImpl::linearize(std::vector<Point2f> s){ // all x values and then all y values
836841
return linearize(Mat(s));
@@ -843,7 +848,7 @@ void FacemarkAAMImpl::delaunay(std::vector<Point2f> s, std::vector<Vec3i> & tria
843848
std::vector<Vec6f> tp;
844849

845850
double min_x, max_x, min_y, max_y;
846-
Mat S = Mat(s).reshape(1);
851+
Mat S = Mat(s).reshape(1, (int)s.size());
847852
Mat s_x = S.col(0);
848853
Mat s_y = S.col(1);
849854
minMaxIdx(s_x, &min_x, &max_x);
@@ -954,8 +959,8 @@ Mat FacemarkAAMImpl::warpImage(
954959
source[1] = curr_shape[triangles[i][1]];
955960
source[2] = curr_shape[triangles[i][2]];
956961

957-
Mat target_mtx = Mat(target).reshape(1)-1.0;
958-
Mat source_mtx = Mat(source).reshape(1)-1.0;
962+
Mat target_mtx = Mat(target).reshape(1,(int)target.size())-1.0;
963+
Mat source_mtx = Mat(source).reshape(1,(int)source.size())-1.0;
959964
Mat U = target_mtx.col(0);
960965
Mat V = target_mtx.col(1);
961966
Mat X = source_mtx.col(0);
@@ -979,7 +984,7 @@ Mat FacemarkAAMImpl::warpImage(
979984
R=A.colRange(0,2);
980985
t=A.colRange(2,3);
981986

982-
Mat pts_ori = Mat(textureIdx[i]).reshape(1);
987+
Mat pts_ori = Mat(textureIdx[i]).reshape(1, (int)textureIdx[i].size());
983988
Mat pts = pts_ori.t(); //matlab
984989
Mat bx = pts_ori.col(0);
985990
Mat by = pts_ori.col(1);
@@ -1081,8 +1086,9 @@ void FacemarkAAMImpl::warpUpdate(std::vector<Point2f> & shape, Mat delta, std::v
10811086
Mat(ds0, Range((int)s0.size(),(int)s0.size()*2)).copyTo(c1);
10821087

10831088
Mat s_new = computeWarpParts(shape,s0,ds0_mat, triangles, Tp);
1089+
s_new -= Mat(s0).reshape(1, (int)s0.size());
10841090

1085-
Mat diff =linearize(Mat(s_new - Mat(s0).reshape(1)));
1091+
Mat diff = linearize(s_new);
10861092

10871093
Mat r = Q.t()*diff;
10881094
Mat p = S.t()*diff;
@@ -1118,8 +1124,9 @@ Mat FacemarkAAMImpl::computeWarpParts(std::vector<Point2f> curr_shape,std::vecto
11181124
source[2] = curr_shape[triangles[idx][2]];
11191125

11201126
A = getAffineTransform(target,source);
1127+
Mat p_m = Mat(p).reshape(1, (int)p.size());
11211128

1122-
Mat(A*Mat(p)).reshape(2).copyTo(v);
1129+
Mat(A*p_m).reshape(2).copyTo(v);
11231130
vx.push_back(v[0].x);
11241131
vy.push_back(v[0].y);
11251132
}// j
@@ -1134,7 +1141,7 @@ Mat FacemarkAAMImpl::computeWarpParts(std::vector<Point2f> curr_shape,std::vecto
11341141
new_shape.push_back(Point2f(mx,my));
11351142
} // s0.size()
11361143

1137-
return Mat(new_shape).reshape(1).clone();
1144+
return Mat(new_shape).reshape(1, (int)new_shape.size()).clone();
11381145
}
11391146

11401147
void FacemarkAAMImpl::gradient(const Mat M, Mat & gx, Mat & gy){

modules/face/src/facemarkLBF.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ void FacemarkLBFImpl::prepareTrainingData(Mat img, std::vector<Point2f> facePoin
536536
std::vector<Mat> & cropped, std::vector<Mat> & shapes, std::vector<BBox> &boxes)
537537
{
538538
Mat shape;
539-
Mat _shape = Mat(facePoints).reshape(1);
539+
Mat _shape = Mat(facePoints).reshape(1, (int)facePoints.size());
540540
Rect box = getBBox(img, _shape);
541541

542542
if(img.channels()>1){
@@ -1359,7 +1359,7 @@ Mat FacemarkLBFImpl::Regressor::supportVectorRegression(
13591359
if(verbose) printf("Objective value = %lf\n", v);
13601360
if(verbose) printf("nSV = %d\n",nSV);
13611361

1362-
return Mat(Mat(w).t()).clone();
1362+
return Mat(w).reshape(2, 1).clone();
13631363

13641364
}//end
13651365

modules/face/test/test_facemark_aam.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ TEST(CV_Face_FacemarkAAM, test_workflow) {
108108

109109
Mat image;
110110
std::vector<Point2f> landmarks;
111-
for(size_t i=0;i<images_train.size();i++)
111+
for(size_t i = 0; i < images_train.size(); i++)
112112
{
113113
image = imread(images_train[i].c_str());
114114
EXPECT_TRUE(loadFacePoints(points_train[i].c_str(),landmarks));

modules/intensity_transform/src/bimef.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,13 @@ static Mat_<float> applyK(const Mat_<float>& I, float k, float a=-0.3293f, float
265265
float gamma = std::pow(k, a);
266266

267267
Mat_<float> J(I.size());
268-
pow(I, gamma, J);
269-
J = J*beta;
270-
268+
CV_Assert(I.isContinuous());
269+
size_t i, npix = I.total();
270+
const float* Iptr = I.ptr<float>();
271+
float* Jptr = J.ptr<float>();
272+
for (i = 0; i < npix; i++) {
273+
Jptr[i] = pow(Iptr[i], gamma)*beta;
274+
}
271275
return J;
272276
}
273277

@@ -293,15 +297,18 @@ static float entropy(const Mat_<float>& I)
293297
float range[] = { 0, 256 };
294298
const float* histRange = { range };
295299
calcHist(&I_uchar, 1, NULL, Mat(), hist, 1, &histSize, &histRange);
300+
double histsum = cv::sum(hist)[0];
296301

297-
Mat_<float> hist_norm = hist / cv::sum(hist)[0];
302+
Mat_<float> hist_norm = hist / histsum;
303+
int i, nbins = (int)hist_norm.total();
298304

299305
float E = 0;
300-
for (int i = 0; i < hist_norm.rows; i++)
306+
for (i = 0; i < nbins; i++)
301307
{
302-
if (hist_norm(i,0) > 0)
308+
float v = hist_norm(i);
309+
if (v > 0)
303310
{
304-
E += hist_norm(i,0) * std::log2(hist_norm(i,0));
311+
E += v * std::log2(v);
305312
}
306313
}
307314

@@ -487,7 +494,7 @@ static void BIMEF_impl(InputArray input_, OutputArray output_, float mu, float *
487494
// t: scene illumination map
488495
Mat_<float> t_b(imgDouble.size());
489496
t_b.forEach(
490-
[&](float &pixel, const int * position) -> void
497+
[&](float &pixel, const int* position) -> void
491498
{
492499
pixel = std::max(std::max(imgDouble(position[0], position[1])[0],
493500
imgDouble(position[0], position[1])[1]),

modules/mcc/src/colorspace.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,14 @@ void RGBBase_::calM()
120120
XYZg = Mat(xyY2XYZ({ xg, yg }), true);
121121
XYZb = Mat(xyY2XYZ({ xb, yb }), true);
122122
merge(std::vector<Mat> { XYZr, XYZg, XYZb }, XYZ_rgbl);
123-
XYZ_rgbl = XYZ_rgbl.reshape(1, XYZ_rgbl.rows);
123+
XYZ_rgbl = XYZ_rgbl.reshape(1, (int)XYZ_rgbl.total());
124124
Mat XYZw = Mat(getIlluminants(io), true);
125+
XYZw = XYZw.reshape(1, (int)XYZw.total());
125126
solve(XYZ_rgbl, XYZw, Srgb);
126127
merge(std::vector<Mat> { Srgb.at<double>(0) * XYZr, Srgb.at<double>(1) * XYZg,
127128
Srgb.at<double>(2) * XYZb },
128129
M_to);
129-
M_to = M_to.reshape(1, M_to.rows);
130+
M_to = M_to.reshape(1, (int)M_to.total());
130131
M_from = M_to.inv();
131132
};
132133

@@ -382,6 +383,8 @@ Mat XYZ::cam_(IO sio, IO dio, CAM method) const
382383
// Function from http://www.brucelindbloom.com/index.html?ColorCheckerRGB.html.
383384
Mat XYZws = Mat(getIlluminants(dio));
384385
Mat XYZWd = Mat(getIlluminants(sio));
386+
XYZws = XYZws.reshape(1, (int)XYZws.total());
387+
XYZWd = XYZWd.reshape(1, (int)XYZWd.total());
385388
Mat MA = MAs.at(method)[0];
386389
Mat MA_inv = MAs.at(method)[1];
387390
Mat M = MA_inv * Mat::diag((MA * XYZws) / (MA * XYZWd)) * MA;

modules/rapid/src/rapid.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void extractLineBundle(int len, InputArray ctl2d, InputArray img, OutputArray bu
174174
CV_Assert(ctl2d.getMat().checkVector(2, CV_32F) > 0);
175175
Mat_<Point2f> contour = ctl2d.getMat();
176176

177-
const int N = contour.rows;
177+
const int N = (int)contour.total();
178178
const int W = len * 2 + 1;
179179

180180
srcLocations.create(N, W, CV_16SC2);
@@ -305,8 +305,8 @@ void convertCorrespondencies(InputArray _cols, InputArray _srcLocations, OutputA
305305
Mat opts3d;
306306
if(!_pts3d.empty())
307307
{
308-
CV_Assert(_cols.rows() == _pts3d.rows());
309-
pts3d = _pts3d.getMat();
308+
pts3d = _pts3d.getMat().t();
309+
CV_Assert(cols.rows == pts3d.rows);
310310
opts3d.create(0, 1, pts3d.type());
311311
opts3d.reserve(cols.rows);
312312
}

modules/wechat_qrcode/test/test_qrcode.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ TEST(Objdetect_QRCode_points_position, rotate45) {
321321
auto decoded_info1 = detector.detectAndDecode(image, points1);
322322
ASSERT_EQ(1ull, decoded_info1.size());
323323
ASSERT_EQ(expect_msg, decoded_info1[0]);
324-
EXPECT_NEAR(0, cvtest::norm(Mat(goldCorners), points1[0].reshape(1, 8), NORM_INF), 8.);
324+
EXPECT_NEAR(0, cvtest::norm(Mat(goldCorners).reshape(1, (int)goldCorners.size()), points1[0].reshape(1, 8), NORM_INF), 8.);
325325

326326
const double angle = 45;
327327
Point2f pc(image.cols/2.f, image.rows/2.f);
@@ -338,7 +338,7 @@ TEST(Objdetect_QRCode_points_position, rotate45) {
338338
auto decoded_info2 = detector.detectAndDecode(image, points2);
339339
ASSERT_EQ(1ull, decoded_info2.size());
340340
ASSERT_EQ(expect_msg, decoded_info2[0]);
341-
EXPECT_NEAR(0, cvtest::norm(Mat(rotateGoldCorners), points2[0].reshape(1, 8), NORM_INF), 11.);
341+
EXPECT_NEAR(0, cvtest::norm(Mat(rotateGoldCorners).reshape(1, (int)rotateGoldCorners.size()), points2[0].reshape(1, 8), NORM_INF), 11.);
342342
}
343343

344344
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode, testing::ValuesIn(qrcode_images_name));

modules/xphoto/test/test_oil_painting.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Mat testOilPainting(Mat imgSrc, int halfSize, int dynRatio, int colorSpace)
4141
double maxVal = 0;
4242
Point pMin, pMax;
4343
minMaxLoc(hist, 0, &maxVal, &pMin, &pMax);
44-
mask.setTo(0, lum != static_cast<int>(pMax.y));
44+
mask.setTo(0, lum != static_cast<int>(pMax.x));
4545
Scalar v = mean(imgSrc, mask);
4646
*vDst = Vec3b(static_cast<uchar>(v[0]), static_cast<uchar>(v[1]), static_cast<uchar>(v[2]));
4747
}
@@ -65,7 +65,7 @@ Mat testOilPainting(Mat imgSrc, int halfSize, int dynRatio, int colorSpace)
6565
double maxVal = 0;
6666
Point pMin, pMax;
6767
minMaxLoc(hist, 0, &maxVal, &pMin, &pMax);
68-
mask.setTo(0, lum != static_cast<int>(pMax.y));
68+
mask.setTo(0, lum != static_cast<int>(pMax.x));
6969
Scalar v = mean(imgSrc, mask);
7070
*vDst = static_cast<uchar>(v[0]);
7171
}
@@ -90,7 +90,8 @@ TEST(xphoto_oil_painting, regression)
9090
double maxVal;
9191
Point pIdx;
9292
minMaxLoc(p, NULL, &maxVal, NULL, &pIdx);
93-
ASSERT_LE(p.at<uchar>(pIdx), 2);
93+
int v = p.at<uchar>(pIdx);
94+
ASSERT_LE(v, 2);
9495
}
9596
Mat orig2 = imread(folder + "exp1.png",IMREAD_GRAYSCALE);
9697
ASSERT_TRUE(!orig2.empty());

0 commit comments

Comments
 (0)