zoukankan      html  css  js  c++  java
  • OpenCV (十)提取水平线与垂直线

    步骤:

    1. 读取图片
    2. 转换为灰度图片
    3. 将灰度图片转化为二值图片 adaptiveThreshold()
    4. 腐蚀
    5. 膨胀

    adapativeThreshold():

    #include<opencv2/opencv.hpp>
    #include<iostream>
    
    using namespace cv;
    using namespace std;
    
    Mat src, dst, bilary_gray, output;
    
    int main(int argc, char** argv) {
    	src = imread("D:/OpenCVprj/image/test6.jpg");
    	if (!src.data) {
    		cout << "Could not load image......
    " << endl; 
    			return -1;
    	}
    	imshow("src", src);
    
    	cvtColor(src, dst, CV_BGR2GRAY);
    	imshow("dst", dst);
    	adaptiveThreshold(~dst, bilary_gray, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
    	//double maxValue,二值图像最大值
    	//int adaptiveMethod,自适应方法,只能其中之一 - ADAPTIVE_THRESH_MEAN_C , ADAPTIVE_THRESH_GAUSSIAN_C 
    	//int thresholdType,阈值类型
    	//int blockSize,块大小
    	//double C, 常量C 可以是正数,0,负数
    	imshow("bilary_gray", bilary_gray);
    	Mat kernel1 = getStructuringElement(MORPH_RECT, Size(dst.cols/16, 1), Point(-1, -1));
    	//Mat kernel2 = getStructuringElement(MORPH_RECT, Size(1, dst.rows/16), Point(-1, -1));
    	erode(bilary_gray, dst, kernel1);
    	dilate(dst, output, kernel1);
    	bitwise_not(output, output);
    	imshow("output", output);
    
    	waitKey(0);
    	return 0;
    }
    

      

    应用补充:

    #include<opencv2/opencv.hpp>
    #include<iostream>
    
    using namespace cv;
    using namespace std;
    
    Mat src, dst, bilary_gray, output;
    
    int main(int argc, char** argv) {
    	src = imread("D:/OpenCVprj/image/test6.jpg");
    	if (!src.data) {
    		cout << "Could not load image......
    " << endl; 
    			return -1;
    	}
    	imshow("src", src);
    
    	cvtColor(src, dst, CV_BGR2GRAY);
    	imshow("dst", dst);
    	adaptiveThreshold(~dst, bilary_gray, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
    	//double maxValue,二值图像最大值
    	//int adaptiveMethod,自适应方法,只能其中之一 - ADAPTIVE_THRESH_MEAN_C , ADAPTIVE_THRESH_GAUSSIAN_C 
    	//int thresholdType,阈值类型
    	//int blockSize,块大小
    	//double C, 常量C 可以是正数,0,负数
    	imshow("bilary_gray", bilary_gray);
    	//Mat kernel1 = getStructuringElement(MORPH_RECT, Size(dst.cols/16, 1), Point(-1, -1));
    	//Mat kernel2 = getStructuringElement(MORPH_RECT, Size(1, dst.rows/16), Point(-1, -1));
    	Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
    	erode(bilary_gray, dst, kernel);
    	dilate(dst, output, kernel);
    	bitwise_not(output, output);
    	//blur(output, output, Size(3, 3), Point(-1, -1));
    	imshow("output", output);
    	
    	waitKey(0);
    	return 0;
    }
    

      

  • 相关阅读:
    php对接java平台 sha256 加密 签名验证不过
    iframe里面表单提交后如何跳出iframe重定向到另一个页面
    composer update install require 三个命令的的别
    PHP RabbitMQ实现简单的延迟队列
    CentOS 7 RPM 离线安装 MySQL 8
    解决SSH连接Linux主机Broken pipe问题
    Oracle查询连接数
    Oracle SYSAUX 表空间减肥
    Oracle URL的几种写法
    Oracle查询和修改序列
  • 原文地址:https://www.cnblogs.com/haiboxiaobai/p/11235972.html
Copyright © 2011-2022 走看看