您的位置:首页 > 运维架构

opencv的几个基础函数

2016-12-15 14:38 627 查看
#include <string.h>

#include "Barcode2D/MaxiCode.h"

static int opencv_entry( int argc, char** argv );

using namespace Barcode2D;

int main(int argc,char *argv[])

{

    return opencv_entry(0, NULL);

}

#include <opencv2/core/core.hpp>

#include <opencv2/imgcodecs.hpp>

#include <opencv2/imgproc/imgproc.hpp>

#include <opencv2/highgui/highgui.hpp>

#include <iostream>

#include <string>

#include "MaxiCode_Map.h"

using namespace cv;

using namespace std;

void getMaxiValues(Mat binaryimage, int *maxiValues);

void getCodeWords(int* maxiValues, unsigned char* testCodeWords);

void printMaxiValues(int* maxiValues);

void printCodeWords(unsigned char* testCodeWords);

static int opencv_entry( int argc, char** argv )

{

    string imageName("./maxicode.result.png"); // by default

    if( argc > 1)

    {

        imageName = argv[1];

    }

    Mat image, binaryimage;

    image = imread(imageName.c_str(), IMREAD_GRAYSCALE); // Read the file

    if( image.empty() )                      // Check for invalid input

    {

        cout <<  "Could not open or find the image" << std::endl ;

        return -1;

    }

    threshold(image, binaryimage, 150, 255, THRESH_BINARY);

    int maxiValues[MAX_BYTE_NUM];

    unsigned char testCodeWords[MAX_DATA_LENGTH];

    //Step 1:

    //Scan the image to get the corresponding data and compare the image and the output information to test whether it is accurate.

    getMaxiValues(binaryimage, maxiValues);

    printMaxiValues(maxiValues);

    //Step 2:

    //Get the character array by calculating the data of the integer array.

    getCodeWords(maxiValues, testCodeWords);

    printCodeWords(testCodeWords);

    //Show the original image and the modified image.

    namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.

    imshow( "Display window", image );                // Show our image inside it.

    namedWindow( "Binary window", WINDOW_AUTOSIZE ); // Create a window for display.

    imshow( "Binary window", binaryimage );                // Show our image inside it.

    waitKey(0); // Wait for a keystroke in the window

    return 0;

}

//Scan the image to get MaxiValues.

void getMaxiValues(Mat binaryimage, int *maxiValues)

{

    int heght = binaryimage.rows;

    int length = binaryimage.cols;

    double binaryimageX = length / 30;

    double binaryimageY = (heght - binaryimageX * sqrt(3) / 6) / 33;

    double binaryimageV = binaryimageY + binaryimageX * sqrt(3) / 6;

    int characterId = 0;

    int rowId, columnId, maxColumn;

    double x, y = binaryimageV / 2;

    uchar pixelval;

    for(rowId = 0;rowId < MAX_ROW;rowId++, y += binaryimageY)

    {

        if((rowId%2) == 0)

        {

            x = binaryimageX/2;

            maxColumn = ODD_MAX_COLUMN;

        }

        else

        {

            x = binaryimageX;

            maxColumn = EVEN_MAX_COLUMN;

        }

        for(columnId = 0;columnId < maxColumn;columnId++, characterId++, x += binaryimageX)

        {

            pixelval = binaryimage.at<uchar>(y+0.5, x+0.5);

            if(pixelval == 0)

            {

                maxiValues[characterId] = 1;

            }else

            {

                maxiValues[characterId] = 0;

            }

        }

        if(maxColumn == EVEN_MAX_COLUMN)

        {

            maxiValues[characterId] = 0;

            characterId++;

        }

    }

}

//Get codewords by calculating the data of maxiValues and MaxiGrid array.

void getCodeWords(int* maxiValues, unsigned char* testCodeWords)

{

    unsigned char* codeword = testCodeWords;

    memset(codeword, 0, MAX_DATA_LENGTH*sizeof(unsigned char));

    for(int index = 0 ;index < MAX_BYTE_NUM;index++)

    {

        int pixel = maxiValues[index];

        int codeindex = MaxiGrid[index];

        if(pixel > 0 && codeindex > 0)

        {

            int byteindex = (codeindex-1)/6;

            int bitindex  = (codeindex-1)%6;

            codeword = testCodeWords + byteindex;

            *codeword |= (1 << (5-bitindex));

        }

    }

}

//Print MaxiValues.

void printMaxiValues(int* maxiValues)

{

    for(int id = 0;id < MAX_BYTE_NUM;id++)

    {

        if(id%ODD_MAX_COLUMN == 0)

            printf("\n");

        printf("%d ", maxiValues[id]);

    }

}

//Print CodeWords.

void printCodeWords(unsigned char* testCodeWords)

{

    for(int ida = 0;ida < MAX_DATA_LENGTH;ida++)

    {

        if(ida%4 == 0)

            printf("\n");

        printf("0x%x ", testCodeWords[ida]);

    }

}

参考:
http://www.learnopencv.com/opencv-threshold-python-cpp/ http://www.cnblogs.com/xianglan/archive/2011/07/30/2122302.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: