OpenCV 2.2 C API Reference Guide
OpenCV 2.2 C API Reference Guide
v2.2
December, 2010
2
Contents
I C API Reference 43
3
4 CONTENTS
cv::CartToPolar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
cv::Cbrt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
cv::ClearND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
cv::CloneImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
cv::CloneMat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
cv::CloneMatND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
cv::CloneSparseMat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
cv::Cmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
cv::CmpS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
cv::ConvertScale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
cv::ConvertScaleAbs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
cv::CvtScaleAbs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
cv::Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
cv::CountNonZero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
cv::CreateData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
cv::CreateImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
cv::CreateImageHeader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
cv::CreateMat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
cv::CreateMatHeader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
cv::CreateMatND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
cv::CreateMatNDHeader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
cv::CreateSparseMat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
cv::CrossProduct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
CvtPixToPlane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
cv::DCT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
cv::DFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
cv::DecRefData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
cv::Det . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
cv::Div . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
cv::DotProduct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
cv::EigenVV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
cv::Exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
cv::FastArctan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
cv::Flip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
cv::GEMM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
cv::Get?D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
cv::GetCol(s) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
cv::GetDiag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
cvGetDims, cvGetDimSize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
cv::GetElemType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
CONTENTS 5
cv::GetImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
cv::GetImageCOI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
cv::GetImageROI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
cv::GetMat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
cv::GetNextSparseNode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
cv::GetOptimalDFTSize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
cv::GetRawData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
cv::GetReal1D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
cv::GetReal2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
cv::GetReal3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
cv::GetRealND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
cv::GetRow(s) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
cv::GetSize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
cv::GetSubRect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
cv::InRange . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
cv::InRangeS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
cv::IncRefData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
cv::InitImageHeader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
cv::InitMatHeader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
cv::InitMatNDHeader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
cv::InitSparseMatIterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
cv::InvSqrt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
cv::Inv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
cv::Invert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
cv::IsInf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
cv::IsNaN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
cv::LUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
cv::Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
cv::Mahalanobis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
cv::Mat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
cv::Max . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
cv::MaxS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
cv::Merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
cv::Min . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
cv::MinMaxLoc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
cv::MinS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Mirror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
cv::MixChannels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
MulAddS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
cv::Mul . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
6 CONTENTS
cv::MulSpectrums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
cv::MulTransposed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
cv::Norm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
cv::Not . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
cv::Or . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
cv::OrS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
cv::PerspectiveTransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
cv::PolarToCart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
cv::Pow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
cv::Ptr?D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
cv::RNG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
cv::RandArr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
cv::RandInt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
cv::RandReal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
cv::Reduce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
cv::ReleaseData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
cv::ReleaseImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
cv::ReleaseImageHeader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
cv::ReleaseMat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
cv::ReleaseMatND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
cv::ReleaseSparseMat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
cv::Repeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
cv::ResetImageROI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
cv::Reshape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
cv::ReshapeMatND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
cvRound, cvFloor, cvCeil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
cv::ScaleAdd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
cv::Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
cv::Set?D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
cv::SetData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
cv::SetIdentity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
cv::SetImageCOI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
cv::SetImageROI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
cv::SetReal?D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
cv::SetZero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
cv::Solve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
cv::SolveCubic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
cv::Split . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
cv::Sqrt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
cv::Sub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
CONTENTS 7
cv::SubRS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
cv::SubS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
cv::Sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
cv::SVBkSb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
cv::SVD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
cv::Trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
cv::Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
cv::Transpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
cv::Xor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
cv::XorS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
cv::mGet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
cv::mSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
1.3 Dynamic Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
CvMemStorage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
CvMemBlock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
CvMemStoragePos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
CvSeq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
CvSeqBlock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
CvSlice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
CvSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
CvGraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
CvGraphScanner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
CV TREE NODE FIELDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
CvTreeNodeIterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
cv::ClearGraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
cv::ClearMemStorage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
cv::ClearSeq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
cv::ClearSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
cv::CloneGraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
cv::CloneSeq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
cv::CreateChildMemStorage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
cv::CreateGraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
cv::CreateGraphScanner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
cv::CreateMemStorage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
cv::CreateSeq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
cv::CreateSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
cv::CvtSeqToArray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
cv::EndWriteSeq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
cv::FindGraphEdge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
cv::FindGraphEdgeByPtr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
8 CONTENTS
cv::FlushSeqWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
cv::GetGraphVtx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
cv::GetSeqElem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
cv::GetSeqReaderPos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
cv::GetSetElem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
cv::GraphAddEdge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
cv::GraphAddEdgeByPtr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
cv::GraphAddVtx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
cv::GraphEdgeIdx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
cv::GraphRemoveEdge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
cv::GraphRemoveEdgeByPtr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
cv::GraphRemoveVtx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
cv::GraphRemoveVtxByPtr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
cv::GraphVtxDegree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
cv::GraphVtxDegreeByPtr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
cv::GraphVtxIdx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
cv::InitTreeNodeIterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
cv::InsertNodeIntoTree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
cv::MakeSeqHeaderForArray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
cv::MemStorageAlloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
cv::MemStorageAllocString . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
cv::NextGraphItem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
cv::NextTreeNode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
cv::PrevTreeNode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
cv::ReleaseGraphScanner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
cv::ReleaseMemStorage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
cv::RestoreMemStoragePos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
cv::SaveMemStoragePos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
cv::SeqElemIdx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
cv::SeqInsert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
cv::SeqInsertSlice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
cv::SeqInvert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
cv::SeqPop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
cv::SeqPopFront . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
cv::SeqPopMulti . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
cv::SeqPush . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
cv::SeqPushFront . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
cv::SeqPushMulti . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
cv::SeqRemove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
cv::SeqRemoveSlice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
CONTENTS 9
cv::SeqSearch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
cv::SeqSlice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
cv::SeqSort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
cv::SetAdd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
cv::SetNew . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
cv::SetRemove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
cv::SetRemoveByPtr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
cv::SetSeqBlockSize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
cv::SetSeqReaderPos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
cv::StartAppendToSeq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
cv::StartReadSeq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
cv::StartWriteSeq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
cv::TreeToNodeSeq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
1.4 Drawing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
cv::Circle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
cv::ClipLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
cv::DrawContours . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
cv::Ellipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
cv::EllipseBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
cv::FillConvexPoly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
cv::FillPoly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
cv::GetTextSize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
cv::InitFont . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
cv::InitLineIterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
cv::Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
cv::PolyLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
cv::PutText . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
cv::Rectangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
CV RGB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
1.5 XML/YAML Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
CvFileStorage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
CvFileNode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
CvAttrList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
CvTypeInfo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
cv::Clone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
cv::EndWriteStruct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
cv::FindType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
cv::FirstType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
cv::GetFileNode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
cv::GetFileNodeByName . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
10 CONTENTS
cv::GetFileNodeName . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
cv::GetHashedKey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
cv::GetRootFileNode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
cv::Load . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
cv::OpenFileStorage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
cv::Read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
cv::ReadByName . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
cv::ReadInt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
cv::ReadIntByName . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
cv::ReadRawData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
cv::ReadRawDataSlice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
cv::ReadReal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
cv::ReadRealByName . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
cv::ReadString . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
cv::ReadStringByName . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
cv::RegisterType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
cv::Release . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
cv::ReleaseFileStorage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
cv::Save . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
cv::StartNextStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
cv::StartReadRawData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
cv::StartWriteStruct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
cv::TypeOf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
cv::UnregisterType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
cv::Write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
cv::WriteComment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
cv::WriteFileNode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
cv::WriteInt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
cv::WriteRawData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
cv::WriteReal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
cv::WriteString . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
1.6 Clustering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
cv::KMeans2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
cv::SeqPartition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
1.7 Utility and System Functions and Macros . . . . . . . . . . . . . . . . . . . . . . . . 235
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Example: Use of Error Handling Macros . . . . . . . . . . . . . . . . . . . . . . . . . 237
cv::GetErrStatus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
cv::SetErrStatus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
cv::GetErrMode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
CONTENTS 11
cv::SetErrMode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
cv::Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
cv::ErrorStr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
cv::RedirectError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
cvNulDevReport cvStdErrReport cvGuiBoxReport . . . . . . . . . . . . . . . . . . . 242
cv::Alloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
cv::Free . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
cv::GetTickCount . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
cv::GetTickFrequency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
cv::RegisterModule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
cv::GetModuleInfo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
cv::UseOptimized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
cv::SetMemoryManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
cv::SetIPLAllocators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
cv::Filter2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
cv::Laplace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
cv::MorphologyEx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
cv::PyrDown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
cv::ReleaseStructuringElement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
cv::Smooth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
cv::Sobel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
2.3 Geometric Image Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
cv::GetRotationMatrix2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
cv::GetAffineTransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
cv::GetPerspectiveTransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
cv::GetQuadrangleSubPix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
cv::GetRectSubPix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
cv::LogPolar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
cv::Remap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
cv::Resize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
cv::WarpAffine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
cv::WarpPerspective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
2.4 Miscellaneous Image Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . 283
cv::AdaptiveThreshold . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
cv::CvtColor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
cv::DistTransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
CvConnectedComp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
cv::FloodFill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
cv::Inpaint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
cv::Integral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
cv::PyrMeanShiftFiltering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
cv::PyrSegmentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
cv::Threshold . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
2.5 Structural Analysis and Shape Descriptors . . . . . . . . . . . . . . . . . . . . . . . . 299
cv::ApproxChains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
cv::ApproxPoly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
cv::ArcLength . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
cv::BoundingRect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
cv::BoxPoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
cv::CalcPGH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
cv::CalcEMD2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
cv::CheckContourConvexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
CvConvexityDefect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
cv::ContourArea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
CONTENTS 13
cv::ContourFromContourTree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
cv::ConvexHull2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
cv::ConvexityDefects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
cv::CreateContourTree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
cv::EndFindContours . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
cv::FindContours . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
cv::FindNextContour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
cv::FitEllipse2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
cv::FitLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
cv::GetCentralMoment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
cv::GetHuMoments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
cv::GetNormalizedCentralMoment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
cv::GetSpatialMoment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
cv::MatchContourTrees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
cv::MatchShapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
cv::MinAreaRect2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
cv::MinEnclosingCircle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
cv::Moments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
cv::PointPolygonTest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
cv::PointSeqFromMat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
cv::ReadChainPoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
cv::StartFindContours . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
cv::StartReadChainPoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
cv::SubstituteContour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
2.6 Planar Subdivisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
CvSubdiv2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
CvQuadEdge2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
CvSubdiv2DPoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
cv::CalcSubdivVoronoi2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
cv::ClearSubdivVoronoi2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
cv::CreateSubdivDelaunay2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
cv::FindNearestPoint2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
cv::Subdiv2DEdgeDst . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
cv::Subdiv2DGetEdge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
cv::Subdiv2DNextEdge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
cv::Subdiv2DLocate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
cv::Subdiv2DRotateEdge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
cv::SubdivDelaunay2DInsert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
2.7 Motion Analysis and Object Tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
cv::Acc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
14 CONTENTS
cv::MultiplyAcc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
cv::RunningAvg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
cv::SquareAcc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
2.8 Feature Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
cv::Canny . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
cv::CornerEigenValsAndVecs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
cv::CornerHarris . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
cv::CornerMinEigenVal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
cv::FindCornerSubPix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
cv::GoodFeaturesToTrack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
cv::HoughLines2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
cv::PreCornerDetect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
cv::SampleLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
2.9 Object Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
cv::MatchTemplate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
cv::CalcOpticalFlowLK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
cv::CalcOpticalFlowPyrLK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
cv::CamShift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
CvConDensation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
cv::CreateConDensation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
cv::ConDensInitSampleSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
CvKalman . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
cv::CreateKalman . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
cv::KalmanCorrect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
cv::KalmanPredict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
KalmanUpdateByMeasurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
KalmanUpdateByTime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
cv::MeanShift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
cv::ReleaseConDensation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
cv::ReleaseKalman . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
cv::SegmentMotion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
cv::SnakeImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
cv::UpdateMotionHistory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
CvCapture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
cv::CaptureFromCAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
cv::CaptureFromFile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
cv::GetCaptureProperty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
cv::GrabFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
cv::QueryFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
cv::ReleaseCapture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
cv::RetrieveFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
cv::SetCaptureProperty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
cv::CreateVideoWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
cv::ReleaseVideoWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
cv::WriteFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
7.3 Qt new functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
cv::SetWindowProperty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
cv::GetWindowProperty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
cv::FontQt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
cv::AddText . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
cv::DisplayOverlay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
cv::DisplayStatusBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
cv::CreateOpenGLCallback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
cv::SaveWindowParameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
cv::LoadWindowParameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
cv::CreateButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
cv::FindHomography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
cv::FindStereoCorrespondenceBM . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
cv::FindStereoCorrespondenceGC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
cv::GetOptimalNewCameraMatrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
cv::InitIntrinsicParams2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
cv::InitUndistortMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
cv::InitUndistortRectifyMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
cv::POSIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
cv::ProjectPoints2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
cv::ReprojectImageTo3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
cv::RQDecomp3x3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
cv::ReleasePOSITObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
cv::ReleaseStereoBMState . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
cv::ReleaseStereoGCState . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
cv::Rodrigues2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
cv::StereoCalibrate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
cv::StereoRectify . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
cv::StereoRectifyUncalibrated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
cv::Undistort2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
cv::UndistortPoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
10 Introduction 465
10.1 C++ Cheatsheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
10.2 Namespace cv and Function Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
10.3 Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
10.4 Memory Management Part II. Automatic Data Allocation . . . . . . . . . . . . . . . . 471
10.5 Algebraic Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
10.6 Fast Element Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
10.7 Saturation Arithmetics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
10.8 Error handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
10.9 Threading and Reenterability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Point3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Rect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
RotatedRect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
TermCriteria . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
Matx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Vec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
Scalar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Mat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Matrix Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
cv::Mat::Mat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
cv::Mat::Mat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
cv::Mat::operator = . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
cv::Mat::operator MatExpr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
cv::Mat::row . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
cv::Mat::col . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
cv::Mat::rowRange . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
cv::Mat::colRange . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
cv::Mat::diag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
cv::Mat::clone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
cv::Mat::copyTo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
cv::Mat::convertTo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
cv::Mat::assignTo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
cv::Mat::setTo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
cv::Mat::reshape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
cv::Mat::t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
cv::Mat::inv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
cv::Mat::mul . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
cv::Mat::cross . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
cv::Mat::dot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
cv::Mat::zeros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
cv::Mat::ones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
cv::Mat::eye . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
cv::Mat::create . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
cv::Mat::addref . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
cv::Mat::release . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
cv::Mat::resize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
CONTENTS 19
cv::countNonZero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
cv::cubeRoot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
cv::cvarrToMat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
cv::dct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
cv::dft . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550
cv::divide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
cv::determinant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555
cv::eigen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
cv::exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
cv::extractImageCOI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
cv::fastAtan2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
cv::flip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
cv::gemm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
cv::getConvertElem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
cv::getOptimalDFTSize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
cv::idct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
cv::idft . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
cv::inRange . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
cv::invert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
cv::log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
cv::LUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
cv::magnitude . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
cv::Mahalanobis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
cv::max . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
cv::mean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
cv::meanStdDev . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
cv::merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
cv::min . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
cv::minMaxLoc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
cv::mixChannels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
cv::mulSpectrums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
cv::multiply . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
cv::mulTransposed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
cv::norm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
cv::normalize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
cv::PCA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
cv::PCA::PCA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
cv::PCA::operator () . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
cv::PCA::project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
cv::PCA::backProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
CONTENTS 21
cv::perspectiveTransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
cv::phase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
cv::polarToCart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
cv::pow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
RNG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
cv::RNG::RNG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
cv::RNG::next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
cv::RNG::operator T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
cv::RNG::operator () . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
cv::RNG::uniform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
cv::RNG::gaussian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
cv::RNG::fill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
cv::randu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
cv::randn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
cv::randShuffle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
cv::reduce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
cv::repeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592
saturate cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
cv::scaleAdd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
cv::setIdentity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
cv::solve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
cv::solveCubic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
cv::solvePoly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
cv::sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
cv::sortIdx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
cv::split . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
cv::sqrt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
cv::subtract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600
cv::SVD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
cv::SVD::SVD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602
cv::SVD::operator () . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
cv::SVD::solveZ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
cv::SVD::backSubst . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
cv::sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
cv::theRNG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
cv::trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
cv::transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606
cv::transpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
11.3 Dynamic Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
11.4 Drawing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
22 CONTENTS
cv::circle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608
cv::clipLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608
cv::ellipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
cv::ellipse2Poly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
cv::fillConvexPoly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
cv::fillPoly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
cv::getTextSize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
cv::line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
cv::LineIterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
cv::rectangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
cv::polylines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
cv::putText . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617
11.5 XML/YAML Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617
cv::FileStorage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617
cv::FileNode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
cv::FileNodeIterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
11.6 Clustering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
cv::kmeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
cv::partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
11.7 Utility and System Functions and Macros . . . . . . . . . . . . . . . . . . . . . . . . 622
cv::alignPtr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
cv::alignSize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
cv::allocate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
cv::deallocate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
CV Assert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
cv::error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
cv::Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
cv::fastMalloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
cv::fastFree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626
cv::format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626
cv::getNumThreads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
cv::getThreadNum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
cv::getTickCount . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
cv::getTickFrequency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
cv::setNumThreads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
cv::compareHist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
cv::equalizeHist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
12.2 Image Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
cv::BaseColumnFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636
cv::BaseFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636
cv::BaseRowFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637
cv::FilterEngine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
cv::bilateralFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
cv::blur . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644
cv::borderInterpolate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644
cv::boxFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645
cv::buildPyramid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
cv::copyMakeBorder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
cv::createBoxFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
cv::createDerivFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
cv::createGaussianFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
cv::createLinearFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
cv::createMorphologyFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651
cv::createSeparableLinearFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652
cv::dilate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653
cv::erode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654
cv::filter2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
cv::GaussianBlur . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656
cv::getDerivKernels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
cv::getGaussianKernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
cv::getKernelType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
cv::getStructuringElement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659
cv::medianBlur . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
cv::morphologyEx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
cv::Laplacian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
cv::pyrDown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662
cv::pyrUp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
cv::sepFilter2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664
cv::Sobel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
cv::Scharr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
12.3 Geometric Image Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
cv::convertMaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
cv::getAffineTransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
cv::getPerspectiveTransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
cv::getRectSubPix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670
24 CONTENTS
cv::getRotationMatrix2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
cv::invertAffineTransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
cv::remap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672
cv::resize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673
cv::warpAffine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674
cv::warpPerspective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
12.4 Miscellaneous Image Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . 676
cv::adaptiveThreshold . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676
cv::cvtColor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677
cv::distanceTransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
cv::floodFill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684
cv::inpaint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686
cv::integral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687
cv::threshold . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688
cv::watershed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 690
cv::grabCut . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
12.5 Structural Analysis and Shape Descriptors . . . . . . . . . . . . . . . . . . . . . . . . 692
cv::moments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692
cv::HuMoments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
cv::findContours . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694
cv::drawContours . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696
cv::approxPolyDP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697
cv::arcLength . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698
cv::boundingRect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699
cv::estimateRigidTransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699
cv::estimateAffine3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700
cv::contourArea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700
cv::convexHull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
cv::fitEllipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702
cv::fitLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702
cv::isContourConvex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
cv::minAreaRect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
cv::minEnclosingCircle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705
cv::matchShapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705
cv::pointPolygonTest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706
12.6 Planar Subdivisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
12.7 Motion Analysis and Object Tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
cv::accumulate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
cv::accumulateSquare . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708
cv::accumulateProduct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
CONTENTS 25
cv::accumulateWeighted . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
12.8 Feature Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710
cv::Canny . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710
cv::cornerEigenValsAndVecs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
cv::cornerHarris . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 712
cv::cornerMinEigenVal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 712
cv::cornerSubPix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713
cv::goodFeaturesToTrack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714
cv::HoughCircles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716
cv::HoughLines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 718
cv::HoughLinesP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719
cv::preCornerDetect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
12.9 Object Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722
cv::matchTemplate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722
cv::FeatureDetector::read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743
cv::FeatureDetector::write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743
cv::FeatureDetector::create . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743
cv::FastFeatureDetector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744
cv::GoodFeaturesToTrackDetector . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744
cv::MserFeatureDetector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745
cv::StarFeatureDetector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745
cv::SiftFeatureDetector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746
cv::SurfFeatureDetector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746
cv::GridAdaptedFeatureDetector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
cv::PyramidAdaptedFeatureDetector . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
cv::DynamicAdaptedFeatureDetector . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
cv::DynamicAdaptedFeatureDetector::DynamicAdaptedFeatureDetector . . . . . . . 748
cv::AdjusterAdapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749
cv::AdjusterAdapter::tooFew . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749
cv::AdjusterAdapter::tooMany . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750
cv::AdjusterAdapter::good . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750
cv::FastAdjuster . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751
cv::StarAdjuster . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751
cv::SurfAdjuster . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751
13.3 Common Interfaces of Descriptor Extractors . . . . . . . . . . . . . . . . . . . . . . . 751
cv::DescriptorExtractor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752
cv::DescriptorExtractor::compute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752
cv::DescriptorExtractor::read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753
cv::DescriptorExtractor::write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753
cv::DescriptorExtractor::create . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754
cv::SiftDescriptorExtractor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754
cv::SurfDescriptorExtractor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755
cv::CalonderDescriptorExtractor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755
cv::OpponentColorDescriptorExtractor . . . . . . . . . . . . . . . . . . . . . . . . . . 755
cv::BriefDescriptorExtractor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 756
13.4 Common Interfaces of Descriptor Matchers . . . . . . . . . . . . . . . . . . . . . . . 756
cv::DMatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757
cv::DescriptorMatcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757
cv::DescriptorMatcher::add . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758
cv::DescriptorMatcher::getTrainDescriptors . . . . . . . . . . . . . . . . . . . . . . . 759
cv::DescriptorMatcher::clear . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
cv::DescriptorMatcher::empty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
cv::DescriptorMatcher::isMaskSupported . . . . . . . . . . . . . . . . . . . . . . . . 759
cv::DescriptorMatcher::train . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760
CONTENTS 27
cv::DescriptorMatcher::match . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760
cv::DescriptorMatcher::knnMatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761
cv::DescriptorMatcher::radiusMatch . . . . . . . . . . . . . . . . . . . . . . . . . . . 762
cv::DescriptorMatcher::clone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762
cv::DescriptorMatcher::create . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763
cv::BruteForceMatcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763
cv::FlannBasedMatcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765
13.5 Common Interfaces of Generic Descriptor Matchers . . . . . . . . . . . . . . . . . . 765
cv::GenericDescriptorMatcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766
cv::GenericDescriptorMatcher::add . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767
cv::GenericDescriptorMatcher::getTrainImages . . . . . . . . . . . . . . . . . . . . . 768
cv::GenericDescriptorMatcher::getTrainKeypoints . . . . . . . . . . . . . . . . . . . . 768
cv::GenericDescriptorMatcher::clear . . . . . . . . . . . . . . . . . . . . . . . . . . . 768
cv::GenericDescriptorMatcher::train . . . . . . . . . . . . . . . . . . . . . . . . . . . 768
cv::GenericDescriptorMatcher::isMaskSupported . . . . . . . . . . . . . . . . . . . . 768
cv::GenericDescriptorMatcher::classify . . . . . . . . . . . . . . . . . . . . . . . . . . 768
cv::GenericDescriptorMatcher::match . . . . . . . . . . . . . . . . . . . . . . . . . . 769
cv::GenericDescriptorMatcher::knnMatch . . . . . . . . . . . . . . . . . . . . . . . . 770
cv::GenericDescriptorMatcher::radiusMatch . . . . . . . . . . . . . . . . . . . . . . . 771
cv::GenericDescriptorMatcher::read . . . . . . . . . . . . . . . . . . . . . . . . . . . 771
cv::GenericDescriptorMatcher::write . . . . . . . . . . . . . . . . . . . . . . . . . . . 772
cv::GenericDescriptorMatcher::clone . . . . . . . . . . . . . . . . . . . . . . . . . . . 772
cv::OneWayDescriptorMatcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772
cv::FernDescriptorMatcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773
cv::VectorDescriptorMatcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
13.6 Drawing Function of Keypoints and Matches . . . . . . . . . . . . . . . . . . . . . . . 775
cv::drawMatches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
cv::drawKeypoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777
13.7 Object Categorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778
cv::BOWTrainer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778
cv::BOWTrainer::add . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778
cv::BOWTrainer::getDescriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 779
cv::BOWTrainer::descripotorsCount . . . . . . . . . . . . . . . . . . . . . . . . . . . 779
cv::BOWTrainer::cluster . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 779
cv::BOWKMeansTrainer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 779
cv::BOWImgDescriptorExtractor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 780
cv::BOWImgDescriptorExtractor::BOWImgDescriptorExtractor . . . . . . . . . . . . . 781
cv::BOWImgDescriptorExtractor::setVocabulary . . . . . . . . . . . . . . . . . . . . . 781
cv::BOWImgDescriptorExtractor::getVocabulary . . . . . . . . . . . . . . . . . . . . . 781
cv::BOWImgDescriptorExtractor::compute . . . . . . . . . . . . . . . . . . . . . . . . 782
28 CONTENTS
cv::BOWImgDescriptorExtractor::descriptorSize . . . . . . . . . . . . . . . . . . . . . 782
cv::BOWImgDescriptorExtractor::descriptorType . . . . . . . . . . . . . . . . . . . . 782
cv::calcMotionGradient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802
cv::calcGlobalOrientation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803
cv::CamShift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803
cv::meanShift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804
cv::KalmanFilter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805
cv::composeRT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834
cv::computeCorrespondEpilines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835
cv::convertPointsHomogeneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836
cv::decomposeProjectionMatrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836
cv::drawChessboardCorners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837
cv::findChessboardCorners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838
cv::solvePnP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839
cv::findFundamentalMat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840
cv::findHomography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842
cv::getDefaultNewCameraMatrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844
cv::getOptimalNewCameraMatrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845
cv::initCameraMatrix2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845
cv::initUndistortRectifyMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846
cv::matMulDeriv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848
cv::projectPoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848
cv::reprojectImageTo3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850
cv::RQDecomp3x3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851
cv::Rodrigues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851
cv::StereoBM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852
cv::StereoSGBM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853
cv::StereoSGBM::StereoSGBM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854
cv::StereoSGBM::operator () . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855
cv::stereoCalibrate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 856
cv::stereoRectify . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858
cv::stereoRectifyUncalibrated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862
cv::undistort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863
cv::undistortPoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863
CvStatModel::predict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 872
19.2 Normal Bayes Classifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 872
cv::CvNormalBayesClassifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 873
CvNormalBayesClassifier::train . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 873
CvNormalBayesClassifier::predict . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874
19.3 K Nearest Neighbors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874
cv::CvKNearest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874
CvKNearest::train . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875
CvKNearest::find nearest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 876
19.4 Support Vector Machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 878
cv::CvSVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879
cv::CvSVMParams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 880
CvSVM::train . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 881
CvSVM::train auto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 881
CvSVM::get default grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 882
CvSVM::get params . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883
CvSVM::get support vector* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883
19.5 Decision Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 884
Predicting with Decision Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 884
Training Decision Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 884
Variable importance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885
cv::CvDTreeSplit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885
cv::CvDTreeNode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 886
cv::CvDTreeParams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 886
cv::CvDTreeTrainData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 887
cv::CvDTree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 890
CvDTree::train . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 892
CvDTree::predict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 892
19.6 Boosting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 893
cv::CvBoostParams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 895
cv::CvBoostTree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 895
cv::CvBoost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 896
CvBoost::train . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 897
CvBoost::predict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898
CvBoost::prune . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898
CvBoost::get weak predictors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898
19.7 Random Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899
cv::CvRTParams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 900
cv::CvRTrees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 900
CvRTrees::train . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 901
32 CONTENTS
CvRTrees::predict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 902
CvRTrees::get var importance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 902
CvRTrees::get proximity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 903
19.8 Expectation-Maximization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 906
cv::CvEMParams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 907
cv::CvEM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 908
CvEM::train . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 909
19.9 Neural Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 912
cv::CvANN MLP TrainParams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915
cv::CvANN MLP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 916
CvANN MLP::create . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 918
CvANN MLP::train . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 918
20 Introduction 923
20.1 Cookbook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 923
Convert an image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 923
Resize an image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924
Compute the Laplacian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924
Using GoodFeaturesToTrack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924
Using GetSubRect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924
Using CreateMat, and accessing an element . . . . . . . . . . . . . . . . . . . . . . 925
ROS image message to OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925
PIL Image to OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925
OpenCV to PIL Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925
NumPy and OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 926
OpenCV to pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 926
OpenCV and OpenEXR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927
CvRect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930
CvScalar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930
CvTermCriteria . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930
CvMat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931
CvMatND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931
IplImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931
CvArr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 932
21.2 Operations on Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 932
cv::AbsDiff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 932
cv::AbsDiffS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 933
cv::Add . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 933
cv::AddS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934
cv::AddWeighted . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934
cv::And . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 935
cv::AndS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 935
cv::Avg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936
cv::AvgSdv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936
cv::CalcCovarMatrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937
cv::CartToPolar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 938
cv::Cbrt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939
cv::ClearND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939
cv::CloneImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
cv::CloneMat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
cv::CloneMatND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
cv::Cmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
cv::CmpS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941
cv::Convert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942
cv::ConvertScale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 943
cv::ConvertScaleAbs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 943
cv::CvtScaleAbs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 944
cv::Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 944
cv::CountNonZero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945
cv::CreateData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945
cv::CreateImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946
cv::CreateImageHeader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946
cv::CreateMat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947
cv::CreateMatHeader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947
cv::CreateMatND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947
cv::CreateMatNDHeader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948
cv::CrossProduct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948
34 CONTENTS
CvtPixToPlane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 949
cv::DCT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 949
cv::DFT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 950
cv::Det . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 952
cv::Div . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 952
cv::DotProduct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 953
cv::EigenVV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 953
cv::Exp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 954
cv::FastArctan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 954
cv::Flip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955
cv::fromarray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 956
cv::GEMM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957
cv::Get1D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957
cv::Get2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 958
cv::Get3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 958
cv::GetND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 959
cv::GetCol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 959
cv::GetCols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 959
cv::GetDiag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 960
cv::GetDims . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 960
cv::GetElemType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961
cv::GetImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961
cv::GetImageCOI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961
cv::GetImageROI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 962
cv::GetMat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 962
cv::GetOptimalDFTSize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963
cv::GetReal1D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963
cv::GetReal2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 964
cv::GetReal3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 964
cv::GetRealND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965
cv::GetRow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965
cv::GetRows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965
cv::GetSize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 966
cv::GetSubRect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 966
cv::InRange . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 967
cv::InRangeS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 967
cv::InvSqrt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 968
cv::Inv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 968
cv::Invert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969
cv::IsInf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969
CONTENTS 35
cv::IsNaN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 970
cv::LUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 970
cv::Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971
cv::Mahalanobis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971
cv::Max . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 972
cv::MaxS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 972
cv::Merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 973
cv::Min . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 973
cv::MinMaxLoc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 974
cv::MinS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 974
Mirror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 975
cv::MixChannels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 975
MulAddS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 976
cv::Mul . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 976
cv::MulSpectrums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 976
cv::MulTransposed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977
cv::Norm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978
cv::Not . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 979
cv::Or . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 979
cv::OrS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 980
cv::PerspectiveTransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 980
cv::PolarToCart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 981
cv::Pow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 982
cv::RNG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 982
cv::RandArr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 983
cv::RandInt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 983
cv::RandReal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 984
cv::Reduce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 984
cv::Repeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985
cv::ResetImageROI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985
cv::Reshape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 986
cv::ReshapeMatND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 986
cv::Round . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 987
cv::Floor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 987
cv::Ceil . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 988
cv::ScaleAdd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 988
cv::Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 988
cv::Set1D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 989
cv::Set2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 989
cv::Set3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 990
36 CONTENTS
cv::SetND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 990
cv::SetData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 991
cv::SetIdentity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 991
cv::SetImageCOI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 992
cv::SetImageROI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 992
cv::SetReal1D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993
cv::SetReal2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993
cv::SetReal3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993
cv::SetRealND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 994
cv::SetZero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 994
cv::Solve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 995
cv::SolveCubic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 995
cv::Split . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 996
cv::Sqrt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997
cv::Sub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997
cv::SubRS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997
cv::SubS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 998
cv::Sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 999
cv::SVBkSb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 999
cv::SVD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1000
cv::Trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1001
cv::Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1002
cv::Transpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1002
cv::Xor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003
cv::XorS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003
cv::mGet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1004
cv::mSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1004
21.3 Dynamic Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1005
CvMemStorage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1005
CvSeq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1005
CvSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1006
cv::CloneSeq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1006
cv::CreateMemStorage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1006
cv::SeqInvert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1007
cv::SeqRemove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1007
cv::SeqRemoveSlice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1007
21.4 Drawing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1008
cv::Circle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1008
cv::ClipLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009
cv::DrawContours . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009
CONTENTS 37
cv::Ellipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1010
cv::EllipseBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1011
cv::FillConvexPoly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1012
cv::FillPoly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1012
cv::GetTextSize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1013
cv::InitFont . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1013
cv::InitLineIterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1014
cv::Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1015
cv::PolyLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1016
cv::PutText . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1017
cv::Rectangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1017
CV RGB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1018
21.5 XML/YAML Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1018
cv::Load . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1018
cv::Save . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1019
21.6 Clustering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1019
cv::KMeans2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1019
21.7 Utility and System Functions and Macros . . . . . . . . . . . . . . . . . . . . . . . . 1020
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1020
cv::GetTickCount . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1020
cv::GetTickFrequency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1021
IplConvKernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1033
cv::CopyMakeBorder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1033
cv::CreateStructuringElementEx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1034
cv::Dilate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1035
cv::Erode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1036
cv::Filter2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1036
cv::Laplace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1037
cv::MorphologyEx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1038
cv::PyrDown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1039
cv::Smooth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1039
cv::Sobel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1041
22.3 Geometric Image Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1042
cv::GetRotationMatrix2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1043
cv::GetAffineTransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1044
cv::GetPerspectiveTransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1044
cv::GetQuadrangleSubPix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1045
cv::GetRectSubPix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1046
cv::LogPolar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1046
cv::Remap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1047
cv::Resize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1048
cv::WarpAffine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1048
cv::WarpPerspective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1049
22.4 Miscellaneous Image Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . 1050
cv::AdaptiveThreshold . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1050
cv::CvtColor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1052
cv::DistTransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1056
CvConnectedComp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1058
cv::FloodFill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1058
cv::Inpaint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1060
cv::Integral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1061
cv::PyrMeanShiftFiltering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1062
cv::PyrSegmentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1063
cv::Threshold . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1064
22.5 Structural Analysis and Shape Descriptors . . . . . . . . . . . . . . . . . . . . . . . . 1066
cv::ApproxChains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1066
cv::ApproxPoly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1066
cv::ArcLength . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1067
cv::BoundingRect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1068
cv::BoxPoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1068
cv::CalcPGH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1069
CONTENTS 39
cv::CalcEMD2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1069
cv::CheckContourConvexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1070
CvConvexityDefect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1071
cv::ContourArea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1071
cv::ContourFromContourTree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1072
cv::ConvexHull2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1073
cv::ConvexityDefects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1073
cv::CreateContourTree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1074
cv::FindContours . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1074
cv::FitEllipse2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1076
cv::FitLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1076
cv::GetCentralMoment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1077
cv::GetHuMoments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1078
cv::GetNormalizedCentralMoment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1079
cv::GetSpatialMoment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1079
cv::MatchContourTrees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1080
cv::MatchShapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1080
cv::MinAreaRect2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1081
cv::MinEnclosingCircle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1082
cv::Moments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1083
cv::PointPolygonTest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1083
22.6 Planar Subdivisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1084
CvSubdiv2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1084
CvSubdiv2DPoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1085
cv::CalcSubdivVoronoi2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1085
cv::ClearSubdivVoronoi2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1086
cv::CreateSubdivDelaunay2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1086
cv::FindNearestPoint2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1086
cv::Subdiv2DEdgeDst . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1087
cv::Subdiv2DGetEdge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1087
cv::Subdiv2DNextEdge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1088
cv::Subdiv2DLocate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1089
cv::Subdiv2DRotateEdge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1090
cv::SubdivDelaunay2DInsert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1091
22.7 Motion Analysis and Object Tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . 1091
cv::Acc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1091
cv::MultiplyAcc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1092
cv::RunningAvg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1092
cv::SquareAcc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1093
22.8 Feature Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1093
40 CONTENTS
cv::Canny . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1093
cv::CornerEigenValsAndVecs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1094
cv::CornerHarris . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1094
cv::CornerMinEigenVal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1095
cv::FindCornerSubPix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1096
cv::GoodFeaturesToTrack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1097
cv::HoughLines2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1098
cv::PreCornerDetect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1099
22.9 Object Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1100
cv::MatchTemplate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1100
cv::MeanShift . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1122
cv::SegmentMotion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1122
cv::SnakeImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1123
cv::UpdateMotionHistory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1124
cv::CreateStereoGCState . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1147
CvStereoBMState . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1148
CvStereoGCState . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1149
cv::DecomposeProjectionMatrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1150
cv::DrawChessboardCorners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1151
cv::FindChessboardCorners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1151
cv::FindExtrinsicCameraParams2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1153
cv::FindFundamentalMat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1153
cv::FindHomography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1155
cv::FindStereoCorrespondenceBM . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1156
cv::FindStereoCorrespondenceGC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1157
cv::GetOptimalNewCameraMatrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1159
cv::InitIntrinsicParams2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1160
cv::InitUndistortMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1161
cv::InitUndistortRectifyMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1161
cv::POSIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1163
cv::ProjectPoints2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1164
cv::ReprojectImageTo3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1165
cv::RQDecomp3x3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1166
cv::Rodrigues2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1166
cv::StereoCalibrate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1167
cv::StereoRectify . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1170
cv::StereoRectifyUncalibrated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1173
cv::Undistort2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1174
cv::UndistortPoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1175
Bibliography 1177
Index 1180
Part I
C API Reference
43
Chapter 1
x x-coordinate
y y-coordinate
/* Constructor */
inline CvPoint cvPoint( int x, int y );
45
46 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
x x-coordinate
y y-coordinate
/* Constructor */
inline CvPoint2D32f cvPoint2D32f( double x, double y );
x x-coordinate
y y-coordinate
z z-coordinate
/* Constructor */
inline CvPoint3D32f cvPoint3D32f( double x, double y, double z );
1.1. BASIC STRUCTURES 47
x x-coordinate
y y-coordinate
/* Constructor */
inline CvPoint2D64f cvPoint2D64f( double x, double y );
x x-coordinate
y y-coordinate
z z-coordinate
/* Constructor */
inline CvPoint3D64f cvPoint3D64f( double x, double y, double z );
48 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
/* Constructor */
inline CvSize cvSize( int width, int height );
/* Constructor */
inline CvSize2D32f cvSize2D32f( double width, double height );
/* Constructor */
inline CvRect cvRect( int x, int y, int width, int height );
/* Constructor:
initializes val[0] with val0, val[1] with val1, etc.
*/
inline CvScalar cvScalar( double val0, double val1=0,
double val2=0, double val3=0 );
/* Constructor:
initializes all of val[0]...val[3] with val0123
*/
inline CvScalar cvScalarAll( double val0123 );
/* Constructor:
initializes val[0] with val0, and all of val[1]...val[3] with zeros
*/
inline CvScalar cvRealScalar( double val0 );
50 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
/* Constructor */
inline CvTermCriteria cvTermCriteria( int type, int max_iter, double epsilon );
int* refcount;
union
1.1. BASIC STRUCTURES 51
{
uchar* ptr;
short* s;
int* i;
float* fl;
double* db;
} data;
#ifdef __cplusplus
union
{
int rows;
int height;
};
union
{
int cols;
int width;
};
#else
int rows;
int cols;
#endif
} CvMat;
type A CvMat signature (CV MAT MAGIC VAL) containing the type of elements and flags
Matrices are stored row by row. All of the rows are aligned by 4 bytes.
int* refcount;
union
{
uchar* ptr;
short* s;
int* i;
float* fl;
double* db;
} data;
struct
{
int size;
int step;
}
dim[CV_MAX_DIM];
} CvMatND;
type A CvMatND signature (CV MATND MAGIC VAL), combining the type of elements and flags
dim For each dimension, the pair (number of elements, distance between elements in bytes)
} CvSparseMat;
type A CvSparseMat signature (CV SPARSE MAT MAGIC VAL), combining the type of elements
and flags.
int dataOrder;
int origin;
int align;
int width;
int height;
struct _IplROI *roi;
struct _IplImage *maskROI;
void *imageId;
struct _IplTileInfo *tileInfo;
int imageSize;
char *imageData;
int widthStep;
int BorderMode[4];
int BorderConst[4];
char *imageDataOrigin;
}
IplImage;
nSize sizeof(IplImage)
depth Channel depth in bits + the optional sign bit (IPL DEPTH SIGN). The supported depths
are:
colorModel Ignored by OpenCV. The OpenCV function CvtColor requires the source and des-
tination color spaces as parameters.
dataOrder 0 = IPL DATA ORDER PIXEL - interleaved color channels, 1 - separate color chan-
nels. CreateImage only creates images with interleaved channels. For example, the usual
layout of a color image is: b00 g00 r00 b10 g10 r10 ...
align Alignment of image rows (4 or 8). OpenCV ignores this and uses widthStep instead.
roi Region Of Interest (ROI). If not NULL, only this image region will be processed.
imageSize Image data size in bytes. For interleaved data, this equals image->height·image->widthStep
imageDataOrigin A pointer to the origin of the image data (not necessarily aligned). This is
used for image deallocation.
The IplImage structure was inherited from the Intel Image Processing Library, in which the
format is native. OpenCV only supports a subset of possible IplImage formats, as outlined in the
parameter list above.
In addition to the above restrictions, OpenCV handles ROIs differently. OpenCV functions
require that the image size or ROI size of all source and destination images match exactly. On
the other hand, the Intel Image Processing Library processes the area of intersection between the
source and destination images (or ROIs), allowing them to vary independently.
56 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
void cvAdd(const CvArr* src1, const CvArr* src2, CvArr* dst, const
CvArr* mask=NULL);
mask Operation mask, 8-bit single channel array; specifies elements of the destination array to
be changed
void cvAddS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr*
mask=NULL);
mask Operation mask, 8-bit single channel array; specifies elements of the destination array to
be changed
The function adds a scalar value to every element in the source array src1 and stores the
result in dst. For types that have limited range this operation is saturating.
dst(I)=src(I)+value if mask(I)!=0
All the arrays must have the same type, except the mask, and the same size (or ROI size).
void cvAnd(const CvArr* src1, const CvArr* src2, CvArr* dst, const
CvArr* mask=NULL);
mask Operation mask, 8-bit single channel array; specifies elements of the destination array to
be changed
void cvAndS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr*
mask=NULL);
mean Pointer to the output mean value, may be NULL if it is not needed
The function calculates the average value and standard deviation of array elements, indepen-
dently for each channel:
P
N = I (mask(I) 6= 0)
meanc = N1
P
q I,Pmask(I)6=0 arr(I)c
stdDevc = N1 I, mask(I)6=0 (arr(I)c − meanc )
2
If the array is IplImage and COI is set, the function processes the selected channel only and
stores the average and standard deviation to the first components of the output scalars (mean0
and stdDev0 ).
void cvCalcCovarMatrix(
const CvArr** vects,
int count,
CvArr* covMat,
CvArr* avg,
int flags);
62 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
vects The input vectors, all of which must have the same type and the same size. The vectors
do not have to be 1D, they can be 2D (e.g., images) and so forth
covMat The output covariance matrix that should be floating-point and square
avg The input or output (depending on the flags) array - the mean (average) vector of the input
vectors
scale ∗ [vects[0] − avg, vects[1] − avg, ...]T · [vects[0] − avg, vects[1] − avg, ...]
, that is, the covariance matrix is count × count. Such an unusual covariance matrix
is used for fast PCA of a set of very large vectors (see, for example, the EigenFaces
technique for face recognition). Eigenvalues of this ”scrambled” matrix will match the
eigenvalues of the true covariance matrix and the ”true” eigenvectors can be easily
calculated from the eigenvectors of the ”scrambled” covariance matrix.
CV COVAR NORMAL The output covariance matrix is calculated as:
scale ∗ [vects[0] − avg, vects[1] − avg, ...] · [vects[0] − avg, vects[1] − avg, ...]T
, that is, covMat will be a covariance matrix with the same linear size as the total
number of elements in each input vector. One and only one of CV COVAR SCRAMBLED
and CV COVAR NORMAL must be specified
CV COVAR USE AVG If the flag is specified, the function does not calculate avg from the
input vectors, but, instead, uses the passed avg vector. This is useful if avg has been
already calculated somehow, or if the covariance matrix is calculated by parts - in this
case, avg is not a mean vector of the input sub-set of vectors, but rather the mean
vector of the whole set.
CV COVAR SCALE If the flag is specified, the covariance matrix is scaled. In the ”normal”
mode scale is ’1./count’; in the ”scrambled” mode scale is the reciprocal of the total
number of elements in each input vector. By default (if the flag is not specified) the
covariance matrix is not scaled (’scale=1’).
CV COVAR ROWS Means that all the input vectors are stored as rows of a single matrix,
vects[0]. count is ignored in this case, and avg should be a single-row vector of an
appropriate size.
1.2. OPERATIONS ON ARRAYS 63
CV COVAR COLS Means that all the input vectors are stored as columns of a single matrix,
vects[0]. count is ignored in this case, and avg should be a single-column vector
of an appropriate size.
The function calculates the covariance matrix and, optionally, the mean vector of the set of input
vectors. The function can be used for PCA, for comparing vectors using Mahalanobis distance and
so forth.
void cvCartToPolar(
const CvArr* x,
const CvArr* y,
CvArr* magnitude,
CvArr* angle=NULL,
int angleInDegrees=0);
magnitude The destination array of magnitudes, may be set to NULL if it is not needed
angle The destination array of angles, may be set to NULL if it is not needed. The angles are
measured in radians (0 to 2π) or in degrees (0 to 360 degrees).
angleInDegrees The flag indicating whether the angles are measured in radians, which is de-
fault mode, or in degrees
The function calculates either the magnitude, angle, or both of every 2d vector (x(I),y(I)):
magnitude(I)=sqrt(x(I)ˆ2ˆ+y(I)ˆ2ˆ ),
angle(I)=atan(y(I)/x(I) )
The angles are calculated with 0.1 degree accuracy. For the (0,0) point, the angle is set to 0.
64 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
The function calculates the cubic root of the argument, and normally it is faster than pow(value,1./3).
In addition, negative arguments are handled properly. Special values (±∞, NaN) are not handled.
The function cvClearND clears (sets to zero) a specific element of a dense array or deletes the
element of a sparse array. If the sparse array element does not exists, the function does nothing.
The function creates a copy of the input array and returns pointer to the copy.
void cvCmp(const CvArr* src1, const CvArr* src2, CvArr* dst, int
cmpOp);
src2 The second source array. Both source arrays must have a single channel.
cmpOp The flag specifying the relation between the elements to be checked
The function compares the corresponding elements of two arrays and fills the destination mask
array:
dst(I)=src1(I) op src2(I),
dst(I) is set to 0xff (all 1-bits) if the specific relation between the elements is true and 0
otherwise. All the arrays must have the same type, except the destination, and the same size (or
ROI size)
void cvCmpS(const CvArr* src, double value, CvArr* dst, int cmpOp);
cmpOp The flag specifying the relation between the elements to be checked
The function compares the corresponding elements of an array and a scalar and fills the des-
tination mask array:
dst(I)=src(I) op scalar
where op is =, >, ≥, <, ≤ or 6=.
dst(I) is set to 0xff (all 1-bits) if the specific relation between the elements is true and 0
otherwise. All the arrays must have the same size (or ROI size).
The function has several different purposes, and thus has several different names. It copies
one array to another with optional scaling, which is performed first, and/or optional type conversion,
performed after:
The function is similar to cvConvertScale, but it stores absolute values of the conversion
results:
The function supports only destination arrays of 8u (8-bit unsigned integers) type; for other
types the function can be emulated by a combination of cvConvertScale and cvAbs functions.
1.2. OPERATIONS ON ARRAYS 69
The function is similar to cvConvertScale, but it stores absolute values of the conversion
results:
mask Operation mask, 8-bit single channel array; specifies elements of the destination array to
be changed
70 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
The function copies selected elements from an input array to an output array:
arr The array must be a single-channel array or a multi-channel image with COI set
The function allocates image, matrix or multi-dimensional array data. Note that in the case
of matrix types OpenCV allocation functions are used and in the case of IplImage they are used
unless CV TURN ON IPL COMPATIBILITY was called. In the latter case IPL functions are used
to allocate the data.
1.2. OPERATIONS ON ARRAYS 71
CvMat* cvCreateMat(
int rows,
int cols,
int type);
type The type of the matrix elements in the form CV <bit depth><S|U|F>C<number of
channels>, where S=signed, U=unsigned, F=float. For example, CV 8UC1 means the
elements are 8-bit unsigned and the there is 1 channel, and CV 32SC2 means the elements
are 32-bit signed and there are 2 channels.
CvMat* cvCreateMatHeader(
int rows,
int cols,
int type);
The function allocates a new matrix header and returns a pointer to it. The matrix data can
then be allocated using cvCreateData or set explicitly to user-allocated data via cvSetData.
CvMatND* cvCreateMatND(
int dims,
const int* sizes,
int type);
dims Number of array dimensions. This must not exceed CV MAX DIM (32 by default, but can
be changed at build time).
CvMatND* cvCreateMatNDHeader(
int dims,
const int* sizes,
int type);
The function allocates a header for a multi-dimensional dense array. The array data can further
be allocated using cvCreateData or set explicitly to user-allocated data via cvSetData.
dims Number of array dimensions. In contrast to the dense matrix, the number of dimensions is
practically unlimited (up to 216 ).
The function allocates a multi-dimensional sparse array. Initially the array contain no elements,
that is cvGet or cvGetReal returns zero for every index.
CvtPixToPlane
Synonym for Split .
Y = C (N ) · X
where
π(2k + 1)j
q
(N )
Cjk = αj /N cos
2N
76 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
T
(since C (N ) is orthogonal matrix, C (N ) · C (N ) = I)
Forward Cosine transform of 2D M × N matrix:
T
Y = C (N ) · X · C (N )
void cvDFT(const CvArr* src, CvArr* dst, int flags, int nonzeroRows=0);
dst Destination array of the same size and same type as the source
nonzeroRows Number of nonzero rows in the source array (in the case of a forward 2d trans-
form), or a number of rows of interest in the destination array (in the case of an inverse 2d
transform). If the value is negative, zero, or greater than the total number of rows, it is ig-
nored. The parameter can be used to speed up 2d convolution/correlation when computing
via DFT. See the example below.
,
i = sqrt(−1)
Y = F (M ) · X · F (N )
In the case of real (single-channel) data, the packed format, borrowed from IPL, is used to
represent the result of a forward Fourier transform or input for an inverse Fourier transform:
Note: the last column is present if N is even, the last row is present if M is even. In the case of
1D real transform the result looks like the first row of the above matrix.
Here is the example of how to compute 2D convolution using DFT.
78 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
// initialize A and B
...
cvCopy(&tmp, conv);
The function decrements the data reference counter in a CvMat or CvMatND if the reference
counter pointer is not NULL. If the counter reaches zero, the data is deallocated. In the current
implementation the reference counter is not NULL only if the data was allocated using the cvCre-
ateData function. The counter will be NULL in other cases such as: external data was assigned to
the header using cvSetData, the matrix header is part of a larger matrix or image, or the header
was converted from an image or n-dimensional matrix header.
The function returns the determinant of the square matrix mat. The direct method is used
for small matrices and Gaussian elimination is used for larger matrices. For symmetric positive-
determined matrices, it is also possible to run cvSVD with U = V = 0 and then calculate the
determinant as a product of the diagonal elements of W .
void cvDiv(const CvArr* src1, const CvArr* src2, CvArr* dst, double
scale=1);
src1 The first source array. If the pointer is NULL, the array is assumed to be all 1’s.
The function calculates and returns the Euclidean dot product of two arrays.
X
src1 • src2 = (src1(I)src2(I))
I
In the case of multiple channel arrays, the results for all channels are accumulated. In par-
ticular, cvDotProduct(a,a) where a is a complex vector, will return ||a||2 . The function can
process multi-dimensional arrays, row by row, layer by layer, and so on.
1.2. OPERATIONS ON ARRAYS 81
void cvEigenVV(
CvArr* mat,
CvArr* evects,
CvArr* evals,
double eps=0,
int lowindex = -1,
int highindex = -1);
mat The input symmetric square matrix, modified during the processing
evals The output vector of eigenvalues, stored in the descending order (order of eigenvalues
and eigenvectors is syncronized, of course)
eps Accuracy of diagonalization. Typically, DBL EPSILON (about 10−15 ) works well. THIS PA-
RAMETER IS CURRENTLY IGNORED.
dst The destination array, it should have double type or the same type as the source
The function calculates the exponent of every element of the input array:
dst[I] = esrc(I)
The maximum relative error is about 7 × 10−6 . Currently, the function converts denormalized
values to zeros on output.
x x-coordinate of 2D vector
y y-coordinate of 2D vector
The function calculates the full-range angle of an input 2D vector. The angle is measured in
degrees and varies from 0 degrees to 360 degrees. The accuracy is about 0.1 degrees.
flipMode Specifies how to flip the array: 0 means flipping around the x-axis, positive (e.g., 1)
means flipping around y-axis, and negative (e.g., -1) means flipping around both axes. See
also the discussion below for the formulas:
The function flips the array in one of three different ways (row and column indices are 0-based):
src(rows(src) − i − 1, j) if flipMode = 0
dst(i, j) = src(i, cols(src) − j − 1) if flipMode > 0
src(rows(src) − i − 1, cols(src) − j − 1) if flipMode < 0
• horizontal flipping of the image with subsequent horizontal shift and absolute difference cal-
culation to check for a vertical-axis symmetry (flipMode > 0)
• simultaneous horizontal and vertical flipping of the image with subsequent shift and absolute
difference calculation to check for a central symmetry (flipMode < 0)
void cvGEMM(
const CvArr* src1,
const CvArr* src2, double alpha,
const CvArr* src3,
double beta,
84 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
CvArr* dst,
int tABC=0);
#define cvMatMulAdd(src1, src2, src3, dst ) cvGEMM(src1, src2, 1, src3,
1, dst, 0 )
#define cvMatMul(src1, src2, dst ) cvMatMulAdd(src1, src2,
0, dst )
The functions return a specific array element. In the case of a sparse array the functions return
0 if the requested node does not exist (no new node is created by the functions).
The functions GetCol and GetCols return the header, corresponding to a specified column
span of the input array. GetCol is a shortcut for cvGetCols:
cvGetCol(arr, submat, col); // ˜ cvGetCols(arr, submat, col, col + 1);
86 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
diag Array diagonal. Zero corresponds to the main diagonal, -1 corresponds to the diagonal
above the main , 1 corresponds to the diagonal below the main, and so forth.
The function returns the header, corresponding to a specified diagonal of the input array.
cvGetDims, cvGetDimSize
Return number of array dimensions and their sizes or the size of a particular dimension.
sizes Optional output vector of the array dimension sizes. For 2d arrays the number of rows
(height) goes first, number of columns (width) next.
index Zero-based dimension index (for matrices 0 means number of rows, 1 means number of
columns; for images 0 means height, 1 means width)
1.2. OPERATIONS ON ARRAYS 87
The function cvGetDims returns the array dimensionality and the array of dimension sizes.
In the case of IplImage or CvMat it always returns 2 regardless of number of image/matrix
rows. The function cvGetDimSize returns the particular dimension size (number of elements per
that dimension). For example, the following code calculates total number of array elements in two
ways:
// via cvGetDims()
int sizes[CV_MAX_DIM];
int i, total = 1;
int dims = cvGetDims(arr, size);
for(i = 0; i < dims; i++ )
total *= sizes[i];
The function returns type of the array elements as described in cvCreateMat discussion:
CV 8UC1 ... CV 64FC4.
The function returns the image header for the input array that can be a matrix - CvMat , or an
image - IplImage*. In the case of an image the function simply returns the input pointer. In the
case of CvMat it initializes an imageHeader structure with the parameters of the input matrix.
Note that if we transform IplImage to CvMat and then transform CvMat back to IplImage, we
can get different headers if the ROI is set, and thus some IPL functions that calculate image stride
from its width and align may fail on the resultant image.
Returns the channel of interest of in an IplImage. Returned values correspond to the coi in
cvSetImageCOI.
The function returns a matrix header for the input array that can be a matrix -
CvMat , an image - IplImage or a multi-dimensional dense array - CvMatND (latter case is
allowed only if allowND != 0) . In the case of matrix the function simply returns the input pointer.
In the case of IplImage* or CvMatND it initializes the header structure with parameters of
the current image ROI and returns the pointer to this temporary structure. Because COI is not
supported by CvMat , it is returned separately.
The function provides an easy way to handle both types of arrays - IplImage and CvMat
- using the same code. Reverse transform from CvMat to IplImage can be done using the
cvGetImage function.
Input array must have underlying data allocated or attached, otherwise the function fails.
If the input array is IplImage with planar data layout and COI set, the function returns the
pointer to the selected plane and COI = 0. It enables per-plane processing of multi-channel images
with planar data layout using OpenCV functions.
sum += val;
}
data Output pointer to the whole image origin or ROI origin if ROI is set
The function fills output variables with low-level information about the array data. All output
parameters are optional, so some of the pointers may be set to NULL. If the array is IplImage
with ROI set, the parameters of ROI are returned.
The following example shows how to get access to array elements. GetRawData calculates
the absolute value of the elements in a single-channel, floating-point array.
float* data;
int step;
CvSize size;
int x, y;
Returns a specific element of a single-channel array. If the array has multiple channels, a
runtime error is raised. Note that cvGet function can be used safely for both single-channel and
multiple-channel arrays though they are a bit slower.
In the case of a sparse array the functions return 0 if the requested node does not exist (no
new node is created by the functions).
Returns a specific element of a single-channel array. If the array has multiple channels, a
runtime error is raised. Note that cvGet function can be used safely for both single-channel and
multiple-channel arrays though they are a bit slower.
In the case of a sparse array the functions return 0 if the requested node does not exist (no
new node is created by the functions).
double cvGetReal3D(const CvArr* arr, int idx0, int idx1, int idx2);
Returns a specific element of a single-channel array. If the array has multiple channels, a
runtime error is raised. Note that cvGet function can be used safely for both single-channel and
multiple-channel arrays though they are a bit slower.
In the case of a sparse array the functions return 0 if the requested node does not exist (no
new node is created by the functions).
Returns a specific element of a single-channel array. If the array has multiple channels, a
runtime error is raised. Note that cvGet function can be used safely for both single-channel and
multiple-channel arrays though they are a bit slower.
In the case of a sparse array the functions return 0 if the requested node does not exist (no
new node is created by the functions).
94 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
The function returns header, corresponding to a specified rectangle of the input array. In other
words, it allows the user to treat a rectangular part of input array as a stand-alone array. ROI is
taken into account by the function so the sub-array of ROI is actually extracted.
The function does the range check for every element of the input array:
dst(I) = lower(I)0 <= src(I)0 < upper(I)0 ∧ lower(I)1 <= src(I)1 < upper(I)1
The function does the range check for every element of the input array:
dst(I) = lower0 <= src(I)0 < upper0 ∧ lower1 <= src(I)1 < upper1
For two-channel arrays nd so forth,
’dst(I)’ is set to 0xff (all 1-bits) if ’src(I)’ is within the range and 0 otherwise. All the arrays must
have the same size (or ROI size).
The function increments CvMat or CvMatND data reference counter and returns the new
counter value if the reference counter pointer is not NULL, otherwise it returns zero.
IplImage* cvInitImageHeader(
IplImage* image,
CvSize size,
int depth,
int channels,
int origin=0,
int align=4);
CvMat* cvInitMatHeader(
CvMat* mat,
int rows,
int cols,
int type,
void* data=NULL,
int step=CV AUTOSTEP);
step Optional: full row width in bytes of the assigned data. By default, the minimal possible step
is used which assumes there are no gaps between subsequent rows of the matrix.
This function is often used to process raw data with OpenCV matrix functions. For example,
the following code computes the matrix product of two matrices, stored as ordinary arrays:
double a[] = { 1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12 };
double b[] = { 1, 5, 9,
2, 6, 10,
3, 7, 11,
4, 8, 12 };
double c[9];
CvMat Ma, Mb, Mc ;
CvMatND* cvInitMatNDHeader(
CvMatND* mat,
int dims,
const int* sizes,
int type,
void* data=NULL);
The function initializes iterator of sparse array elements and returns pointer to the first element,
or NULL if the array is empty.
The function calculates the inverse square root of the argument, and normally it is faster than
1./sqrt(value). If the argument is zero or negative, the result is not determined. Special
values (±∞ , NaN) are not handled.
The function inverts matrix src1 and stores the result in src2.
In the case of LU method, the function returns the src1 determinant (src1 must be square). If
it is 0, the matrix is not inverted and src2 is filled with zeros.
In the case of SVD methods, the function returns the inversed condition of src1 (ratio of the
smallest singular value to the largest singular value) and 0 if src1 is all zeros. The SVD methods
calculate a pseudo-inverse matrix if src1 is singular.
The function returns 1 if the argument is ±∞ (as defined by IEEE754 standard), 0 otherwise.
The function returns 1 if the argument is Not A Number (as defined by IEEE754 standard), 0
otherwise.
The function fills the destination array with values from the look-up table. Indices of the entries
are taken from the source array. That is, the function processes each element of src as follows:
dsti ← lutsrci +d
where
0 if src has depth CV 8U
d=
128 if src has depth CV 8S
The function calculates the natural logarithm of the absolute value of every element of the input
array:
log |src(I) if src[I] 6= 0
dst[I] =
C otherwise
Where C is a large negative number (about -700 in the current implementation).
1.2. OPERATIONS ON ARRAYS 103
double cvMahalanobis(
const CvArr* vec1,
const CvArr* vec2,
CvArr* mat);
The function calculates and returns the weighted distance between two vectors:
sX
d(vec1, vec2) = icovar(i,j) · (vec1(I) − vec2(I)) · (vec1(j) − vec2(j))
i,j
The covariance matrix may be calculated using the cvCalcCovarMatrix function and further
inverted using the cvInvert function (CV SVD method is the prefered one because the matrix
might be singular).
CvMat cvMat(
int rows,
int cols,
int type,
void* data=NULL);
Initializes a matrix header and assigns data to it. The matrix is filled row-wise (the first cols
elements of data form the first row of the matrix, etc.)
This function is a fast inline substitution for cvInitMatHeader. Namely, it is equivalent to:
CvMat mat;
cvInitMatHeader(&mat, rows, cols, type, data, CV\_AUTOSTEP);
void cvMerge(const CvArr* src0, const CvArr* src1, const CvArr* src2,
const CvArr* src3, CvArr* dst);
The function is the opposite to cvSplit. If the destination array has N channels then if the first
N input channels are not NULL, they all are copied to the destination array; if only a single source
channel of the first N is not NULL, this particular channel is copied into the destination array;
otherwise an error is raised. The rest of the source channels (beyond the first N) must always
be NULL. For IplImage cvCopy with COI set can be also used to insert a single channel into the
image.
106 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
The function finds minimum and maximum element values and their positions. The extremums
are searched across the whole array, selected ROI (in the case of IplImage) or, if mask is not
NULL, in the specified array region. If the array has more than one channel, it must be IplImage
with COI set. In the case of multi-dimensional arrays, minLoc->x and maxLoc->x will contain
raw (linear) positions of the extremums.
Mirror
Synonym for Flip .
MulAddS
Synonym for ScaleAdd .
void cvMul(const CvArr* src1, const CvArr* src2, CvArr* dst, double
scale=1);
1.2. OPERATIONS ON ARRAYS 109
void cvMulSpectrums(
const CvArr* src1,
const CvArr* src2,
CvArr* dst,
int flags);
dst The destination array of the same type and the same size as the source arrays
CV DXT ROWS treats each row of the arrays as a separate spectrum (see cvDFT parameters
description).
CV DXT MUL CONJ conjugate the second source array before the multiplication.
The function performs per-element multiplication of the two CCS-packed or complex matrices
that are results of a real or complex Fourier transform.
The function, together with cvDFT, may be used to calculate convolution of two arrays rapidly.
110 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
arr2 The second source image. If it is NULL, the absolute norm of arr1 is calculated, otherwise
the absolute or relative norm of arr1-arr2 is calculated.
1.2. OPERATIONS ON ARRAYS 111
or the relative difference norm if arr2 is not NULL and (normType & CV RELATIVE) !=
0:
||arr1−arr2||C
||arr2||C if normType = CV RELATIVE C
||arr1−arr2||L1
norm = ||arr2||L1 if normType = CV RELATIVE L1
||arr1−arr2||L2
if normType = CV RELATIVE L2
||arr2||L2
The function returns the calculated norm. A multiple-channel array is treated as a single-
channel, that is, the results for all channels are combined.
void cvOr(const CvArr* src1, const CvArr* src2, CvArr* dst, const
CvArr* mask=NULL);
mask Operation mask, 8-bit single channel array; specifies elements of the destination array to
be changed
void cvOrS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr*
mask=NULL);
mask Operation mask, 8-bit single channel array; specifies elements of the destination array to
be changed
1.2. OPERATIONS ON ARRAYS 113
The function OrS calculates per-element bit-wise disjunction of an array and a scalar:
dst(I)=src(I)|value if mask(I)!=0
Prior to the actual operation, the scalar is converted to the same type as that of the array(s). In
the case of floating-point arrays their bit representations are used for the operation. All the arrays
must have the same type, except the mask, and the same size.
The function transforms every element of src (by treating it as 2D or 3D vector) in the following
way:
where
(x0 , y 0 , z 0 , w0 ) = mat · x y z 1
and
w0 if w0 6= 0
w=
∞ otherwise
void cvPolarToCart(
const CvArr* magnitude,
const CvArr* angle,
CvArr* x,
CvArr* y,
int angleInDegrees=0);
magnitude The array of magnitudes. If it is NULL, the magnitudes are assumed to be all 1’s.
angleInDegrees The flag indicating whether the angles are measured in radians, which is de-
fault mode, or in degrees
The function calculates either the x-coodinate, y-coordinate or both of every vector magnitude(I)*exp(angl
j=sqrt(-1):
x(I)=magnitude(I)*cos(angle(I)),
y(I)=magnitude(I)*sin(angle(I))
void cvPow(
const CvArr* src,
CvArr* dst,
double power);
dst The destination array, should be the same type as the source
1.2. OPERATIONS ON ARRAYS 115
src(I)p
if p is integer
dst[I] =
|src(I)p | otherwise
That is, for a non-integer power exponent the absolute values of input array elements are used.
However, it is possible to get true values for negative values using some extra operations, as the
following example, computing the cube root of array elements, shows:
CvSize size = cvGetSize(src);
CvMat* mask = cvCreateMat([Link], [Link], CV_8UC1);
cvCmpS(src, 0, mask, CV_CMP_LT); /* find negative elements */
cvPow(src, dst, 1./3);
cvSubRS(dst, cvScalarAll(0), dst, mask); /* negate the results of negative inputs */
cvReleaseMat(&mask);
For some values of power, such as integer values, 0.5, and -0.5, specialized faster algorithms
are used.
createNode Optional input parameter for sparse matrices. Non-zero value of the parameter
means that the requested element is created if it does not exist already.
precalcHashval Optional input parameter for sparse matrices. If the pointer is not NULL, the
function does not recalculate the node hash value, but takes it from the specified location. It
is useful for speeding up pair-wise operations (TODO: provide an example)
The functions return a pointer to a specific array element. Number of array dimension should
match to the number of indices passed to the function except for cvPtr1D function that can be
used for sequential access to 1D, 2D or nD dense arrays.
The functions can be used for sparse arrays as well - if the requested node does not exist they
create it and set it to zero.
All these as well as other functions accessing array elements ( cvGet, cvGetReal, cvSet,
cvSetReal) raise an error in case if the element index is out of range.
The function initializes a random number generator and returns the state. The pointer to the
state can be then passed to the cvRandInt, cvRandReal and cvRandArr functions. In the current
implementation a multiply-with-carry generator is used.
void cvRandArr(
CvRNG* rng,
CvArr* arr,
1.2. OPERATIONS ON ARRAYS 117
int distType,
CvScalar param1,
CvScalar param2);
param1 The first parameter of the distribution. In the case of a uniform distribution it is the inclu-
sive lower boundary of the random numbers range. In the case of a normal distribution it is
the mean value of the random numbers.
param2 The second parameter of the distribution. In the case of a uniform distribution it is the
exclusive upper boundary of the random numbers range. In the case of a normal distribution
it is the standard deviation of the random numbers.
The function fills the destination array with uniformly or normally distributed random numbers.
In the example below, the function is used to add a few normally distributed floating-point
numbers to random locations within a 2d array.
/* let noisy_screen be the floating-point 2d array that is to be "crapped" */
CvRNG rng_state = cvRNG(0xffffffff);
int i, pointCount = 1000;
/* allocate the array of coordinates of points */
CvMat* locations = cvCreateMat(pointCount, 1, CV_32SC2);
/* arr of random point values */
CvMat* values = cvCreateMat(pointCount, 1, CV_32FC1);
CvSize size = cvGetSize(noisy_screen);
/* generate values */
cvRandArr(&rng_state, values, CV_RAND_NORMAL,
cvRealScalar(100), // average intensity
cvRealScalar(30) // deviation of the intensity
118 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
);
rng RNG state initialized by RandInit and, optionally, customized by RandSetRange (though,
the latter function does not affect the discussed function outcome)
The function returns a uniformly-distributed random 32-bit unsigned integer and updates the
RNG state. It is similar to the rand() function from the C runtime library, but it always generates a
32-bit number whereas rand() returns a number in between 0 and RAND MAX which is 216 or 232 ,
depending on the platform.
The function is useful for generating scalar random numbers, such as points, patch sizes, table
indices, etc., where integer numbers of a certain range can be generated using a modulo operation
and floating-point numbers can be generated by scaling from 0 to 1 or any other specific range.
Here is the example from the previous function discussion rewritten using cvRandInt:
/* the input and the task is the same as in the previous sample. */
CvRNG rnggstate = cvRNG(0xffffffff);
int i, pointCount = 1000;
/* ... - no arrays are allocated here */
CvSize size = cvGetSize(noisygscreen);
1.2. OPERATIONS ON ARRAYS 119
if(valuesLeft <= 0 )
{
/* fulfill the buffer with normally distributed numbers
if the buffer is empty */
cvRandArr(&rnggstate, &normalValueMat, CV\_RAND\_NORMAL,
cvRealScalar(100), cvRealScalar(30));
valuesLeft = bufferSize;
}
*((float*)cvPtr2D(noisygscreen, pt.y, pt.x, 0 ) =
normalValueBuffer[--valuesLeft];
}
void cvReduce(const CvArr* src, CvArr* dst, int dim = -1, int
op=CV REDUCE SUM);
dst The output single-row/single-column vector that accumulates somehow all the matrix rows/-
columns.
dim The dimension index along which the matrix is reduced. 0 means that the matrix is reduced
to a single row, 1 means that the matrix is reduced to a single column and -1 means that the
dimension is chosen automatically by analysing the dst size.
CV REDUCE SUM The output is the sum of all of the matrix’s rows/columns.
CV REDUCE AVG The output is the mean vector of all of the matrix’s rows/columns.
CV REDUCE MAX The output is the maximum (column/row-wise) of all of the matrix’s rows/-
columns.
CV REDUCE MIN The output is the minimum (column/row-wise) of all of the matrix’s rows/-
columns.
The function reduces matrix to a vector by treating the matrix rows/columns as a set of 1D
vectors and performing the specified operation on the vectors until a single row/column is obtained.
For example, the function can be used to compute horizontal and vertical projections of an raster
image. In the case of CV REDUCE SUM and CV REDUCE AVG the output may have a larger element
bit-depth to preserve accuracy. And multi-channel arrays are also supported in these two reduction
modes.
The function releases the array data. In the case of CvMat or CvMatND it simply calls
cvDecRefData(), that is the function can not deallocate external data. See also the note to cvCre-
ateData.
The function releases the sparse array and clears the array pointer upon exit.
The function fills the destination array with repeated copies of the source array:
dst(i,j)=src(i mod rows(src), j mod cols(src))
So the destination array may be as larger as well as smaller than the source array.
This produces a similar result to the following , but in addition it releases the ROI structure.
cvSetImageROI(image, cvRect(0, 0, image->width, image->height ));
cvSetImageCOI(image, 0);
newCn New number of channels. ’newCn = 0’ means that the number of channels remains un-
changed.
newRows New number of rows. ’newRows = 0’ means that the number of rows remains un-
changed unless it needs to be changed according to newCn value.
The function initializes the CvMat header so that it points to the same data as the original array
but has a different shape - different number of channels, different number of rows, or both.
The following example code creates one image buffer and two image headers, the first is for a
320x240x3 image and the second is for a 960x240x1 image:
IplImage* color_img = cvCreateImage(cvSize(320,240), IPL_DEPTH_8U, 3);
CvMat gray_mat_hdr;
IplImage gray_img_hdr, *gray_img;
cvReshape(color_img, &gray_mat_hdr, 1);
gray_img = cvGetImage(&gray_mat_hdr, &gray_img_hdr);
And the next example converts a 3x3 matrix to a single 1x9 vector:
CvMat* mat = cvCreateMat(3, 3, CV_32F);
CvMat row_header, *row;
row = cvReshape(mat, &row_header, 0, 1);
1.2. OPERATIONS ON ARRAYS 125
sizeofHeader Size of output header to distinguish between IplImage, CvMat and CvMatND
output headers
newCn New number of channels. newCn = 0 means that the number of channels remains un-
changed.
newDims New number of dimensions. newDims = 0 means that the number of dimensions
remains the same.
newSizes Array of new dimension sizes. Only newDims − 1 values are used, because the total
number of elements must remain the same. Thus, if newDims = 1, newSizes array is not
used.
The function is an advanced version of cvReshape that can work with multi-dimensional ar-
rays as well (though it can work with ordinary images and matrices) and change the number of
dimensions.
Below are the two samples from the cvReshape description rewritten using cvReshape-
MatND:
...
126 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
The functions convert the input floating-point number to an integer using one of the rounding
modes. Round returns the nearest integer value to the argument. Floor returns the maximum
integer value that is not larger than the argument. Ceil returns the minimum integer value that
is not smaller than the argument. On some architectures the functions work much faster than the
standard cast operations in C. If the absolute value of the argument is greater than 231 , the result
is not determined. Special values (±∞ , NaN) are not handled.
The function calculates the sum of a scaled array and another array:
mask Operation mask, 8-bit single channel array; specifies elements of the destination array to
be changed
The function copies the scalar value to every selected element of the destination array:
The functions assign the new value to a particular array element. In the case of a sparse array
the functions create the node if it does not exist yet.
The function assigns user data to the array header. Header should be initialized before using
cvCreate*Header, cvInit*Header or cvMat (in the case of matrix) function.
void cvSetImageCOI(
IplImage* image,
int coi);
coi The channel of interest. 0 - all channels are selected, 1 - first channel is selected, etc. Note
that the channel indices become 1-based.
If the ROI is set to NULL and the coi is not 0, the ROI is allocated. Most OpenCV functions do
not support the COI setting, so to process an individual image/matrix channel one may copy (via
cvCopy or cvSplit) the channel to a separate image/matrix, process it and then copy the result
back (via cvCopy or cvMerge) if needed.
void cvSetImageROI(
IplImage* image,
CvRect rect);
If the original image ROI was NULL and the rect is not the whole image, the ROI structure is
allocated.
Most OpenCV functions support the use of ROI and treat the image rectangle as a separate
image. For example, all of the pixel coordinates are counted from the top-left (or bottom-left)
corner of the ROI, not the original image.
The functions assign a new value to a specific element of a single-channel array. If the array
has multiple channels, a runtime error is raised. Note that the cvSet*D function can be used safely
for both single-channel and multiple-channel arrays, though they are a bit slower.
In the case of a sparse array the functions create the node if it does not yet exist.
The function clears the array. In the case of dense arrays (CvMat, CvMatND or IplImage),
cvZero(array) is equivalent to cvSet(array,cvScalarAll(0),0). In the case of sparse arrays all the
elements are removed.
int cvSolve(const CvArr* src1, const CvArr* src2, CvArr* dst, int
method=CV LU);
The function solves a linear system or least-squares problem (the latter is possible with SVD
methods):
roots The output array of real roots which should have 3 elements
void cvSplit(const CvArr* src, CvArr* dst0, CvArr* dst1, CvArr* dst2,
CvArr* dst3);
The function divides a multi-channel array into separate single-channel arrays. Two modes
are available for the operation. If the source array has N channels then if the first N destination
channels are not NULL, they all are extracted from the source array; if only a single destination
channel of the first N is not NULL, this particular channel is extracted; otherwise an error is raised.
The rest of the destination channels (beyond the first N) must always be NULL. For IplImage
cvCopy with COI set can be also used to extract a single channel from the image.
The function calculates the square root of the argument. If the argument is negative, the result
is not determined.
void cvSub(const CvArr* src1, const CvArr* src2, CvArr* dst, const
CvArr* mask=NULL);
mask Operation mask, 8-bit single channel array; specifies elements of the destination array to
be changed
134 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
void cvSubRS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr*
mask=NULL);
mask Operation mask, 8-bit single channel array; specifies elements of the destination array to
be changed
void cvSubS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr*
mask=NULL);
mask Operation mask, 8-bit single channel array; specifies elements of the destination array to
be changed
The function subtracts a scalar from every element of the source array:
dst(I)=src(I)-value if mask(I)!=0
All the arrays must have the same type, except the mask, and the same size (or ROI size). For
types that have limited range this operation is saturating.
The function calculates the sum S of array elements, independently for each channel:
X
arr(I)c
I
If the array is IplImage and COI is set, the function processes the selected channel only and
stores the sum to the first scalar component.
void cvSVBkSb(
const CvArr* W,
const CvArr* U,
const CvArr* V,
136 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
const CvArr* B,
CvArr* X,
int flags);
B The matrix to multiply the pseudo-inverse of the original matrix A by. This is an optional param-
eter. If it is omitted then it is assumed to be an identity matrix of an appropriate size (so that
X will be the reconstructed pseudo-inverse of A).
flags Operation flags, should match exactly to the flags passed to cvSVD
The function calculates back substitution for decomposed matrix A (see cvSVD description)
and matrix B:
X = VW−1 UT B
where
P
−1 1/W(i,i) if W(i,i) > i W(i,i)
W(i,i) =
0 otherwise
void cvSVD(
CvArr* A,
CvArr* W,
CvArr* U=NULL,
CvArr* V=NULL,
int flags=0);
A Source M × N matrix
W Resulting singular value diagonal matrix (M × N or min(M, N) × min(M, N)) or min(M, N) × 1 vector
of the singular values
U Optional left orthogonal matrix, M × min(M, N) (when CV SVD U T is not set), or min(M, N) × M
(when CV SVD U T is set), or M × M (regardless of CV SVD U T flag).
V Optional right orthogonal matrix, N × min(M, N) (when CV SVD V T is not set), or min(M, N) × N
(when CV SVD V T is set), or N × N (regardless of CV SVD V T flag).
CV SVD MODIFY A enables modification of matrix A during the operation. It speeds up the
processing.
CV SVD U T means that the transposed matrix U is returned. Specifying the flag speeds up
the processing.
CV SVD V T means that the transposed matrix V is returned. Specifying the flag speeds up
the processing.
The function decomposes matrix A into the product of a diagonal matrix and two
orthogonal matrices:
A=UW VT
where W is a diagonal matrix of singular values that can be coded as a 1D vector of singular
values and U and V . All the singular values are non-negative and sorted (together with U and V
columns) in descending order.
An SVD algorithm is numerically robust and its typical applications include:
• accurate eigenvalue problem solution when matrix A is a square, symmetric, and positively
defined matrix, for example, when it is a covariance matrix. W in this case will be a vector/-
matrix of the eigenvalues, and U = V will be a matrix of the eigenvectors.
138 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
• least-squares solution of an overdetermined linear system. This and the preceeding is done
by using the cvSolve function with the CV SVD method.
• accurate calculation of different matrix characteristics such as the matrix rank (the number of
non-zero singular values), condition number (ratio of the largest singular value to the smallest
one), and determinant (absolute value of the determinant is equal to the product of singular
values).
The function returns the sum of the diagonal elements of the matrix src1.
X
tr(mat) = mat(i, i)
i
The function performs matrix transformation of every element of array src and stores the
results in dst:
dst(i, j) = src(j, i)
Note that no complex conjugation is done in the case of a complex matrix. Conjugation should
be done separately: look at the sample code in cvXorS for an example.
void cvXor(const CvArr* src1, const CvArr* src2, CvArr* dst, const
CvArr* mask=NULL);
mask Operation mask, 8-bit single channel array; specifies elements of the destination array to
be changed
void cvXorS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr*
mask=NULL);
mask Operation mask, 8-bit single channel array; specifies elements of the destination array to
be changed
The function XorS calculates per-element bit-wise conjunction of an array and a scalar:
dst(I)=src(I)ˆvalue if mask(I)!=0
1.2. OPERATIONS ON ARRAYS 141
Prior to the actual operation, the scalar is converted to the same type as that of the array(s). In
the case of floating-point arrays their bit representations are used for the operation. All the arrays
must have the same type, except the mask, and the same size
The following sample demonstrates how to conjugate complex vector by switching the most-
significant bit of imaging part:
The function is a fast replacement for cvGetReal2D in the case of single-channel floating-point
matrices. It is faster because it is inline, it does fewer checks for array type and array element type,
and it checks for the row and column ranges only in debug mode.
The function is a fast replacement for cvSetReal2D in the case of single-channel floating-point
matrices. It is faster because it is inline, it does fewer checks for array type and array element type,
and it checks for the row and column ranges only in debug mode.
Memory storage is a low-level structure used to store dynamicly growing data structures such
as sequences, contours, graphs, subdivisions, etc. It is organized as a list of memory blocks of
equal size - bottom field is the beginning of the list of blocks and top is the currently used block,
but not necessarily the last block of the list. All blocks between bottom and top, not including
the latter, are considered fully occupied; all blocks between top and the last block, not including
top, are considered free and top itself is partly ocupied - free space contains the number of
free bytes left in the end of top.
A new memory buffer that may be allocated explicitly by cvMemStorageAlloc function or im-
plicitly by higher-level functions, such as cvSeqPush, cvGraphAddEdge, etc., always starts in
the end of the current block if it fits there. After allocation, free space is decremented by the
1.3. DYNAMIC STRUCTURES 143
size of the allocated buffer plus some padding to keep the proper alignment. When the allocated
buffer does not fit into the available portion of top, the next storage block from the list is taken as
top and free space is reset to the whole block size prior to the allocation.
If there are no more free blocks, a new block is allocated (or borrowed from the parent, see
cvCreateChildMemStorage) and added to the end of list. Thus, the storage behaves as a stack
with bottom indicating bottom of the stack and the pair (top, free space) indicating top of the
stack. The stack top may be saved via cvSaveMemStoragePos, restored via cvRestoreMemStor-
agePos, or reset via cvClearStorage.
#define CV_SEQUENCE\_FIELDS() \
int flags; /* micsellaneous flags */ \
144 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
The structure CvSeq is a base for all of OpenCV dynamic data structures.
Such an unusual definition via a helper macro simplifies the extension of the structure CvSeq
with additional parameters. To extend CvSeq the user may define a new structure and put user-
defined fields after all CvSeq fields that are included via the macro CV SEQUENCE FIELDS().
There are two types of sequences - dense and sparse. The base type for dense sequences is
CvSeq and such sequences are used to represent growable 1d arrays - vectors, stacks, queues,
and deques. They have no gaps in the middle - if an element is removed from the middle or
inserted into the middle of the sequence, the elements from the closer end are shifted. Sparse
sequences have CvSet as a base class and they are discussed later in more detail. They are
sequences of nodes; each may be either occupied or free as indicated by the node flag. Such
sequences are used for unordered data structures such as sets of elements, graphs, hash tables
and so forth.
The field header size contains the actual size of the sequence header and should be greater
than or equal to sizeof(CvSeq).
The fields h prev, h next, v prev, v next can be used to create hierarchical structures from
separate sequences. The fields h prev and h next point to the previous and the next sequences
on the same hierarchical level, while the fields v prev and v next point to the previous and the
next sequences in the vertical direction, that is, the parent and its first child. But these are just
names and the pointers can be used in a different way.
The field first points to the first sequence block, whose structure is described below.
The field total contains the actual number of dense sequence elements and number of allo-
cated nodes in a sparse sequence.
1.3. DYNAMIC STRUCTURES 145
The field flags contains the particular dynamic type signature (CV SEQ MAGIC VAL for dense
sequences and CV SET MAGIC VAL for sparse sequences) in the highest 16 bits and miscella-
neous information about the sequence. The lowest CV SEQ ELTYPE BITS bits contain the ID of
the element type. Most of sequence processing functions do not use element type but rather el-
ement size stored in elem size. If a sequence contains the numeric data for one of the CvMat
type then the element type matches to the corresponding CvMat element type, e.g., CV 32SC2
may be used for a sequence of 2D points, CV 32FC1 for sequences of floating-point values, etc. A
CV SEQ ELTYPE(seq header ptr) macro retrieves the type of sequence elements. Processing
functions that work with numerical sequences check that elem size is equal to that calculated
from the type element size. Besides CvMat compatible types, there are few extra element types
defined in the cvtypes.h header:
Standard Types of Sequence Elements
Some of functions that operate on sequences take a CvSlice slice parameter that is of-
ten set to the whole sequence (CV WHOLE SEQ) by default. Either of the startIndex and
endIndex may be negative or exceed the sequence length, startIndex is inclusive, and endIndex
is an exclusive boundary. If they are equal, the slice is considered empty (i.e., contains no ele-
ments). Because sequences are treated as circular structures, the slice may select a few elements
in the end of a sequence followed by a few elements at the beginning of the sequence. For ex-
ample, cvSlice(-2, 3) in the case of a 10-element sequence will select a 5-element slice,
containing the pre-last (8th), last (9th), the very first (0th), second (1th) and third (2nd) elements.
The functions normalize the slice argument in the following way: first, cvSliceLength is called to
determine the length of the slice, then, startIndex of the slice is normalized similarly to the ar-
gument of cvGetSeqElem (i.e., negative indices are allowed). The actual slice to process starts at
the normalized startIndex and lasts cvSliceLength elements (again, assuming the sequence
is a circular structure).
If a function does not accept a slice argument, but you want to process only a part of the
sequence, the sub-sequence may be extracted using the cvSeqSlice function, or stored into a
continuous buffer with CvtSeqToArray (optionally, followed by cvMakeSeqHeaderForArray).
#define CV_SET_FIELDS() \
CV_SEQUENCE_FIELDS() /* inherits from [#CvSeq CvSeq] */ \
struct CvSetElem* free_elems; /* list of free nodes */
CV_SET_FIELDS()
} CvSet;
#define CV_GRAPH_EDGE_FIELDS() \
int flags; /* edge flags */ \
1.3. DYNAMIC STRUCTURES 149
#define CV_GRAPH_FIELDS() \
CV_SET_FIELDS() /* set of vertices */ \
CvSet* edges; /* set of edges */
The structure CvGraphScanner is used for depth-first graph traversal. See discussion of the
functions below.
#define CV_TREE_NODE_FIELDS(node_type) \
int flags; /* micsellaneous flags */ \
int header_size; /* size of sequence header */ \
struct node_type* h_prev; /* previous sequence */ \
struct node_type* h_next; /* next sequence */ \
struct node_type* v_prev; /* 2nd previous sequence */ \
struct node_type* v_next; /* 2nd next sequence */
1.3. DYNAMIC STRUCTURES 151
The structure CvTreeNodeIterator is used to traverse trees. Each tree node should start with
the certain fields which are defined by CV TREE NODE FIELDS(...) macro. In C++ terms, each
tree node should be a structure ”derived” from
struct _BaseTreeNode
{
CV_TREE_NODE_FIELDS(_BaseTreeNode);
}
CvSeq, CvSet, CvGraph and other dynamic structures derived from CvSeq comply with the
requirement.
graph Graph
The function removes all vertices and edges from a graph. The function has O(1) time com-
plexity.
The function resets the top (free space boundary) of the storage to the very beginning. This
function does not deallocate any memory. If the storage has a parent, the function returns all
blocks to the parent.
152 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
seq Sequence
The function removes all elements from a sequence. The function does not return the mem-
ory to the storage block, but this memory is reused later when new elements are added to the
sequence. The function has ’O(1)’ time complexity.
The function removes all elements from set. It has O(1) time complexity.
CvGraph* cvCloneGraph(
const CvGraph* graph,
CvMemStorage* storage );
The function creates a full copy of the specified graph. If the graph vertices or edges have
pointers to some external data, it can still be shared between the copies. The vertex and edge
indices in the new graph may be different from the original because the function defragments the
vertex and edge sets.
CvSeq* cvCloneSeq(
const CvSeq* seq,
CvMemStorage* storage=NULL );
seq Sequence
storage The destination storage block to hold the new sequence header and the copied data, if
any. If it is NULL, the function uses the storage block containing the input sequence.
The function makes a complete copy of the input sequence and returns it.
The call
cvCloneSeq( seq, storage )
is equivalent to
cvSeqSlice( seq, CV_WHOLE_SEQ, storage, 1 )
The function creates a child memory storage that is similar to simple memory storage except
for the differences in the memory allocation/deallocation mechanism. When a child storage needs
a new block to add to the block list, it tries to get this block from the parent. The first unoccupied
parent block available is taken and excluded from the parent block list. If no blocks are available,
the parent either allocates a block or borrows one from its own parent, if any. In other words, the
chain, or a more complex structure, of memory storages where every storage is a child/parent of
another is possible. When a child storage is released or even cleared, it returns all blocks to the
parent. In other aspects, child storage is the same as simple storage.
Child storage is useful in the following situation. Imagine that the user needs to process dy-
namic data residing in a given storage area and put the result back to that same storage area.
With the simplest approach, when temporary data is resided in the same storage area as the input
and output data, the storage area will look as follows after processing:
That is, garbage appears in the middle of the storage. However, if one creates a child memory
storage at the beginning of processing, writes temporary data there, and releases the child storage
at the end, no garbage will appear in the source/destination storage:
CvGraph* cvCreateGraph(
int graph flags,
int header size,
int vtx size,
int edge size,
CvMemStorage* storage );
graph flags Type of the created graph. Usually, it is either CV SEQ KIND GRAPH for generic
unoriented graphs and CV SEQ KIND GRAPH | CV GRAPH FLAG ORIENTED for generic ori-
ented graphs.
header size Graph header size; may not be less than sizeof(CvGraph)
vtx size Graph vertex size; the custom vertex structure must start with CvGraphVtx (use
CV GRAPH VERTEX FIELDS())
edge size Graph edge size; the custom edge structure must start with CvGraphEdge (use
CV GRAPH EDGE FIELDS())
CvGraphScanner* cvCreateGraphScanner(
CvGraph* graph,
CvGraphVtx* vtx=NULL,
int mask=CV GRAPH ALL ITEMS );
156 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
graph Graph
vtx Initial vertex to start from. If NULL, the traversal starts from the first vertex (a vertex with the
minimal index in the sequence of vertices).
mask Event mask indicating which events are of interest to the user (where cvNextGraphItem
function returns control to the user) It can be CV GRAPH ALL ITEMS (all events are of inter-
est) or a combination of the following flags:
CV GRAPH VERTEX stop at the graph vertices visited for the first time
CV GRAPH TREE EDGE stop at tree edges (tree edge is the edge connecting the last vis-
ited vertex and the vertex to be visited next)
CV GRAPH BACK EDGE stop at back edges (back edge is an edge connecting the last vis-
ited vertex with some of its ancestors in the search tree)
CV GRAPH FORWARD EDGE stop at forward edges (forward edge is an edge conecting the
last visited vertex with some of its descendants in the search tree. The forward edges
are only possible during oriented graph traversal)
CV GRAPH CROSS EDGE stop at cross edges (cross edge is an edge connecting different
search trees or branches of the same tree. The cross edges are only possible during
oriented graph traversal)
CV GRAPH ANY EDGE stop at any edge (tree, back, forward, and cross edges)
CV GRAPH NEW TREE stop in the beginning of every new search tree. When the traversal
procedure visits all vertices and edges reachable from the initial vertex (the visited
vertices together with tree edges make up a tree), it searches for some unvisited vertex
in the graph and resumes the traversal process from that vertex. Before starting a new
tree (including the very first tree when cvNextGraphItem is called for the first time)
it generates a CV GRAPH NEW TREE event. For unoriented graphs, each search tree
corresponds to a connected component of the graph.
CV GRAPH BACKTRACKING stop at every already visited vertex during backtracking - return-
ing to already visited vertexes of the traversal tree.
The function creates a structure for depth-first graph traversal/search. The initialized structure
is used in the cvNextGraphItem function - the incremental traversal procedure.
blockSize Size of the storage blocks in bytes. If it is 0, the block size is set to a default value -
currently it is about 64K.
CvSeq* cvCreateSeq(
int seqFlags,
int headerSize,
int elemSize,
CvMemStorage* storage);
seqFlags Flags of the created sequence. If the sequence is not passed to any function work-
ing with a specific type of sequences, the sequence value may be set to 0, otherwise the
appropriate type must be selected from the list of predefined sequence types.
headerSize Size of the sequence header; must be greater than or equal to sizeof(CvSeq).
If a specific type or its extension is indicated, this type must fit the base type header.
elemSize Size of the sequence elements in bytes. The size must be consistent with the se-
quence type. For example, for a sequence of points to be created, the element type
CV SEQ ELTYPE POINT should be specified and the parameter elemSize must be equal to
sizeof(CvPoint).
The function creates a sequence and returns the pointer to it. The function allocates the se-
quence header in the storage block as one continuous chunk and sets the structure fields flags,
elemSize, headerSize, and storage to passed values, sets delta elems to the default value
(that may be reassigned using the cvSetSeqBlockSize function), and clears other header fields,
including the space following the first sizeof(CvSeq) bytes.
158 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
CvSet* cvCreateSet(
int set flags,
int header size,
int elem size,
CvMemStorage* storage );
void* cvCvtSeqToArray(
const CvSeq* seq,
void* elements,
CvSlice slice=CV WHOLE SEQ );
seq Sequence
elements Pointer to the destination array that must be large enough. It should be a pointer to
data, not a matrix header.
slice The sequence portion to copy to the array
The function copies the entire sequence or subsequence to the specified buffer and returns
the pointer to the buffer.
1.3. DYNAMIC STRUCTURES 159
The function finishes the writing process and returns the pointer to the written sequence. The
function also truncates the last incomplete sequence block to return the remaining part of the
block to memory storage. After that, the sequence can be read and modified safely. See cvcvS-
tartWriteSeq and cvcvStartAppendToSeq
graph Graph
end idx Index of the ending vertex of the edge. For an unoriented graph, the order of the vertex
parameters does not matter.
The function finds the graph edge connecting two specified vertices and returns a pointer to it
or NULL if the edge does not exist.
160 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
CvGraphEdge* cvFindGraphEdgeByPtr(
const CvGraph* graph,
const CvGraphVtx* startVtx,
const CvGraphVtx* endVtx );
graph Graph
endVtx Pointer to the ending vertex of the edge. For an unoriented graph, the order of the vertex
parameters does not matter.
The function finds the graph edge connecting two specified vertices and returns pointer to it or
NULL if the edge does not exists.
The function is intended to enable the user to read sequence elements, whenever required,
during the writing process, e.g., in order to check specific conditions. The function updates the
sequence headers to make reading from the sequence possible. The writer is not closed, however,
so that the writing process can be continued at any time. If an algorithm requires frequent flushes,
consider using cvSeqPush instead.
1.3. DYNAMIC STRUCTURES 161
CvGraphVtx* cvGetGraphVtx(
CvGraph* graph,
int vtx idx );
graph Graph
vtx idx Index of the vertex
The function finds the graph vertex by using its index and returns the pointer to it or NULL if
the vertex does not belong to the graph.
seq Sequence
index Index of element
The function finds the element with the given index in the sequence and returns the pointer to
it. If the element is not found, the function returns 0. The function supports negative indices, where
-1 stands for the last sequence element, -2 stands for the one before last, etc. If the sequence
is most likely to consist of a single sequence block or the desired element is likely to be located
in the first block, then the macro CV GET SEQ ELEM( elemType, seq, index ) should be
used, where the parameter elemType is the type of sequence elements ( CvPoint for example),
the parameter seq is a sequence, and the parameter index is the index of the desired element.
The macro checks first whether the desired element belongs to the first block of the sequence and
returns it if it does; otherwise the macro calls the main function GetSeqElem. Negative indices
always cause the cvGetSeqElem call. The function has O(1) time complexity assuming that the
number of blocks is much smaller than the number of elements.
162 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
The function returns the current reader position (within 0 ... reader->seq->total - 1).
CvSetElem* cvGetSetElem(
const CvSet* setHeader,
int index );
setHeader Set
The function finds a set element by its index. The function returns the pointer to it or 0 if the
index is invalid or the corresponding node is free. The function supports negative indices as it
uses cvGetSeqElem to locate the node.
int cvGraphAddEdge(
CvGraph* graph,
int start idx,
1.3. DYNAMIC STRUCTURES 163
graph Graph
start idx Index of the starting vertex of the edge
end idx Index of the ending vertex of the edge. For an unoriented graph, the order of the vertex
parameters does not matter.
edge Optional input parameter, initialization data for the edge
inserted edge Optional output parameter to contain the address of the inserted edge
The function connects two specified vertices. The function returns 1 if the edge has been
added successfully, 0 if the edge connecting the two vertices exists already and -1 if either of the
vertices was not found, the starting and the ending vertex are the same, or there is some other
critical situation. In the latter case (i.e., when the result is negative), the function also reports an
error by default.
int cvGraphAddEdgeByPtr(
CvGraph* graph,
CvGraphVtx* start vtx,
CvGraphVtx* end vtx,
const CvGraphEdge* edge=NULL,
CvGraphEdge** inserted edge=NULL );
graph Graph
start vtx Pointer to the starting vertex of the edge
end vtx Pointer to the ending vertex of the edge. For an unoriented graph, the order of the vertex
parameters does not matter.
164 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
int cvGraphAddVtx(
CvGraph* graph,
const CvGraphVtx* vtx=NULL,
CvGraphVtx** inserted vtx=NULL );
graph Graph
vtx Optional input argument used to initialize the added vertex (only user-defined fields beyond
sizeof(CvGraphVtx) are copied)
inserted vertex Optional output argument. If not NULL, the address of the new vertex is
written here.
The function adds a vertex to the graph and returns the vertex index.
int cvGraphEdgeIdx(
CvGraph* graph,
CvGraphEdge* edge );
1.3. DYNAMIC STRUCTURES 165
graph Graph
void cvGraphRemoveEdge(
CvGraph* graph,
int start idx,
int end idx );
graph Graph
end idx Index of the ending vertex of the edge. For an unoriented graph, the order of the vertex
parameters does not matter.
The function removes the edge connecting two specified vertices. If the vertices are not con-
nected [in that order], the function does nothing.
void cvGraphRemoveEdgeByPtr(
CvGraph* graph,
CvGraphVtx* start vtx,
CvGraphVtx* end vtx );
graph Graph
166 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
end vtx Pointer to the ending vertex of the edge. For an unoriented graph, the order of the vertex
parameters does not matter.
The function removes the edge connecting two specified vertices. If the vertices are not con-
nected [in that order], the function does nothing.
int cvGraphRemoveVtx(
CvGraph* graph,
int index );
graph Graph
The function removes a vertex from a graph together with all the edges incident to it. The
function reports an error if the input vertex does not belong to the graph. The return value is the
number of edges deleted, or -1 if the vertex does not belong to the graph.
int cvGraphRemoveVtxByPtr(
CvGraph* graph,
CvGraphVtx* vtx );
graph Graph
The function removes a vertex from the graph by using its pointer together with all the edges
incident to it. The function reports an error if the vertex does not belong to the graph. The return
value is the number of edges deleted, or -1 if the vertex does not belong to the graph.
graph Graph
The function returns the number of edges incident to the specified vertex, both incoming and
outgoing. To count the edges, the following code is used:
CvGraphEdge* edge = vertex->first; int count = 0;
while( edge )
{
edge = CV_NEXT_GRAPH_EDGE( edge, vertex );
count++;
}
The macro CV NEXT GRAPH EDGE( edge, vertex ) returns the edge incident to vertex
that follows after edge.
int cvGraphVtxDegreeByPtr(
const CvGraph* graph,
const CvGraphVtx* vtx );
graph Graph
168 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
int cvGraphVtxIdx(
CvGraph* graph,
CvGraphVtx* vtx );
graph Graph
vtx Pointer to the graph vertex
The function returns the index of a graph vertex.
void cvInitTreeNodeIterator(
CvTreeNodeIterator* tree iterator,
const void* first,
int max level );
void cvInsertNodeIntoTree(
void* node,
void* parent,
void* frame );
frame The top level node. If parent and frame are the same, the v prev field of node is set
to NULL rather than parent.
The function adds another node into tree. The function does not allocate any memory, it can
only modify links of the tree nodes.
CvSeq* cvMakeSeqHeaderForArray(
int seq type,
int header size,
int elem size,
void* elements,
int total,
CvSeq* seq,
CvSeqBlock* block );
header size Size of the header of the sequence. Parameter sequence must point to the struc-
ture of that size or greater
170 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
total Total number of elements in the sequence. The number of array elements must be equal
to the value of this parameter.
seq Pointer to the local variable that is used as the sequence header
block Pointer to the local variable that is the header of the single sequence block
The function initializes a sequence header for an array. The sequence header as well as the
sequence block are allocated by the user (for example, on stack). No data is copied by the function.
The resultant sequence will consists of a single block and have NULL storage pointer; thus, it is
possible to read its elements, but the attempts to add elements to the sequence will raise an error
in most cases.
void* cvMemStorageAlloc(
CvMemStorage* storage,
size t size );
The function allocates a memory buffer in a storage block. The buffer size must not exceed
the storage block size, otherwise a runtime error is raised. The buffer address is aligned by
CV STRUCT ALIGN=sizeof(double) (for the moment) bytes.
len Length of the string (not counting the ending NUL) . If the parameter is negative, the function
computes the length.
The function creates copy of the string in memory storage. It returns the structure that contains
user-passed or computed length of the string and pointer to the copied string.
The function traverses through the graph until an event of interest to the user (that is, an event,
specified in the mask in the cvCreateGraphScanner call) is met or the traversal is completed. In
the first case, it returns one of the events listed in the description of the mask parameter above
and with the next call it resumes the traversal. In the latter case, it returns CV GRAPH OVER (-1).
When the event is CV GRAPH VERTEX, CV GRAPH BACKTRACKING, or CV GRAPH NEW TREE, the
currently observed vertex is stored in scanner->vtx. And if the event is edge-related, the edge
itself is stored at scanner->edge, the previously visited vertex - at scanner->vtx and the
other ending vertex of the edge - at scanner->dst.
172 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
The function returns the currently observed node and then updates the iterator - moving it
toward the next node. In other words, the function behavior is similar to the *p++ expression on a
typical C pointer or C++ collection iterator. The function returns NULL if there are no more nodes.
The function returns the currently observed node and then updates the iterator - moving it
toward the previous node. In other words, the function behavior is similar to the *p-- expression
on a typical C pointer or C++ collection iterator. The function returns NULL if there are no more
nodes.
The function completes the graph traversal procedure and releases the traverser state.
1.3. DYNAMIC STRUCTURES 173
The function deallocates all storage memory blocks or returns them to the parent, if any. Then
it deallocates the storage header and clears the pointer to the storage. All child storage associated
with a given parent storage block must be released before the parent storage block is released.
void cvRestoreMemStoragePos(
CvMemStorage* storage,
CvMemStoragePos* pos);
The function restores the position of the storage top from the parameter pos. This function and
the function cvClearMemStorage are the only methods to release memory occupied in memory
blocks. Note again that there is no way to free memory in the middle of an occupied portion of a
storage block.
void cvSaveMemStoragePos(
const CvMemStorage* storage,
CvMemStoragePos* pos);
The function saves the current position of the storage top to the parameter pos. The function
cvRestoreMemStoragePos can further retrieve this position.
int cvSeqElemIdx(
const CvSeq* seq,
const void* element,
CvSeqBlock** block=NULL );
seq Sequence
block Optional argument. If the pointer is not NULL, the address of the sequence block that
contains the element is stored in this location.
The function returns the index of a sequence element or a negative number if the element is
not found.
char* cvSeqInsert(
CvSeq* seq,
int beforeIndex,
void* element=NULL );
seq Sequence
beforeIndex Index before which the element is inserted. Inserting before 0 (the minimal allowed
value of the parameter) is equal to cvSeqPushFront and inserting before seq->total (the
maximal allowed value of the parameter) is equal to cvSeqPush.
The function shifts the sequence elements from the inserted position to the nearest end of the
sequence and copies the element content there if the pointer is not NULL. The function returns
a pointer to the inserted element.
void cvSeqInsertSlice(
CvSeq* seq,
int beforeIndex,
const CvArr* fromArr );
seq Sequence
The function inserts all fromArr array elements at the specified position of the sequence. The
array fromArr can be a matrix or another sequence.
176 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
seq Sequence
The function reverses the sequence in-place - makes the first element go last, the last element
go first and so forth.
void cvSeqPop(
CvSeq* seq,
void* element=NULL );
seq Sequence
element Optional parameter . If the pointer is not zero, the function copies the removed element
to this location.
The function removes an element from a sequence. The function reports an error if the se-
quence is already empty. The function has O(1) complexity.
void cvSeqPopFront(
CvSeq* seq,
void* element=NULL );
seq Sequence
element Optional parameter. If the pointer is not zero, the function copies the removed element
to this location.
The function removes an element from the beginning of a sequence. The function reports an
error if the sequence is already empty. The function has O(1) complexity.
void cvSeqPopMulti(
CvSeq* seq,
void* elements,
int count,
int in front=0 );
seq Sequence
elements Removed elements
count Number of elements to pop
in front The flags specifying which end of the modified sequence.
CV BACK the elements are added to the end of the sequence
CV FRONT the elements are added to the beginning of the sequence
The function removes several elements from either end of the sequence. If the number of
the elements to be removed exceeds the total number of elements in the sequence, the function
removes as many elements as possible.
178 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
char* cvSeqPush(
CvSeq* seq,
void* element=NULL );
seq Sequence
The function adds an element to the end of a sequence and returns a pointer to the allo-
cated element. If the input element is NULL, the function simply allocates a space for one more
element.
The following code demonstrates how to create a new sequence using this function:
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* seq = cvCreateSeq( CV_32SC1, /* sequence of integer elements */
sizeof(CvSeq), /* header size - no extra fields */
sizeof(int), /* element size */
storage /* the container storage */ );
int i;
for( i = 0; i < 100; i++ )
{
int* added = (int*)cvSeqPush( seq, &i );
printf( "%d is added\n", *added );
}
...
/* release memory storage in the end */
cvReleaseMemStorage( &storage );
The function has O(1) complexity, but there is a faster method for writing large sequences (see
cvStartWriteSeq and related functions).
seq Sequence
The function is similar to cvSeqPush but it adds the new element to the beginning of the
sequence. The function has O(1) complexity.
void cvSeqPushMulti(
CvSeq* seq,
void* elements,
int count,
int in front=0 );
seq Sequence
The function adds several elements to either end of a sequence. The elements are added
to the sequence in the same order as they are arranged in the input array but they can fall into
different sequence blocks.
180 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
void cvSeqRemove(
CvSeq* seq,
int index );
seq Sequence
The function removes elements with the given index. If the index is out of range the function
reports an error. An attempt to remove an element from an empty sequence is a special case of
this situation. The function removes an element by shifting the sequence elements between the
nearest end of the sequence and the index-th position, not counting the latter.
seq Sequence
char* cvSeqSearch( CvSeq* seq, const void* elem, CvCmpFunc func, int
is sorted, int* elem idx, void* userdata=NULL );
func The comparison function that returns negative, zero or positive value depending on the
relationships among the elements (see also cvSeqSort)
userdata The user parameter passed to the compasion function; helps to avoid global variables
in some cases
/* a < b ? -1 : a > b ? 1 : 0 */
typedef int (CV_CDECL* CvCmpFunc)(const void* a, const void* b, void* userdata);
The function searches for the element in the sequence. If the sequence is sorted, a binary
O(log(N)) search is used; otherwise, a simple linear search is used. If the element is not found,
the function returns a NULL pointer and the index is set to the number of sequence elements if a
linear search is used, or to the smallest index i, seq(i)>elem.
CvSeq* cvSeqSlice(
const CvSeq* seq,
CvSlice slice,
CvMemStorage* storage=NULL,
int copy data=0 );
seq Sequence
182 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
storage The destination storage block to hold the new sequence header and the copied data, if
any. If it is NULL, the function uses the storage block containing the input sequence.
copy data The flag that indicates whether to copy the elements of the extracted slice (copy data!=0)
or not (copy data=0)
The function creates a sequence that represents the specified slice of the input sequence. The
new sequence either shares the elements with the original sequence or has its own copy of the
elements. So if one needs to process a part of sequence but the processing function does not
have a slice parameter, the required sub-sequence may be extracted using this function.
/* a < b ? -1 : a > b ? 1 : 0 */
typedef int (CV_CDECL* CvCmpFunc)(const void* a, const void* b, void* userdata);
func The comparison function that returns a negative, zero, or positive value depending on the
relationships among the elements (see the above declaration and the example below) - a
similar function is used by qsort from C runline except that in the latter, userdata is not
used
userdata The user parameter passed to the compasion function; helps to avoid global variables
in some cases
The function sorts the sequence in-place using the specified criteria. Below is an example of
using this function:
/* Sort 2d points in top-to-bottom left-to-right order */
static int cmp_func( const void* _a, const void* _b, void* userdata )
{
CvPoint* a = (CvPoint*)_a;
1.3. DYNAMIC STRUCTURES 183
CvPoint* b = (CvPoint*)_b;
int y_diff = a->y - b->y;
int x_diff = a->x - b->x;
return y_diff ? y_diff : x_diff;
}
...
cvReleaseMemStorage( &storage );
int cvSetAdd(
CvSet* setHeader,
CvSetElem* elem=NULL,
CvSetElem** inserted elem=NULL );
184 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
setHeader Set
elem Optional input argument, an inserted element. If not NULL, the function copies the data to
the allocated node (the MSB of the first integer field is cleared after copying).
inserted elem Optional output argument; the pointer to the allocated cell
The function allocates a new node, optionally copies input element data to it, and returns the
pointer and the index to the node. The index value is taken from the lower bits of the flags field
of the node. The function has O(1) complexity; however, there exists a faster function for allocating
set nodes (see cvSetNew).
setHeader Set
The function is an inline lightweight variant of cvSetAdd. It occupies a new node and returns
a pointer to it rather than an index.
void cvSetRemove(
CvSet* setHeader,
int index );
setHeader Set
index Index of the removed element
The function removes an element with a specified index from the set. If the node at the spec-
ified location is not occupied, the function does nothing. The function has O(1) complexity; how-
ever, cvSetRemoveByPtr provides a quicker way to remove a set element if it is located already.
1.3. DYNAMIC STRUCTURES 185
void cvSetRemoveByPtr(
CvSet* setHeader,
void* elem );
setHeader Set
The function is an inline lightweight variant of cvSetRemove that requires an element pointer.
The function does not check whether the node is occupied or not - the user should take care of
that.
void cvSetSeqBlockSize(
CvSeq* seq,
int deltaElems );
seq Sequence
The function affects memory allocation granularity. When the free space in the sequence
buffers has run out, the function allocates the space for deltaElems sequence elements. If this
block immediately follows the one previously allocated, the two blocks are concatenated; other-
wise, a new sequence block is created. Therefore, the bigger the parameter is, the lower the
possible sequence fragmentation, but the more space in the storage block is wasted. When the
sequence is created, the parameter deltaElems is set to the default value of about 1K. The
function can be called any time after the sequence is created and affects future allocations. The
function can modify the passed value of the parameter to meet memory storage constraints.
186 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
void cvSetSeqReaderPos(
CvSeqReader* reader,
int index,
int is relative=0 );
index The destination position. If the positioning mode is used (see the next parameter), the
actual position will be index mod reader->seq->total.
The function moves the read position to an absolute position or relative to the current position.
void cvStartAppendToSeq(
CvSeq* seq,
CvSeqWriter* writer );
The function initializes the process of writing data to a sequence. Written elements are added
to the end of the sequence by using the CV WRITE SEQ ELEM( written elem, writer )
macro. Note that during the writing process, other operations on the sequence may yield an
incorrect result or even corrupt the sequence (see description of cvFlushSeqWriter, which helps
to avoid some of these problems).
1.3. DYNAMIC STRUCTURES 187
void cvStartReadSeq(
const CvSeq* seq,
CvSeqReader* reader,
int reverse=0 );
seq Sequence
reverse Determines the direction of the sequence traversal. If reverse is 0, the reader is
positioned at the first sequence element; otherwise it is positioned at the last element.
The function initializes the reader state. After that, all the sequence elements from the first
one down to the last one can be read by subsequent calls of the macro CV READ SEQ ELEM(
read elem, reader ) in the case of forward reading and by using CV REV READ SEQ ELEM(
read elem, reader ) in the case of reverse reading. Both macros put the sequence element
to read elem and move the reading pointer toward the next element. A circular structure of
sequence blocks is used for the reading process, that is, after the last element has been read
by the macro CV READ SEQ ELEM, the first element is read when the macro is called again. The
same applies to CV REV READ SEQ ELEM. There is no function to finish the reading process, since
it neither changes the sequence nor creates any temporary buffers. The reader field ptr points to
the current element of the sequence that is to be read next. The code below demonstrates how to
use the sequence writer and reader.
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* seq = cvCreateSeq( CV_32SC1, sizeof(CvSeq), sizeof(int), storage );
CvSeqWriter writer;
CvSeqReader reader;
int i;
}
cvEndWriteSeq( &writer );
cvReleaseStorage( &storage );
void cvStartWriteSeq(
int seq flags,
int header size,
int elem size,
CvMemStorage* storage,
CvSeqWriter* writer );
seq flags Flags of the created sequence. If the sequence is not passed to any function working
with a specific type of sequences, the sequence value may be equal to 0; otherwise the
appropriate type must be selected from the list of predefined sequence types.
header size Size of the sequence header. The parameter value may not be less than sizeof(CvSeq).
If a certain type or extension is specified, it must fit within the base type header.
1.4. DRAWING FUNCTIONS 189
elem size Size of the sequence elements in bytes; must be consistent with the sequence type.
For example, if a sequence of points is created (element type CV SEQ ELTYPE POINT ),
then the parameter elem size must be equal to sizeof(CvPoint).
CvSeq* cvTreeToNodeSeq(
const void* first,
int header size,
CvMemStorage* storage );
header size Header size of the created sequence (sizeof(CvSeq) is the most frequently used
value)
The function puts pointers of all nodes reacheable from first into a single sequence. The
pointers are written sequentially in the depth-first order.
order channel is normally Blue, Green, Red, this is what cv, cv and cv expect , so if you form a
color using cv, it should look like:
If you are using your own image rendering and I/O functions, you can use any channel ordering,
the drawing functions process each channel independently and do not depend on the channel
order or even on the color space used. The whole image can be converted from BGR to RGB or
to a different color space using cv.
If a drawn figure is partially or completely outside the image, the drawing functions clip it.
Also, many drawing functions can handle pixel coordinates specified with sub-pixel accuracy, that
is, the coordinates can be passed as fixed-point numbers, encoded as integers. The number of
fractional bits is specified by the shift parameter and the real point coordinates are calculated as
Point(x, y) → Point2f(x∗2−shif t , y∗2−shif t ). This feature is especially effective wehn rendering
antialiased shapes.
Also, note that the functions do not support alpha-transparency - when the target image is
4-channnel, then the color[3] is simply copied to the repainted pixels. Thus, if you want to paint
semi-transparent shapes, you can paint them in a separate buffer and then blend it with the main
image.
void cvCircle(
CvArr* img,
CvPoint center,
int radius,
CvScalar color,
int thickness=1,
int lineType=8,
int shift=0 );
thickness Thickness of the circle outline if positive, otherwise this indicates that a filled circle is
to be drawn
shift Number of fractional bits in the center coordinates and radius value
The function draws a simple or filled circle with a given center and radius.
int cvClipLine(
CvSize imgSize,
CvPoint* pt1,
CvPoint* pt2 );
pt1 First ending point of the line segment. It is modified by the function.
pt2 Second ending point of the line segment. It is modified by the function.
The function calculates a part of the line segment which is entirely within the image. It returns
0 if the line segment is completely outside the image and 1 otherwise.
void cvDrawContours(
CvArr *img,
CvSeq* contour,
CvScalar external color,
192 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
img Image where the contours are to be drawn. As with any other drawing function, the contours
are clipped with the ROI.
max level Maximal level for drawn contours. If 0, only contour is drawn. If 1, the contour
and all contours following it on the same level are drawn. If 2, all contours following and all
contours one level below the contours are drawn, and so forth. If the value is negative, the
function does not draw the contours following after contour but draws the child contours of
contour up to the |max level| − 1 level.
thickness Thickness of lines the contours are drawn with. If it is negative (For example, =CV FILLED),
the contour interiors are drawn.
The function draws contour outlines in the image if thickness ≥ 0 or fills the area bounded
by the contours if thickness < 0.
Example: Connected component detection via contour functions
#include "cv.h"
#include "highgui.h"
cvNamedWindow( "Components", 1 );
cvShowImage( "Components", dst );
cvWaitKey(0);
}
}
void cvEllipse(
CvArr* img,
CvPoint center,
CvSize axes,
double angle,
double start angle,
double end angle,
CvScalar color,
int thickness=1,
int lineType=8,
int shift=0 );
thickness Thickness of the ellipse arc outline if positive, otherwise this indicates that a filled
ellipse sector is to be drawn
shift Number of fractional bits in the center coordinates and axes’ values
The function draws a simple or thick elliptic arc or fills an ellipse sector. The arc is clipped by
the ROI rectangle. A piecewise-linear approximation is used for antialiased arcs and thick arcs.
All the angles are given in degrees. The picture below explains the meaning of the parameters.
Parameters of Elliptic Arc
void cvEllipseBox(
CvArr* img,
CvBox2D box,
CvScalar color,
int thickness=1,
int lineType=8,
int shift=0 );
img Image
The function draws a simple or thick ellipse outline, or fills an ellipse. The functions provides
a convenient way to draw an ellipse approximating some shape; that is what CamShift and
FitEllipse do. The ellipse drawn is clipped by ROI rectangle. A piecewise-linear approximation is
used for antialiased arcs and thick arcs.
void cvFillConvexPoly(
CvArr* img,
CvPoint* pts,
int npts,
CvScalar color,
int lineType=8,
int shift=0 );
img Image
196 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
void cvFillPoly(
CvArr* img,
CvPoint** pts,
int* npts,
int contours,
CvScalar color,
int lineType=8,
int shift=0 );
img Image
pts Array of pointers to polygons
npts Array of polygon vertex counters
contours Number of contours that bind the filled region
color Polygon color
lineType Type of the polygon boundaries, see Line description
shift Number of fractional bits in the vertex coordinates
The function fills an area bounded by several polygonal contours. The function fills complex
areas, for example, areas with holes, contour self-intersection, and so forth.
1.4. DRAWING FUNCTIONS 197
void cvGetTextSize(
const char* textString,
const CvFont* font,
CvSize* textSize,
int* baseline );
textSize Resultant size of the text string. Height of the text does not include the height of
character parts that are below the baseline.
The function calculates the dimensions of a rectangle to enclose a text string when a specified
font is used.
void cvInitFont(
CvFont* font,
int fontFace,
double hscale,
double vscale,
double shear=0,
int thickness=1,
int lineType=8 );
The parameter can be composited from one of the values above and an optional CV FONT ITALIC
flag, which indicates italic or oblique font.
hscale Horizontal scale. If equal to 1.0f, the characters have the original width depending on
the font type. If equal to 0.5f, the characters are of half the original width.
vscale Vertical scale. If equal to 1.0f, the characters have the original height depending on the
font type. If equal to 0.5f, the characters are of half the original height.
shear Approximate tangent of the character slope relative to the vertical line. A zero value means
a non-italic font, 1.0f means about a 45 degree slope, etc.
The function initializes the font structure that can be passed to text rendering functions.
int cvInitLineIterator(
const CvArr* image,
CvPoint pt1,
1.4. DRAWING FUNCTIONS 199
CvPoint pt2,
CvLineIterator* line iterator,
int connectivity=8,
int left to right=0 );
left to right If (left to right = 0 ) then the line is scanned in the specified order, from
pt1 to pt2. If (left to right 6= 0) the line is scanned from left-most point to right-most.
The function initializes the line iterator and returns the number of pixels between the two end
points. Both points must be inside the image. After the iterator has been initialized, all the points
on the raster line that connects the two ending points may be retrieved by successive calls of
CV NEXT LINE POINT point.
The points on the line are calculated one by one using a 4-connected or 8-connected Bresen-
ham algorithm.
Example: Using line iterator to calculate the sum of pixel values along the color line.
int offset, x, y;
/* assume that ROI is not set, otherwise need to take it
into account. */
offset = [Link] - (uchar*)(image->imageData);
y = offset/image->widthStep;
x = (offset - y*image->widthStep)/(3*sizeof(uchar)
/* size of pixel */);
printf("(%d,%d)\n", x, y );
}
}
return cvScalar( blue_sum, green_sum, red_sum );
}
void cvLine(
CvArr* img,
CvPoint pt1,
CvPoint pt2,
CvScalar color,
int thickness=1,
int lineType=8,
int shift=0 );
CV AA antialiased line.
The function draws the line segment between pt1 and pt2 points in the image. The line
is clipped by the image or ROI rectangle. For non-antialiased lines with integer coordinates the
8-connected or 4-connected Bresenham algorithm is used. Thick lines are drawn with rounding
endings. Antialiased lines are drawn using Gaussian filtering. To specify the line color, the user
may use the macro CV RGB( r, g, b ).
void cvPolyLine(
CvArr* img,
CvPoint** pts,
int* npts,
int contours,
int is closed,
CvScalar color,
int thickness=1,
int lineType=8,
int shift=0 );
img Image
is closed Indicates whether the polylines must be drawn closed. If closed, the function draws
the line from the last vertex of every contour to the first vertex.
void cvPutText(
CvArr* img,
const char* text,
CvPoint org,
const CvFont* font,
CvScalar color );
The function renders the text in the image with the specified font and color. The printed text is
clipped by the ROI rectangle. Symbols that do not belong to the specified font are replaced with
the symbol for a rectangle.
void cvRectangle(
CvArr* img,
CvPoint pt1,
CvPoint pt2,
CvScalar color,
int thickness=1,
int lineType=8,
int shift=0 );
img Image
thickness Thickness of lines that make up the rectangle. Negative values, e.g., CV FILLED,
cause the function to draw a filled rectangle.
The function draws a rectangle with two opposite corners pt1 and pt2.
<?xml version="1.0">
<opencv_storage>
<A type_id="opencv-matrix">
<rows>3</rows>
<cols>3</cols>
<dt>f</dt>
<data>1. 0. 0. 0. 1. 0. 0. 0. 1.</data>
</A>
</opencv_storage>
YAML:
%YAML:1.0
A: !!opencv-matrix
rows: 3
cols: 3
dt: f
data: [ 1., 0., 0., 0., 1., 0., 0., 0., 1.]
As it can be seen from the examples, XML uses nested tags to represent hierarchy, while YAML
uses indentation for that purpose (similar to the Python programming language).
The same CXCore functions can read and write data in both formats; the particular format is
determined by the extension of the opened file, .xml for XML files and .yml or .yaml for YAML.
1.5. XML/YAML PERSISTENCE 205
/* optional flags */
#define CV_NODE_USER 16
#define CV_NODE_EMPTY 32
#define CV_NODE_NAMED 64
The structure is used only for retrieving data from file storage (i.e., for loading data from the
file). When data is written to a file, it is done sequentially, with minimal buffering. No data is stored
in the file storage.
In opposite, when data is read from a file, the whole file is parsed and represented in memory
as a tree. Every node of the tree is represented by CvFileNode . The type of file node N can
be retrieved as CV NODE TYPE(N->tag). Some file nodes (leaves) are scalars: text strings,
integers, or floating-point numbers. Other file nodes are collections of file nodes, which can be
scalars or collections in their turn. There are two types of collections: sequences and maps (we
use YAML notation, however, the same is true for XML streams). Sequences (do not mix them
with CvSeq ) are ordered collections of unnamed file nodes; maps are unordered collections of
named file nodes. Thus, elements of sequences are accessed by index ( GetSeqElem ), while
elements of maps are accessed by name ( GetFileNodeByName ). The table below describes the
different types of file nodes:
1.5. XML/YAML PERSISTENCE 207
/* methods */
CvIsInstanceFunc is_instance; /* checks if the passed object belongs to the type */
CvReleaseFunc release; /* releases object (memory etc.) */
CvReadFunc read; /* reads object from file storage */
CvWriteFunc write; /* writes object to file storage */
CvCloneFunc clone; /* creates a copy of the object */
}
CvTypeInfo;
The structure CvTypeInfo contains information about one of the standard or user-defined
types. Instances of the type may or may not contain a pointer to the corresponding CvTypeInfo
structure. In any case, there is a way to find the type info structure for a given object using the
TypeOf function. Aternatively, type info can be found by type name using FindType , which is used
when an object is read from file storage. The user can register a new type with RegisterType that
adds the type information structure into the beginning of the type list. Thus, it is possible to create
specialized types from generic standard types and override the basic methods.
The function finds the type of a given object and calls clone with the passed object.
1.5. XML/YAML PERSISTENCE 209
fs File storage
The function finds a registered type by its name. It returns NULL if there is no type with the
specified name.
CvTypeInfo* cvFirstType(void);
The function returns the first type in the list of registered types. Navigation through the list can
be done via the prev and next fields of the CvTypeInfo structure.
210 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
CvFileNode* cvGetFileNode(
CvFileStorage* fs,
CvFileNode* map,
const CvStringHashNode* key,
int createMissing=0 );
fs File storage
map The parent map. If it is NULL, the function searches a top-level node. If both map and key
are NULLs, the function returns the root file node - a map that contains top-level nodes.
createMissing Flag that specifies whether an absent node should be added to the map
The function finds a file node. It is a faster version of GetFileNodeByName (see GetHashed-
Key discussion). Also, the function can insert a new node, if it is not in the map yet.
CvFileNode* cvGetFileNodeByName(
const CvFileStorage* fs,
const CvFileNode* map,
const char* name);
fs File storage
map The parent map. If it is NULL, the function searches in all the top-level nodes (streams),
starting with the first one.
1.5. XML/YAML PERSISTENCE 211
The function finds a file node by name. The node is searched either in map or, if the pointer
is NULL, among the top-level file storage nodes. Using this function for maps and GetSeqElem
(or sequence reader) for sequences, it is possible to nagivate through the file storage. To speed
up multiple queries for a certain key (e.g., in the case of an array of structures) one may use a
combination of GetHashedKey and GetFileNode .
The function returns the name of a file node or NULL, if the file node does not have a name or
if node is NULL.
CvStringHashNode* cvGetHashedKey(
CvFileStorage* fs,
const char* name,
int len=-1,
int createMissing=0 );
fs File storage
createMissing Flag that specifies, whether an absent key should be added into the hash table
212 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
The function returns a unique pointer for each particular file node name. This pointer can be
then passed to the GetFileNode function that is faster than GetFileNodeByName because it
compares text strings by comparing pointers rather than the strings’ content.
Consider the following example where an array of points is encoded as a sequence of 2-entry
maps:
%YAML:1.0
points:
- { x: 10, y: 10 }
- { x: 20, y: 20 }
- { x: 30, y: 30 }
# ...
Then, it is possible to get hashed ”x” and ”y” pointers to speed up decoding of the points.
#include "cxcore.h"
if( CV\_NODE\_IS\_SEQ(points->tag) )
{
CvSeq* seq = points->[Link];
int i, total = seq->total;
CvSeqReader reader;
cvStartReadSeq( seq, &reader, 0 );
for( i = 0; i < total; i++ )
{
CvFileNode* pt = (CvFileNode*)[Link];
#if 1 /* faster variant */
CvFileNode* xnode = cvGetFileNode( fs, pt, x\_key, 0 );
CvFileNode* ynode = cvGetFileNode( fs, pt, y\_key, 0 );
assert( xnode && CV\_NODE\_IS\_INT(xnode->tag) &&
ynode && CV\_NODE\_IS\_INT(ynode->tag));
int x = xnode->data.i; // or x = cvReadInt( xnode, 0 );
int y = ynode->data.i; // or y = cvReadInt( ynode, 0 );
#elif 1 /* slower variant; does not use x\_key & y\_key */
CvFileNode* xnode = cvGetFileNodeByName( fs, pt, "x" );
CvFileNode* ynode = cvGetFileNodeByName( fs, pt, "y" );
assert( xnode && CV\_NODE\_IS\_INT(xnode->tag) &&
1.5. XML/YAML PERSISTENCE 213
Please note that whatever method of accessing a map you are using, it is still much slower
than using plain sequences; for example, in the above example, it is more efficient to encode the
points as pairs of integers in a single numeric sequence.
CvFileNode* cvGetRootFileNode(
const CvFileStorage* fs,
int stream index=0 );
fs File storage
stream index Zero-based index of the stream. See StartNextStream . In most cases, there is
only one stream in the file; however, there can be several.
The function returns one of the top-level file nodes. The top-level nodes do not have a name,
they correspond to the streams that are stored one after another in the file storage. If the index
is out of range, the function returns a NULL pointer, so all the top-level nodes may be iterated by
subsequent calls to the function with stream index=0,1,..., until the NULL pointer is returned.
This function may be used as a base for recursive traversal of the file storage.
214 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
void* cvLoad(
const char* filename,
CvMemStorage* storage=NULL,
const char* name=NULL,
const char** realName=NULL );
storage Memory storage for dynamic structures, such as CvSeq or CvGraph . It is not used
for matrices or images.
name Optional object name. If it is NULL, the first top-level object in the storage will be loaded.
realName Optional output parameter that will contain the name of the loaded object (useful if
name=NULL)
The function loads an object from a file. It provides a simple interface to cvRead. After the
object is loaded, the file storage is closed and all the temporary buffers are deleted. Thus, to load
a dynamic structure, such as a sequence, contour, or graph, one should pass a valid memory
storage destination to the function.
CvFileStorage* cvOpenFileStorage(
const char* filename,
CvMemStorage* memstorage,
int flags);
memstorage Memory storage used for temporary data and for storing dynamic structures, such
as CvSeq or CvGraph . If it is NULL, a temporary memory storage is created and used.
The function opens file storage for reading or writing data. In the latter case, a new file is
created or an existing file is rewritten. The type of the read or written file is determined by the
filename extension: .xml for XML and .yml or .yaml for YAML. The function returns a pointer to
the CvFileStorage structure.
void* cvRead(
CvFileStorage* fs,
CvFileNode* node,
CvAttrList* attributes=NULL );
fs File storage
The function decodes a user object (creates an object in a native representation from the file
storage subtree) and returns it. The object to be decoded must be an instance of a registered type
that supports the read method (see CvTypeInfo ). The type of the object is determined by the
type name that is encoded in the file. If the object is a dynamic structure, it is created either in
memory storage and passed to OpenFileStorage or, if a NULL pointer was passed, in temporary
memory storage, which is released when ReleaseFileStorage is called. Otherwise, if the object is
not a dynamic structure, it is created in a heap and should be released with a specialized function
or by using the generic Release .
216 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
void* cvReadByName(
CvFileStorage* fs,
const CvFileNode* map,
const char* name,
CvAttrList* attributes=NULL );
fs File storage
map The parent map. If it is NULL, the function searches a top-level node.
name The node name
attributes Unused parameter
int cvReadInt(
const CvFileNode* node,
int defaultValue=0 );
The function returns an integer that is represented by the file node. If the file node is NULL,
the defaultValue is returned (thus, it is convenient to call the function right after GetFileNode
without checking for a NULL pointer). If the file node has type CV NODE INT, then node->data.i
is returned. If the file node has type CV NODE REAL, then node->data.f is converted to an
integer and returned. Otherwise the result is not determined.
1.5. XML/YAML PERSISTENCE 217
int cvReadIntByName(
const CvFileStorage* fs,
const CvFileNode* map,
const char* name,
int defaultValue=0 );
fs File storage
map The parent map. If it is NULL, the function searches a top-level node.
name The node name
defaultValue The value that is returned if the file node is not found
The function is a simple superposition of GetFileNodeByName and ReadInt .
void cvReadRawData(
const CvFileStorage* fs,
const CvFileNode* src,
void* dst,
const char* dt);
fs File storage
src The file node (a sequence) to read numbers from
dst Pointer to the destination array
dt Specification of each array element. It has the same format as in WriteRawData .
The function reads elements from a file node that represents a sequence of scalars.
218 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
void cvReadRawDataSlice(
const CvFileStorage* fs,
CvSeqReader* reader,
int count,
void* dst,
const char* dt );
fs File storage
The function reads one or more elements from the file node, representing a sequence, to a
user-specified array. The total number of read sequence elements is a product of total and
the number of components in each array element. For example, if dt=2if, the function will read
total × 3 sequence elements. As with any sequence, some parts of the file node sequence may
be skipped or read repeatedly by repositioning the reader using SetSeqReaderPos .
double cvReadReal(
const CvFileNode* node,
double defaultValue=0. );
The function returns a floating-point value that is represented by the file node. If the file node
is NULL, the defaultValue is returned (thus, it is convenient to call the function right after
GetFileNode without checking for a NULL pointer). If the file node has type CV NODE REAL,
then node->data.f is returned. If the file node has type CV NODE INT, then node->data.f is
converted to floating-point and returned. Otherwise the result is not determined.
double cvReadRealByName(
const CvFileStorage* fs,
const CvFileNode* map,
const char* name,
double defaultValue=0.);
fs File storage
map The parent map. If it is NULL, the function searches a top-level node.
defaultValue The value that is returned if the file node is not found
fs File storage
map The parent map. If it is NULL, the function searches a top-level node.
name The node name
defaultValue The value that is returned if the file node is not found
The function is a simple superposition of GetFileNodeByName and ReadString .
The function finds the type of a given object and calls release with the double pointer.
The function closes the file associated with the storage and releases all the temporary struc-
tures. It must be called after all I/O operations with the storage are finished.
void cvSave(
const char* filename,
const void* structPtr,
const char* name=NULL,
const char* comment=NULL,
CvAttrList attributes=cvAttrList());
222 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
fs File storage
The function starts the next stream in file storage. Both YAML and XML support multiple
”streams.” This is useful for concatenating files or for resuming the writing process.
void cvStartReadRawData(
const CvFileStorage* fs,
const CvFileNode* src,
CvSeqReader* reader);
fs File storage
src The file node (a sequence) to read numbers from
reader Pointer to the sequence reader
The function initializes the sequence reader to read data from a file node. The initialized reader
can be then passed to ReadRawDataSlice .
1.5. XML/YAML PERSISTENCE 223
fs File storage
name Name of the written structure. The structure can be accessed by this name when the
storage is read.
struct flags A combination one of the following values:
CV NODE SEQ the written structure is a sequence (see discussion of CvFileStorage ), that
is, its elements do not have a name.
CV NODE MAP the written structure is a map (see discussion of CvFileStorage ), that is, all
its elements have names.
One and only one of the two above flags must be specified
CV NODE FLOW the optional flag that makes sense only for YAML streams. It means that the
structure is written as a flow (not as a block), which is more compact. It is recommended to
use this flag for structures or arrays whose elements are all scalars.
typeName Optional parameter - the object type name. In case of XML it is written as a type id
attribute of the structure opening tag. In the case of YAML it is written after a colon following
the structure name (see the example in CvFileStorage description). Mainly it is used with
user objects. When the storage is read, the encoded type name is used to determine the
object type (see CvTypeInfo and FindTypeInfo ).
attributes This parameter is not used in the current implementation
The function starts writing a compound structure (collection) that can be a sequence or a
map. After all the structure fields, which can be scalars or structures, are written, EndWriteStruct
should be called. The function can be used to group some objects or to implement the write
function for a some user object (see CvTypeInfo ).
224 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
fs File storage
name Name of the written object. Should be NULL if and only if the parent structure is a sequence.
attributes The attributes of the object. They are specific for each particular type (see the
dicsussion below).
The function writes an object to file storage. First, the appropriate type info is found using
TypeOf . Then, the write method associated with the type info is called.
Attributes are used to customize the writing procedure. The standard types support the follow-
ing attributes (all the *dt attributes have the same format as in WriteRawData ):
1. CvSeq
header dt description of user fields of the sequence header that follow CvSeq, or CvChain
(if the sequence is a Freeman chain) or CvContour (if the sequence is a contour or point
sequence)
dt description of the sequence elements.
recursive if the attribute is present and is not equal to ”0” or ”false”, the whole tree of
sequences (contours) is stored.
2. Cvgraph
header dt description of user fields of the graph header that follows CvGraph;
vertex dt description of user fields of graph vertices
edge dt description of user fields of graph edges (note that the edge weight is always
written, so there is no need to specify it explicitly)
Below is the code that creates the YAML file shown in the CvFileStorage description:
#include "cxcore.h"
cvSetIdentity( mat );
cvWrite( fs, "A", mat, cvAttrList(0,0) );
cvReleaseFileStorage( &fs );
226 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
cvReleaseMat( &mat );
return 0;
}
void cvWriteComment(
CvFileStorage* fs,
const char* comment,
int eolComment);
fs File storage
eolComment If non-zero, the function tries to put the comment at the end of current line. If the
flag is zero, if the comment is multi-line, or if it does not fit at the end of the current line, the
comment starts a new line.
The function writes a comment into file storage. The comments are skipped when the storage
is read, so they may be used only for debugging or descriptive purposes.
void cvWriteFileNode(
CvFileStorage* fs,
const char* new node name,
const CvFileNode* node,
int embed );
new file node New name of the file node in the destination file storage. To keep the existing
name, use cvGetFileNodeName
embed If the written node is a collection and this parameter is not zero, no extra level of hiararchy
is created. Instead, all the elements of node are written into the currently written structure.
Of course, map elements may be written only to a map, and sequence elements may be
written only to a sequence.
The function writes a copy of a file node to file storage. Possible applications of the function
are merging several file storages into one and conversion between XML and YAML formats.
void cvWriteInt(
CvFileStorage* fs,
const char* name,
int value);
fs File storage
name Name of the written value. Should be NULL if and only if the parent structure is a sequence.
The function writes a single integer value (with or without a name) to the file storage.
void cvWriteRawData(
CvFileStorage* fs,
const void* src,
int len,
const char* dt );
fs File storage
The function writes an array, whose elements consist of single or multiple numbers. The func-
tion call can be replaced with a loop containing a few WriteInt and WriteReal calls, but a single
call is more efficient. Note that because none of the elements have a name, they should be written
to a sequence rather than a map.
1.5. XML/YAML PERSISTENCE 229
void cvWriteReal(
CvFileStorage* fs,
const char* name,
double value );
fs File storage
name Name of the written value. Should be NULL if and only if the parent structure is a sequence.
The function writes a single floating-point value (with or without a name) to file storage. Special
values are encoded as follows: NaN (Not A Number) as .NaN, ±∞ as +.Inf (-.Inf).
The following example shows how to use the low-level writing functions to store custom struc-
tures, such as termination criteria, without registering a new type.
void write_termcriteria( CvFileStorage* fs, const char* struct_name,
CvTermCriteria* termcrit )
{
cvStartWriteStruct( fs, struct_name, CV_NODE_MAP, NULL, cvAttrList(0,0));
cvWriteComment( fs, "termination criteria", 1 ); // just a description
if( termcrit->type & CV_TERMCRIT_ITER )
cvWriteInteger( fs, "max_iterations", termcrit->max_iter );
if( termcrit->type & CV_TERMCRIT_EPS )
cvWriteReal( fs, "accuracy", termcrit->epsilon );
cvEndWriteStruct( fs );
}
void cvWriteString(
CvFileStorage* fs,
const char* name,
const char* str,
int quote=0 );
fs File storage
name Name of the written string . Should be NULL if and only if the parent structure is a sequence.
quote If non-zero, the written string is put in quotes, regardless of whether they are required.
Otherwise, if the flag is zero, quotes are used only when they are required (e.g. when the
string starts with a digit or contains spaces).
1.6 Clustering
labels Output integer vector storing cluster indices for every sample
1.6. CLUSTERING 231
termcrit Specifies maximum number of iterations and/or accuracy (distance the centers can
move by between subsequent iterations)
attempts How many times the algorithm is executed using different initial labelings. The algo-
rithm returns labels that yield the best compactness (see the last function parameter)
rng Optional external random number generator; can be used to fully control the function be-
haviour
flags Can be 0 or CV KMEANS USE INITIAL LABELS. The latter value means that during the
first (and possibly the only) attempt, the function uses the user-supplied labels as the initial
approximation instead of generating random labels. For the second and further attempts,
the function will use randomly generated labels in any case
compactness The optional output parameter, which is computed as i ||samplesi −centerslabelsi ||2
P
after every attempt; the best (minimum) value is chosen and the corresponding labels are re-
turned by the function. Basically, the user can use only the core of the function, set the num-
ber of attempts to 1, initialize labels each time using a custom algorithm (flags=CV KMEAN USE INITIAL L
and, based on the output compactness or any other criteria, choose the best clustering.
The function cvKMeans2 implements a k-means algorithm that finds the centers of nclusters
clusters and groups the input samples around the clusters. On output, labelsi contains a cluster
index for samples stored in the i-th row of the samples matrix.
#include "cxcore.h"
#include "highgui.h"
color_tab[0] = CV_RGB(255,0,0);
color_tab[1] = CV_RGB(0,255,0);
color_tab[2] = CV_RGB(100,100,255);
color_tab[3] = CV_RGB(255,0,255);
color_tab[4] = CV_RGB(255,255,0);
cvNamedWindow( "clusters", 1 );
232 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
for(;;)
{
int k, cluster_count = cvRandInt(&rng)%MAX_CLUSTERS + 1;
int i, sample_count = cvRandInt(&rng)%1000 + 1;
CvMat* points = cvCreateMat( sample_count, 1, CV_32FC2 );
CvMat* clusters = cvCreateMat( sample_count, 1, CV_32SC1 );
/* shuffle samples */
for( i = 0; i < sample_count/2; i++ )
{
CvPoint2D32f* pt1 =
(CvPoint2D32f*)points->[Link] + cvRandInt(&rng)%sample_count;
CvPoint2D32f* pt2 =
(CvPoint2D32f*)points->[Link] + cvRandInt(&rng)%sample_count;
CvPoint2D32f temp;
CV_SWAP( *pt1, *pt2, temp );
}
cvZero( img );
cvCircle( img,
cvPointFrom32f(pt),
2,
color_tab[cluster_idx],
CV_FILLED );
}
cvReleaseMat( &points );
cvReleaseMat( &clusters );
int cvSeqPartition(
const CvSeq* seq,
CvMemStorage* storage,
CvSeq** labels,
CvCmpFunc is equal,
void* userdata );
The function cvSeqPartition implements a quadratic algorithm for splitting a set into one
or more equivalancy classes. The function returns the number of equivalency classes.
#include "cxcore.h"
#include "highgui.h"
#include <stdio.h>
CvSeq* point_seq = 0;
IplImage* canvas = 0;
CvScalar* colors = 0;
int pos = 10;
int is_equal( const void* _a, const void* _b, void* userdata )
{
CvPoint a = *(const CvPoint*)_a;
CvPoint b = *(const CvPoint*)_b;
double threshold = *(double*)userdata;
return (double)((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y)) <=
threshold;
}
cvNamedWindow( "points", 1 );
cvCreateTrackbar( "threshold", "points", &pos, 50, on_track );
on_track(pos);
cvWaitKey(0);
return 0;
}
Error Handling
Error handling in OpenCV is similar to IPL (Image Processing Library). In the case of an error,
functions do not return the error code. Instead, they raise an error using CV ERROR macro that
236 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
calls cvError that, in its turn, sets the error status with cvSetErrStatus and calls a standard or user-
defined error handler (that can display a message box, write to log, etc., see cvRedirectError).
There is a global variable, one per each program thread, that contains current error status (an
integer value). The status can be retrieved with the cvGetErrStatus function.
There are three modes of error handling (see cvSetErrMode and cvGetErrMode):
• Leaf. The program is terminated after the error handler is called. This is the default value.
It is useful for debugging, as the error is signalled immediately after it occurs. However, for
production systems, other two methods may be preferable as they provide more control.
• Parent. The program is not terminated, but the error handler is called. The stack is unwound
(it is done w/o using a C++ exception mechanism). The user may check error code after
calling the CxCore function with cvGetErrStatus and react.
Actually, the semantics of the Leaf and Parent modes are implemented by error handlers
and the above description is true for them. cvGuiBoxReport behaves slightly differently, and
some custom error handlers may implement quite different semantics.
Macros for raising an error, checking for errors, etc.
/* special macros for enclosing processing statements within a function and separating
them from prologue (resource initialization) and epilogue (guaranteed resource release)
#define __BEGIN__ {
#define __END__ goto exit; exit: ; }
/* proceeds to "resource release" stage */
#define EXIT goto exit
#include "cxcore.h"
#include <stdio.h>
CV_ERROR( CV_StsBadArg,
"input_array or output_array are not valid matrices" );
temp_array->cols = input_array->cols;
CV_CALL( cvDCT( input_array, temp_array, CV_DXT_FORWARD ));
temp_array->cols = output_array->cols;
CV_CALL( cvDCT( temp_array, output_array, CV_DXT_INVERSE ));
CV_CALL( cvScale( output_array,
output_array,
1./sqrt((double)input_array->cols*output_array->cols), 0 ));
The function returns the current error status - the value set with the last cvSetErrStatus call.
Note that in Leaf mode, the program terminates immediately after an error occurs, so to always
gain control after the function call, one should call cvSetErrMode and set the Parent or Silent
error mode.
The function sets the error status to the specified value. Mostly, the function is used to reset
the error status (set to it CV StsOk) to recover after an error. In other cases it is more natural to
call cvError or CV ERROR.
int cvGetErrMode(void);
The function returns the current error mode - the value set with the last cvSetErrMode call.
The function sets the specified error mode. For descriptions of different error modes, see the
beginning of the error section.
int cvError(
int status,
const char* func name,
const char* err msg,
const char* filename,
int line );
The function sets the error status to the specified value (via cvSetErrStatus) and, if the error
mode is not Silent, calls the error handler.
The function returns the textual description for the specified error status code. In the case of
unknown status, the function returns a NULL pointer.
CvErrorCallback cvRedirectError(
CvErrorCallback error handler,
void* userdata=NULL,
void** prevUserdata=NULL );
The function sets a new error handler that can be one of the standard handlers or a custom
handler that has a specific interface. The handler takes the same parameters as the cvError
function. If the handler returns a non-zero value, the program is terminated; otherwise, it continues.
The error handler may check the current error mode with cvGetErrMode to make a decision.
int cvNulDevReport( int status, const char* func name, const char*
err msg, const char* file name, int line, void* userdata );
The function allocates size bytes and returns a pointer to the allocated buffer. In the case
of an error the function reports an error and returns a NULL pointer. By default, cvAlloc calls
icvAlloc which itself calls malloc. However it is possible to assign user-defined memory allo-
cation/deallocation functions using the cvSetMemoryManager function.
The function deallocates a memory buffer allocated by cvAlloc. It clears the pointer to buffer
upon exit, which is why the double pointer is used. If the *buffer is already NULL, the function
does nothing.
The function returns number of the ticks starting from some platform-dependent event (number
of CPU ticks from the startup, number of milliseconds from 1970th year, etc.). The function is useful
for accurate measurement of a function/user-code execution time. To convert the number of ticks
to time units, use cvGetTickFrequency.
The function returns the number of ticks per microsecond. Thus, the quotient of cvGetTick-
Count and cvGetTickFrequency will give the number of microseconds starting from the platform-
dependent event.
int search_modules;
int loaded_from;
}
CvPluginFuncInfo;
The function adds a module to the list of registered modules. After the module is registered,
information about it can be retrieved using the cvGetModuleInfo function. Also, the registered
module makes full use of optimized plugins (IPP, MKL, ...), supported by CXCORE. CXCORE it-
self, CV (computer vision), CVAUX (auxilary computer vision), and HIGHGUI (visualization and
image/video acquisition) are examples of modules. Registration is usually done when the shared
library is loaded. See cxcore/src/[Link] and cv/src/[Link] for de-
tails about how registration is done and look at cxcore/src/[Link], cxcore/src/ cxipp.h
on how IPP and MKL are connected to the modules.
void cvGetModuleInfo(
const char* moduleName,
const char** version,
const char** loadedAddonPlugins);
246 CHAPTER 1. CORE. THE CORE FUNCTIONALITY
moduleName Name of the module of interest, or NULL, which means all the modules
version The output parameter. Information about the module(s), including version
loadedAddonPlugins The list of names and versions of the optimized plugins that CXCORE
was able to find and load
The function returns information about one or all of the registered modules. The returned
information is stored inside the libraries, so the user should not deallocate or modify the returned
text strings.
void cvSetMemoryManager(
CvAllocFunc allocFunc=NULL,
CvFreeFunc freeFunc=NULL,
void* userdata=NULL );
1.7. UTILITY AND SYSTEM FUNCTIONS AND MACROS 247
allocFunc Allocation function; the interface is similar to malloc, except that userdata may be
used to determine the context
The function sets user-defined memory managment functions (substitutes for malloc and
free) that will be called by cvAlloc, cvFree and higher-level functions (e.g., cvCreateImage).
Note that the function should be called when there is data allocated using cvAlloc. Also, to
avoid infinite recursive calls, it is not allowed to call cvAlloc and cvFree from the custom alloca-
tion/deallocation functions.
If the alloc func and free func pointers are NULL, the default memory managing functions
are restored.
#define CV_TURN_ON_IPL_COMPATIBILITY() \
cvSetIPLAllocators( iplCreateImageHeader, iplAllocateImage, \
iplDeallocate, iplCreateROI, iplCloneImage )
void cvSetIPLAllocators(
Cv iplCreateImageHeader create header,
Cv iplAllocateImageData allocate data,
Cv iplDeallocate deallocate,
Cv iplCreateROI create roi,
Cv iplCloneImage clone image );
The function causes CXCORE to use IPL functions for image allocation/deallocation opera-
tions. For convenience, there is the wrapping macro CV TURN ON IPL COMPATIBILITY. The
function is useful for applications where IPL and CXCORE/OpenCV are used together and still
there are calls to iplCreateImageHeader, etc. The function is not necessary if IPL is called
only for data processing and all the allocation/deallocation is done by CXCORE, or if all the allo-
cation/deallocation is done by IPL and some of OpenCV functions are used to process the data.
Chapter 2
2.1 Histograms
void cvCalcBackProject(
IplImage** image,
CvArr* back project,
const CvHistogram* hist );
249
250 CHAPTER 2. IMGPROC. IMAGE PROCESSING
back project Destination back projection image of the same type as the source images
hist Histogram
The function calculates the back project of the histogram. For each tuple of pixels at the
same position of all input single-channel images the function puts the value of the histogram bin,
corresponding to the tuple in the destination image. In terms of statistics, the value of each output
image pixel is the probability of the observed tuple given the distribution (histogram). For example,
to find a red object in the picture, one may do the following:
1. Calculate a hue histogram for the red object assuming the image contains only this object.
The histogram is likely to have a strong maximum, corresponding to red color.
2. Calculate back projection of a hue plane of input image where the object is searched, using
the histogram. Threshold the image.
3. Find connected components in the resulting picture and choose the right component using
some additional criteria, for example, the largest connected component.
That is the approximate algorithm of Camshift color object tracker, except for the 3rd step,
instead of which CAMSHIFT algorithm is used to locate the object on the back projection given
the previous object position.
void cvCalcBackProjectPatch(
IplImage** images,
CvArr* dst,
CvSize patch size,
CvHistogram* hist,
int method,
double factor );
patch size Size of the patch slid though the source image
hist Histogram
factor Normalization factor for histograms, will affect the normalization scale of the destination
image, pass 1 if unsure
The function calculates the back projection by comparing histograms of the source image
patches with the given histogram. Taking measurement results from some image at each location
over ROI creates an array image. These results might be one or more of hue, x derivative, y
derivative, Laplacian filter, oriented Gabor filter, etc. Each measurement output is collected into
its own separate image. The image image array is a collection of these measurement images. A
multi-dimensional histogram hist is constructed by sampling from the image image array. The
final histogram is normalized. The hist histogram has as many dimensions as the number of
elements in image array.
Each new image is measured and then converted into an image image array over a cho-
sen ROI. Histograms are taken from this image image in an area covered by a ”patch” with an
anchor at center as shown in the picture below. The histogram is normalized using the param-
eter norm factor so that it may be compared with hist. The calculated histogram is com-
pared to the model histogram; hist uses The function cvCompareHist with the comparison
method=method). The resulting output is placed at the location corresponding to the patch an-
chor in the probability image dst. This process is repeated as the patch is slid over the ROI.
Iterative histogram update by subtracting trailing pixels covered by the patch and adding newly
covered pixels to the histogram can save a lot of operations, though it is not implemented yet.
Back Project Calculation by Patches
252 CHAPTER 2. IMGPROC. IMAGE PROCESSING
void cvCalcHist(
IplImage** image,
CvHistogram* hist,
int accumulate=0,
const CvArr* mask=NULL );
accumulate Accumulation flag. If it is set, the histogram is not cleared in the beginning. This
feature allows user to compute a single histogram from several images, or to update the
histogram online
mask The operation mask, determines what pixels of the source images are counted
The function calculates the histogram of one or more single-channel images. The elements
of a tuple that is used to increment a histogram bin are taken at the same location from the
corresponding input images.
#include <cv.h>
#include <highgui.h>
cvNamedWindow( "Source", 1 );
cvShowImage( "Source", src );
cvWaitKey(0);
}
}
void cvCalcProbDensity(
const CvHistogram* hist1,
const CvHistogram* hist2,
CvHistogram* dst hist,
double scale=255 );
The function calculates the object probability density from the two histograms as:
0
if hist1(I) = 0
dist hist(I) = scale if hist1(I) 6= 0 and hist2(I) > hist1(I)
hist2(I)·scale
if hist1(I) 6= 0 and hist2(I) ≤ hist1(I)
hist1(I)
hist Histogram
The function sets all of the histogram bins to 0 in the case of a dense histogram and removes
all histogram bins in the case of a sparse array.
2.1. HISTOGRAMS 255
double cvCompareHist(
const CvHistogram* hist1,
const CvHistogram* hist2,
int method );
The function compares two dense histograms using the specified method (H1 denotes the first
histogram, H2 the second):
where
Hk (I) − 1
Hk0 (I) = P
N · J Hk (J)
where N is the number of histogram bins.
The function makes a copy of the histogram. If the second histogram pointer *dst is NULL, a
new histogram of the same size as src is created. Otherwise, both histograms must have equal
types and sizes. Then the function copies the source histogram’s bin values to the destination
histogram and sets the same bin value ranges as in src.
CvHistogram* cvCreateHist(
int dims,
int* sizes,
int type,
float** ranges=NULL,
int uniform=1 );
type Histogram representation format: CV HIST ARRAY means that the histogram data is rep-
resented as a multi-dimensional dense array CvMatND; CV HIST SPARSE means that his-
togram data is represented as a multi-dimensional sparse array CvSparseMat
ranges Array of ranges for the histogram bins. Its meaning depends on the uniform param-
eter value. The ranges are used for when the histogram is calculated or backprojected to
determine which histogram bin corresponds to which value/tuple of values from the input
image(s)
uniform Uniformity flag; if not 0, the histogram has evenly spaced bins and for every 0 <= i <
cDims ranges[i] is an array of two numbers: lower and upper boundaries for the i-th his-
togram dimension. The whole range [lower,upper] is then split into dims[i] equal parts to
determine the i-th input tuple value ranges for every histogram bin. And if uniform=0,
then i-th element of ranges array contains dims[i]+1 elements: lower0 , upper0 , lower1 , upper1 =
lower2 , ...upperdims[i]−1 where lowerj and upperj are lower and upper boundaries of
i-th input tuple value for j-th bin, respectively. In either case, the input values that are
beyond the specified range for a histogram bin are not counted by cvCalcHist and filled with
0 by cvCalcBackProject
The function creates a histogram of the specified size and returns a pointer to the created
histogram. If the array ranges is 0, the histogram bin ranges must be specified later via the
function cvSetHistBinRanges. Though cvCalcHist and cvCalcBackProject may process 8-bit
images without setting bin ranges, they assume thy are equally spaced in 0 to 255 bins.
hist Histogram
The macros GetHistValue return a pointer to the specified bin of the 1D, 2D, 3D or N-D
histogram. In the case of a sparse histogram the function creates a new bin and sets it to 0,
unless it exists already.
void cvGetMinMaxHistValue(
const CvHistogram* hist,
float* min value,
float* max value,
int* min idx=NULL,
int* max idx=NULL );
2.1. HISTOGRAMS 259
hist Histogram
min value Pointer to the minimum value of the histogram
max value Pointer to the maximum value of the histogram
min idx Pointer to the array of coordinates for the minimum
max idx Pointer to the array of coordinates for the maximum
The function finds the minimum and maximum histogram bins and their positions. All of output
arguments are optional. Among several extremas with the same value the ones with the minimum
index (in lexicographical order) are returned. In the case of several maximums or minimums, the
earliest in lexicographical order (extrema locations) is returned.
CvHistogram* cvMakeHistHeaderForArray(
int dims,
int* sizes,
CvHistogram* hist,
float* data,
float** ranges=NULL,
int uniform=1 );
The function initializes the histogram, whose header and bins are allocated by th user. cvRe-
leaseHist does not need to be called afterwards. Only dense histograms can be initialized this
way. The function returns hist.
260 CHAPTER 2. IMGPROC. IMAGE PROCESSING
The function normalizes the histogram bins by scaling them, such that the sum of the bins
becomes equal to factor.
hist Histogram
The function releases the histogram (header and the data). The pointer to the histogram is
cleared by the function. If *hist pointer is already NULL, the function does nothing.
void cvSetHistBinRanges(
CvHistogram* hist,
float** ranges,
int uniform=1 );
hist Histogram
The function is a stand-alone function for setting bin ranges in the histogram. For a more
detailed description of the parameters ranges and uniform see the cvCalcHist function, that
can initialize the ranges as well. Ranges for the histogram bins must be set before the histogram
is calculated or the backproject of the histogram is calculated.
The function clears histogram bins that are below the specified threshold.
void cvCopyMakeBorder(
const CvArr* src,
CvArr* dst,
CvPoint offset,
int bordertype,
CvScalar value=cvScalarAll(0) );
offset Coordinates of the top-left corner (or bottom-left in the case of images with bottom-left
origin) of the destination image rectangle where the source image (or its ROI) is copied. Size
of the rectanlge matches the source image size/ROI size
bordertype Type of the border to create around the copied source image rectangle; types in-
clude:
IPL BORDER CONSTANT border is filled with the fixed value, passed as last parameter of
the function.
IPL BORDER REPLICATE the pixels from the top and bottom rows, the left-most and right-
most columns are replicated to fill the border.
(The other two border types from IPL, IPL BORDER REFLECT and IPL BORDER WRAP, are
currently unsupported)
The function copies the source 2D array into the interior of the destination array and makes
a border of the specified type around the copied area. The function is useful when one needs to
emulate border type that is different from the one embedded into a specific algorithm implementa-
tion. For example, morphological functions, as well as most of other filtering functions in OpenCV,
internally use replication border type, while the user may need a zero border or a border, filled with
1’s or 255’s.
IplConvKernel* cvCreateStructuringElementEx(
int cols,
int rows,
int anchorX,
int anchorY,
int shape,
int* values=NULL );
shape Shape of the structuring element; may have the following values:
values Pointer to the structuring element data, a plane array, representing row-by-row scanning
of the element matrix. Non-zero values indicate points that belong to the element. If the
pointer is NULL, then all values are considered non-zero, that is, the element is of a rectan-
gular shape. This parameter is considered only if the shape is CV SHAPE CUSTOM
void cvDilate(
const CvArr* src,
CvArr* dst,
IplConvKernel* element=NULL,
int iterations=1 );
The function dilates the source image using the specified structuring element that determines
the shape of a pixel neighborhood over which the maximum is taken:
max src(x + x0 , y + y 0 )
(x0 ,y 0 ) in element
The function supports the in-place mode. Dilation can be applied several (iterations) times.
For color images, each channel is processed independently.
void cvErode(
const CvArr* src,
CvArr* dst,
IplConvKernel* element=NULL,
int iterations=1);
The function erodes the source image using the specified structuring element that determines
the shape of a pixel neighborhood over which the minimum is taken:
min src(x + x0 , y + y 0 )
(x0 ,y 0 ) in element
The function supports the in-place mode. Erosion can be applied several (iterations) times.
For color images, each channel is processed independently.
void cvFilter2D(
const CvArr* src,
CvArr* dst,
const CvMat* kernel,
CvPoint anchor=cvPoint(-1,-1));
kernel Convolution kernel, a single-channel floating point matrix. If you want to apply different
kernels to different channels, split the image into separate color planes using cvSplit and
process them individually
anchor The anchor of the kernel that indicates the relative position of a filtered point within the
kernel. The anchor shoud lie within the kernel. The special default value (-1,-1) means that
it is at the kernel center
The function applies an arbitrary linear filter to the image. In-place operation is supported.
When the aperture is partially outside the image, the function interpolates outlier pixel values from
the nearest pixels that are inside the image.
2.2. IMAGE FILTERING 267
void cvLaplace(
const CvArr* src,
CvArr* dst,
int apertureSize=3);
The function calculates the Laplacian of the source image by adding up the second x and y
derivatives calculated using the Sobel operator:
d2 src d2 src
dst(x, y) = +
dx2 dy 2
Setting apertureSize = 1 gives the fastest variant that is equal to convolving the image with
the following kernel:
0 1 0
1 −4 1
0 1 0
Similar to the cvSobel function, no scaling is done and the same combinations of input and
output formats are supported.
void cvMorphologyEx(
const CvArr* src,
CvArr* dst,
CvArr* temp,
IplConvKernel* element,
int operation,
int iterations=1 );
The function can perform advanced morphological transformations using erosion and dilation
as basic operations.
Opening:
”Top hat”:
void cvPyrDown(
const CvArr* src,
CvArr* dst,
int filter=CV GAUSSIAN 5x5 );
dst The destination image, should have a half as large width and height than the source
filter Type of the filter used for convolution; only CV GAUSSIAN 5x5 is currently supported
The function performs the downsampling step of the Gaussian pyramid decomposition. First it
convolves the source image with the specified filter and then downsamples the image by rejecting
even rows and columns.
The function releases the structure IplConvKernel that is no longer needed. If *element
is NULL, the function has no effect.
void cvSmooth(
const CvArr* src,
CvArr* dst,
int smoothtype=CV GAUSSIAN,
int param1=3,
int param2=0,
double param3=0,
double param4=0);
CV BLUR NO SCALE linear convolution with param1 × param2 box kernel (all 1’s). If you
want to smooth different pixels with different-size box kernels, you can use the integral
image that is computed using cvIntegral
CV BLUR linear convolution with param1 × param2 box kernel (all 1’s) with subsequent
scaling by 1/(param1 · param2)
CV GAUSSIAN linear convolution with a param1 × param2 Gaussian kernel
CV MEDIAN median filter with a param1 × param1 square aperture
CV BILATERAL bilateral filter with a param1×param1 square aperture, color sigma=param3
and spatial sigma=param4. If param1=0, the aperture square side is set to cvRound(param4*1.5)*
Information about bilateral filtering can be found at [Link]
CVonline/LOCAL_COPIES/MANDUCHI1/Bilateral_Filtering.html
param1 The first parameter of the smoothing operation, the aperture width. Must be a positive
odd number (1, 3, 5, ...)
2.2. IMAGE FILTERING 271
param2 The second parameter of the smoothing operation, the aperture height. Ignored by
CV MEDIAN and CV BILATERAL methods. In the case of simple scaled/non-scaled and
Gaussian blur if param2 is zero, it is set to param1. Otherwise it must be a positive odd
number.
param3 In the case of a Gaussian parameter this parameter may specify Gaussian σ (standard
deviation). If it is zero, it is calculated from the kernel size:
Using standard sigma for small kernels (3 × 3 to 7 × 7) gives better speed. If param3 is not
zero, while param1 and param2 are zeros, the kernel size is calculated from the sigma (to
provide accurate enough operation).
The function smooths an image using one of several methods. Every of the methods has some
features and restrictions listed below
Blur with no scaling works with single-channel images only and supports accumulation of 8-bit
to 16-bit format (similar to cvSobel and cvLaplace) and 32-bit floating point to 32-bit floating-point
format.
Simple blur and Gaussian blur support 1- or 3-channel, 8-bit and 32-bit floating point images.
These two methods can process images in-place.
Median and bilateral filters work with 1- or 3-channel 8-bit images and can not process images
in-place.
void cvSobel(
const CvArr* src,
CvArr* dst,
int xorder,
int yorder,
int apertureSize=3 );
dxorder+yorder src
dst(x, y) =
dxxorder · dy yorder
The Sobel operators combine Gaussian smoothing and differentiation so the result is more
or less resistant to the noise. Most often, the function is called with (xorder = 1, yorder = 0,
apertureSize = 3) or (xorder = 0, yorder = 1, apertureSize = 3) to calculate the first x- or
y- image derivative. The first case corresponds to a kernel of:
−1 0 1
−2 0 2
−1 0 1
and the second one corresponds to a kernel of:
−1 −2 −1
0 0 0
1 2 1
or a kernel of:
1 2 1
0 0 0
−1 2 −1
2.3. GEOMETRIC IMAGE TRANSFORMATIONS 273
depending on the image origin (origin field of IplImage structure). No scaling is done, so
the destination image usually has larger numbers (in absolute values) than the source image does.
To avoid overflow, the function requires a 16-bit destination image if the source image is 8-bit.
The result can be converted back to 8-bit using the cvConvertScale or the cvConvertScaleAbs
function. Besides 8-bit images the function can process 32-bit floating-point images. Both the
source and the destination must be single-channel images of equal size or equal ROI size.
CvMat* cv2DRotationMatrix(
CvPoint2D32f center,
double angle,
double scale,
CvMat* mapMatrix );
CvMat* cvGetAffineTransform(
const CvPoint2D32f* src,
const CvPoint2D32f* dst,
CvMat* mapMatrix );
2.3. GEOMETRIC IMAGE TRANSFORMATIONS 275
The function cvGetAffineTransform calculates the matrix of an affine transform such that:
0 xi
xi
= mapMatrix · yi
yi0
1
where
CvMat* cvGetPerspectiveTransform(
const CvPoint2D32f* src,
const CvPoint2D32f* dst,
CvMat* mapMatrix );
void cvGetQuadrangleSubPix(
const CvArr* src,
CvArr* dst,
const CvMat* mapMatrix );
The function cvGetQuadrangleSubPix extracts pixels from src at sub-pixel accuracy and
stores them to dst as follows:
where
(width(dst) − 1) 0 (height(dst) − 1)
x0 = x − ,y = y −
2 2
and
A11 A12 b1
mapMatrix =
A21 A22 b2
The values of pixels at non-integer coordinates are retrieved using bilinear interpolation. When
the function needs pixels outside of the image, it uses replication border mode to reconstruct the
values. Every channel of multiple-channel images is processed independently.
void cvGetRectSubPix(
const CvArr* src,
CvArr* dst,
CvPoint2D32f center );
center Floating point coordinates of the extracted rectangle center within the source image. The
center must be inside the image
where the values of the pixels at non-integer coordinates are retrieved using bilinear interpo-
lation. Every channel of multiple-channel images is processed independently. While the rectangle
center must be inside the image, parts of the rectangle may be outside. In this case, the replication
border mode is used to get pixel values beyond the image boundaries.
void cvLogPolar(
const CvArr* src,
CvArr* dst,
CvPoint2D32f center,
double M,
int flags=CV INTER LINEAR+CV WARP FILL OUTLIERS );
CV WARP FILL OUTLIERS fills all of the destination image pixels. If some of them corre-
spond to outliers in the source image, they are set to zero
CV WARP INVERSE MAP See below
The function cvLogPolar transforms the source image using the following transformation:
Forward transformation (CV WARP INVERSE MAP is not set):
dst(φ, ρ) = src(x, y)
Inverse transformation (CV WARP INVERSE MAP is set):
dst(x, y) = src(φ, ρ)
where
p
ρ = M · log x2 + y 2 , φ = atan(y/x)
The function emulates the human ”foveal” vision and can be used for fast scale and rotation-
invariant template matching, for object tracking and so forth. The function can not operate in-place.
#include <cv.h>
#include <highgui.h>
cvWaitKey();
}
return 0;
}
it
void cvRemap(
const CvArr* src,
CvArr* dst,
const CvArr* mapx,
const CvArr* mapy,
int flags=CV INTER LINEAR+CV WARP FILL OUTLIERS,
CvScalar fillval=cvScalarAll(0) );
CV WARP FILL OUTLIERS fills all of the destination image pixels. If some of them corre-
spond to outliers in the source image, they are set to fillval
The function cvRemap transforms the source image using the specified map:
void cvResize(
const CvArr* src,
CvArr* dst,
int interpolation=CV INTER LINEAR );
The function cvResize resizes an image src so that it fits exactly into dst. If ROI is set, the
function considers the ROI as supported.
2.3. GEOMETRIC IMAGE TRANSFORMATIONS 281
void cvWarpAffine(
const CvArr* src,
CvArr* dst,
const CvMat* mapMatrix,
int flags=CV INTER LINEAR+CV WARP FILL OUTLIERS,
CvScalar fillval=cvScalarAll(0) );
CV WARP FILL OUTLIERS fills all of the destination image pixels; if some of them corre-
spond to outliers in the source image, they are set to fillval
CV WARP INVERSE MAP indicates that matrix is inversely transformed from the destination
image to the source and, thus, can be used directly for pixel interpolation. Otherwise,
the function finds the inverse transform from mapMatrix
The function cvWarpAffine transforms the source image using the specified matrix:
dst(x0 , y 0 ) = src(x, y)
where
0 x
x
= mapMatrix · y if CV WARP INVERSE MAP is not set
y0
10
x
x
= mapMatrix · y 0 otherwise
y
1
282 CHAPTER 2. IMGPROC. IMAGE PROCESSING
The function is similar to cvGetQuadrangleSubPix but they are not exactly the same. cvWarpAffine
requires input and output image have the same data type, has larger overhead (so it is not quite
suitable for small images) and can leave part of destination image unchanged. While cvGetQuad-
rangleSubPix may extract quadrangles from 8-bit images into floating-point buffer, has smaller
overhead and always changes the whole destination image content. Note that the function can not
operate in-place.
To transform a sparse set of points, use the cvTransform function from cxcore.
void cvWarpPerspective(
const CvArr* src,
CvArr* dst,
const CvMat* mapMatrix,
int flags=CV INTER LINEAR+CV WARP FILL OUTLIERS,
CvScalar fillval=cvScalarAll(0) );
CV WARP FILL OUTLIERS fills all of the destination image pixels; if some of them corre-
spond to outliers in the source image, they are set to fillval
CV WARP INVERSE MAP indicates that matrix is inversely transformed from the destination
image to the source and, thus, can be used directly for pixel interpolation. Otherwise,
the function finds the inverse transform from mapMatrix
The function cvWarpPerspective transforms the source image using the specified matrix:
2.4. MISCELLANEOUS IMAGE TRANSFORMATIONS 283
0 x
x
= mapMatrix · y if CV WARP INVERSE MAP is not set
y0
10
x
x
= mapMatrix · y 0 otherwise
y
1
Note that the function can not operate in-place. For a sparse set of points use the cvPerspec-
tiveTransform function from CxCore.
void cvAdaptiveThreshold(
const CvArr* src,
CvArr* dst,
double maxValue,
int adaptive method=CV ADAPTIVE THRESH MEAN C,
int thresholdType=CV THRESH BINARY,
int blockSize=3,
double param1=5 );
maxValue Maximum value that is used with CV THRESH BINARY and CV THRESH BINARY INV
blockSize The size of a pixel neighborhood that is used to calculate a threshold value for the
pixel: 3, 5, 7, and so on
param1 The method-dependent parameter. For the methods CV ADAPTIVE THRESH MEAN C
and CV ADAPTIVE THRESH GAUSSIAN C it is a constant subtracted from the mean or weighted
mean (see the discussion), though it may be negative
The function transforms a grayscale image to a binary image according to the formulas:
CV THRESH BINARY
maxValue if src(x, y) > T (x, y)
dst(x, y) =
0 otherwise
CV THRESH BINARY INV
0 if src(x, y) > T (x, y)
dst(x, y) =
maxValue otherwise
void cvCvtColor(
const CvArr* src,
CvArr* dst,
int code );
src The source 8-bit (8u), 16-bit (16u) or single-precision floating-point (32f) image
dst The destination image of the same data type as the source. The number of channels may be
different
code Color conversion operation that can be specifed using CV src color space 2 dst color space
constants (see below)
2.4. MISCELLANEOUS IMAGE TRANSFORMATIONS 285
The function converts the input image from one color space to another. The function ignores
the colorModel and channelSeq fields of the IplImage header, so the source image color
space should be specified correctly (including order of the channels in the case of RGB space.
For example, BGR means 24-bit format with B0 , G0 , R0 , B1 , G1 , R1 , ... layout whereas RGB means
24-format with R0 , G0 , B0 , R1 , G1 , B1 , ... layout).
The conventional range for R,G,B channel values is:
Of course, in the case of linear transformations the range can be specific, but in order to get
correct results in the case of non-linear transformations, the input image should be scaled.
The function can do the following transformations:
• Transformations within RGB space like adding/removing the alpha channel, reversing the
channel order, conversion to/from 16-bit RGB color (R5:G6:B5 or R5:G5:B5), as well as
conversion to/from grayscale using:
and
Gray to RGB[A]:R ← Y, G ← Y, B ← Y, A ← 0
• RGB ↔ CIE [Link] 709 with D65 white point (CV BGR2XYZ, CV RGB2XYZ, CV XYZ2BGR,
CV XYZ2RGB):
X 0.412453 0.357580 0.180423 R
Y ← 0.212671 0.715160 0.072169 · G
Z 0.019334 0.119193 0.950227 B
R 3.240479 −1.53715 −0.498535 X
G ← −0.969256 1.875991 0.041556 · Y
B 0.055648 −0.204043 1.057311 Z
X, Y and Z cover the whole value range (in the case of floating-point images Z may exceed
1).
286 CHAPTER 2. IMGPROC. IMAGE PROCESSING
V −min(R,G,B)
if V 6= 0
S← V
0 otherwise
60(G − B)/S if V = R
H← 120 + 60(B − R)/S if V = G
240 + 60(R − G)/S if V = B
8-bit images
V ← 255V, S ← 255S, H ← H/2(to fit to 0 to 255)
16-bit images (currently not supported)
8-bit images
V ← 255V, S ← 255S, H ← H/2(to fit to 0 to 255)
• RGB ↔ CIE L*a*b* (CV BGR2Lab, CV RGB2Lab, CV Lab2BGR, CV Lab2RGB) in the case
of 8-bit and 16-bit images R, G and B are converted to floating-point format and scaled to fit
the 0 to 1 range
X 0.412453 0.357580 0.180423 R
Y ← 0.212671 0.715160 0.072169 · G
Z 0.019334 0.119193 0.950227 B
X ← X/Xn , whereXn = 0.950456
Z ← Z/Zn , whereZn = 1.088754
116 ∗ Y 1/3 − 16 for Y > 0.008856
L←
903.3 ∗ Y for Y ≤ 0.008856
a ← 500(f (X) − f (Y )) + delta
b ← 200(f (Y ) − f (Z)) + delta
288 CHAPTER 2. IMGPROC. IMAGE PROCESSING
where
t1/3
for t > 0.008856
f (t) =
7.787t + 16/116 for t <= 0.008856
and
128 for 8-bit images
delta =
0 for floating-point images
On output 0 ≤ L ≤ 100, −127 ≤ a ≤ 127, −127 ≤ b ≤ 127
The values are then converted to the destination data type:
8-bit images
L ← L ∗ 255/100, a ← a + 128, b ← b + 128
• RGB ↔ CIE L*u*v* (CV BGR2Luv, CV RGB2Luv, CV Luv2BGR, CV Luv2RGB) in the case
of 8-bit and 16-bit images R, G and B are converted to floating-point format and scaled to fit
0 to 1 range
X 0.412453 0.357580 0.180423 R
Y ← 0.212671 0.715160 0.072169 · G
Z 0.019334 0.119193 0.950227 B
8-bit images
The above formulas for converting RGB to/from various color spaces have been taken from
multiple sources on Web, primarily from the Ford98 at the Charles Poynton site.
R G R G R
G B G B G
R G R G R
G B G B G
R G R G R
The output RGB components of a pixel are interpolated from 1, 2 or 4 neighbors of the pixel
having the same color. There are several modifications of the above pattern that can be
achieved by shifting the pattern one pixel left and/or one pixel up. The two letters C1 and
C2 in the conversion constants CV Bayer C1 C2 2BGR and CV Bayer C1 C2 2RGB indicate
the particular pattern type - these are components from the second row, second and third
columns, respectively. For example, the above pattern has very popular ”BG” type.
void cvDistTransform(
const CvArr* src,
CvArr* dst,
int distance type=CV DIST L2,
int mask size=3,
const float* mask=NULL,
CvArr* labels=NULL );
distance type Type of distance; can be CV DIST L1, CV DIST L2, CV DIST C or CV DIST USER
mask size Size of the distance transform mask; can be 3 or 5. in the case of CV DIST L1 or
CV DIST C the parameter is forced to 3, because a 3 × 3 mask gives the same result as a
5 × 5 yet it is faster
mask User-defined mask in the case of a user-defined distance, it consists of 2 numbers (hor-
izontal/vertical shift cost, diagonal shift cost) in the case ofa 3 × 3 mask and 3 numbers
(horizontal/vertical shift cost, diagonal shift cost, knight’s move cost) in the case of a 5 × 5
mask
labels The optional output 2d array of integer type labels, the same size as src and dst
The function calculates the approximated distance from every binary image pixel to the nearest
zero pixel. For zero pixels the function sets the zero distance, for others it finds the shortest path
consisting of basic shifts: horizontal, vertical, diagonal or knight’s move (the latest is available for
a 5 × 5 mask). The overall distance is calculated as a sum of these basic distances. Because the
distance function should be symmetric, all of the horizontal and vertical shifts must have the same
cost (that is denoted as a), all the diagonal shifts must have the same cost (denoted b), and all
knight’s moves must have the same cost (denoted c). For CV DIST C and CV DIST L1 types the
distance is calculated precisely, whereas for CV DIST L2 (Euclidian distance) the distance can be
calculated only with some relative error (a 5 × 5 mask gives more accurate results), OpenCV uses
the values suggested in [4]:
CV DIST C (3 × 3) a = 1, b = 1
CV DIST L1 (3 × 3) a = 1, b = 2
CV DIST L2 (3 × 3) a=0.955, b=1.3693
CV DIST L2 (5 × 5) a=1, b=1.4, c=2.1969
And below are samples of the distance field (black (0) pixel is in the middle of white square) in
the case of a user-defined distance:
User-defined 3 × 3 mask (a=1, b=1.5)
4.5 4 3.5 3 3.5 4 4.5
4 3 2.5 2 2.5 3 4
3.5 2.5 1.5 1 1.5 2.5 3.5
3 2 1 1 2 3
3.5 2.5 1.5 1 1.5 2.5 3.5
4 3 2.5 2 2.5 3 4
4.5 4 3.5 3 3.5 4 4.5
User-defined 5 × 5 mask (a=1, b=1.5, c=2)
2.4. MISCELLANEOUS IMAGE TRANSFORMATIONS 291
void cvFloodFill(
CvArr* image,
CvPoint seed point,
CvScalar new val,
CvScalar lo diff=cvScalarAll(0),
CvScalar up diff=cvScalarAll(0),
CvConnectedComp* comp=NULL,
int flags=4,
CvArr* mask=NULL );
292 CHAPTER 2. IMGPROC. IMAGE PROCESSING
image Input 1- or 3-channel, 8-bit or floating-point image. It is modified by the function unless the
CV FLOODFILL MASK ONLY flag is set (see below)
seed point The starting point
new val New value of the repainted domain pixels
lo diff Maximal lower brightness/color difference between the currently observed pixel and one
of its neighbors belonging to the component, or a seed pixel being added to the component.
In the case of 8-bit color images it is a packed value
up diff Maximal upper brightness/color difference between the currently observed pixel and one
of its neighbors belonging to the component, or a seed pixel being added to the component.
In the case of 8-bit color images it is a packed value
comp Pointer to the structure that the function fills with the information about the repainted do-
main. Note that the function does not fill comp->contour field. The boundary of the filled
component can be retrieved from the output mask image using cvFindContours
flags The operation flags. Lower bits contain connectivity value, 4 (by default) or 8, used within
the function. Connectivity determines which neighbors of a pixel are considered. Upper bits
can be 0 or a combination of the following flags:
CV FLOODFILL FIXED RANGE if set, the difference between the current pixel and seed
pixel is considered, otherwise the difference between neighbor pixels is considered (the
range is floating)
CV FLOODFILL MASK ONLY if set, the function does not fill the image (new val is ignored),
but fills the mask (that must be non-NULL in this case)
mask Operation mask, should be a single-channel 8-bit image, 2 pixels wider and 2 pixels taller
than image. If not NULL, the function uses and updates the mask, so the user takes re-
sponsibility of initializing the mask content. Floodfilling can’t go across non-zero pixels in the
mask, for example, an edge detector output can be used as a mask to stop filling at edges.
It is possible to use the same mask in multiple calls to the function to make sure the filled
area do not overlap. Note: because the mask is larger than the filled image, a pixel in mask
that corresponds to (x, y) pixel in image will have coordinates (x + 1, y + 1)
The function fills a connected component starting from the seed point with the specified color.
The connectivity is determined by the closeness of pixel values. The pixel at (x, y) is considered
to belong to the repainted domain if:
src(seed.x, seed.y)r − lo diffr <= src(x, y)r <= src(seed.x, seed.y)r + up diffr
src(seed.x, seed.y)g − lo diffg <= src(x, y)g <= src(seed.x, seed.y)g + up diffg
src(seed.x, seed.y)b − lo diffb <= src(x, y)b <= src(seed.x, seed.y)b + up diffb
where src(x0 , y 0 ) is the value of one of pixel neighbors. That is, to be added to the connected
component, a pixel’s color/brightness should be close enough to the:
• color/brightness of one of its neighbors that are already referred to the connected component
in the case of floating range
void cvInpaint(
const CvArr* src,
const CvArr* mask,
CvArr* dst,
double inpaintRadius,
int flags);
mask The inpainting mask, 8-bit 1-channel image. Non-zero pixels indicate the area that needs
to be inpainted.
dst The output image of the same format and the same size as input.
inpaintRadius The radius of circlular neighborhood of each point inpainted that is considered
by the algorithm.
The function reconstructs the selected image area from the pixel near the area boundary.
The function may be used to remove dust and scratches from a scanned photo, or to remove
undesirable objects from still images or video.
void cvIntegral(
const CvArr* image,
CvArr* sum,
CvArr* sqsum=NULL,
CvArr* tiltedSum=NULL );
sum The integral image, (W + 1) × (H + 1), 32-bit integer or double precision floating-point (64f)
sqsum The integral image for squared pixel values, (W + 1) × (H + 1), double precision floating-
point (64f)
tiltedSum The integral for the image rotated by 45 degrees, (W + 1) × (H + 1), the same data
type as sum
The function calculates one or more integral images for the source image as following:
2.4. MISCELLANEOUS IMAGE TRANSFORMATIONS 295
X
sum(X, Y ) = image(x, y)
x<X,y<Y
X
sqsum(X, Y ) = image(x, y)2
x<X,y<Y
X
tiltedSum(X, Y ) = image(x, y)
y<Y,abs(x−X+1)≤Y −y−1
Using these integral images, one may calculate sum, mean and standard deviation over a
specific up-right or rotated rectangular region of the image in a constant time, for example:
X
= sum(x2 , y2 ) − sum(x1 , y2 ) − sum(x2 , y1 ) + sum(x1 , x1 )
x1 <=x<x2 , y1 <=y<y2
It makes possible to do a fast blurring or fast block correlation with variable window size, for
example. In the case of multi-channel images, sums for each channel are accumulated indepen-
dently.
void cvPyrMeanShiftFiltering(
const CvArr* src,
CvArr* dst,
double sp,
double sr,
int max level=1,
CvTermCriteria termcrit=
cvTermCriteria(CV TERMCRIT ITER+CV TERMCRIT EPS,5,1));
dst The destination image of the same format and the same size as the source.
void cvPyrSegmentation(
IplImage* src,
IplImage* dst,
CvMemStorage* storage,
CvSeq** comp,
int level,
double threshold1,
double threshold2 );
2.4. MISCELLANEOUS IMAGE TRANSFORMATIONS 297
The function implements image segmentation by pyramids. The pyramid builds up to the level
level. The links between any pixel a on level i and its candidate father pixel b on the adjacent
level are established if p(c(a), c(b)) < threshold1. After the connected components are defined,
they are joined into several clusters. Any two segments A and B belong to the same cluster, if
p(c(A), c(B)) < threshold2. If the input image has only one channel, then p(c1 , c2 ) = |c1 − c2 |. If
the input image has three channels (red, green and blue), then
There may be more than one connected component per a cluster. The images src and dst
should be 8-bit single-channel or 3-channel images or equal size.
double cvThreshold(
const CvArr* src,
CvArr* dst,
double threshold,
double maxValue,
int thresholdType );
298 CHAPTER 2. IMGPROC. IMAGE PROCESSING
dst Destination array; must be either the same type as src or 8-bit
maxValue Maximum value to use with CV THRESH BINARY and CV THRESH BINARY INV thresh-
olding types
The function applies fixed-level thresholding to a single-channel array. The function is typi-
cally used to get a bi-level (binary) image out of a grayscale image ( cvCmpS could be also used
for this purpose) or for removing a noise, i.e. filtering out pixels with too small or too large val-
ues. There are several types of thresholding that the function supports that are determined by
thresholdType:
CV THRESH BINARY
maxValue if src(x, y) > threshold
dst(x, y) =
0 otherwise
CV THRESH TRUNC
threshold if src(x, y) > threshold
dst(x, y) =
src(x, y) otherwise
CV THRESH TOZERO
src(x, y) if src(x, y) > threshold
dst(x, y) =
0 otherwise
Also, the special value CV THRESH OTSU may be combined with one of the above values. In
this case the function determines the optimal threshold value using Otsu’s algorithm and uses it
instead of the specified thresh. The function returns the computed threshold value. Currently,
Otsu’s method is implemented only for 8-bit images.
CvSeq* cvApproxChains(
CvSeq* src seq,
CvMemStorage* storage,
int method=CV CHAIN APPROX SIMPLE,
300 CHAPTER 2. IMGPROC. IMAGE PROCESSING
double parameter=0,
int minimal perimeter=0,
int recursive=0 );
src seq Pointer to the chain that can refer to other chains
minimal perimeter Approximates only those contours whose perimeters are not less than
minimal perimeter. Other chains are removed from the resulting structure
recursive If not 0, the function approximates all chains that access can be obtained to from
src seq by using the h next or v next links. If 0, the single chain is approximated
CvSeq* cvApproxPoly(
const void* src seq,
int header size,
CvMemStorage* storage,
int method,
double parameter,
int parameter2=0 );
storage Container for the approximated contours. If it is NULL, the input sequences’ storage is
used
method Approximation method; only CV POLY APPROX DP is supported, that corresponds to the
Douglas-Peucker algorithm
parameter2 If case if src seq is a sequence, the parameter determines whether the single se-
quence should be approximated or all sequences on the same level or below src seq (see
cvFindContours for description of hierarchical contour structures). If src seq is an array
CvMat* of points, the parameter specifies whether the curve is closed (parameter2!=0) or
not (parameter2 =0)
The function approximates one or more curves and returns the approximation result[s]. In
the case of multiple curves, the resultant tree will have the same structure as the input one (1:1
correspondence).
double cvArcLength(
const void* curve,
CvSlice slice=CV WHOLE SEQ,
int isClosed=-1 );
slice Starting and ending points of the curve, by default, the whole curve length is calculated
isClosed Indicates whether the curve is closed or not. There are 3 cases:
• isClosed < 0 if curve is sequence, the flag CV SEQ FLAG CLOSED of ((CvSeq*)curve)->flags
is checked to determine if the curve is closed or not, otherwise (curve is represented by
array (CvMat*) of points) it is assumed to be unclosed.
The function calculates the length or curve as the sum of lengths of segments between subse-
quent points
void cvBoxPoints(
CvBox2D box,
CvPoint2D32f pt[4] );
2.5. STRUCTURAL ANALYSIS AND SHAPE DESCRIPTORS 303
box Box
contour Input contour. Currently, only integer point coordinates are allowed
is transposed relatively to the cvIivarninen97 definition). The histogram can be used for contour
matching.
float cvCalcEMD2(
const CvArr* signature1,
const CvArr* signature2,
int distance type,
CvDistanceFunction distance func=NULL,
const CvArr* cost matrix=NULL,
CvArr* flow=NULL,
float* lower bound=NULL,
void* userdata=NULL );
signature1 First signature, a size1 × dims + 1 floating-point matrix. Each row stores the point
weight followed by the point coordinates. The matrix is allowed to have a single column
(weights only) if the user-defined cost matrix is used
signature2 Second signature of the same format as signature1, though the number of rows
may be different. The total weights may be different, in this case an extra ”dummy” point is
added to either signature1 or signature2
distance type Metrics used; CV DIST L1, CV DIST L2, and CV DIST C stand for one of the
standard metrics; CV DIST USER means that a user-defined function distance func or
pre-calculated cost matrix is used
distance func The user-supplied distance function. It takes coordinates of two points and re-
turns the distance between the points typedef float (*CvDistanceFunction)(const
float* f1, const float* f2, void* userdata);
cost matrix The user-defined size1 × size2 cost matrix. At least one of cost matrix and
distance func must be NULL. Also, if a cost matrix is used, lower boundary (see below)
can not be calculated, because it needs a metric function
flow The resultant size1 × size2 flow matrix: flowi,j is a flow from i th point of signature1
to j th point of signature2
2.5. STRUCTURAL ANALYSIS AND SHAPE DESCRIPTORS 305
lower bound Optional input/output parameter: lower boundary of distance between the two sig-
natures that is a distance between mass centers. The lower boundary may not be calculated
if the user-defined cost matrix is used, the total weights of point configurations are not equal,
or if the signatures consist of weights only (i.e. the signature matrices have a single column).
The user must initialize *lower bound. If the calculated distance between mass centers is
greater or equal to *lower bound (it means that the signatures are far enough) the function
does not calculate EMD. In any case *lower bound is set to the calculated distance be-
tween mass centers on return. Thus, if user wants to calculate both distance between mass
centers and EMD, *lower bound should be set to 0
userdata Pointer to optional data that is passed into the user-defined distance function
The function computes the earth mover distance and/or a lower boundary of the distance
between the two weighted point configurations. One of the applications described in cvRubn-
erSept98 is multi-dimensional histogram comparison for image retrieval. EMD is a a transporta-
tion problem that is solved using some modification of a simplex algorithm, thus the complexity is
exponential in the worst case, though, on average it is much faster. In the case of a real metric
the lower boundary can be calculated even faster (using linear-time algorithm) and it can be used
to determine roughly whether the two signatures are far enough so that they cannot relate to the
same object.
The function tests whether the input contour is convex or not. The contour must be simple,
without self-intersections.
double cvContourArea(
const CvArr* contour,
CvSlice slice=CV WHOLE SEQ );
slice Starting and ending points of the contour section of interest, by default, the area of the
whole contour is calculated
2.5. STRUCTURAL ANALYSIS AND SHAPE DESCRIPTORS 307
The function calculates the area of a whole contour or a contour section. In the latter case the
total area bounded by the contour arc and the chord connecting the 2 selected points is calculated
as shown on the picture below:
Orientation of the contour affects the area sign, thus the function may return a negative result.
Use the fabs() function from C runtime to get the absolute value of the area.
CvSeq* cvContourFromContourTree(
const CvContourTree* tree,
CvMemStorage* storage,
CvTermCriteria criteria );
The function restores the contour from its binary tree representation. The parameter criteria
determines the accuracy and/or the number of tree levels used for reconstruction, so it is possible
to build an approximated contour. The function returns the reconstructed contour.
308 CHAPTER 2. IMGPROC. IMAGE PROCESSING
CvSeq* cvConvexHull2(
const CvArr* input,
void* storage=NULL,
int orientation=CV CLOCKWISE,
int return points=0 );
storage The destination array (CvMat*) or memory storage (CvMemStorage*) that will store the
convex hull. If it is an array, it should be 1d and have the same number of elements as the
input array/sequence. On output the header is modified as to truncate the array down to the
hull size. If storage is NULL then the convex hull will be stored in the same storage as the
input sequence
return points If non-zero, the points themselves will be stored in the hull instead of indices if
storage is an array, or pointers if storage is memory storage
The function finds the convex hull of a 2D point set using Sklansky’s algorithm. If storage is
memory storage, the function creates a sequence containing the hull points or pointers to them,
depending on return points value and returns the sequence on output. If storage is a CvMat,
the function returns NULL.
Example. Building convex hull for a sequence or array of points
#include "cv.h"
#include "highgui.h"
#include <stdlib.h>
#if !ARRAY
CvMemStorage* storage = cvCreateMemStorage();
#endif
for(;;)
{
int i, count = rand()%100 + 1, hullcount;
CvPoint pt0;
#if !ARRAY
CvSeq* ptseq = cvCreateSeq( CV_SEQ_KIND_GENERIC|CV_32SC2,
sizeof(CvContour),
sizeof(CvPoint),
storage );
CvSeq* hull;
#endif
cvCircle( img, pt0, 2, CV_RGB( 255, 0, 0 ), CV_FILLED );
}
#if !ARRAY
pt0 = **CV_GET_SEQ_ELEM( CvPoint*, hull, hullcount - 1 );
#else
pt0 = points[hull[hullcount-1]];
#endif
#if !ARRAY
cvClearMemStorage( storage );
#else
free( points );
free( hull );
#endif
}
}
CvSeq* cvConvexityDefects(
2.5. STRUCTURAL ANALYSIS AND SHAPE DESCRIPTORS 311
convexhull Convex hull obtained using cvConvexHull2 that should contain pointers or indices
to the contour points, not the hull points themselves (the return points parameter in
cvConvexHull2 should be 0)
storage Container for the output sequence of convexity defects. If it is NULL, the contour or hull
(in that order) storage is used
The function finds all convexity defects of the input contour and returns a sequence of the
CvConvexityDefect structures.
CvContourTree* cvCreateContourTree(
const CvSeq* contour,
CvMemStorage* storage,
double threshold );
The function creates a binary tree representation for the input contour and returns the pointer
to its root. If the parameter threshold is less than or equal to 0, the function creates a full binary
tree representation. If the threshold is greater than 0, the function creates a representation with
the precision threshold: if the vertices with the interceptive area of its base line are less than
threshold, the tree should not be built any further. The function returns the created tree.
312 CHAPTER 2. IMGPROC. IMAGE PROCESSING
CvSeq* cvEndFindContours(
CvContourScanner* scanner );
The function finishes the scanning process and returns a pointer to the first contour on the
highest level.
int cvFindContours(
CvArr* image,
CvMemStorage* storage,
CvSeq** first contour,
int header size=sizeof(CvContour),
int mode=CV RETR LIST,
int method=CV CHAIN APPROX SIMPLE,
CvPoint offset=cvPoint(0,0) );
image The source, an 8-bit single channel image. Non-zero pixels are treated as 1’s, zero pixels
remain 0’s - the image is treated as binary. To get such a binary image from grayscale,
one may use cvThreshold, cvAdaptiveThreshold or cvCanny. The function modifies the
source image’s content
first contour Output parameter, will contain the pointer to the first outer contour
header size Size of the sequence header, ≥ sizeof(CvChain) if method = CV CHAIN CODE,
and ≥ sizeof(CvContour) otherwise
2.5. STRUCTURAL ANALYSIS AND SHAPE DESCRIPTORS 313
method Approximation method (for all the modes, except CV LINK RUNS, which uses built-in
approximation)
CV CHAIN CODE outputs contours in the Freeman chain code. All other methods output
polygons (sequences of vertices)
CV CHAIN APPROX NONE translates all of the points from the chain code into points
CV CHAIN APPROX SIMPLE compresses horizontal, vertical, and diagonal segments and
leaves only their end points
CV CHAIN APPROX TC89 L1,CV CHAIN APPROX TC89 KCOS applies one of the flavors of
the Teh-Chin chain approximation algorithm.
CV LINK RUNS uses a completely different contour retrieval algorithm by linking horizontal
segments of 1’s. Only the CV RETR LIST retrieval mode can be used with this method.
offset Offset, by which every contour point is shifted. This is useful if the contours are extracted
from the image ROI and then they should be analyzed in the whole image context
The function retrieves contours from the binary image using the algorithm [19]. The contours
are a useful tool for shape analysis and object detection and recognition.
The function retrieves contours from the binary image and returns the number of retrieved
contours. The pointer first contour is filled by the function. It will contain a pointer to the
first outermost contour or NULL if no contours are detected (if the image is completely black).
Other contours may be reached from first contour using the h next and v next links. The
sample in the cvDrawContours discussion shows how to use contours for connected component
detection. Contours can be also used for shape analysis and object recognition - see squares.c
in the OpenCV sample directory.
Note: the source image is modified by this function.
314 CHAPTER 2. IMGPROC. IMAGE PROCESSING
CvSeq* cvFindNextContour(
CvContourScanner scanner );
The function locates and retrieves the next contour in the image and returns a pointer to it. The
function returns NULL if there are no more contours.
CvBox2D cvFitEllipse2(
const CvArr* points );
The function calculates the ellipse that fits best (in least-squares sense) around a set of 2D
points. The meaning of the returned structure fields is similar to those in cvEllipse except that
size stores the full lengths of the ellipse axises, not half-lengths.
void cvFitLine(
const CvArr* points,
int dist type,
double param,
2.5. STRUCTURAL ANALYSIS AND SHAPE DESCRIPTORS 315
double reps,
double aeps,
float* line );
dist type The distance used for fitting (see the discussion)
param Numerical parameter (C) for some types of distances, if 0 then some optimal value is
chosen
reps Sufficient accuracy for the radius (distance between the coordinate origin and the line). 0.01
is a good default value.
aeps Sufficient accuracy for the angle. 0.01 is a good default value.
line The output line parameters. In the case of a 2d fitting, it is an array of 4 floats (vx,
vy, x0, y0) where (vx, vy) is a normalized vector collinear to the line and (x0, y0)
is some point on the line. in the case of a 3D fitting it is an array of 6 floats (vx, vy,
vz, x0, y0, z0) where (vx, vy, vz) is a normalized vector collinear to the line and
(x0, y0, z0) is some point on the line
P
The function fits a line to a 2D or 3D point set by minimizing i ρ(ri ) where ri is the distance
between the i th point and the line and ρ(r) is a distance function, one of:
C2
r 2
ρ (r) = · 1 − exp − where C = 2.9846
2 C
r2 /2
if r < C
ρ(r) = where C = 1.345
C · (r − C/2) otherwise
double cvGetCentralMoment(
CvMoments* moments,
int x order,
int y order );
y order y order of the retrieved moment, y order >= 0 and x order + y order <= 3
The function retrieves the central moment, which in the case of image moments is defined as:
X
µx order, y order = (I(x, y) · (x − xc )x order · (y − yc )y order )
x,y
M10 M01
xc = , yc =
M00 M00
double cvGetNormalizedCentralMoment(
CvMoments* moments,
int x order,
int y order );
y order y order of the retrieved moment, y order >= 0 and x order + y order <= 3
The function retrieves the normalized central moment:
µx order, y order
ηx order, y order = (y order+x order)/2+1
M00
double cvGetSpatialMoment(
CvMoments* moments,
int x order,
int y order );
double cvMatchContourTrees(
const CvContourTree* tree1,
const CvContourTree* tree2,
int method,
double threshold );
2.5. STRUCTURAL ANALYSIS AND SHAPE DESCRIPTORS 319
The function calculates the value of the matching measure for two contour trees. The similarity
measure is calculated level by level from the binary tree roots. If at a certain level the difference
between contours becomes less than threshold, the reconstruction process is interrupted and
the current difference is returned.
double cvMatchShapes(
const void* object1,
const void* object2,
int method,
double parameter=0 );
method Comparison method; CV CONTOUR MATCH I1, CV CONTOURS MATCH I2 or CV CONTOURS MATCH I3
The function compares two shapes. The 3 implemented methods all use Hu moments (see
cvGetHuMoments) (A is object1, B is object2):
where
mA A A B B B
i = sign(hi ) · log hi mi = sign(hi ) · log hi
and hA B
i , hi are the Hu moments of A and B respectively.
CvBox2D cvMinAreaRect2(
const CvArr* points,
CvMemStorage* storage=NULL );
The function finds a circumscribed rectangle of the minimal area for a 2D point set by building
a convex hull for the set and applying the rotating calipers technique to the hull.
Picture. Minimal-area bounding rectangle for contour
2.5. STRUCTURAL ANALYSIS AND SHAPE DESCRIPTORS 321
int cvMinEnclosingCircle(
const CvArr* points,
CvPoint2D32f* center,
float* radius );
The function finds the minimal circumscribed circle for a 2D point set using an iterative algo-
rithm. It returns nonzero if the resultant circle contains all the input points and zero otherwise (i.e.
the algorithm failed).
322 CHAPTER 2. IMGPROC. IMAGE PROCESSING
void cvMoments(
const CvArr* arr,
CvMoments* moments,
int binary=0 );
arr Image (1-channel or 3-channel with COI set) or polygon (CvSeq of points or a vector of
points)
binary (For images only) If the flag is non-zero, all of the zero pixel values are treated as zeroes,
and all of the others are treated as 1’s
The function calculates spatial and central moments up to the third order and writes them to
moments. The moments may then be used then to calculate the gravity center of the shape, its
area, main axises and various shape characeteristics including 7 Hu invariants.
double cvPointPolygonTest(
const CvArr* contour,
CvPoint2D32f pt,
int measure dist );
measure dist If it is non-zero, the function estimates the distance from the point to the nearest
contour edge
2.5. STRUCTURAL ANALYSIS AND SHAPE DESCRIPTORS 323
The function determines whether the point is inside a contour, outside, or lies on an edge
(or coinsides with a vertex). It returns positive, negative or zero value, correspondingly. When
measure dist = 0, the return value is +1, -1 and 0, respectively. When measure dist 6= 0, it is
a signed distance between the point and the nearest contour edge.
Here is the sample output of the function, where each image pixel is tested against the contour.
CvSeq* cvPointSeqFromMat(
int seq kind,
const CvArr* mat,
CvContour* contour header,
CvSeqBlock* block );
seq kind Type of the point sequence: point set (0), a curve (CV SEQ KIND CURVE), closed curve
(CV SEQ KIND CURVE+CV SEQ FLAG CLOSED) etc.
mat Input matrix. It should be a continuous, 1-dimensional vector of points, that is, it should have
type CV 32SC2 or CV 32FC2
324 CHAPTER 2. IMGPROC. IMAGE PROCESSING
cvDrawContours( img,
cvPointSeqFromMat(CV_SEQ_KIND_CURVE+CV_SEQ_FLAG_CLOSED,
vector,
&header,
&block),
CV_RGB(255,0,0),
CV_RGB(255,0,0),
0, 3, 8, cvPoint(0,0));
CvContourScanner cvStartFindContours(
CvArr* image,
CvMemStorage* storage,
int header size=sizeof(CvContour),
int mode=CV RETR LIST,
int method=CV CHAIN APPROX SIMPLE,
CvPoint offset=cvPoint(0,
0) );
header size Size of the sequence header, >= sizeof (CvChain) if method =CV CHAIN CODE,
and >= sizeof (CvContour) otherwise
method Approximation method. It has the same meaning in cvFindContours, but CV LINK RUNS
can not be used here
The function initializes and returns a pointer to the contour scanner. The scanner is used in
cvFindNextContour to retrieve the rest of the contours.
void cvSubstituteContour(
CvContourScanner scanner,
CvSeq* new contour );
The function replaces the retrieved contour, that was returned from the preceding call of
cvFindNextContour and stored inside the contour scanner state, with the user-specified contour.
The contour is inserted into the resulting structure, list, two-level hierarchy, or tree, depending on
the retrieval mode. If the parameter new contour is NULL, the retrieved contour is not included
in the resulting structure, nor are any of its children that might be added to this structure later.
where the points are linked together and form a planar graph, which, together with a few edges
connecting the exterior subdivision points (namely, convex hull points) with infinity, subdivides a
plane into facets by its edges.
For every subdivision there exists a dual subdivision in which facets and points (subdivision
vertices) swap their roles, that is, a facet is treated as a vertex (called a virtual point below) of the
dual subdivision and the original subdivision vertices become facets. On the picture below original
subdivision is marked with solid lines and dual subdivision with dotted lines.
OpenCV subdivides a plane into triangles using Delaunay’s algorithm. Subdivision is built
iteratively starting from a dummy triangle that includes all the subdivision points for sure. In this
case the dual subdivision is a Voronoi diagram of the input 2d point set. The subdivisions can
be used for the 3d piece-wise transformation of a plane, morphing, fast location of points on the
plane, building special graphs (such as NNG,RNG) and so forth.
CvQuadEdge2D;
Quad-edge is a basic element of subdivision containing four edges (e, eRot, reversed e and
reversed eRot):
id This integer can be used to index auxillary data associated with each vertex of the planar
subdivision
2.6. PLANAR SUBDIVISIONS 329
void cvCalcSubdivVoronoi2D(
CvSubdiv2D* subdiv );
subdiv Delaunay subdivision, in which all the points are already added
The function calculates the coordinates of virtual points. All virtual points corresponding to
some vertex of the original subdivision form (when connected together) a boundary of the Voronoi
cell at that point.
The function removes all of the virtual points. It is called internally in cvCalcSubdivVoronoi2D
if the subdivision was modified after previous call to the function.
CvSubdiv2D* cvCreateSubdivDelaunay2D(
CvRect rect,
CvMemStorage* storage );
330 CHAPTER 2. IMGPROC. IMAGE PROCESSING
rect Rectangle that includes all of the 2d points that are to be added to the subdivision
The function creates an empty Delaunay subdivision, where 2d points can be added using the
function cvSubdivDelaunay2DInsert. All of the points to be added must be within the specified
rectangle, otherwise a runtime error will be raised.
Note that the triangulation is a single large triangle that covers the given rectangle. Hence the
three vertices of this triangle are outside the rectangle rect.
CvSubdiv2DPoint* cvFindNearestPoint2D(
CvSubdiv2D* subdiv,
CvPoint2D32f pt );
pt Input point
The function is another function that locates the input point within the subdivision. It finds the
subdivision vertex that is the closest to the input point. It is not necessarily one of vertices of the
facet containing the input point, though the facet (located using cvSubdiv2DLocate) is used as a
starting point. The function returns a pointer to the found subdivision vertex.
CvSubdiv2DPoint* cvSubdiv2DEdgeDst(
CvSubdiv2DEdge edge );
The function returns the edge destination. The returned pointer may be NULL if the edge is
from dual subdivision and the virtual point coordinates are not calculated yet. The virtual points
can be calculated using the function cvCalcSubdivVoronoi2D.
type Specifies which of the related edges to return, one of the following:
CV NEXT AROUND ORG next around the edge origin (eOnext on the picture below if e is the
input edge)
CV PREV AROUND ORG previous around the edge origin (reversed eRnext)
CV PREV AROUND DST previous around the edge destination (reversed eLnext)
CV PREV AROUND LEFT previous around the left facet (reversed eOnext)
CV PREV AROUND RIGHT previous around the right facet (reversed eDnext)
332 CHAPTER 2. IMGPROC. IMAGE PROCESSING
The function returns one of the edges related to the input edge.
The function returns the next edge around the edge origin: eOnext on the picture above if e
is the input edge)
CvSubdiv2DPointLocation cvSubdiv2DLocate(
CvSubdiv2D* subdiv,
CvPoint2D32f pt,
CvSubdiv2DEdge* edge,
CvSubdiv2DPoint** vertex=NULL );
vertex Optional output vertex double pointer the input point coinsides with
The function locates the input point within the subdivision. There are 5 cases:
334 CHAPTER 2. IMGPROC. IMAGE PROCESSING
• The point falls into some facet. The function returns CV PTLOC INSIDE and *edge will
contain one of edges of the facet.
• The point falls onto the edge. The function returns CV PTLOC ON EDGE and *edge will
contain this edge.
• The point coincides with one of the subdivision vertices. The function returns CV PTLOC VERTEX
and *vertex will contain a pointer to the vertex.
• The point is outside the subdivsion reference rectangle. The function returns CV PTLOC OUTSIDE RECT
and no pointers are filled.
• One of input arguments is invalid. A runtime error is raised or, if silent or ”parent” error
processing mode is selected, CV PTLOC ERROR is returnd.
CvSubdiv2DEdge cvSubdiv2DRotateEdge(
CvSubdiv2DEdge edge,
int rotate );
rotate Specifies which of the edges of the same quad-edge as the input one to return, one of
the following:
The function returns one of the edges of the same quad-edge as the input edge.
CvSubdiv2DPoint* cvSubdivDelaunay2DInsert(
CvSubdiv2D* subdiv,
CvPoint2D32f pt);
pt Inserted point
The function inserts a single point into a subdivision and modifies the subdivision topology
appropriately. If a point with the same coordinates exists already, no new point is added. The
function returns a pointer to the allocated point. No virtual point coordinates are calculated at this
stage.
336 CHAPTER 2. IMGPROC. IMAGE PROCESSING
void cvAcc(
const CvArr* image,
CvArr* sum,
const CvArr* mask=NULL );
image Input image, 1- or 3-channel, 8-bit or 32-bit floating point. (each channel of multi-channel
image is processed independently)
sum Accumulator with the same number of channels as input image, 32-bit or 64-bit floating-point
The function adds the whole image image or its selected region to the accumulator sum:
void cvMultiplyAcc(
const CvArr* image1,
const CvArr* image2,
CvArr* acc,
const CvArr* mask=NULL );
image1 First input image, 1- or 3-channel, 8-bit or 32-bit floating point (each channel of multi-
channel image is processed independently)
2.7. MOTION ANALYSIS AND OBJECT TRACKING 337
image2 Second input image, the same format as the first one
acc Accumulator with the same number of channels as input images, 32-bit or 64-bit floating-point
The function adds the product of 2 images or their selected regions to the accumulator acc:
void cvRunningAvg(
const CvArr* image,
CvArr* acc,
double alpha,
const CvArr* mask=NULL );
image Input image, 1- or 3-channel, 8-bit or 32-bit floating point (each channel of multi-channel
image is processed independently)
acc Accumulator with the same number of channels as input image, 32-bit or 64-bit floating-point
The function calculates the weighted sum of the input image image and the accumulator acc
so that acc becomes a running average of frame sequence:
where α regulates the update speed (how fast the accumulator forgets about previous frames).
338 CHAPTER 2. IMGPROC. IMAGE PROCESSING
void cvSquareAcc(
const CvArr* image,
CvArr* sqsum,
const CvArr* mask=NULL );
image Input image, 1- or 3-channel, 8-bit or 32-bit floating point (each channel of multi-channel
image is processed independently)
sqsum Accumulator with the same number of channels as input image, 32-bit or 64-bit floating-
point
The function adds the input image image or its selected region, raised to power 2, to the
accumulator sqsum:
void cvCanny(
const CvArr* image,
CvArr* edges,
double threshold1,
double threshold2,
int aperture size=3 );
2.8. FEATURE DETECTION 339
aperture size Aperture parameter for the Sobel operator (see cvSobel)
The function finds the edges on the input image image and marks them in the output image
edges using the Canny algorithm. The smallest value between threshold1 and threshold2
is used for edge linking, the largest value is used to find the initial segments of strong edges.
void cvCornerEigenValsAndVecs(
const CvArr* image,
CvArr* eigenvv,
int blockSize,
int aperture size=3 );
eigenvv Image to store the results. It must be 6 times wider than the input image
aperture size Aperture parameter for the Sobel operator (see cvSobel)
After that it finds eigenvectors and eigenvalues of the matrix and stores them into destination
image in form (λ1 , λ2 , x1 , y1 , x2 , y2 ) where
340 CHAPTER 2. IMGPROC. IMAGE PROCESSING
void cvCornerHarris(
const CvArr* image,
CvArr* harris dst,
int blockSize,
int aperture size=3,
double k=0.04 );
harris dst Image to store the Harris detector responses. Should have the same size as image
aperture size Aperture parameter for the Sobel operator (see cvSobel).
The function runs the Harris edge detector on the image. Similarly to cvCornerMinEigenVal
and cvCornerEigenValsAndVecs, for each pixel it calculates a 2 × 2 gradient covariation matrix M
over a blockSize × blockSize neighborhood. Then, it stores
det(M ) − k trace(M )2
to the destination image. Corners in the image can be found as the local maxima of the
destination image.
2.8. FEATURE DETECTION 341
void cvCornerMinEigenVal(
const CvArr* image,
CvArr* eigenval,
int blockSize,
int aperture size=3 );
eigenval Image to store the minimal eigenvalues. Should have the same size as image
aperture size Aperture parameter for the Sobel operator (see cvSobel).
The function is similar to cvCornerEigenValsAndVecs but it calculates and stores only the
minimal eigen value of derivative covariation matrix for every pixel, i.e. min(λ1 , λ2 ) in terms of the
previous function.
void cvFindCornerSubPix(
const CvArr* image,
CvPoint2D32f* corners,
int count,
CvSize win,
CvSize zero zone,
CvTermCriteria criteria );
Sub-pixel accurate corner locator is based on the observation that every vector from the center
q to a point p located within a neighborhood of q is orthogonal to the image gradient at p subject
to image and measurement noise. Consider the expression:
i = DIpi T · (q − pi )
where DIpi is the image gradient at the one of the points pi in a neighborhood of q. The value
of q is to be found such that i is minimized. A system of equations may be set up with i set to
zero:
2.8. FEATURE DETECTION 343
X X
(DIpi · DIpi T )q = (DIpi · DIpi T · pi )
i i
where the gradients are summed within a neighborhood (”search window”) of q. Calling the
first gradient term G and the second gradient term b gives:
q = G−1 · b
The algorithm sets the center of the neighborhood window at this new center q and then iterates
until the center keeps within a set threshold.
void cvGoodFeaturesToTrack(
const CvArr* image
CvArr* eigImage, CvArr* tempImage
CvPoint2D32f* corners
int* cornerCount
double qualityLevel
double minDistance
const CvArr* mask=NULL
int blockSize=3
int useHarris=0
double k=0.04 );
tempImage Another temporary image, the same size and format as eigImage
qualityLevel Multiplier for the max/min eigenvalue; specifies the minimal accepted quality of
image corners
344 CHAPTER 2. IMGPROC. IMAGE PROCESSING
minDistance Limit, specifying the minimum possible distance between the returned corners;
Euclidian distance is used
mask Region of interest. The function selects points either in the specified region or in the whole
image if the mask is NULL
blockSize Size of the averaging block, passed to the underlying cvCornerMinEigenVal or cv-
CornerHarris used by the function
The function finds the corners with big eigenvalues in the image. The function first calculates
the minimal eigenvalue for every source image pixel using the cvCornerMinEigenVal function and
stores them in eigImage. Then it performs non-maxima suppression (only the local maxima in
3 × 3 neighborhood are retained). The next step rejects the corners with the minimal eigenvalue
less than qualityLevel · max(eigImage(x, y)). Finally, the function ensures that the distance
between any two corners is not smaller than minDistance. The weaker corners (with a smaller
min eigenvalue) that are too close to the stronger corners are rejected.
Note that the if the function is called with different values A and B of the parameter qualityLevel,
and A ¿ B, the array of returned corners with qualityLevel=A will be the prefix of the output
corners array with qualityLevel=B.
CvSeq* cvHoughLines2(
CvArr* image,
void* storage,
int method,
double rho,
double theta,
int threshold,
double param1=0,
double param2=0 );
2.8. FEATURE DETECTION 345
image The 8-bit, single-channel, binary source image. In the case of a probabilistic method, the
image is modified by the function
storage The storage for the lines that are detected. It can be a memory storage (in this case a
sequence of lines is created in the storage and returned by the function) or single row/single
column matrix (CvMat*) of a particular type (see below) to which the lines’ parameters are
written. The matrix header is modified by the function so its cols or rows will contain the
number of lines detected. If storage is a matrix and the actual number of lines exceeds
the matrix size, the maximum possible number of lines is returned (in the case of standard
hough transform the lines are sorted by the accumulator value)
threshold Threshold parameter. A line is returned by the function if the corresponding accumu-
lator value is greater than threshold
• For the probabilistic Hough transform it is the maximum gap between line segments
lying on the same line to treat them as a single line segment (i.e. to join them).
• For the multi-scale Hough transform it is the divisor for the angle resolution θ. (The
coarse angle resolution will be θ and the accurate resolution will be (θ/param2)).
The function implements a few variants of the Hough transform for line detection.
Example. Detecting lines with Hough transform.
/* This is a standalone program. Pass an image name as a first parameter
of the program. Switch between standard and probabilistic Hough transform
by changing "#if 1" to "#if 0" and back */
#include <cv.h>
#include <highgui.h>
#include <math.h>
cvNamedWindow( "Hough", 1 );
cvShowImage( "Hough", color_dst );
cvWaitKey(0);
}
}
This is the sample picture the function parameters have been tuned for:
348 CHAPTER 2. IMGPROC. IMAGE PROCESSING
And this is the output of the above program in the case of probabilistic Hough transform (#if
0 case):
void cvPreCornerDetect(
const CvArr* image,
CvArr* corners,
int apertureSize=3 );
int cvSampleLine(
const CvArr* image
CvPoint pt1
CvPoint pt2
void* buffer
int connectivity=8 );
buffer Buffer to store the line points; must have enough size to store max(|pt2.x − pt1.x| +
1, |pt2.y − pt1.y| + 1) points in the case of an 8-connected line and (|pt2.x − pt1.x| +
|pt2.y − pt1.y| + 1) in the case of a 4-connected line
The function implements a particular application of line iterators. The function reads all of the
image points lying on the line between pt1 and pt2, including the end points, and stores them
into the buffer.
void cvMatchTemplate(
const CvArr* image,
const CvArr* templ,
CvArr* result,
int method );
image Image where the search is running; should be 8-bit or 32-bit floating-point
templ Searched template; must be not greater than the source image and the same data type
as the image
method Specifies the way the template must be compared with the image regions (see below)
method=CV TM SQDIFF
X
R(x, y) = (T (x0 , y 0 ) − I(x + x0 , y + y 0 ))2
x0 ,y 0
method=CV TM CCORR
X
R(x, y) = (T (x0 , y 0 ) · I(x + x0 , y + y 0 ))
x0 ,y 0
method=CV TM CCOEFF
X
R(x, y) = (T 0 (x0 , y 0 ) · I(x + x0 , y + y 0 ))
x0 ,y 0
where
T 0 (x0 , y 0 ) = T (x0 , y 0 ) − 1/(w · h) · x00 ,y00 T (x00 , y 00 )
P
After the function finishes the comparison, the best matches can be found as global minimums
(CV TM SQDIFF) or maximums (CV TM CCORR and CV TM CCOEFF) using the cvMinMaxLoc func-
tion. In the case of a color image, template summation in the numerator and each sum in the
denominator is done over all of the channels (and separate mean values are used for each chan-
nel).
352 CHAPTER 2. IMGPROC. IMAGE PROCESSING
Chapter 3
void cvExtractSURF(
const CvArr* image,
const CvArr* mask,
CvSeq** keypoints,
CvSeq** descriptors,
CvMemStorage* storage,
CvSURFParams params );
353
354 CHAPTER 3. FEATURES2D. FEATURE DETECTION AND DESCRIPTOR EXTRACTION
mask The optional input 8-bit mask. The features are only found in the areas that contain more
than 50% of non-zero mask pixels
keypoints The output parameter; double pointer to the sequence of keypoints. The sequence
of CvSURFPoint structures is as follows:
typedef struct CvSURFPoint
{
CvPoint2D32f pt; // position of the feature within the image
int laplacian; // -1, 0 or +1. sign of the laplacian at the point.
// can be used to speedup feature comparison
// (normally features with laplacians of different
// signs can not match)
int size; // size of the feature
float dir; // orientation of the feature: 0..360 degrees
float hessian; // value of the hessian (can be used to
// approximately estimate the feature strengths;
// see also [Link])
}
CvSURFPoint;
descriptors The optional output parameter; double pointer to the sequence of descriptors.
Depending on the [Link] value, each element of the sequence will be either a
64-element or a 128-element floating-point (CV 32F) vector. If the parameter is NULL, the
descriptors are not computed
// (4 by default)
}
CvSURFParams;
The function cvExtractSURF finds robust features in the image, as described in [3]. For each
feature it returns its location, size, orientation and optionally the descriptor, basic or extended. The
function can be used for object tracking and localization, image stitching etc.
See the find [Link] demo in OpenCV samples directory.
CvSeq* cvGetStarKeypoints(
const CvArr* image,
CvMemStorage* storage,
CvStarDetectorParams params=cvStarDetectorParams() );
}
CvStarDetectorParams;
The function GetStarKeypoints extracts keypoints that are local scale-space extremas. The
scale-space is constructed by computing approximate values of laplacians with different sigma’s
at each pixel. Instead of using pyramids, a popular approach to save computing time, all of the
laplacians are computed at each pixel of the original high-resolution image. But each approximate
laplacian value is computed in O(1) time regardless of the sigma, thanks to the use of integral
images. The algorithm is based on the paper Agrawal08 , but instead of a square, hexagon or
octagon it uses an 8-end star shape, hence the name, consisting of overlapping upright and tilted
squares.
Each computed feature is represented by the following structure:
typedef struct CvStarKeypoint
{
CvPoint pt; // coordinates of the feature
int size; // feature size, see CvStarDetectorParams::maxSize
float response; // the approximated laplacian value at that point.
}
CvStarKeypoint;
if( !img )
return 0;
cvNamedWindow( "image", 1 );
cvShowImage( "image", img );
cvNamedWindow( "features", 1 );
cimg = cvCreateImage( cvGetSize(img), 8, 3 );
cvCvtColor( img, cimg, CV_GRAY2BGR );
359
360 CHAPTER 4. FLANN. CLUSTERING AND SEARCH IN MULTI-DIMENSIONAL SPACES
Chapter 5
361
362 CHAPTER 5. OBJDETECT. OBJECT DETECTION
The feature used in a particular classifier is specified by its shape (1a, 2b etc.), position within
the region of interest and the scale (this scale is not the same as the scale used at the detection
stage, though these two scales are multiplied). For example, in the case of the third line feature (2c)
the response is calculated as the difference between the sum of image pixels under the rectangle
covering the whole feature (including the two white stripes and the black stripe in the middle) and
the sum of the image pixels under the black stripe multiplied by 3 in order to compensate for the
differences in the size of areas. The sums of pixel values over a rectangular regions are calculated
rapidly using integral images (see below and the cvIntegral description).
To see the object detector at work, have a look at the HaarFaceDetect demo.
The following reference is for the detection part only. There is a separate application called
haartraining that can train a cascade of boosted classifiers from a set of samples. See
opencv/apps/haartraining for details.
CvRect r;
float weight;
} rect[CV_HAAR_FEATURE_MAX];
}
CvHaarFeature;
/* a single tree classifier (stump in the simplest case) that returns the
response for the feature at the particular image location (i.e. pixel
sum over subrectangles of the window) and gives out a value depending
on the response */
typedef struct CvHaarClassifier
{
int count; /* number of nodes in the decision tree */
int child;
int parent;
}
CvHaarStageClassifier;
Cascade:
Stage,,1,,:
Classifier,,11,,:
Feature,,11,,
Classifier,,12,,:
Feature,,12,,
...
Stage,,2,,:
Classifier,,21,,:
Feature,,21,,
...
...
The whole hierarchy can be constructed manually or loaded from a file or an embedded base
using the function cvLoadHaarClassifierCascade.
5.1. CASCADE CLASSIFICATION 365
CvHaarClassifierCascade* cvLoadHaarClassifierCascade(
const char* directory,
CvSize orig window size );
directory Name of the directory containing the description of a trained cascade classifier
orig window size Original size of the objects the cascade has been trained on. Note that it is
not stored in the cascade and therefore must be specified separately
The function loads a trained cascade of haar classifiers from a file or the classifier database
embedded in OpenCV. The base can be trained using the haartraining application (see opencv/app-
s/haartraining for details).
The function is obsolete. Nowadays object detection classifiers are stored in XML or YAML
files, rather than in directories. To load a cascade from a file, use the cvLoad function.
CvSeq* cvHaarDetectObjects(
const CvArr* image,
CvHaarClassifierCascade* cascade,
CvMemStorage* storage,
double scaleFactor=1.1,
int minNeighbors=3,
366 CHAPTER 5. OBJDETECT. OBJECT DETECTION
int flags=0,
CvSize minSize=cvSize(0, 0),
CvSize maxSize=cvSize(0,0) );
storage Memory storage to store the resultant sequence of the object candidate rectangles
scaleFactor The factor by which the search window is scaled between the subsequent scans,
1.1 means increasing window by 10%
flags Mode of operation. Currently the only flag that may be specified is CV HAAR DO CANNY PRUNING.
If it is set, the function uses Canny edge detector to reject some image regions that contain
too few or too much edges and thus can not contain the searched object. The particular
threshold values are tuned for face detection and in this case the pruning speeds up the
processing
minSize Minimum window size. By default, it is set to the size of samples the classifier has been
trained on (∼ 20 × 20 for face detection)
maxSize Maximum window size to use. By default, it is set to the size of the image.
The function finds rectangular regions in the given image that are likely to contain objects the
cascade has been trained for and returns those regions as a sequence of rectangles. The function
scans the image several times at different scales (see cvSetImagesForHaarClassifierCascade).
Each time it considers overlapping regions in the image and applies the classifiers to the regions
using cvRunHaarClassifierCascade. It may also apply some heuristics to reduce number of an-
alyzed regions, such as Canny prunning. After it has proceeded and collected the candidate
rectangles (regions that passed the classifier cascade), it groups them and returns a sequence of
average rectangles for each large enough group. The default parameters (scale factor =1.1,
min neighbors =3, flags =0) are tuned for accurate yet slow object detection. For a faster op-
eration on real video images the settings are: scale factor =1.2, min neighbors =2, flags
5.1. CASCADE CLASSIFICATION 367
=CV HAAR DO CANNY PRUNING, min size =minimum possible face size (for example, ∼ 1/4 to
1/16 of the image area in the case of video conferencing).
#include "cv.h"
#include "highgui.h"
cvReleaseMemStorage( &storage );
}
/* takes image filename and cascade path from the command line */
int main( int argc, char** argv )
{
IplImage* image;
if( argc==3 && (image = cvLoadImage( argv[1], 1 )) != 0 )
{
CvHaarClassifierCascade* cascade = load_object_detector(argv[2]);
detect_and_draw_objects( image, cascade, 1 );
cvNamedWindow( "test", 0 );
cvShowImage( "test", image );
cvWaitKey(0);
cvReleaseHaarClassifierCascade( &cascade );
cvReleaseImage( &image );
}
return 0;
}
void cvSetImagesForHaarClassifierCascade(
CvHaarClassifierCascade* cascade,
const CvArr* sum,
const CvArr* sqsum,
const CvArr* tilted sum,
double scale );
The function assigns images and/or window scale to the hidden classifier cascade. If image
pointers are NULL, the previously set images are used further (i.e. NULLs mean ”do not change
images”). Scale parameter has no such a ”protection” value, but the previous value can be re-
trieved by the cvGetHaarClassifierCascadeScale function and reused again. The function is used
to prepare cascade for detecting object of the particular size in the particular image. The function
is called internally by cvHaarDetectObjects, but it can be called by the user if they are using the
lower-level function cvRunHaarClassifierCascade.
void cvReleaseHaarClassifierCascade(
CvHaarClassifierCascade** cascade );
cascade Double pointer to the released cascade. The pointer is cleared by the function
The function deallocates the cascade that has been created manually or loaded using cvLoad-
HaarClassifierCascade or cvLoad.
int cvRunHaarClassifierCascade(
CvHaarClassifierCascade* cascade,
CvPoint pt,
int start stage=0 );
370 CHAPTER 5. OBJDETECT. OBJECT DETECTION
pt Top-left corner of the analyzed region. Size of the region is a original window size scaled by
the currenly set scale. The current window size may be retrieved using the cvGetHaarClas-
sifierCascadeWindowSize function
start stage Initial zero-based index of the cascade stage to start from. The function assumes
that all the previous stages are passed. This feature is used internally by cvHaarDetectOb-
jects for better processor cache utilization
The function runs the Haar classifier cascade at a single image location. Before using this
function the integral images and the appropriate scale (window size) should be set using cvSe-
tImagesForHaarClassifierCascade. The function returns a positive value if the analyzed rectangle
passed all the classifier stages (it is a candidate) and a zero or negative value otherwise.
Chapter 6
double cvCalcGlobalOrientation(
const CvArr* orientation,
const CvArr* mask,
const CvArr* mhi,
double timestamp,
double duration );
mask Mask image. It may be a conjunction of a valid gradient mask, obtained with cvCalcMotion-
Gradient and the mask of the region, whose direction needs to be calculated
timestamp Current time in milliseconds or other units, it is better to store time passed to cvUp-
dateMotionHistory before and reuse it here, because running cvUpdateMotionHistory and
cvCalcMotionGradient on large images may take some time
371
372 CHAPTER 6. VIDEO. VIDEO ANALYSIS
The function calculates the general motion direction in the selected region and returns the
angle between 0 degrees and 360 degrees . At first the function builds the orientation histogram
and finds the basic orientation as a coordinate of the histogram maximum. After that the function
calculates the shift relative to the basic orientation as a weighted sum of all of the orientation
vectors: the more recent the motion, the greater the weight. The resultant angle is a circular sum
of the basic orientation and the shift.
void cvCalcMotionGradient(
const CvArr* mhi,
CvArr* mask,
CvArr* orientation,
double delta1,
double delta2,
int apertureSize=3 );
mask Mask image; marks pixels where the motion gradient data is correct; output parameter
orientation Motion gradient orientation image; contains angles from 0 to 360 degrees
The function calculates the derivatives Dx and Dy of mhi and then calculates gradient orien-
tation as:
Dy(x, y)
orientation(x, y) = arctan
Dx(x, y)
6.1. MOTION ANALYSIS AND OBJECT TRACKING 373
where both Dx(x, y) and Dy(x, y) signs are taken into account (as in the cvCartToPolar func-
tion). After that mask is filled to indicate where the orientation is valid (see the delta1 and
delta2 description).
The function finds the minimum (m(x, y)) and maximum (M (x, y)) mhi values over each pixel
(x, y) neighborhood and assumes the gradient is valid only if