OpenCV常用操作的代码

    将OpenCV中常用的代码集中一下,以便查找和使用。所有的代码都使用cv名字空间,使用C++接口。

    常用的操作包括图像像素的遍历、二值化、边缘检测、形态学处理和轮廓提取等。

1、图像像素遍历

//单通道1
int row = image.rows;
int col = image.cols;
for(int j = 0; j < row; j++){
    uchar* data = image.ptr
   
   
    
    (j);
    for(int i=0; i
    
    
     
     (j,i) = 255;
    }
}

//三通道
int row = image.rows;
int col = image.cols;
for(int j = 0; j < row; j++){
    for(int i=0; i
     
     
      
      (j,i)[0] = 255;
         image.at
      
      
       
       (j,i)[1] = 255;
         image.at
       
       
         (j,i)[2] = 255; } } 
       
      
      
     
     
    
    
   
   

2、二值化图像

Mat bin;
threshold(image, bin, 128, 255, THRESH_BINARY); // dst = (src>128)?255:0
threshold(image, bin, 128, 255, THRESH_BINARY_INV); // dst = (src>128)?0:255

3、边缘检测

// Canny 
// The low threshold should be chosen in a way that it includes all edge pixels that
// are considered to belong to a significant image contour
// The high threshold is then to define the edges that belong to all important contours
Mat src = imread("/home/u/src.ppm",0);
Mat edge;
Canny(src, // gray-level image 
      edge,  // output edge image
      125,   // low threshold
      350); // high threshold
 
 // Sobel
 Mat src = imread("/home/u/src.ppm",0);
 int scale = 1;
 int delta = 0;
 int ddepth = CV_16S;
 //GaussianBlur(src, src, Size(9, 9),1.5);
 Mat grad;
 Mat grad_x, grad_y;
 Mat abs_grad_x, abs_grad_y;
 Sobel(src, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT);
 convertScaleAbs(grad_x, abs_grad_x);
 Sobel(src, 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);

 // Laplacian
 Mat src = imread("/home/u/src.ppm",0);
 //GaussianBlur(src, src, Size(9, 9),1.5);
 Mat laplacian;
 Mat result;
 Laplacian(src,laplacian,CV_16S,3);
 convertScaleAbs(laplacian, result, 1);

4、形态学处理

 // Erosion 腐蚀
 Mat eroded;
 Mat element(7, 7, CV_8U, Scalar(1));
 erode(src, eroded, element);  
 // Or erode the image 3 times
 erode(src, eroded, element, Point(-1,-1), 3);

 // Dilation 膨胀
 Mat dilated;
 Mat element(7, 7, CV_8U, Scalar(1));
 dilate(src, dilated, element);
 // Or dilate the image 3 times
 dilate(src, dilated, element, Point(-1, -1), 3);

 // Closed 闭操作
 Mat closed;
 Mat element(7, 7, CV_8U, Scalar(1));
 morphologyEx(src, closed, MORPH_CLOSE, element);  

 // Open 开操作
 Mat opened;
 Mat element(7, 7, CV_8U, Scalar(1));
 morphologyEx(src, closed, MORPH_OPEN, element); 

5、轮廓获取

 typedef vector< vector
   
   
    
     > Contours;
 Contours contours;
 findcontours(src, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
 // Draw all contours
 Mat result(image.size(), CV_8U, Scalar(255));
 drawContours(result, contours, -1, Scalar(0, ,0, 255), 2);


   
   


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值