文章目录
- 算子
- 形态学提取直线示例
算子
adaptiveThreshold 二值化算子
adaptiveThreshold(src, dst=None,maxValue, adaptiveMethod, thresholdType, blockSize, C, )
/*
*src:灰度化的图片
*dst:输出图像,可选
*maxValue:满足条件的像素点需要设置的灰度值
*adaptiveMethod:自适应方法。有2种:ADAPTIVE_THRESH_MEAN_C 或 ADAPTIVE_THRESH_GAUSSIAN_C
*thresholdType:二值化方法,可以设置为THRESH_BINARY或者THRESH_BINARY_INV
*blockSize:分割计算的区域大小,取奇数
* C:常数,每个区域计算出的阈值的基础上在减去这个常数作为这个区域的最终阈值,可以为负数
*/
形态学提取直线示例
想法:把获取二值化的图片轮廓,对直线进行开闭运算
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
int main() {
Mat src, dst;
src = imread("chars.png");
if (!src.data) {
printf("could not load image...\n");
return -1;
}
char INPUT_WIN[] = "input image";
char OUTPUT_WIN[] = "result image";
namedWindow(INPUT_WIN);
imshow(INPUT_WIN, src);
Mat gray_src;
cvtColor(src, gray_src, CV_BGR2GRAY);
imshow("gray image", gray_src);
Mat binImg;
adaptiveThreshold(gray_src, binImg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
imshow("binary image", binImg);
// 水平结构元素
Mat hline = getStructuringElement(MORPH_RECT, Size(src.cols / 16, 1), Point(-1, -1));
// 垂直结构元素
Mat vline = getStructuringElement(MORPH_RECT, Size(1, src.rows / 16), Point(-1, -1));
Mat temp;
erode(binImg, temp, hline );
dilate(temp, dst, hline );
// morphologyEx(binImg, dst, CV_MOP_OPEN, vline);
bitwise_not(dst, dst);
//blur(dst, dst, Size(3, 3), Point(-1, -1));
imshow("Final Result", dst);
waitKey(0);
return 0;
}