opencv提取水平和垂直线
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include <istream> #include <opencv2/opencv.hpp> using namespace cv; using namespace std; int main( int argc, char **argv) { Mat src, gray_dst, bin_dst, h_dst, v_dst, dst; // 加载图片 src = imread( "../../picture/chars.png" , 1); if (!src.data) { printf ( "No image data \n" ); return -1; } char src_title[] = "src" ; namedWindow(src_title, WINDOW_AUTOSIZE); imshow(src_title, src); // 转换成灰度图像 char gray_title[] = "gray" ; namedWindow(gray_title, WINDOW_AUTOSIZE); cvtColor(src, gray_dst, COLOR_BGR2GRAY); imshow(gray_title, gray_dst); //转换成二值图像 char bin_title[] = "bin" ; namedWindow(bin_title, WINDOW_AUTOSIZE); adaptiveThreshold(~gray_dst, bin_dst, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2); imshow(bin_title, bin_dst); // 提取水平直线 char h_title[] = "h" ; Mat h = getStructuringElement(MORPH_RECT, Size(src.cols / 16, 1), Point(-1, -1)); Mat tmp1; erode(bin_dst, tmp1, h); dilate(tmp1, h_dst, h); // 提取水平直线等价api // morphologyEx(bin_dst, h_dst, MORPH_OPEN, h); namedWindow(h_title, WINDOW_AUTOSIZE); imshow(h_title, h_dst); // 提取垂直直线 char v_title[] = "v" ; Mat v = getStructuringElement(MORPH_RECT, Size(1, src.rows / 16), Point(-1, -1)); Mat tmp2; erode(bin_dst, tmp2, v); dilate(tmp2, v_dst, v); // 提取垂直直线等价api // morphologyEx(bin_dst, v_dst, MORPH_OPEN, v); namedWindow(v_title, WINDOW_AUTOSIZE); imshow(v_title, v_dst); // 字符提取 char dst_title[] = "dst" ; Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); // Mat tmp3; // erode(bin_dst, tmp3, kernel); // dilate(tmp3, dst, kernel); morphologyEx(bin_dst, dst, MORPH_OPEN, kernel); bitwise_not(dst, dst); blur(dst, dst, Size(3, 3), Point(-1, -1)); namedWindow(dst_title, WINDOW_AUTOSIZE); imshow(dst_title, dst); // 等待按键 waitKey(0); return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具