{"id":852,"date":"2013-05-02T14:48:57","date_gmt":"2013-05-02T06:48:57","guid":{"rendered":"http:\/\/www.shizhiyou.com\/blog\/?p=852"},"modified":"2013-05-02T14:48:57","modified_gmt":"2013-05-02T06:48:57","slug":"opencv-2-0-%e4%b9%8b-mat","status":"publish","type":"post","link":"http:\/\/www.youthtribe.com\/archives\/852","title":{"rendered":"opencv 2.0 \u4e4b Mat"},"content":{"rendered":"
Mat\u662fopencv2.0\u63a8\u51fa\u7684\u5904\u7406\u56fe\u50cf\u7684\u65b0\u7684\u6570\u636e\u7ed3\u6784\uff0c\u73b0\u5728\u8d8a\u6765\u8d8a\u6709\u8d8b\u52bf\u53d6\u4ee3\u4e4b\u524d\u7684cvMat\u548clplImage\uff0c\u76f8\u6bd4\u4e4b\u4e0bMat\u6700\u5927\u7684\u597d\u5904\u5c31\u662f\u80fd\u591f\u66f4\u52a0\u65b9\u4fbf\u7684\u8fdb\u884c\u5185\u5b58\u7ba1\u7406\uff0c\u4e0d\u518d\u9700\u8981\u7a0b\u5e8f\u5458\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u7684\u91ca\u653e\u3002opencv2.3\u4e2d\u63d0\u5230Mat\u662f\u4e00\u4e2a\u591a\u7ef4\u7684\u5bc6\u96c6\u6570\u636e\u6570\u7ec4\uff0c\u53ef\u4ee5\u7528\u6765\u5904\u7406\u5411\u91cf\u548c\u77e9\u9635\u3001\u56fe\u50cf\u3001\u76f4\u65b9\u56fe\u7b49\u7b49\u5e38\u89c1\u7684\u591a\u7ef4\u6570\u636e\u3002<\/p>\n
\nclass\u00a0CV_EXPORTS Mat\r\n{\r\n\r\npublic\uff1a\r\n\r\n\r\n\r\n\r\nint\u00a0flags;\uff08Note \uff1a\u76ee\u524d\u8fd8\u4e0d\u77e5\u9053flags\u505a\u4ec0\u4e48\u7528\u7684\uff09\r\nint\u00a0dims;\u00a0\r\nint\u00a0rows,cols;\u00a0\r\nuchar *data;\u00a0\r\nint\u00a0* refcount;\u00a0<\/span>\r\n...\r\n\r\n};<\/pre>\n<\/p>\n
<\/pre>\n<\/blockquote>\n\u00a0\u4ece\u4ee5\u4e0a\u7ed3\u6784\u4f53\u53ef\u4ee5\u770b\u51faMat\u4e5f\u662f\u4e00\u4e2a\u77e9\u9635\u5934\uff0c\u9ed8\u8ba4\u4e0d\u5206\u914d\u5185\u5b58\uff0c\u53ea\u662f\u6307\u5411\u4e00\u5757\u5185\u5b58(\u6ce8\u610f\u8bfb\u5199\u4fdd\u62a4)\u3002\u521d\u59cb\u5316\u4f7f\u7528create\u51fd\u6570\u6216\u8005Mat\u6784\u9020\u51fd\u6570\uff0c\u4ee5\u4e0b\u6574\u7406\u81eaopencv2.3.1 Manual:<\/pre>\n\nMat(nrows, ncols, type, fillValue]);\u00a0\r\nM.create(nrows, ncols, type);<\/pre>\n\r\n\u4f8b\u5b50\uff1a\r\nMat M(7,7,CV_32FC2,Scalar(1,3));\u00a0\r\nM.create(100,\u00a060, CV_8UC(15));<\/span><\/pre>\n\r\n\r\nint\u00a0sz[] = {100,\u00a0100,\u00a0100};\u00a0\r\nMat bigCube(3, sz, CV_8U, Scalar:all(0<\/span>));<\/pre>\n\r\n\r\ndouble\u00a0m[3][3] = {{a, b, c}, {d, e, f}, {g, h, i}};\r\nMat M = Mat(3,\u00a03<\/span>, CV_64F, m).inv();<\/pre>\n\r\n\r\nMat img(Size(320,240),CV_8UC3);\u00a0\r\nMat img(height, width, CV_8UC3, pixels, step);<\/span><\/pre>\n\r\n\r\nIplImage* img = cvLoadImage(\"greatwave.jpg\",\u00a01);\r\nMat mtx(img,0);\u00a0\/\/\u00a0convert IplImage* -> Mat;<\/span><\/pre>\n<\/blockquote>\n\u8bbf\u95eeMat\u7684\u6570\u636e\u5143\u7d20\uff1a\r\n<\/span><\/pre>\n\nMat M;\r\nM.row(3) = M.row(3) + M.row(5) *\u00a03;\u00a0\r\n\r\n\r\nMat M1 = M.col(1);\r\nM.col(7).copyTo(M1);\u00a0\r\n\r\n\r\nMat M;\r\nM.at<double>(i,j);\u00a0\r\nM.at(uchar)(i,j);\u00a0\r\nVec3i bgr1 = M.at(Vec3b)(i,j)\u00a0\r\nVec3s bgr2 = M.at(Vec3s)(i,j)\u00a0\r\nVec3w bgr3 = M.at(Vec3w)(i,j)\u00a0\r\n\r\n\r\ndouble\u00a0sum =\u00a00.0f;\r\nfor(int\u00a0row =\u00a00; row < M.rows; row++)\r\n{\u00a0\r\nconst\u00a0double\u00a0* Mi = M.ptr<double>(row);\u00a0\r\nfor\u00a0(int\u00a0col =\u00a00; col < M.cols; col++)\u00a0\r\nsum += std::max(Mi[j],\u00a00.);\r\n}\r\n\r\n\r\ndouble\u00a0sum=0;\r\nMatConstIterator<double> it = M.begin<double>(), it_end = M.end<double>();\r\nfor(; it != it_end; ++it)\u00a0\r\nsum += std::max(*it,\u00a00<\/span>.);<\/pre>\n<\/blockquote>\nMat\u53ef\u8fdb\u884cMatlab\u98ce\u683c\u7684\u77e9\u9635\u64cd\u4f5c\uff0c\u5982\u521d\u59cb\u5316\u7684\u65f6\u5019\u53ef\u4ee5\u7528initializers,zeros(), ones(), eye(). \u9664\u4ee5\u4e0a\u5185\u5bb9\u4e4b\u5916\uff0cMat\u8fd8\u6709\u67093\u4e2a\u91cd\u8981\u7684\u65b9\u6cd5\uff1a<\/pre>\n\nMat mat = imread(const\u00a0String* filename);\u00a0\/\/\u00a0\u8bfb\u53d6\u56fe\u50cf<\/span>\r\nimshow(const\u00a0string\u00a0frameName, InputArray mat);\u00a0\/\/\u00a0\u663e\u793a\u56fe\u50cf<\/span>\r\nimwrite (const\u00a0string& filename, InputArray img);\u00a0\/\/\u50a8\u5b58\u56fe\u50cf<\/span><\/pre>\n\r\n<\/span><\/pre>\n<\/blockquote>\nCvMat, Mat, IplImage\u4e4b\u95f4\u7684\u4e92\u76f8\u8f6c\u6362<\/strong>\r\n<\/span><\/pre>\n\n<\/pre>\nIpIImage -> CvMat\r\n\r\nCvMat matheader;\r\nCvMat * mat = cvGetMat(img, &matheader);\r\n\r\n<\/span>CvMat * mat = cvCreateMat(img->height, img->width, CV_64FC3);\r\ncvConvert(img, mat)<\/pre>\n<\/pre>\n\r\n\r\nIplImage -> Mat\r\nMat::Mat(const\u00a0IplImage* img,\u00a0bool\u00a0copyData=false);\r\n\u4f8b\u5b50\uff1a\r\nIplImage* iplImg = cvLoadImage(\"greatwave.jpg\",\u00a01);\r\nMat mtx(iplImg);<\/span><\/pre>\n<\/pre>\n\r\n\u00a0<\/wbr><\/pre>\n<\/pre>\n\r\nMat -> IplImage\r\nMat M\r\nIplImage iplimage = M;<\/pre>\n<\/pre>\n\r\nCvMat -> Mat\r\nMat::Mat(const\u00a0CvMat* m,\u00a0bool\u00a0copyData=false);<\/span><\/pre>\n<\/pre>\n\r\nMat -> CvMat\r\n\u4f8b\u5b50(\u5047\u8bbeMat\u7c7b\u578b\u7684imgMat\u56fe\u50cf\u6570\u636e\u5b58\u5728)\uff1a\r\nCvMat cvMat = imgMat;\/*Mat -> CvMat, \u7c7b\u4f3c\u8f6c\u6362\u5230IplImage\uff0c\u4e0d\u590d\u5236\u6570\u636e\u53ea\u521b\u5efa\u77e9\u9635\u5934<\/span><\/pre>\n\r\n<\/span><\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"Mat\u662fopencv2.0\u63a8\u51fa\u7684\u5904\u7406\u56fe\u50cf\u7684\u65b0\u7684\u6570\u636e\u7ed3\u6784\uff0c\u73b0\u5728\u8d8a\u6765\u8d8a\u6709\u8d8b\u52bf\u53d6\u4ee3\u4e4b\u524d\u7684cvMat\u548clplImage\uff0c\u76f8\u6bd4\u4e4b\u4e0bMat\u6700\u5927\u7684\u597d\u5904\u5c31\u662f\u80fd\u591f\u66f4\u52a0\u65b9\u4fbf\u7684\u8fdb\u884c\u5185\u5b58\u7ba1\u7406\uff0c\u4e0d\u518d\u9700\u8981\u7a0b\u5e8f\u5458\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u7684\u91ca\u653e\u3002opencv2.3\u4e2d\u63d0\u5230Mat\u662f\u4e00\u4e2a\u591a\u7ef4\u7684\u5bc6\u96c6\u6570\u636e\u6570\u7ec4\uff0c\u53ef\u4ee5\u7528\u6765\u5904\u7406\u5411\u91cf\u548c\u77e9\u9635\u3001\u56fe\u50cf\u3001\u76f4\u65b9\u56fe\u7b49\u7b49\u5e38\u89c1\u7684\u591a\u7ef4\u6570\u636e\u3002 class\u00a0CV_EXPORTS Mat { public\uff1a int\u00a0flags;\uff08Note \uff1a\u76ee\u524d\u8fd8\u4e0d\u77e5\u9053flags\u505a\u4ec0\u4e48\u7528\u7684\uff09 int\u00a0dims;\u00a0 int\u00a0rows,cols;\u00a0 uchar *data;\u00a0 int\u00a0* refcount;\u00a0 … };<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,9],"tags":[142,141,140,139],"_links":{"self":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts\/852"}],"collection":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/comments?post=852"}],"version-history":[{"count":2,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts\/852\/revisions"}],"predecessor-version":[{"id":854,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts\/852\/revisions\/854"}],"wp:attachment":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/media?parent=852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/categories?post=852"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/tags?post=852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}