Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attempt to add 0d/1d mat support #3511

Merged
merged 6 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion modules/cudaimgproc/test/test_histogram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ CUDA_TEST_P(HistEven, Accuracy)
int channels[] = {0};
cv::calcHist(&src, 1, channels, cv::Mat(), hist_gold, 1, histSize, ranges);

hist_gold = hist_gold.t();
hist_gold.convertTo(hist_gold, CV_32S);

EXPECT_MAT_NEAR(hist_gold, hist, 0.0);
Expand Down
2 changes: 2 additions & 0 deletions modules/cudev/include/opencv2/cudev/util/vec_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ template<> struct VecTraits<char4>

namespace cv {

#ifndef CV_32U
template <> class DataType<uint>
{
public:
Expand All @@ -202,6 +203,7 @@ template <> class DataType<uint>
type = CV_MAKE_TYPE(depth, channels)
};
};
#endif

#define CV_CUDEV_DATA_TYPE_INST(_depth_type, _channel_num) \
template <> class DataType< _depth_type ## _channel_num > \
Expand Down
6 changes: 0 additions & 6 deletions modules/cudev/test/test_nd.cu
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ public:
else
ret.push_back(Range::all());

if (dims == 1)
{
// Mat expects two ranges even in this case
ret.push_back(Range::all());
}

return ret;
}

Expand Down
57 changes: 32 additions & 25 deletions modules/face/src/facemarkAAM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ void FacemarkAAMImpl::training(void* parameters){

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

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

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

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

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

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

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

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

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

//normalization
X0 = X0/normX;
Y0 = Y0/normY;

//reshape, convert to 2D Matrix
Mat Xn=X0.reshape(1);
Mat Yn=Y0.reshape(1);
Mat Xn=X0.reshape(1, (int)X0.total());
Mat Yn=Y0.reshape(1, (int)Y0.total());

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

// calculate the recovered form
Mat Qmat = Mat(Q).reshape(1);
Mat Qmat = Mat(Q).reshape(1, (int)Q.size());

return Mat(scale*Qmat*rot+trans).clone();
}
Expand Down Expand Up @@ -777,7 +778,7 @@ Mat FacemarkAAMImpl::orthonormal(Mat Mo){
return O.colRange(0,k).clone();
}

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

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

/*c1 = [-s0(npts:2*npts); s0(0:npts-1)]*/
Mat s0_mat = Mat(s0).reshape(1);
Mat s0_mat = Mat(s0).reshape(1, (int)s0.size());
// s0_mat.convertTo(s0_mat, CV_64FC1);
Mat swapper = Mat::zeros(2,npts,CV_32FC1);
Mat s00 = s0_mat.col(0);
Expand Down Expand Up @@ -826,11 +827,15 @@ void FacemarkAAMImpl::calcSimilarityEig(std::vector<Point2f> s0,Mat S, Mat & Q_o
Mat allOrth = orthonormal(all.t());
Q_orth = allOrth.colRange(0,4).clone();
S_orth = allOrth.colRange(4,allOrth.cols).clone();

}

