mexopencv  3.4.1
MEX interface for OpenCV library
drawLineMatches.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::line_descriptor;
13 
14 namespace {
21 {
22  KeyLine keyline;
23  keyline.angle = arr.at("angle", idx).toFloat();
24  keyline.class_id = arr.at("class_id", idx).toInt();
25  keyline.octave = arr.at("octave", idx).toInt();
26  keyline.pt = arr.at("pt", idx).toPoint2f();
27  keyline.response = arr.at("response", idx).toFloat();
28  keyline.size = arr.at("size", idx).toFloat();
29  keyline.startPointX = arr.at("startPoint", idx).toPoint2f().x;
30  keyline.startPointY = arr.at("startPoint", idx).toPoint2f().y;
31  keyline.endPointX = arr.at("endPoint", idx).toPoint2f().x;
32  keyline.endPointY = arr.at("endPoint", idx).toPoint2f().y;
33  keyline.sPointInOctaveX = arr.at("startPointInOctave", idx).toPoint2f().x;
34  keyline.sPointInOctaveY = arr.at("startPointInOctave", idx).toPoint2f().y;
35  keyline.ePointInOctaveX = arr.at("endPointInOctave", idx).toPoint2f().x;
36  keyline.ePointInOctaveY = arr.at("endPointInOctave", idx).toPoint2f().y;
37  keyline.lineLength = arr.at("lineLength", idx).toFloat();
38  keyline.numOfPixels = arr.at("numOfPixels", idx).toInt();
39  return keyline;
40 }
41 
47 {
48  const mwSize n = arr.numel();
49  vector<KeyLine> vk;
50  vk.reserve(n);
51  if (arr.isCell())
52  for (mwIndex i = 0; i < n; ++i)
53  vk.push_back(MxArrayToKeyLine(arr.at<MxArray>(i)));
54  else if (arr.isStruct())
55  for (mwIndex i = 0; i < n; ++i)
56  vk.push_back(MxArrayToKeyLine(arr,i));
57  else
58  mexErrMsgIdAndTxt("mexopencv:error",
59  "MxArray unable to convert to std::vector<cv::line_descriptor::KeyLine>");
60  return vk;
61 }
62 }
63 
71 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
72 {
73  // Check the number of arguments
74  nargchk(nrhs>=5 && (nrhs%2)==1 && nlhs<=1);
75 
76  // Argument vector
77  vector<MxArray> rhs(prhs, prhs+nrhs);
78 
79  //TODO: cv::line_descriptor::drawLineMatches doesnt parse flags correctly,
80  // they are parsed as mutually exclusive!
81 
82  // Option processing
83  Mat outImg;
84  Scalar matchColor(Scalar::all(-1));
85  Scalar singleLineColor(Scalar::all(-1));
86  vector<char> matchesMask;
88  for (int i=5; i<nrhs; i+=2) {
89  string key(rhs[i].toString());
90  if (key == "MatchColor")
91  matchColor = (rhs[i+1].isChar()) ?
92  ColorType[rhs[i+1].toString()] : rhs[i+1].toScalar();
93  else if (key == "SingleLineColor")
94  singleLineColor = (rhs[i+1].isChar()) ?
95  ColorType[rhs[i+1].toString()] : rhs[i+1].toScalar();
96  else if (key == "MatchesMask")
97  rhs[i+1].toMat(CV_8S).reshape(1,1).copyTo(matchesMask);
98  else if (key == "NotDrawSingleLines")
99  UPDATE_FLAG(flags, rhs[i+1].toBool(),
101  else if (key == "OutImage") {
102  outImg = rhs[i+1].toMat(CV_8U);
104  }
105  else
106  mexErrMsgIdAndTxt("mexopencv:error",
107  "Unrecognized option %s", key.c_str());
108  }
109 
110  // Process
111  Mat img1(rhs[0].toMat(CV_8U)),
112  img2(rhs[2].toMat(CV_8U));
113  vector<KeyLine> keylines1(MxArrayToVectorKeyLine(rhs[1])),
114  keylines2(MxArrayToVectorKeyLine(rhs[3]));
115  vector<DMatch> matches1to2(rhs[4].toVector<DMatch>());
116  //HACK: drawLineMatches does not check size of mask, so we do it
117  //HACK: also it doesnt like its default value!
118  if (matchesMask.empty())
119  matchesMask.assign(matches1to2.size(), 1);
120  else if (matchesMask.size() != matches1to2.size())
121  mexErrMsgIdAndTxt("mexopencv:error", "Incorrect mask size");
122  drawLineMatches(img1, keylines1, img2, keylines2, matches1to2, outImg,
123  matchColor, singleLineColor, matchesMask, flags);
124  plhs[0] = MxArray(outImg);
125 }
T empty(T... args)
KeyLine MxArrayToKeyLine(const MxArray &arr, mwIndex idx=0)
Convert an MxArray to cv::line_descriptor::KeyLine.
mwSize numel() const
Number of elements in an array.
Definition: MxArray.hpp:546
T at(mwIndex index) const
Template for numeric array element accessor.
Definition: MxArray.hpp:1250
#define CV_8U
STL namespace.
#define CV_8S
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
T push_back(T... args)
vector< KeyLine > MxArrayToVectorKeyLine(const MxArray &arr)
Convert an MxArray to std::vector<cv::line_descriptor::KeyLine>
#define UPDATE_FLAG(NUM, TF, BIT)
set or clear a bit in flag depending on bool value
Definition: mexopencv.hpp:174
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
bool isCell() const
Determine whether input is cell array.
Definition: MxArray.hpp:610
void drawLineMatches(const Mat &img1, const std::vector< KeyLine > &keylines1, const Mat &img2, const std::vector< KeyLine > &keylines2, const std::vector< DMatch > &matches1to2, Mat &outImg, const Scalar &matchColor=Scalar::all(-1), const Scalar &singleLineColor=Scalar::all(-1), const std::vector< char > &matchesMask=std::vector< char >(), int flags=DrawLinesMatchesFlags::DEFAULT)
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/output arguments number check.
Definition: mexopencv.hpp:181
bool isStruct() const
Determine whether input is structure array.
Definition: MxArray.hpp:708
T size(T... args)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
T assign(T... args)
STL class.
Global constant definitions.
T c_str(T... args)
const ConstMap< std::string, cv::Scalar > ColorType
Translates MATLAB color names (see ColorSpec) into OpenCV scalars.
Definition: mexopencv.hpp:55
static Scalar_< double > all(double v0)
cv::Mat toMat() const
T reserve(T... args)