本文中代码为作者结合网络相关资料基础上的开发成果,仅供参考学习,如有其他机器视觉领起算
法和软件定制需要,请联系作者Ema466650587@ qq.com
#include cvtools h
ifdef WIN32# include cstdint tends
namespace cvtoolsf
/ Phase correlation image registration including scale, rotation and translational shift
void phasecorrelate(const cv: Mat &iml, const cv: Mat &im2, float &scale, float &angle, cv: Point2f &shift)(
assert(iml size( ==im2 size) assert(iml type(==im2 type0)
scale= 1.0: angle = 0.0:
// cv: Mat imlfloat, im2float //iml convertto(imlfloat, CV 32F) //im2 convertto(im2float, CV 32F)
//cv: Mat imllogpolar= cvtools: logpolar(imlfloat, 100.0) //cv: Mat im2logpolar=cvtools: logpolar(im2float, 100.0)
/ hanning window cv: Mat window
cv: createhanningwindow(window, iml size), CV 32F)
/// determine scale and rotation // cv: Point2f scalerotation
phasecorrelation: phasecorrelate(imllogpolar, im2logpolar, window)
/// convert scale to proper scale
//scale=cv: exp(scalerotation x/100.0)
/// convert rotation angle to degrees
//angle=-scalerotation y *180.0/(im1 cols/2.0)
本文中代码为作者结合网络相关资料基础上的开发成果,仅供参考学习,如有其他机器视觉领起域算
法和软件定制需要,请联系作者Ema466650587@ qq.com
correct for scale and rotation // cv: Mat imlscaledrotated
//cv: Mat scalerotationmatrix
cv: getrotationmatrix2d(cv: Point2f(iml cols/2.0, iml rows/2.0) angle, scale
//cv: warpaffine(imifloat, imiscaledrotated, scalerotationmatrix imlfloat. size()
/ determine translational shift
//shift =phasecorrelation: phasecorrelate(iml, im2, window)
/ Log polar image transformation with log scaling factor (to bring
Intensities into proper range.
cv: Mat logpolar(const cv: Mat &image, float scale)[
cv: Mat result(image size 0, image type0)
Ipl Image imageipl(image
Ipl Image resultipl(result)
cvlogpolar(&imageipl, &resultipl, cv: Point2f(imageipl width/2.0 imagelpl. height/2.0), scale);
return result
/ Forward distortion of points. The inverse of the undistortion in cv: initundistortrectifymapo / Inspired by Pascal Thom
http: / / code . opencv . org / issues / 1 3 8 7 # note - 1 1 / Convention for distortion parameters
http //www.vision. caltech edu / bouguetj / calib doc / htmls / parameters . htm
void initdistortmap(const cv: Matx33f cameramatrix, const
cv: Vec<float, 5> distcoeffs, const cv: Size size, cv: Mat &mapl cv: Mat &map2)(
float fx = camerallatrix(0, 0)
本文中代码为作者结合网络相关资料基础上的开发成果,仅供参考学习,如有其他机器视觉领起算
法和软件定制需要,请联系作者Ema466650587@ qq.com
float fy= cameramatrix(1, 1) float ux cameramatrix(0, 2) float uy cameramlatrix(1, 2)
float ki distcoeffs [0 float k2 distcoeffs1] float p1=distcoeffs[2] float p2 =distcoeffs [ 3] float k3= distcoeffs[4]
mapl create(size, CV 32P) map2 create(size, CV 32F)
for(int col =0: col< size width: co1+)( for(int row=0: row<size. height: row++)(
/ move origo to principal point and convert using focal
length
float x =(col-ux)/fx float y =(row-uy)/fy
float xcorrected, corrected
//Step 1 correct distortion float r2=xx+y*y; //radial
corrected=x(1.+k1r2+k2ャr2ャr2+k3r2r2r2) corrected =y (1. +k1r2 +k2r2r2+ k3r2r2r2) //tangential
xcorrected=corrected+(2plxy +p2(r2+2xx)) corrected= corrected +(pl*(r2+2yy)+2p2x*y)
//convert back to pixel coordinates
float col displaced= corrected fx + ux float row displaced = corrected fy uy
/ correct the vector in the opposite direction mapl at(row, col)=col+(col-co1 displaced) map2. at(row, col)=row+(row-row displaced)
本文中代码为作者结合网络相关资料基础上的开发成果,仅供参考学习,如有其他机器视觉领起算
法和软件定制需要,请联系作者Ema466650587@ qq.com
/ Downsample a texture which was created in virtual column/row space for a diamond pixel array projector
cv: Mat diamonddownsample(cv: Mat &pattern)f
cv: Mat pattern diamond(pattern rows, pattern cols/2, CV 8UC3
for(unsigned int col=0: col pattern diamond cols: col++)( for(unsigned int row =0: row pattern diamond rows: row++)(
pattern_diamond at<cv: Vec3b>(row, co1)=pattern. at<cv: Vec3b>(row, col*2+ro%2);
return pattern diamond
roid mousecallback(int evt, int x, int y, int flags, void param)[ cv: Mat im =(cv: Mat)param: if (evt== cv: EVENT LBUTTONDOWN)( if(im->type( ==CV 8UC3)( printf("%d %d: %d, %d, %d\n
(int)(*im).at<cv: Vec3b>(y, 3)[0 (int)(kim). at<cv: Vec3b>(y, x)11 (int)(*im). at<cv: Vec3b(y, 3)[2])
else if (im->type(==CV 32P)( printf(%d %d: %f\n
im->at(y, x))
void imshow(const char *windowname, cv: Mat im, unsigned int unsigned int y)
/ Imshe
//if(!cv: Get Windowlandle(windowname))(
int windowflags =CV_WINDOW FREERATI0 CV WINDON KEEPRATIO
本文中代码为作者结合网络相关资料基础上的开发成果,仅供参考学习,如有其他机器视觉领起算
法和软件定制需要,请联系作者Ema466650587@ qq.com
cv: namedwindow(windowname, windowflags) cv: movewindow(windowname, x, y)
cv: imshow(windowname, im)
void images(const char *windowname, cv: Mat im)(
/ Imshow with scaled image
cv: Mat histimage(cv: Mat histogram)(
cv: Mat histimage(512, 640, CV8UC3, cv: Scalar(0))
/ Normalize the result to I2, histimage rows-2 I cv: normalize(histogram, histogram, 2, hist Image. rows-2
Cv: NORM MINMAX, -1, cv: Mato)
float bin w=(float)histimage. cols/(float)histogram rows
//Draw main histogram
for(int i=1: i< histogram. rows-10: i++)
cv: line (histimage, cv: Point(bin w*(i-1), histimage rows
cwround(histogram at(i-1)))
cv: Point( bin w*(i), histimage rows
cwround(histogram at(i)))
cv:: Scalar(255,255,255),2,4)
/ Draw red max
for(int i histogram. rows-10: i< histogram rows: i++)
cv: line(histimage, cv: Point(bin w*(i-1), hist Image. rows
cwround(histogram at(i-1)))
cv: Point(bin w*(i), histimage rows
cvround(histogram at (1)))
cv:: Scalar(255,0,0),2,4)
return histimage:
本文中代码为作者结合网络相关资料基础上的开发成果,仅供参考学习,如有其他机器视觉领域算
法和软件定制需要,请联系作者 Email:1466650587@qq.com
void hist(const char *windowname, cv: Mat histogram, unsigned int x unsigned int y)
/ Display
imshow(windowname, histimage(histogram), x, y) cv: Point(1, 2)
void writemat(cv: Mat const& mat, const char* filename, const char* varname, bool bgr2rgb)(
author Philip G. Lee <rocketman768egmail com
Write \b mat into \b filename
The variable name in matlab will be \ b varname. etla* in uncompressed. mat format (Level 5 MATLAB)for Ma
Ab bgr2rgb is true and there are 3 channels, swaps 1st
and 3rd
channels in the output. This is needed because Opencv
matrices
are bgr, while Matlab is rgb. This has been tested to
work with
3-channel single-precision floating point matrices, and I
hope
k it works on other types/channels, but not exactly sur xk Documentation at
Khttp: //www.mathworks. com / help / pdf doc / matlab / matfile format pdf
int textlen 116: chark text
int subsysoffsetlen= 8 char* subsysoffset: int verlen =2 char* ver: char flags int bytes: int padbytes:
int bytesperelement int i, j, k, k2: bool dobgrswap nar molass int32 t miclass:
本文中代码为作者结合网络相关资料基础上的开发成果,仅供参考学习,如有其他机器视觉领起算
法和软件定制需要,请联系作者Ema466650587@ qq.com
uchar constk rowptruint3_ttmp32; float tm FILE fp
// Matlab constants
const uint16 t MI=0x4d49: //Contains Miin ascii const int32 t MIINT8 1 const int32 t MIULNT8 =2 const nt32 t MIINT16 3: const int32 t MIUINT16 =4 const int32 t MIINT32 =5 const nt32 t MIUINT32 =6 const int32 t MISINGLE 7 const int32 t MIDOUBLE 9 const int32 t MMATRIX 14 const char MXDQUBLE CLASS =6 const char MXSINGLE CLASS 7 const char MXINT8 CLASS =8 const char MXUINT8 CLASS =9 const char MXINT16 CLASS= 10 const char MXUINT16 CLASS 11 const char MXINT32 CLASS 12 const char MXUINT32 CLASS=13
const uint61t zero =0: //Used for padding
fp= fopen(filename, wb)
if( fp==0) return:
const Int rows mat rows const int cols mat cols
const int chans =mat channelso
dobgrswap =(chans=3)&& bgr2rgb
//I hope this mapping is right: - switch(mat depth)f case CV 8U:
mxclass MXUINT8 CLASS miclass = MIUINT8 bytesperelement= 1
本文中代码为作者结合网络相关资料基础上的开发成果,仅供参考学习,如有其他机器视觉领起算
法和软件定制需要,请联系作者Ema466650587@ qq.com
break case CV 8S:
mxclass MXINT8 CLASS: miclass MIINT8: bytesperelement
本文中代码为作者结合网络相关资料基础上的开发成果,仅供参考学习,如有其他机器视觉领起算
法和软件定制需要,请联系作者Ema466650587@ qq.com
break case CV 8S:
mxclass MXINT8 CLASS: miclass MIINT8: bytesperelement 1 break: case CV 160:
mxclass= MXUINT16 CLASS miclass = MIUINT16: bytesperelement =2 break case CV 16s.
mxclass = MXINT16 CLASS miclass = MIINT16 bytesperelement 2 break case CV 32s:
mxclass MXINT32 CLASS miclass MIINT32: bytesperelement =4 break case CV 32F.
mxclass MXSINGLE CLASS miclass= MISINGLE bytesperelement 4 break case CV 64F:
mxclass= MXDOUBLE CLASS miclass MIDOUBLE: bytesperelement =8 break
default
return
//
=mat-file header (128 bytes, page 1-
text= new char [textlen]: // Human-readable text.
text[textlen-1]=,exten memset(text
0
const char* t = MATLAB 5.0 Mat-file Platform: PCWTN memcpy( text, t, strlen(t))
本文中代码为作者结合网络相关资料基础上的开发成果,仅供参考学习,如有其他机器视觉领起域算
法和软件定制需要,请联系作者Ema466650587@ qq.com
subsysoffset=new char [subsysoffsetlen]: //Zeros for us. memset( subsysoffset, 0x00, subsysoffsetlen ver= new char [ verlen]ver[0]=0x00;
ver[]=0x01
fwrite( text, 1, textlen, fp
fwrite(subsysoffset, 1, subsysoffsetlen, fp ): fwrite( ver, 1, verlen, fp)
//Endian indicator. MI will show up as MI"on big-endian / systems and "IM"on little-endian systems. fwrite(&MI, 2, 1, fp
/+++++++++++++++++++
++++++
//
Data element tag (8 bytes, page 1
bytes =16 +24+(8+ strlen(varname)+(8 (strlen(varname)%8))%8)
fwrite(&MIMATRIX, 4, 1, fp): //Data type fwrite( &bytes, 4, 1, fp): / Data size in bytes.
++++++++++++++++++++++++++++++++++++++++++++++++++
++++++
//a
Array flags (16 bytes, page 1
bytes =8
fwrite(&MIUINT32, 4, 1, fp) write( &bytes, 4, 1, fp
flags =0x00: //Complex, logical, and global flags all off
tmp32 0
tmp32 =(flags 8)(mxclass rite(&tmp32, 4, 1, fp
fwrite(&zero, 4, 1, fp): //Padding to 64-bit boundary.
/+++++++++++++++++++++
++++++