opencv入門-函數說明

函數說明

  • imread
  • getStructuringElement
  • erode
  • dilate

imread


  • 官方說明一句話就是加載支持格式的圖片

The function imread loads an image from the specified file and returns it. If the image cannot be
read (because of missing file, improper permissions, unsupported or invalid format), the function
returns an empty matrix ( Mat::data==NULL ).
-支持如下的所有格式的圖片

 Currently, the following file formats are supported:

-   Windows bitmaps - \*.bmp, \*.dib (always supported)
-   JPEG files - \*.jpeg, \*.jpg, \*.jpe (see the *Notes* section)
-   JPEG 2000 files - \*.jp2 (see the *Notes* section)
-   Portable Network Graphics - \*.png (see the *Notes* section)
-   WebP - \*.webp (see the *Notes* section)
-   Portable image format - \*.pbm, \*.pgm, \*.ppm \*.pxm, \*.pnm (always supported)
-   Sun rasters - \*.sr, \*.ras (always supported)
-   TIFF files - \*.tiff, \*.tif (see the *Notes* section)
-   OpenEXR Image files - \*.exr (see the *Notes* section)
-   Radiance HDR - \*.hdr, \*.pic (always supported)
-   Raster and Vector geospatial data supported by Gdal (see the *Notes* section)
  • 函數原型
CV_EXPORTS_W Mat imread( const String& filename, int flags = IMREAD_COLOR );
  • 參數說明
@param filename Name of file to be loaded.
@param flags Flag that can take values of cv::ImreadModes

第二個參數有以下幾種格式

//! Imread flags
enum ImreadModes {
       IMREAD_UNCHANGED            = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
       IMREAD_GRAYSCALE            = 0,  //!< If set, always convert image to the single channel grayscale image.
       IMREAD_COLOR                = 1,  //!< If set, always convert image to the 3 channel BGR color image.
       IMREAD_ANYDEPTH             = 2,  //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
       IMREAD_ANYCOLOR             = 4,  //!< If set, the image is read in any possible color format.
       IMREAD_LOAD_GDAL            = 8,  //!< If set, use the gdal driver for loading the image.
       IMREAD_REDUCED_GRAYSCALE_2  = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
       IMREAD_REDUCED_COLOR_2      = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
       IMREAD_REDUCED_GRAYSCALE_4  = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
       IMREAD_REDUCED_COLOR_4      = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
       IMREAD_REDUCED_GRAYSCALE_8  = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
       IMREAD_REDUCED_COLOR_8      = 65, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
       IMREAD_IGNORE_ORIENTATION   = 128 //!< If set, do not rotate the image according to EXIF's orientation flag.
     };

可以分爲三類(只是參考)
1. >0 加載BRG三個通道的圖
2. =0 加載單通道的圖
3. <0 加載Alpha通道的圖,默認爲1
- 示例

 Mat srcimg = imread(filename);

getStructuringElement


  • 官方說明:返回指定大小和形狀的元素操作

Returns a structuring element of the specified size and shape for morphological operations.

  • 參數說明
    @param shape Element shape that could be one of cv::MorphShapes
    @param ksize Size of the structuring element.
    @param anchor Anchor position within the element. The default value \f(1,1)\f means that the
    anchor is at the center. Note that only the shape of a cross-shaped element depends on the anchor
    position. In other cases the anchor just regulates how much the result of the morphological
    operation is shifted.

  • 第一個參數
    MorphShapes有以下幾種
    矩形: MORPH_RECT
    交叉形: MORPH_CROSS
    橢圓形: MORPH_ELLIPSE
    第二個參數
    就是內核的大小
    第三個錨點的位置(-1,-1)表示中心位置,只有十字中心錨點有作用其它的只是形狀轉換操作
    - 示例

     Mat element = getStructuringElement(MORPH_RECT, Size(15, 15)); 

    erode 操作結果就是高亮的部份會變小


    • 官方說明:用指定的核元素來腐蝕源圖片


    The function erodes the source image using the specified structuring element that determines the
    shape of a pixel neighborhood over which the minimum is taken:
  • 參數說明
    @param src input image; the number of channels can be arbitrary, but the depth should be one of
    CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
    @param dst output image of the same size and type as src.
    //核,可以用getStructuringElement自定義
    @param kernel structuring element used for erosion; if element=Mat(), a 3 x 3 rectangular
    structuring element is used. Kernel can be created using getStructuringElement.
    //默認中心
    @param anchor position of the anchor within the element; default value (-1, -1) means that the
    anchor is at the element center.
    //迭代次數
    @param iterations number of times erosion is applied.
    //邊界模式
    @param borderType pixel extrapolation method, see cv::BorderTypes
    //邊界值
    @param borderValue border value in case of a constant border
    @sa dilate, morphologyEx, getStructuringElement
  • 示例
  •  erode(srcimg, dstimg, element);
     ```
     ## dilate 膨脹操作,就是高亮的部分會放大
     - 官方說明
     > The function dilates the source image using the specified structuring element that determines the
    shape of a pixel neighborhood over which the maximum is taken:
    - 參數說明
    >@param src input image; the number of channels can be arbitrary, but the depth should be one of
    CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
    @param dst output image of the same size and type as src\`.
    @param kernel structuring element used for dilation; if elemenat=Mat(), a 3 x 3 rectangular
    structuring element is used. Kernel can be created using getStructuringElement
    @param anchor position of the anchor within the element; default value (-1, -1) means that the
    anchor is at the element center.
    @param iterations number of times dilation is applied.
    @param borderType pixel extrapolation method, see cv::BorderTypes
    @param borderValue border value in case of a constant border
    - 示例
     dilate(srcimg, dstimg, element);

    最後整合後的例子

    
    //顯示圖片 
    int showimg( const string& filename)
    {
        Mat img = imread(filename,0);
        imshow("test", img);
        waitKey();
        return 0;
    }
    //腐蝕操作
     int erodeimg(const string & filename)
    {
         Mat srcimg = imread(filename);
         imshow("sourceimg", srcimg);
         //進行腐蝕操作
         Mat element = getStructuringElement(MORPH_RECT, Size(15, 15));
         Mat dstimg;
         erode(srcimg, dstimg, element);
         imshow("erodeimg", dstimg);
         //waitKey(0);
         return 0;
    }
     //進行膨脹操作
      int dilateimg(const string & filename)
     {
          Mat srcimg = imread(filename);
          imshow("sourceimg", srcimg);
          //進行膨脹操作
          Mat element = getStructuringElement(MORPH_RECT, Size(15, 15));
          Mat dstimg;
          dilate(srcimg, dstimg, element);
          imshow("dilateimg", dstimg);
          waitKey(0);
          return 0; 
     }
    
    發表評論
    所有評論
    還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
    相關文章