mexopencv  3.4.1
MEX interface for OpenCV library
drawKeylines.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>=2 && (nrhs%2)==0 && nlhs<=1);
75 
76  // Argument vector
77  vector<MxArray> rhs(prhs, prhs+nrhs);
78 
79  //TODO: cv::line_descriptor::drawKeylines doesnt parse flags correctly,
80  // they are parsed as mutually exclusive!
81 
82  // Option processing
83  Mat outImage;
84  Scalar color(Scalar::all(-1));
86  for (int i=2; i<nrhs; i+=2) {
87  string key(rhs[i].toString());
88  if (key == "Color")
89  color = (rhs[i+1].isChar()) ?
90  ColorType[rhs[i+1].toString()] : rhs[i+1].toScalar();
91  else if (key == "OutImage") {
92  outImage = rhs[i+1].toMat(CV_8U);
94  }
95  else
96  mexErrMsgIdAndTxt("mexopencv:error",
97  "Unrecognized option %s", key.c_str());
98  }
99 
100  // Process
101  Mat image(rhs[0].toMat(CV_8U));
102  vector<KeyLine> keylines(MxArrayToVectorKeyLine(rhs[1]));
103  drawKeylines(image, keylines, outImage, color, flags);
104  plhs[0] = MxArray(outImage);
105 }
void drawKeylines(const Mat &image, const std::vector< KeyLine > &keylines, Mat &outImage, const Scalar &color=Scalar::all(-1), int flags=DrawLinesMatchesFlags::DEFAULT)
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.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
T push_back(T... args)
KeyLine MxArrayToKeyLine(const MxArray &arr, mwIndex idx=0)
Convert an MxArray to cv::line_descriptor::KeyLine.
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 mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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
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)
vector< KeyLine > MxArrayToVectorKeyLine(const MxArray &arr)
Convert an MxArray to std::vector<cv::line_descriptor::KeyLine>
cv::Mat toMat() const
T reserve(T... args)