| Rev 28 |
|
Rev 29 |
|
|
|
#include "ofxCvImage.h" |
|
#include "ofxCvImage.h" |
#include "ofxCvGrayscaleImage.h" |
|
#include "ofxCvGrayscaleImage.h" |
#include "ofxCvColorImage.h" |
|
#include "ofxCvColorImage.h" |
#include "ofxCvFloatImage.h" |
|
#include "ofxCvFloatImage.h" |
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
ofxCvImage::ofxCvImage() { |
|
ofxCvImage::ofxCvImage() { |
width = 0; |
|
width = 0; |
height = 0; |
|
height = 0; |
bUseTexture = true; |
|
bUseTexture = true; |
bAllocated = false; |
|
bAllocated = false; |
pixels = NULL; |
|
pixels = NULL; |
} |
|
} |
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
ofxCvImage::~ofxCvImage() { |
|
ofxCvImage::~ofxCvImage() { |
clear(); |
|
clear(); |
} |
|
} |
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::convertToRange(float scaleMin, float scaleMax ){ |
|
void ofxCvImage::convertToRange(float scaleMin, float scaleMax ){ |
float range = (scaleMax - scaleMin); |
|
float range = (scaleMax - scaleMin); |
float scale = 255/range; |
|
float scale = 255/range; |
float offset = - (scaleMin * scale); // ie, 0.5 - 1 = scale by (255*2), subtract 255, 128-255 = scale by 1/2, subtract 128 |
|
float offset = - (scaleMin * scale); // ie, 0.5 - 1 = scale by (255*2), subtract 255, 128-255 = scale by 1/2, subtract 128 |
cvConvertScale( cvImage, cvImageTemp, scale, offset ); |
|
cvConvertScale( cvImage, cvImageTemp, scale, offset ); |
swapTemp(); |
|
swapTemp(); |
} |
|
} |
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::clear() { |
|
void ofxCvImage::clear() { |
|
|
|
// ------------------------------ only delete if the |
|
// ------------------------------ only delete if the |
// ------------------------------ image is really an image. |
|
// ------------------------------ image is really an image. |
// ------------------------------ ie, w > 0, h > 0 |
|
// ------------------------------ ie, w > 0, h > 0 |
|
|
|
if (bAllocated == true){ |
|
if (bAllocated == true){ |
if (width > 0 && height > 0){ |
|
if (width > 0 && height > 0){ |
cvReleaseImage( &cvImage ); |
|
cvReleaseImage( &cvImage ); |
cvReleaseImage( &cvImageTemp ); |
|
cvReleaseImage( &cvImageTemp ); |
} |
|
} |
delete pixels; |
|
delete pixels; |
width = 0; |
|
width = 0; |
height = 0; |
|
height = 0; |
|
|
|
if( bUseTexture ) { |
|
if( bUseTexture ) { |
tex.clear(); |
|
tex.clear(); |
} |
|
} |
|
|
|
bAllocated = false; |
|
bAllocated = false; |
} |
|
} |
} |
|
} |
|
|
|
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::setUseTexture( bool bUse ) { |
|
void ofxCvImage::setUseTexture( bool bUse ) { |
bUseTexture = bUse; |
|
bUseTexture = bUse; |
} |
|
} |
|
|
|
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::swapTemp() { |
|
void ofxCvImage::swapTemp() { |
IplImage* temp; |
|
IplImage* temp; |
temp = cvImage; |
|
temp = cvImage; |
cvImage = cvImageTemp; |
|
cvImage = cvImageTemp; |
cvImageTemp = temp; |
|
cvImageTemp = temp; |
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set Pixel Data - Scalars |
|
// Set Pixel Data - Scalars |
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::set( int value ) { |
|
void ofxCvImage::set( int value ) { |
cvSet( cvImage, cvScalar(value) ); |
|
cvSet( cvImage, cvScalar(value) ); |
} |
|
} |
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::operator -= ( float scalar ) { |
|
void ofxCvImage::operator -= ( float scalar ) { |
cvSubS( cvImage, cvScalar(scalar), cvImageTemp ); |
|
cvSubS( cvImage, cvScalar(scalar), cvImageTemp ); |
swapTemp(); |
|
swapTemp(); |
} |
|
} |
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::operator += ( float scalar ) { |
|
void ofxCvImage::operator += ( float scalar ) { |
cvAddS( cvImage, cvScalar(scalar), cvImageTemp ); |
|
cvAddS( cvImage, cvScalar(scalar), cvImageTemp ); |
swapTemp(); |
|
swapTemp(); |
} |
|
} |
|
|
|
|
|
|
|
|
|
// Image Filter Operations |
|
// Image Filter Operations |
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::dilate() { |
|
void ofxCvImage::dilate() { |
cvDilate( cvImage, cvImageTemp, 0, 1 ); |
|
cvDilate( cvImage, cvImageTemp, 0, 1 ); |
swapTemp(); |
|
swapTemp(); |
} |
|
} |
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::erode() { |
|
void ofxCvImage::erode() { |
cvErode( cvImage, cvImageTemp, 0, 1 ); |
|
cvErode( cvImage, cvImageTemp, 0, 1 ); |
swapTemp(); |
|
swapTemp(); |
} |
|
} |
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::blur( int value ) { |
|
void ofxCvImage::blur( int value ) { |
cvSmooth( cvImage, cvImageTemp, CV_BLUR , value); |
|
cvSmooth( cvImage, cvImageTemp, CV_BLUR , value); |
swapTemp(); |
|
swapTemp(); |
} |
|
} |
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::blurGaussian( int value ) { |
|
void ofxCvImage::blurGaussian( int value ) { |
cvSmooth( cvImage, cvImageTemp, CV_GAUSSIAN ,value ); |
|
cvSmooth( cvImage, cvImageTemp, CV_GAUSSIAN ,value ); |
swapTemp(); |
|
swapTemp(); |
} |
|
} |
|
|
|
| |
|
//-------------------------------------------------------------------------------- |
| |
|
void ofxCvGrayscaleImage::invert(){ |
| |
|
cvNot(cvImage, cvImage); |
| |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
// Image Transformation Operations |
|
// Image Transformation Operations |
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::mirror( bool bFlipVertically, bool bFlipHorizontally ) { |
|
void ofxCvImage::mirror( bool bFlipVertically, bool bFlipHorizontally ) { |
int flipMode = 0; |
|
int flipMode = 0; |
|
|
|
if( bFlipVertically && !bFlipHorizontally ) flipMode = 0; |
|
if( bFlipVertically && !bFlipHorizontally ) flipMode = 0; |
else if( !bFlipVertically && bFlipHorizontally ) flipMode = 1; |
|
else if( !bFlipVertically && bFlipHorizontally ) flipMode = 1; |
else if( bFlipVertically && bFlipHorizontally ) flipMode = -1; |
|
else if( bFlipVertically && bFlipHorizontally ) flipMode = -1; |
else return; |
|
else return; |
|
|
|
cvFlip( cvImage, cvImageTemp, flipMode ); |
|
cvFlip( cvImage, cvImageTemp, flipMode ); |
swapTemp(); |
|
swapTemp(); |
} |
|
} |
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::translate( float x, float y ) { |
|
void ofxCvImage::translate( float x, float y ) { |
transform( 0, 0,0, 1,1, x,y ); |
|
transform( 0, 0,0, 1,1, x,y ); |
} |
|
} |
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::rotate( float angle, float centerX, float centerY ) { |
|
void ofxCvImage::rotate( float angle, float centerX, float centerY ) { |
transform( angle, centerX, centerY, 1,1, 0,0 ); |
|
transform( angle, centerX, centerY, 1,1, 0,0 ); |
} |
|
} |
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::scale( float scaleX, float scaleY ) { |
|
void ofxCvImage::scale( float scaleX, float scaleY ) { |
transform( 0, 0,0, scaleX,scaleY, 0,0 ); |
|
transform( 0, 0,0, scaleX,scaleY, 0,0 ); |
} |
|
} |
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::transform( float angle, float centerX, float centerY, |
|
void ofxCvImage::transform( float angle, float centerX, float centerY, |
float scaleX, float scaleY, |
|
float scaleX, float scaleY, |
float moveX, float moveY ) |
|
float moveX, float moveY ) |
{ |
|
{ |
float sina = sin(angle * DEG_TO_RAD); |
|
float sina = sin(angle * DEG_TO_RAD); |
float cosa = cos(angle * DEG_TO_RAD); |
|
float cosa = cos(angle * DEG_TO_RAD); |
CvMat* transmat = cvCreateMat( 2,3, CV_32F ); |
|
CvMat* transmat = cvCreateMat( 2,3, CV_32F ); |
cvmSet( transmat, 0,0, scaleX*cosa ); |
|
cvmSet( transmat, 0,0, scaleX*cosa ); |
cvmSet( transmat, 0,1, scaleY*sina ); |
|
cvmSet( transmat, 0,1, scaleY*sina ); |
cvmSet( transmat, 0,2, -centerX*scaleX*cosa - centerY*scaleY*sina + moveX + centerX ); |
|
cvmSet( transmat, 0,2, -centerX*scaleX*cosa - centerY*scaleY*sina + moveX + centerX ); |
cvmSet( transmat, 1,0, -1.0*scaleX*sina ); |
|
cvmSet( transmat, 1,0, -1.0*scaleX*sina ); |
cvmSet( transmat, 1,1, scaleY*cosa ); |
|
cvmSet( transmat, 1,1, scaleY*cosa ); |
cvmSet( transmat, 1,2, -centerY*scaleY*cosa + centerX*scaleX*sina + moveY + centerY); |
|
cvmSet( transmat, 1,2, -centerY*scaleY*cosa + centerX*scaleX*sina + moveY + centerY); |
|
|
|
cvWarpAffine( cvImage, cvImageTemp, transmat ); |
|
cvWarpAffine( cvImage, cvImageTemp, transmat ); |
swapTemp(); |
|
swapTemp(); |
|
|
|
cvReleaseMat( &transmat ); |
|
cvReleaseMat( &transmat ); |
} |
|
} |
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::undistort( float radialDistX, float radialDistY, |
|
void ofxCvImage::undistort( float radialDistX, float radialDistY, |
float tangentDistX, float tangentDistY, |
|
float tangentDistX, float tangentDistY, |
float focalX, float focalY, |
|
float focalX, float focalY, |
float centerX, float centerY ){ |
|
float centerX, float centerY ){ |
float camIntrinsics[] = { focalX, 0, centerX, 0, focalY, centerY, 0, 0, 1 }; |
|
float camIntrinsics[] = { focalX, 0, centerX, 0, focalY, centerY, 0, 0, 1 }; |
float distortionCoeffs[] = { radialDistX, radialDistY, tangentDistX, tangentDistY }; |
|
float distortionCoeffs[] = { radialDistX, radialDistY, tangentDistX, tangentDistY }; |
cvUnDistortOnce( cvImage, cvImageTemp, camIntrinsics, distortionCoeffs, 1 ); |
|
cvUnDistortOnce( cvImage, cvImageTemp, camIntrinsics, distortionCoeffs, 1 ); |
swapTemp(); |
|
swapTemp(); |
} |
|
} |
|
|
|
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::remap( IplImage* mapX, IplImage* mapY ) { |
|
void ofxCvImage::remap( IplImage* mapX, IplImage* mapY ) { |
cvRemap( cvImage, cvImageTemp, mapX, mapY ); |
|
cvRemap( cvImage, cvImageTemp, mapX, mapY ); |
swapTemp(); |
|
swapTemp(); |
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
/** |
* A +-------------+ B |
|
* A +-------------+ B |
* / \ |
|
* / \ |
* / \ |
|
* / \ |
* / \ |
|
* / \ |
* D +-------------------- + C |
|
* D +-------------------- + C |
*/ |
|
*/ |
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::warpPerspective( const ofPoint& A, const ofPoint& B, |
|
void ofxCvImage::warpPerspective( const ofPoint& A, const ofPoint& B, |
const ofPoint& C, const ofPoint& D ) |
|
const ofPoint& C, const ofPoint& D ) |
{ |
|
{ |
// compute matrix for perspectival warping (homography) |
|
// compute matrix for perspectival warping (homography) |
CvPoint2D32f cvsrc[4]; |
|
CvPoint2D32f cvsrc[4]; |
CvPoint2D32f cvdst[4]; |
|
CvPoint2D32f cvdst[4]; |
CvMat* translate = cvCreateMat( 3,3, CV_32FC1 ); |
|
CvMat* translate = cvCreateMat( 3,3, CV_32FC1 ); |
cvSetZero( translate ); |
|
cvSetZero( translate ); |
|
|
|
cvdst[0].x = 0; |
|
cvdst[0].x = 0; |
cvdst[0].y = 0; |
|
cvdst[0].y = 0; |
cvdst[1].x = width; |
|
cvdst[1].x = width; |
cvdst[1].y = 0; |
|
cvdst[1].y = 0; |
cvdst[2].x = width; |
|
cvdst[2].x = width; |
cvdst[2].y = height; |
|
cvdst[2].y = height; |
cvdst[3].x = 0; |
|
cvdst[3].x = 0; |
cvdst[3].y = height; |
|
cvdst[3].y = height; |
|
|
|
cvsrc[0].x = A.x; |
|
cvsrc[0].x = A.x; |
cvsrc[0].y = A.y; |
|
cvsrc[0].y = A.y; |
cvsrc[1].x = B.x; |
|
cvsrc[1].x = B.x; |
cvsrc[1].y = B.y; |
|
cvsrc[1].y = B.y; |
cvsrc[2].x = C.x; |
|
cvsrc[2].x = C.x; |
cvsrc[2].y = C.y; |
|
cvsrc[2].y = C.y; |
cvsrc[3].x = D.x; |
|
cvsrc[3].x = D.x; |
cvsrc[3].y = D.y; |
|
cvsrc[3].y = D.y; |
|
|
|
cvWarpPerspectiveQMatrix( cvsrc, cvdst, translate ); // calculate homography |
|
cvWarpPerspectiveQMatrix( cvsrc, cvdst, translate ); // calculate homography |
cvWarpPerspective( cvImage, cvImageTemp, translate ); |
|
cvWarpPerspective( cvImage, cvImageTemp, translate ); |
swapTemp(); |
|
swapTemp(); |
cvReleaseMat( &translate ); |
|
cvReleaseMat( &translate ); |
} |
|
} |
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
void ofxCvImage::warpIntoMe( const ofxCvGrayscaleImage& mom, |
|
void ofxCvImage::warpIntoMe( const ofxCvGrayscaleImage& mom, |
ofPoint src[4], ofPoint dst[4] ) |
|
ofPoint src[4], ofPoint dst[4] ) |
{ |
|
{ |
// compute matrix for perspectival warping (homography) |
|
// compute matrix for perspectival warping (homography) |
CvPoint2D32f cvsrc[4]; |
|
CvPoint2D32f cvsrc[4]; |
CvPoint2D32f cvdst[4]; |
|
CvPoint2D32f cvdst[4]; |
CvMat* translate = cvCreateMat( 3, 3, CV_32FC1 ); |
|
CvMat* translate = cvCreateMat( 3, 3, CV_32FC1 ); |
cvSetZero( translate ); |
|
cvSetZero( translate ); |
for (int i = 0; i < 4; i++ ) { |
|
for (int i = 0; i < 4; i++ ) { |
cvsrc[i].x = src[i].x; |
|
cvsrc[i].x = src[i].x; |
cvsrc[i].y = src[i].y; |
|
cvsrc[i].y = src[i].y; |
cvdst[i].x = dst[i].x; |
|
cvdst[i].x = dst[i].x; |
cvdst[i].y = dst[i].y; |
|
cvdst[i].y = dst[i].y; |
} |
|
} |
cvWarpPerspectiveQMatrix( cvsrc, cvdst, translate ); // calculate homography |
|
cvWarpPerspectiveQMatrix( cvsrc, cvdst, translate ); // calculate homography |
cvWarpPerspective( mom.getCvImage(), cvImage, translate); |
|
cvWarpPerspective( mom.getCvImage(), cvImage, translate); |
cvReleaseMat( &translate ); |
|
cvReleaseMat( &translate ); |
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
// Other Image Operations |
|
// Other Image Operations |
|
|
|
//-------------------------------------------------------------------------------- |
|
//-------------------------------------------------------------------------------- |
int ofxCvImage::countNonZeroInRegion( int x, int y, int w, int h ) const { |
|
int ofxCvImage::countNonZeroInRegion( int x, int y, int w, int h ) const { |
if (w == 0 || h == 0) return 0; |
|
if (w == 0 || h == 0) return 0; |
int count = 0; |
|
int count = 0; |
cvSetImageROI( cvImage, cvRect(x,y,w,h) ); |
|
cvSetImageROI( cvImage, cvRect(x,y,w,h) ); |
count = cvCountNonZero( cvImage ); |
|
count = cvCountNonZero( cvImage ); |
cvResetImageROI( cvImage ); |
|
cvResetImageROI( cvImage ); |
return count; |
|
return count; |
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
|