opencv库函数
1,像素操作
nt nl= image.rows; //行数
int nc= image.cols * image.channels(); // 每行的元素个数,每行的像素数*颜色通道数(RGB = 3)
for (int j=0; j<nl; j++) {
uchar* data= image.ptr<uchar>(j);
for (int i=0; i<nc; i++) {
// process each pixel ---------------------
data[i]= data[i]/div*div + div/2;
// end of pixel processing ----------------
} // end of line
}
2,opencv数据结构
C1 C2 C3 C4
CV_8U 0 8 16 24
CV_8S 1 9 17 25
CV_16U 2 10 18 26
CV_16S 3 11 19 27
CV_32S 4 12 20 28
CV_32F 5 13 21 29
CV_64F 6 14 22 30
C1 C2 C3 C4 C6
uchar uchar cv::Vec2b cv::Vec3b cv::Vec4b
short short cv::Vec2s cv::Vec3s cv::Vec4s
int int cv::Vec2i cv::Vec3i cv::Vec4i
float float cv::Vec2f cv::Vec3f cv::Vec4f cv::Vec6f
double double cv::Vec2d cv::Vec3d cv::Vec4d cv::Vec6d
v::Vec3b vec3b = img.at<cv::Vec3b>(0,0);
uchar vec3b0 = img.at<cv::Vec3b>(0,0)[0];
uchar vec3b1 = img.at<cv::Vec3b>(0,0)[1];
uchar vec3b2 = img.at<cv::Vec3b>(0,0)[2];
std::cout<<"vec3b = "<<vec3b<<std::endl;
std::cout<<"vec3b0 = "<<(int)vec3b0<<std::endl;
std::cout<<"vec3b1 = "<<(int)vec3b1<<std::endl;
std::cout<<"vec3b2 = "<<(int)vec3b2<<std::endl;
数值 具体类型 取值范围
CV_8U 8 位无符号整数 (0…..255)
CV_8S 8 位符号整数 (-128…..127)
CV_16U 16 位无符号整数 (0……65535)
CV_16S 16 位符号整数 (-32768…..32767)
CV_32S 32 位符号整数 (-2147483648……2147483647)
CV_32F 32 位浮点数 (-FLT_MAX ………FLT_MAX,INF,NAN)
CV_64F 64 位浮点数 (-DBL_MAX ……….DBL_MAX,INF,NAN)
3,Sobel算子求梯度
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
using namespace cv;
/** @function main */
int main( int argc, char** argv )
{
Mat src, src_gray;
Mat grad;
char* window_name = "Sobel Demo - Simple Edge Detector";
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
int c;
/// 装载图像
src = imread( argv[1] );
if( !src.data )
{ return -1; }
GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
/// 转换为灰度图
cvtColor( src, src_gray, CV_RGB2GRAY );
/// 创建显示窗口
namedWindow( window_name, CV_WINDOW_AUTOSIZE );
/// 创建 grad_x 和 grad_y 矩阵
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
/// 求 X方向梯度
//Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_x, abs_grad_x );
/// 求Y方向梯度
//Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_y, abs_grad_y );
/// 合并梯度(近似)
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
imshow( window_name, grad );
waitKey(0);
return 0;
}
4,高斯滤波
#include <QCoreApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
Mat srcImage=imread("Valley_logo.jpg");//读入原图
namedWindow("高斯滤波[原图]");
namedWindow("高斯滤波[效果图]");
imshow("高斯滤波[原图]",srcImage);
//进行高斯滤波操作
Mat dstImage;
GaussianBlur(srcImage,dstImage,Size(5,5),0,0);
//显示效果图
imshow("高斯滤波[效果图]",dstImage);
waitKey(0);
return 0;
}
5,分离颜色通道
void split(InputArray m,OutputArrayOfArrays mv);
void merge(InputArrayOfArrays mv,OutputArray dst);
6,distanceTransform() 距离变换函数
7,findContours
findContours
contourArea
threshold
threshold(imggray,140,255,cv2.THRESH_BINARY)
8,XML 文件读取
//将名字为"frameCount"的文件 存到变量frameCount中
int frameCount = static_cast<int>(fs2["frameCount"]);
//将名字为"calibrationData"的文件 存到变量data中
string data;
fs2["calibrationData"] >> data;