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
#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
相关文章推荐
- 基础学习笔记之opencv(18):kmeans函数使用实例
- OpenCV 基础函数
- opencv读取图像的几个基本函数,imread、namedWindow、imshow、imwrite
- opencv第四课----smooth函数使用(提出几个课后作业,希望完成)
- opencv画图的几个函数例程
- Opencv中常用的几个函数
- 【Python&NLP】nltk的几个基础函数
- 图像混合学习。运用加权函数,学习opencv基础操作
- 基础学习笔记之opencv(24):imwrite函数的使用
- Python基础-几个基础函数应用
- OpenCV几个函数用法(笔记)
- 贴几个常用的基础函数
- 基础学习笔记之opencv(18):kmeans函数使用实例
- 基础学习笔记之opencv(24):imwrite函数的使用
- 基础学习笔记之opencv(24):imwrite函数的使用
- 【笨木头Lua专栏】基础补充02:函数的几个特别之处
- OpenCV做视频处理用到的几个函数
- opencv几个函数
- 有关OpenCV1.0中GUI命令的几个函数学习总结
- 基础学习笔记之opencv(24):imwrite函数的使用