矩陣類型及對應的at類型

轉載地址:

http://www.prism.gatech.edu/~ahuaman3/docs/OpenCV_Docs/tutorials/basic_0/basic_0.html

CV_8UC1 : 8-bit unsigned integer (uchar)    #define CV_8U   0
CV_8SC1 : 8-bit signed integer (schar)      #define CV_8S   1
CV_16UC1 : 16-bit unsigned integer(ushort)  #define CV_16U  2
CV_16SC1 : 16-bit signed integer (short)    #define CV_16S  3
CV_32SC1 : 32-bit signed integer (int)      #define CV_32S  4
CV_32FC1 : 32-bit floating-point (float)    #define CV_32F  5
CV_64FC1 : 64-bit floating-point (double)   #define CV_32F  5

In this section we will modify our first program in order to create and print matrices of different types. Below is the code and following is the explanation of how it works:

#include "opencv2/core/core.hpp"
#include <stdio.h>

// Function declaration
void printMat1D( cv::Mat _m );

// function main
int main( int argc, char* argv[] ) {

    // Create matrices of different types and print them
    cv::Mat m1( 4, 5, CV_8UC1, cv::Scalar(23) );
    printMat1D( m1 );

    cv::Mat m2( 3, 2, CV_8SC1, cv::Scalar(-27) );
    printMat1D( m2 );

    cv::Mat m3( 5, 3, CV_16UC1, cv::Scalar(1939) );
    printMat1D( m3 );

    cv::Mat m4( 2, 4, CV_16SC1, cv::Scalar(-1961) );
    printMat1D( m4 );

    cv::Mat m5( 5, 3, CV_32SC1, cv::Scalar(23985) );
    printMat1D( m5 );

    cv::Mat m6( 2, 4, CV_32FC1, cv::Scalar(2011.02) );
    printMat1D( m6 );

    cv::Mat m7( 2, 4, CV_64FC1, cv::Scalar(242012.36) );
    printMat1D( m7 );

    return 0;
 }


 // function printMat1D
 void printMat1D( cv::Mat _m ) {

   int type = _m.type();

   switch( type ) {

     // Unsigned 8-bits int
     case CV_8U:
       printf("\n --> Matrix type is CV_8U \n");

       for( size_t i = 0; i < _m.rows; i++ ) {
         for( size_t j = 0; j < _m.cols; j++ ) {
           printf( " %d  ", _m.at<uchar>(i,j) );
         } printf("\n");
       }
     break;

     // Signed 8-bits int
     case CV_8S:
       printf("\n --> Matrix type is CV_8S \n");

       for( size_t i = 0; i < _m.rows; i++ ) {
         for( size_t j = 0; j < _m.cols; j++ ) {
           printf( " %d  ", _m.at<schar>(i,j) );
         } printf("\n");
       }
     break;

     // Unsigned 16-bits int
     case CV_16U:
       printf("\n --> Matrix type is CV_16U \n");

       for( size_t i = 0; i < _m.rows; i++ ) {
         for( size_t j = 0; j < _m.cols; j++ ) {
           printf( " %d  ", _m.at<ushort>(i,j) );
         } printf("\n");
       }
     break;

     // Signed 16-bits int
     case CV_16S:
       printf("\n --> Matrix type is CV_16S \n");

       for( size_t i = 0; i < _m.rows; i++ ) {
         for( size_t j = 0; j < _m.cols; j++ ) {
           printf( " %d  ", _m.at<short>(i,j) );
         } printf("\n");
       }
     break;

     // Signed 32-bits int
     case CV_32S:
       printf("\n --> Matrix type is CV_32S \n");

       for( size_t i = 0; i < _m.rows; i++ ) {
         for( size_t j = 0; j < _m.cols; j++ ) {
           printf( " %d  ", _m.at<int>(i,j) );
         } printf("\n");
       }
     break;

     // 32-bits float
     case CV_32F:
       printf("\n --> Matrix type is CV_32F \n");

       for( size_t i = 0; i < _m.rows; i++ ) {
         for( size_t j = 0; j < _m.cols; j++ ) {
           printf( " %.3f  ", _m.at<float>(i,j) );
         } printf("\n");
       }
     break;

     // 64-bit double
     case CV_64F:
       printf("\n --> Matrix type is CV_64F \n");

       for( size_t i = 0; i < _m.rows; i++ ) {
         for( size_t j = 0; j < _m.cols; j++ ) {
           printf( " %.3f  ", _m.at<double>(i,j) );
         } printf("\n");
       }
     break;
   }
 }

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章