OpenCV - BGUdip201/wiki.files/OpenCV.pdf · 2019. 11. 18. · Mat. at (row, col)[channel] - returns...
Transcript of OpenCV - BGUdip201/wiki.files/OpenCV.pdf · 2019. 11. 18. · Mat. at (row, col)[channel] - returns...
-
OpenCV
Introduction
-
Basic Structures (C++)• Class Point_typedef Point_ Point2i;typedef Point2i Point;typedef Point_ Point2f;typedef Point_ Point2d;
Point operator + Point operator += Point operator -Point operator -= Point operator * Point operator *= bool operator == bool operator != double
• Class Point3_Similar to Point_ for 3D (x,y,z)
template class CV_EXPORTS Point_ {public:
Point_();Point_(_Tp _x, _Tp _y);Point_(const Point_& pt);Point_(const CvPoint& pt);Point_(const CvPoint2D32f& pt);Point_(const Size_& sz);Point_(const Vec& v);
Point_& operator = (const Point_& pt);//! conversion to another data typetemplate operator Point_() const;
_Tp dot(const Point_& pt) const;double ddot(const Point_& pt) const;double cross(const Point_& pt) const;bool inside(const Rect_& r) const;
_Tp x, y; //< the point coordinates};
-
Basic Structures (C++)• Class Point_typedef Point_ Point2i;typedef Point2i Point;typedef Point_ Point2f;typedef Point_ Point2d;
Point pt1, pt2, pt3 ;pt1 = pt2 + pt3; pt1 = pt2 - pt3; pt1 = pt2 * a; pt1 = a * pt2; pt1 += pt2; pt1 -= pt2; pt1 *= a; double value = norm(pt); // L2 normpt1 == pt2; pt1 != pt2;
• Class Point3_Similar to Point_ for 3D (x,y,z)
-
Basic Structures (C++)Class Size_ Template class for specifying the size of an image or rectangle.int x, y, width, height;
Class Rect_Rect rect ;rect = rect +/- pt ; (shifting a rectangle by an offset)rect = rect +/- size; (expanding or shrinking a size) rect +/-= point ;rect +/-= size ;rect = rect1 & rect2 (rectangle intersection)rect = rect1 | rect2 (minimum area rectangle containing rect1 and rect2 )Or rect &= rect1 rect |= rect1
rectangle comparisonrect == rect1, rect != rect1
Class RotatedRectPoint.tl() - return top left pointPoint.br() - return bottom right point
template class CV_EXPORTS Rect_ {public:
Rect_();Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);Rect_(const Rect_& r);Rect_(const CvRect& r);Rect_(const Point_& org, const Size_& sz);Rect_(const Point_& pt1, const Point_& pt2);
//_Tp x, y ; // the top-left corner_TP width, height; // width and height of the rectangle
};
-
Basic Structures (C++)• class Vec
Template class for short numerical vectors, a partial case of Matx:v1 = v2 + v3
• v1 = v2 - v3• v1 = v2 * scale• v1 = scale * v2• v1 = -v2• v1 += v2 and other augmenting operations• v1 == v2, v1 != v2• norm(v1) (euclidean norm)
• class Scalar_Template class for a 4-element vector derived from Vec.
• class RangeTemplate class specifying a continuous subsequence (slice) of a sequence.
•
typedef Vec Vec2b;typedef Vec Vec3b;typedef Vec Vec4b;
typedef Vec Vec2s;typedef Vec Vec3s;typedef Vec Vec4s;
typedef Vec Vec2i;typedef Vec Vec3i;typedef Vec Vec4i;
typedef Vec Vec2f;typedef Vec Vec3f;typedef Vec Vec4f;typedef Vec Vec6f;
typedef Vec Vec2d;typedef Vec Vec3d;typedef Vec Vec4d;typedef Vec Vec6d;
http://docs.opencv.org/modules/core/doc/basic_structures.html#Matx
-
Basic Structures (C++)
class MatOpenCV C++ n-dimensional dense array classrows, cols // mat dimensions channels // 1 grayscale, 3 for RGB create a new 320x240 image
Mat img(Size(320,240),CV_8UC3);Select a region of interest
Mat roi(img, Rect(10,10,100,100));add the 5-th row, multiplied by 3 to the 3rd row
M.row(3) = M.row(3) + M.row(5)*3; Initialize as the unit matrix
Mat A = Mat::eye(10, 10, CV_32S);Matrix Expressions
Example:// compute sum of positive matrix elements// (assuming that M isa double-precision matrix)double sum=0;for(int i = 0; i < M.rows; i++) {
const double* Mi = M.ptr(i);for(int j = 0; j < M.cols; j++)
sum += std::max(Mi[j], 0.);}
-
Matrix ExpressionsAddition, subtraction, negation, and scaling
A+B, A-B, A+s, A-s, s+A, s-A, -A, A*alphaPer-element multiplication and division
A.mul(B), A/B, alpha/AMatrix multiplication
A*BTransposition (AT )
A.t()Matrix inversion and pseudo-inversion
A.inv([method]) (~ A-1) , A.inv([method])*B (~ X: AX=B)
Comparison (The result of comparison is an 8-bit single channel mask whose elements are set to 255 (true) or 0 (false);
>, >=, ==, !=,
-
Matrix ExpressionsMat.at(row, col)[channel] - returns pointer to image locationMat.channels() - returns the number of channelsMat.clone() - returns a deep copy of the imageMat.create( rows, cols, TYPE) - re-allocates new memory to matrixMat.cross() - computes cross product of two matriciesMat.depth() - returns data type of matrixMat.dot() - computes the dot product of two matrices
-
Example#include #include #include #include
int main(int argc, char* argv[]){cv::Mat img ;cv::Mat gray ;cv::imread(argv[1]);if ( img.channels() == 1 )
img = convertTo(gray, CV_8U) ;if ( img.channels() == 3 && to_type == CV_8U)
cvtColor(img, gray, CV_BGR2GRAY);
// display image namedWindow(“Window”, cv::WINDOW_NORMAL);imshow(“Window, img);
}
-
Scanning an Image
Iterator UsageMat.begin() - moves Mat iterator to beginning of imageMat.end() - moves Mat iterator to end of image
-
Image TYPES
The TYPE determine the data type of the mat elements• Represented as CV_C• Example Datatypes/ Depths
-
Image I/OwaitKey( int x ) if x > 0, then waitKey waits x millisecondsif x = 0, then waitKey waits for pressing a key//Read an imageMat image = imread( , )
Write an imageimwrite( , image );
Create window for outputnamedWindow( );
Output image to windowimshow( , );
-
Drawing
void circle(image, Point(x,y),int rad, CV_BGR(b,g,r), intthickness=1)void ellipse(image, RotatedRect box, CV_BGR(b,g,r), int thickness=1)void line(image, Point(x,y), Point(x,y), CV_BGR(b,g,r), int thickness= 1)void rectangle(img, Point(x,y), Point(x,y), CV_BGR(b,g,r), intthickness)
-
Using Mouse
-
Image Threshold
OpenCVBasic Structures (C++)Basic Structures (C++)Basic Structures (C++)Basic Structures (C++)Basic Structures (C++)Matrix ExpressionsMatrix ExpressionsExampleScanning an ImageImage TYPESImage I/ODrawingUsing �MouseImage Threshold