opencv 應用 提取水平與垂直直線 去噪

2021-10-03 05:51:59 字數 1606 閱讀 3285

#include#include#includeusing namespace std;

using namespace cv;

int main(int argc, char** ar**)

char input_win = "input image";

char output_win = "output image";

namedwindow(input_win, cv_window_autosize);

imshow(input_win, src);

//轉化成灰度影象

mat gray_src;

cvtcolor(src, gray_src, cv_bgr2gray);

imshow(output_win, gray_src);

//轉化為二值影象

mat bin_img;

//注意src如果是白底的,下面就用~gray_src(先取反),如果是黑底的就用gray_src

adaptivethreshold(~gray_src, bin_img, 255, adaptive_thresh_mean_c, thresh_binary, 15, -2);

imshow("binary image", bin_img);

//建立結構元素

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(bin_img, temp, hline);

dilate(temp, dst, hline);

//上面兩句等同於開操作,效果一樣。

//morphologyex(src, dst, cv_mop_open, vline);

//bitwise_not(dst,dst);//將顏色取反(背景變成白色,主體變成黑色)

//可以對結果再進行濾波blur,使得結果變得更圓滑

imshow("heng xian", dst);

//保留豎線

mat shuxian, dst1;

erode(bin_img, shuxian, vline); //腐蝕

dilate(shuxian, dst1, vline); //膨脹

imshow("shu xian", dst1);

//矩形結構

mat kernel1 = getstructuringelement(morph_rect, size(3, 3), point(-1, -1));

mat temp1,dst2;

erode(bin_img, temp1, kernel1); //腐蝕

dilate(temp1, dst2, kernel1); //膨脹

imshow("shu xian_kernel", dst2);

waitkey(0);

return 0;

}

opencv 形態學應用 提取提取水平與垂直線

現象 影象形態學操作時候,可以通過自定義的結構元素實現結構元素對輸入影象一些物件敏感 另外一些物件不敏感,這樣就會讓敏感的物件改變而不敏感的物件保留輸出。通過使用兩個最基本的形態學操作 膨脹與腐蝕,使用不同的結構元素實現對輸入影象的操作 得到想要的結果。比如開操作,先腐蝕後膨脹,將白色的小塊去掉了 ...

提取水平與垂直線

輸入彩色影象 imread 轉換為灰度影象 cvtcolor 轉換為二值影象 adaptivethreshold 定義結構元素 開操作 膨脹 腐蝕 提取水平與垂直線 include include using namespace cv using namespace std int main int...

opencv形態學操作,提取水平與垂直線

可做驗證碼識別 mat temp,temp1 cvtcolor src,temp,cv bgr2gray imshow temp temp adaptivethreshold temp,temp1,255,adaptive thresh mean c,thresh binary,15,2 轉換為二值...