【OpenCV】音符提取(形態學執行個體)

來源:互聯網
上載者:User
提取水平線和垂直線


  下圖很好地詮釋了腐蝕膨脹的操作原理。


  自適應閾值

adaptiveThreshold()void cv::adaptiveThreshold  (   InputArray  src,   OutputArray  dst,                                double maxValue,   int adaptiveMethod,                                int thresholdType, int  blockSize,                                  double  C )   說明:src為輸入映像,dst為輸出映像,maxValue是最大灰階值,     adaptiveMethod有ADAPTIVE_THRESH_MEAN_C 、ADAPTIVE_THRESH_GAUSSIAN_C      blockSize為掩模大小,C為從均值或加權平均值中減去的常數值。


  掩模

getStructuringElement()Mat cv::getStructuringElement   (   int     shape,                                    Size    ksize,                                    Point   anchor = Point(-1,-1) )   說明:shape有MORPH_RECT、MORPH_CROSS、MORPH_ELLIPSE。ksize為掩模大小。     anchor為掩模中心,Point(-1,-1)預設中心。
程式碼範例
#include <opencv2/opencv.hpp>using namespace std;using namespace cv;int main(int, char** argv){    Mat src,bw,horizontal, vertical;    char* filename = "../data/src.png";    src = imread(filename, IMREAD_GRAYSCALE);    if (src.empty()) { return -1; }    imshow("src",src);    adaptiveThreshold(~src, bw, 255, CV_ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);    // 自適應閾值    horizontal = bw.clone();    vertical = bw.clone();    Mat structure_h = getStructuringElement(MORPH_RECT, Size(horizontal.cols / 30, 1));    Mat structure_v = getStructuringElement(MORPH_RECT, Size(1, vertical.rows / 30));    erode(horizontal, horizontal, structure_h, Point(-1, -1));              // Point(-1,-1)預設中心    dilate(horizontal, horizontal, structure_h, Point(-1, -1));    erode(vertical, vertical, structure_v, Point(-1, -1));                  // Point(-1,-1)預設中心    dilate(vertical, vertical, structure_v, Point(-1, -1));    imshow("horizontal", horizontal);                                       // 水平    imshow("vertical", vertical);                                           // 垂直    bitwise_not(vertical, vertical);                                        // 取反    Mat edges;    adaptiveThreshold(vertical, edges, 255, CV_ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 3, -2);    Mat kernel = Mat::ones(2, 2, CV_8UC1);    dilate(edges, edges, kernel);    Mat smooth;    vertical.copyTo(smooth);    blur(smooth, smooth, Size(2, 2));                                       // 平滑    smooth.copyTo(vertical, edges);                                         // vertical為輸出,edges為同大小的掩模,                                                                            // 掩模非零部分才被複製    imshow("final", vertical);    waitKey(0);    return 0;}


運行結果




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.