Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
openframeworks openCV cvRemap
#include "ofApp.h"
float latency;
//https://forum.openframeworks.cc/t/copying-a-row-of-pixels-to-another-row-with-sub-pixel-accuracy/7610/3
//--------------------------------------------------------------
void ofApp::setup(){
cameraWidth = 640; //320; //640;
cameraHeight = 480; //405; //480;
cameraWidth2 = cameraWidth; // /2;
cameraHeight2 = cameraHeight; // /2;
vidGrabber.setVerbose(true);
vidGrabber.initGrabber(cameraWidth,cameraHeight);
unwarpedImageOpenCV.allocate(cameraWidth2,cameraHeight2);
warpedImageOpenCV.allocate(cameraWidth2,cameraHeight2);
srcxArrayOpenCV.allocate(cameraWidth2,cameraHeight2);
srcyArrayOpenCV.allocate(cameraWidth2,cameraHeight2);
blackOpenCV = cvScalarAll(0);
/* Interpolation method:
[http://opencv.willowgarage.com/documentation/cpp/geometric-image-transformations.html#cv-resize](http://opencv.willowgarage.com/documentation/cpp/geometric-image-transformations.html#cv-resize)
CV_INTER_NEAREST nearest-neighbor interpolation
CV_INTER_LINEAR bilinear interpolation (used by default)
CV_INTER_AREA resampling using pixel area relation. It may be the
preferred method for image decimation, as it gives moire-free results.
But when the image is zoomed, it is similar to the INTER_NEAREST method
CV_INTER_CUBIC bicubic interpolation over 4x4 pixel neighborhood
CV_INTER_LANCZOS4 Lanczos interpolation over 8x8 pixel neighborhood
*/
interpMethod = CV_INTER_CUBIC;
// interpMethod = CV_INTER_LANCZOS4;
/* from:
[http://stackoverflow.com/questions/3596385/can-anybody-explain-cvremap-with-a-code-using-cvremap-in-opencv](http://stackoverflow.com/questions/3596385/can-anybody-explain-cvremap-with-a-code-using-cvremap-in-opencv)
I use cvRemap to apply distortion correction. The map_x part is in
image resolution and stores for each pixel the x-offset to be applied,
while map_y part is the same for the y-offset.
in case of undistortion
# create map_x/map_y
self.map_x = cvCreateImage(cvGetSize(self.image), IPL_DEPTH_32F, 1)
self.map_y = cvCreateImage(cvGetSize(self.image), IPL_DEPTH_32F, 1)
# I know the camera intrisic already so create a distortion map out
# of it for each image pixel
# this defined where each pixel has to go so the image is no longer
# distorded
cvInitUndistortMap(self.intrinsic, self.distortion, self.map_x,
self.map_y)
# later in the code:
# "image_raw" is the distorted image, i want to store the
undistorted into
# "self.image"
cvRemap(image_raw, self.image, self.map_x, self.map_y)
Therefore: map_x/map_y are floating point values and in image
resolution, like two images in 1024x768. What happens in cvRemap is
basicly something like
orig_pixel = input_image[x,y]
new_x = map_x[x,y]
new_y = map_y[x,y]
output_image[new_x,new_y] = orig_pixel
*/
ofBackground(100,100,100);
verticalDistanceToMoveX_2 = 0;
}
void ofApp::exit(){
}
//--------------------------------------------------------------
void ofApp::update(){
ofBackground(100, 100, 100);
bool bNewFrame = false;
/* so lets make a map that just moves all the pixels down 100.4f
pixels */
verticalDistanceToMoveX_2 += 0.1; //100.4f;
float verticalDistanceToMoveY = ofGetMouseY();
int offsetX = 200;
int roiW = cameraWidth2/2;
int roiH = cameraHeight2/2;
for(int j=0; j < cameraHeight2; j++){
int jj = ofMap(j, 0, cameraHeight2, 0, roiH);
for(int i=0; i < cameraWidth2; i++){
int positionInArray = j*cameraWidth2 + i;
int ii = ofMap(i, 0, cameraWidth2, 0, roiW);
srcxArrayOpenCV.getPixelsAsFloats()[positionInArray] = (float)ii+verticalDistanceToMoveX_2;
srcyArrayOpenCV.getPixelsAsFloats()[positionInArray] = (float)jj+verticalDistanceToMoveY;
}
}
vidGrabber.update();
bNewFrame = vidGrabber.isFrameNew();
if (bNewFrame){
unwarpedImageOpenCV.setFromPixels(vidGrabber.getPixels());
cvRemap(unwarpedImageOpenCV.getCvImage(),
warpedImageOpenCV.getCvImage(),
srcxArrayOpenCV.getCvImage(),
srcyArrayOpenCV.getCvImage(),
interpMethod | CV_WARP_FILL_OUTLIERS, blackOpenCV );
unwarpedImageOpenCV.flagImageChanged();
warpedImageOpenCV.flagImageChanged();
}
}
//--------------------------------------------------------------
void ofApp::draw(){
ofSetColor(255,255,255);
unwarpedImageOpenCV.draw(0,0);
warpedImageOpenCV.draw(cameraWidth,0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment