在OpenCV中提取水平直線,垂直直線和一些字元

2021-09-25 12:32:49 字數 3782 閱讀 7593

imshow("原影象", src);

首先要獲取原始檔,方便進行操作。 

//將原影象轉換成灰度影象

//將灰度影象轉換成二值影象

mat binaryimage;

adaptivethreshold(~grayimage, binaryimage, 255, adaptive_thresh_mean_c, thresh_binary, 15, -2);

其中的adaptivethreshold()函式是自適應閾值函式,能夠將灰度影象自適應的轉換成二值影象(影象中畫素值除了0就是1,兩種畫素值)。閾值就像是乙個門檻,能夠對超過或者是低於某個值的畫素值進行某些操作。

adaptivethreshold()函式的函式模型是:

c++: void adaptivethreshold(inputarray src, outputarray dst, double maxvalue, int adaptivemethod, int thresholdtype, int blocksize, double c);

第乙個引數src,是指要進行輸入的影象;

第二個引數dst,是指呼叫函式之後要輸出的影象;

第三個引數maxvalue,是指二值影象中的非零值,可以是255或者是其他;

第四個引數adaptivemethod,是指自適用使用的方法,通常使用adaptive_thresh_mean_c;

第五個引數thresholdtype,是指閾值的型別,通常使用thresh_binary;

第六個引數blocksize,是指計算閾值大小的乙個畫素的鄰域尺寸,通常為3,5,7等;

第七個引數c,是指常數值,可以取2;

值得注意的是,輸入的二進位制影象經過了乙個非預算,目的能夠將二值影象中的物件轉換成黑底白字,方便進行操作。

//建立結構元素-水平直線

mat hlinekernel = getstructuringelement(morph_rect, size(binaryimage.cols / 16, 1), point(-1, -1));

//建立結構元素-垂直直線

mat vlinekernel = getstructuringelement(morph_rect, size(1, binaryimage.rows / 16), point(-1, -1));

//提取字母

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

5、對影象進行開操作(先腐蝕後膨脹 -> 去掉區域較小的部分)

//提取水平線

mat hlineimage;

morphologyex(binaryimage, hlineimage, morph_open, hlinekernel);

//提取垂直線

mat vlineimage;

morphologyex(binaryimage, vlineimage, morph_open, vlinekernel);

//提取字母

mat alpimage;

morphologyex(binaryimage, alpimage, morph_open, alpkernel);

效果一:提取水平直線 

效果二:提取垂直直線

效果三:提取字母

//顯示原影象

imshow("原影象", src);

//將原影象轉換成灰度影象

mat grayimage;

cvtcolor(src, grayimage, color_bgr2gray);

imshow("灰度影象", grayimage);

imwrite("gray.png",grayimage);

//將灰度影象轉換成二值影象

mat binaryimage;

adaptivethreshold(~grayimage, binaryimage, 255, adaptive_thresh_mean_c, thresh_binary, 15, -2);

imshow("二值影象", binaryimage);

imwrite("binary.png", binaryimage);

//建立結構元素-水平直線

mat hlinekernel = getstructuringelement(morph_rect, size(binaryimage.cols / 16, 1), point(-1, -1));

mat hlineimage;

morphologyex(binaryimage, hlineimage, morph_open, hlinekernel);

imshow("提取水平直線", hlineimage);

imwrite("hlineimage.png", hlineimage);

//建立結構元素-垂直直線

mat vlinekernel = getstructuringelement(morph_rect, size(1, binaryimage.rows / 16), point(-1, -1));

mat vlineimage;

morphologyex(binaryimage, vlineimage, morph_open, vlinekernel);

imshow("提取垂直直線", vlineimage);

imwrite("vlineimage.png", vlineimage);

//提取字母

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

mat alpimage;

morphologyex(binaryimage, alpimage, morph_open, alpkernel);

imshow("提取字母", alpimage);

imwrite("alpimage.png", alpimage);

while(char(waitkey(1)) != 'q'){}

return 0;

}

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

include include includeusing namespace std using namespace cv int main int argc,char ar char input win input image char output win output image namedw...

提取水平與垂直線

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

OpenCV 提取水平或垂直線,過濾細 小雜質

灰度化 二值化 建立滿足不同需求的結構元素 開運算 若要去除垂直線,則建立水平長條狀矩形結構元素 若要去除水平線,則建立垂直長條狀矩形結構元素 include include include include using namespace std using namespace cv int mai...