inline Mat FacemarkAAMImpl::linearize(Mat s){ // all x values and then all y values
return Mat(s.reshape(1).t()).reshape(1,2*s.rows);
int npoints = s.rows;
if (s.channels() > 1) {
npoints = (int)s.total();
s = s.reshape(1, npoints);
}
return Mat(s.t()).reshape(1, 2*npoints);
}
inline Mat FacemarkAAMImpl::linearize(std::vector<Point2f> s){ // all x values and then all y values
return linearize(Mat(s));
Expand All @@ -843,7 +848,7 @@ void FacemarkAAMImpl::delaunay(std::vector<Point2f> s, std::vector<Vec3i> & tria
std::vector<Vec6f> tp;

double min_x, max_x, min_y, max_y;
Mat S = Mat(s).reshape(1);
Mat S = Mat(s).reshape(1, (int)s.size());
Mat s_x = S.col(0);
Mat s_y = S.col(1);
minMaxIdx(s_x, &min_x, &max_x);
Expand Down Expand Up @@ -954,8 +959,8 @@ Mat FacemarkAAMImpl::warpImage(
source[1] = curr_shape[triangles[i][1]];
source[2] = curr_shape[triangles[i][2]];

Mat target_mtx = Mat(target).reshape(1)-1.0;
Mat source_mtx = Mat(source).reshape(1)-1.0;
Mat target_mtx = Mat(target).reshape(1,(int)target.size())-1.0;
Mat source_mtx = Mat(source).reshape(1,(int)source.size())-1.0;
Mat U = target_mtx.col(0);
Mat V = target_mtx.col(1);
Mat X = source_mtx.col(0);
Expand All @@ -979,7 +984,7 @@ Mat FacemarkAAMImpl::warpImage(
R=A.colRange(0,2);
t=A.colRange(2,3);

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

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

Mat diff =linearize(Mat(s_new - Mat(s0).reshape(1)));
Mat diff = linearize(s_new);

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

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

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

return Mat(new_shape).reshape(1).clone();
return Mat(new_shape).reshape(1, (int)new_shape.size()).clone();
}

void FacemarkAAMImpl::gradient(const Mat M, Mat & gx, Mat & gy){
Expand Down
4 changes: 2 additions & 2 deletions modules/face/src/facemarkLBF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ void FacemarkLBFImpl::prepareTrainingData(Mat img, std::vector<Point2f> facePoin
std::vector<Mat> & cropped, std::vector<Mat> & shapes, std::vector<BBox> &boxes)
{
Mat shape;
Mat _shape = Mat(facePoints).reshape(1);
Mat _shape = Mat(facePoints).reshape(1, (int)facePoints.size());
Rect box = getBBox(img, _shape);

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

return Mat(Mat(w).t()).clone();
return Mat(w).reshape(2, 1).clone();

}//end

Expand Down
2 changes: 1 addition & 1 deletion modules/face/test/test_facemark_aam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ TEST(CV_Face_FacemarkAAM, test_workflow) {

Mat image;
std::vector<Point2f> landmarks;
for(size_t i=0;i<images_train.size();i++)
for(size_t i = 0; i < images_train.size(); i++)
{
image = imread(images_train[i].c_str());
EXPECT_TRUE(loadFacePoints(points_train[i].c_str(),landmarks));
Expand Down
25 changes: 15 additions & 10 deletions modules/intensity_transform/src/bimef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,13 @@ static Mat_<float> applyK(const Mat_<float>& I, float k, float a=-0.3293f, float
float gamma = std::pow(k, a);

Mat_<float> J(I.size());
pow(I, gamma, J);
J = J*beta;

CV_Assert(I.isContinuous());
size_t i, npix = I.total();
const float* Iptr = I.ptr<float>();
float* Jptr = J.ptr<float>();
for (i = 0; i < npix; i++) {
Jptr[i] = pow(Iptr[i], gamma)*beta;
}
return J;
}

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

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

float E = 0;
for (int i = 0; i < hist_norm.rows; i++)
for (i = 0; i < nbins; i++)
{
if (hist_norm(i,0) > 0)
float v = hist_norm(i);
if (v > 0)
{
E += hist_norm(i,0) * std::log2(hist_norm(i,0));
E += v * std::log2(v);
}
}

Expand Down Expand Up @@ -345,7 +352,6 @@ static double minimize_scalar_bounded(const Mat_<float>& I, double begin, double
double rat = 0.0, e = 0.0;
double x = xf;
double fx = -entropy(applyK(I, static_cast<float>(x)));
int num = 1;
double fu = std::numeric_limits<double>::infinity();

double ffulc = fx, fnfc = fx;
Expand Down Expand Up @@ -398,7 +404,6 @@ static double minimize_scalar_bounded(const Mat_<float>& I, double begin, double
double si = sgn(rat) + (rat == 0);
x = xf + si * std::max(std::abs(rat), tol1);
fu = -entropy(applyK(I, static_cast<float>(x)));
num += 1;

if (fu <= fx) {
if (x >= xf) {
Expand Down Expand Up @@ -489,7 +494,7 @@ static void BIMEF_impl(InputArray input_, OutputArray output_, float mu, float *
// t: scene illumination map
Mat_<float> t_b(imgDouble.size());
t_b.forEach(
[&](float &pixel, const int * position) -> void
[&](float &pixel, const int* position) -> void
{
pixel = std::max(std::max(imgDouble(position[0], position[1])[0],
imgDouble(position[0], position[1])[1]),
Expand Down
7 changes: 5 additions & 2 deletions modules/mcc/src/colorspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,14 @@ void RGBBase_::calM()
XYZg = Mat(xyY2XYZ({ xg, yg }), true);
XYZb = Mat(xyY2XYZ({ xb, yb }), true);
merge(std::vector<Mat> { XYZr, XYZg, XYZb }, XYZ_rgbl);
XYZ_rgbl = XYZ_rgbl.reshape(1, XYZ_rgbl.rows);
XYZ_rgbl = XYZ_rgbl.reshape(1, (int)XYZ_rgbl.total());
Mat XYZw = Mat(getIlluminants(io), true);
XYZw = XYZw.reshape(1, (int)XYZw.total());
solve(XYZ_rgbl, XYZw, Srgb);
merge(std::vector<Mat> { Srgb.at<double>(0) * XYZr, Srgb.at<double>(1) * XYZg,
Srgb.at<double>(2) * XYZb },
M_to);
M_to = M_to.reshape(1, M_to.rows);
M_to = M_to.reshape(1, (int)M_to.total());
M_from = M_to.inv();
};

Expand Down Expand Up @@ -382,6 +383,8 @@ Mat XYZ::cam_(IO sio, IO dio, CAM method) const
// Function from http://www.brucelindbloom.com/index.html?ColorCheckerRGB.html.
Mat XYZws = Mat(getIlluminants(dio));
Mat XYZWd = Mat(getIlluminants(sio));
XYZws = XYZws.reshape(1, (int)XYZws.total());
XYZWd = XYZWd.reshape(1, (int)XYZWd.total());
Mat MA = MAs.at(method)[0];
Mat MA_inv = MAs.at(method)[1];
Mat M = MA_inv * Mat::diag((MA * XYZws) / (MA * XYZWd)) * MA;
Expand Down
6 changes: 3 additions & 3 deletions modules/rapid/src/rapid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void extractLineBundle(int len, InputArray ctl2d, InputArray img, OutputArray bu
CV_Assert(ctl2d.getMat().checkVector(2, CV_32F) > 0);
Mat_<Point2f> contour = ctl2d.getMat();

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

srcLocations.create(N, W, CV_16SC2);
Expand Down Expand Up @@ -305,8 +305,8 @@ void convertCorrespondencies(InputArray _cols, InputArray _srcLocations, OutputA
Mat opts3d;
if(!_pts3d.empty())
{
CV_Assert(_cols.rows() == _pts3d.rows());
pts3d = _pts3d.getMat();
pts3d = _pts3d.getMat().t();
CV_Assert(cols.rows == pts3d.rows);
opts3d.create(0, 1, pts3d.type());
opts3d.reserve(cols.rows);
}
Expand Down
4 changes: 2 additions & 2 deletions modules/wechat_qrcode/test/test_qrcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ TEST(Objdetect_QRCode_points_position, rotate45) {
auto decoded_info1 = detector.detectAndDecode(image, points1);
ASSERT_EQ(1ull, decoded_info1.size());
ASSERT_EQ(expect_msg, decoded_info1[0]);
EXPECT_NEAR(0, cvtest::norm(Mat(goldCorners), points1[0].reshape(1, 8), NORM_INF), 8.);
EXPECT_NEAR(0, cvtest::norm(Mat(goldCorners).reshape(1, (int)goldCorners.size()), points1[0].reshape(1, 8), NORM_INF), 8.);

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

INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode, testing::ValuesIn(qrcode_images_name));
Expand Down
Loading