由于在使用OpenCV的神经网络的过程中,需要将样本保存为列向量或者行向量的形式,而在图像处理中,OpenCV读取进行的图像是以矩阵的形式保存的,因此,需要将矩阵转换为列向量的形式,由于我并没有在OpenCV的帮助文档中找到这样的一个函数(如果您发现有这样函数,请告诉我,谢谢),所以考虑自己写一个简单的转化器。
matconverter.h
/*M/////////////////////////////////////////////////////////////////////////// // Copyright (c) 2014, sheng // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // //M*/ #ifndef MATCONVERTER_H #define MATCONVERTER_H #include "opencv2/opencv.hpp" /** * @brief The MatConverter class The class used to change the size of the mat. * * @author sheng * @date 2014-09-18 * @version 0.1 * * @history * <author> <date> <version> <description> * sheng 2014-09-18 0.1 build the module * */ class MatConverter { public: MatConverter(); static bool MatToColumnVector(const cv::Mat& InputMat, cv::Mat& OutputColumnVector); static bool ColumnVectorToMat(const cv::Mat& InputColumnVector, cv::Mat& OutputMat, int Rows , int Cols); private: static bool Convert(const cv::Mat& InputMat, cv::Mat& Output, int Rows, int Cols); }; #endif // MATCONVERTER_H
matconverter.cpp
/*M/////////////////////////////////////////////////////////////////////////// // Copyright (c) 2014, sheng // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // //M*/ #include "matconverter.h" /** * @brief MatConverter::MatConverter The default constructor * * @author sheng * @date 2014-09-19 * @version 0.1 * * @history * <author> <date> <version> <description> * sheng 2014-09-19 0.1 build the module * */ MatConverter::MatConverter() { } /** * @brief MatConverter::MatToColumnVector Convert the mat to a colmn vector * @param InputMat The input mat. * @param OutputColumnVector The output column vector. * @return true if the operation is successed. * false otherwise * * @author sheng * @date 2014-09-19 * @version 0.1 * * @history * <author> <date> <version> <description> * sheng 2014-09-19 0.1 build the module * */ bool MatConverter::MatToColumnVector(const cv::Mat &InputMat, cv::Mat &OutputColumnVector) { int Rows = InputMat.rows * InputMat.cols; int Cols = 1; return Convert(InputMat, OutputColumnVector, Rows, Cols); } /** * @brief MatConverter::ColumnVectorToMat Convert the column vector to a mat. * @param InputColumnVector The input column vector * @param OutputMat The output mat * @param Rows The number of the rows of the output mat. * @param Cols The number of the cols of the output mat. * @return true if the operation is successed. * false otherwise * * @author sheng * @date 2014-09-19 * @version 0.1 * * @history * <author> <date> <version> <description> * sheng 2014-09-19 0.1 build the module * */ bool MatConverter::ColumnVectorToMat(const cv::Mat &InputColumnVector, cv::Mat &OutputMat, int Rows, int Cols) { return Convert(InputColumnVector, OutputMat, Rows, Cols); } /** * @brief MatConverter::Convert Resize the input mat and saved it in output mat. * @param InputMat The input mat * @param Output The output mat which have the same element and the same type * of the input mat but in different number of rows and cols. * @param Rows The number of the rows of the output mat. * @param Cols The number of the cols of the output mat. * @return true if the operator is succeed. * false otherwise. * * @author sheng * @date 2014-09-19 * @version 0.1 * * @history * <author> <date> <version> <description> * sheng 2014-09-19 0.1 build the module * */ bool MatConverter::Convert(const cv::Mat &InputMat, cv::Mat &Output, int Rows, int Cols) { // return false if the data of the SourceMat is empty. assert(InputMat.data); if (!InputMat.data) { return false; } // return false if the rows is not positive or the cols is not positive assert((Rows > 0) && (Cols > 0)); if ((Rows < 1) || (Cols < 1)) { return false; } // return false if the size of the element of the input mat is smaller than // the size of the element of the output mat. assert((InputMat.rows * InputMat.cols) == (Rows * Cols)); if ((InputMat.rows * InputMat.cols) != (Rows * Cols)) { return false; } cv::Mat TmpMat(Rows, Cols, InputMat.type(), InputMat.data); TmpMat.copyTo(Output); // return false if the data of the DstMat is null assert(Output.data); if (!Output.data) { return false; } return true; }
Test_MatConverter.cpp
/*M/////////////////////////////////////////////////////////////////////////// // Copyright (c) 2014, sheng // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // //M*/ #include "matconverter.h" #include <iostream> void Test_MatConverter() { float array[2][3] = {{11.0, 12.2, 13.3}, {21.4, 22.5, 23.6}}; cv::Mat TestMat(2, 3, CV_32FC1, array); std::cout << "The test Mat is " << TestMat << std::endl; cv::Mat ColumnVector; MatConverter::MatToColumnVector(TestMat, ColumnVector); std::cout << "The column vector is " << ColumnVector << std::endl; cv::Mat OutputMat; MatConverter::ColumnVectorToMat(ColumnVector, OutputMat, 2, 3); std::cout << "The output mat is " << OutputMat << std::endl; } void Test_MatConverter_badcase() { cv::Mat TestMat; std::cout << "The test Mat is " << TestMat << std::endl; cv::Mat ColumnVector; MatConverter::MatToColumnVector(TestMat, ColumnVector); std::cout << "The column vector is " << ColumnVector << std::endl; cv::Mat OutputMat; MatConverter::ColumnVectorToMat(ColumnVector, OutputMat, 2, 2); std::cout << "The output mat is " << OutputMat << std::endl; } void Test_MatConverter_badcase1() { float array[2][3] = {{11.0, 12.2, 13.3}, {21.4, 22.5, 23.6}}; cv::Mat TestMat(2, 3, CV_32FC1, array); std::cout << "The test Mat is " << TestMat << std::endl; cv::Mat ColumnVector; MatConverter::MatToColumnVector(TestMat, ColumnVector); std::cout << "The column vector is " << ColumnVector << std::endl; cv::Mat OutputMat; MatConverter::ColumnVectorToMat(ColumnVector, OutputMat, -2, 2); std::cout << "The output mat is " << OutputMat << std::endl; } void Test_MatConverter_badcase2() { float array[2][3] = {{11.0, 12.2, 13.3}, {21.4, 22.5, 23.6}}; cv::Mat TestMat(2, 3, CV_32FC1, array); std::cout << "The test Mat is " << TestMat << std::endl; cv::Mat ColumnVector; MatConverter::MatToColumnVector(TestMat, ColumnVector); std::cout << "The column vector is " << ColumnVector << std::endl; cv::Mat OutputMat; MatConverter::ColumnVectorToMat(ColumnVector, OutputMat, 2, -2); std::cout << "The output mat is " << OutputMat << std::endl; } void Test_MatConverter_badcase3() { float array[2][3] = {{11.0, 12.2, 13.3}, {21.4, 22.5, 23.6}}; cv::Mat TestMat(2, 3, CV_32FC1, array); std::cout << "The test Mat is " << TestMat << std::endl; cv::Mat ColumnVector; MatConverter::MatToColumnVector(TestMat, ColumnVector); std::cout << "The column vector is " << ColumnVector << std::endl; cv::Mat OutputMat; MatConverter::ColumnVectorToMat(ColumnVector, OutputMat, 2, 2); std::cout << "The output mat is " << OutputMat << std::endl; }