OpenCV三

水平线垂直线提取

1 RGB图像转灰度,灰度转二值化图像 

API: 

cvtColor(src,gray_src,CV_BGR2GRAY);
adaptiveThreshold(gray_src,binimg,255,ADAPTIVE_THRESH_GAUSSIAN_C,THRESH_BINARY,15,0);

2 定义kernel,通过先腐蚀,再膨胀得到线条//可以不需要膨胀

Mat hline=getStructuringElement(MORPH_RECT,Size(src.cols/15,1),Point(-1,-1));
erode(binimg,dst,vline);//腐蚀
dilate(dst,dst,vline);//膨胀

上面两步也就是开操作,可以下面一步做到

morphologyEx(binimg,dst,CV_MOP_OPEN,value);//开操作,先腐蚀再膨胀

得到上面的图像之后,还可以进一步操作,二值化图像减掉上面得到的图像,也就是相当于“背景”

morphologyEx(binimg,dst,CV_MOP_TOPHAT,vline);

上采样,下采样

这个操作是把图像放大或者缩小四倍

pyrUp(src,dst,Size(src.cols*2,src.rows*2));
pyrDown(src,down,Size(src.cols/2,src.rows/2));

阈值操作

threshold(gray_src,dst,threshold_value,255,type_value);
//type_value是5种类型
//threshold_value是阈值

详情转https://blog.csdn.net/u012566751/article/details/77046445

边缘处理

为了解决卷积不能处理边缘像素的问题,通过给原图像增加上下左右像素值

    int top=(int)(0.05*src.rows);
    int bottom=(int)(0.05*src.rows);
    int left=(int)(0.05*src.cols);
    int right=(int)(0.05*src.cols);
    copyMakeBorder(src,dst,top,bottom,left,right,border_type,color);
    //border_type,边缘补充类型
    //BORDER_REPLICATE复制,既是这样:aaa||abcde||eee
    //BORDER_CONSTANT常数,既是给定某种颜色来填充
    //BORDER_WRAP围巾,很形象。。。就是:cde||abcde||abc
    //BORDER_DEFAULT,像镜子一样。。是:cba||abcde||edc

在使用高斯滤波时候,参数里面有选择:

GaussianBlur(src,dst,Size(5,5),0,0,BORDER_WRAP);

算子与边缘检测

sobel:

    Sobel(dst,xgrad,CV_16S,1,0,3);//先求x方向,对x方向求一阶导数
    Sobel(dst,ygrad,CV_16S,0,1,3);

sobel加强:Scharr

Scharr(dst,xgrad,CV_32F,1,0,3);

拉普拉斯算子Laplacian:

    Laplacian(dst,out,CV_16S,3);
    convertScaleAbs(out,out);
    threshold(out,out,127,255,THRESH_BINARY);//阈值

上面的算子可以实现边缘检测,但更多直接调用成型API:

Canny(dst,out,current,2*current,3,false);//threshold2一般是threshol1的2或者3倍
//

霍夫检测---直线

1.调用Canny得到轮廓//当然这里可以使用别的方法得到轮廓,如果图像简单,例如sobel,分别对X,Y

2.调用霍夫检测:

vector<Vec4f>plines;
HoughLinesP(gray_src,plines,1,CV_PI/180,cur_dst,0,current);

详情转    https://blog.csdn.net/wlz7201/article/details/47205175

猜你喜欢

转载自blog.csdn.net/shuiyihang0981/article/details/82947418
今日推荐