-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmexResize.cpp
executable file
·65 lines (54 loc) · 1.69 KB
/
mexResize.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <math.h>
#include <assert.h>
#include <string.h>
#include "mex.h"
#include "MxArray.hpp"
#include <vector>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
/*
* Use opencv function to resample image quickly
*/
// matlab entry point
// dst = resize(src, scale)
// image should be color with double values
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
if (nrhs < 2){
mexErrMsgTxt("Wrong number of inputs");
}
if (nlhs != 1){
mexErrMsgTxt("Wrong number of outputs");
}
vector<MxArray> rhs(prhs,prhs+nrhs);
//convert input data to opencv matrix
Mat img = rhs[0].toMat();
Mat imgr;
Size s = rhs[1].toSize();
Size newSize = Size(s.height,s.width);
Size oldSize = img.size();
//interpolation method
int interpolation = INTER_LINEAR;
//if interpolation method provided set it
if(nrhs == 3){
string interp = rhs[2].toString();
if(interp.compare("antialias") == 0){
interpolation = INTER_AREA;
}else if(interp.compare("linear") == 0){
interpolation = INTER_LINEAR;
}else if(interp.compare("auto") == 0){ //if we are zooming, use linear else use area interpolation
//old array has width and height swapped, newArray does not
if(newSize.width > oldSize.height){
interpolation = INTER_LINEAR;
}else{
interpolation = INTER_AREA;
}
}else{
mexErrMsgTxt("Invalid interpolation provided, valid is linear (default), antialias, auto");
}
}
//use opencv resize function
resize(img,imgr,newSize,0,0,interpolation);
//convert back to matlab representation
plhs[0] = MxArray(imgr);
}