Pixel Rotation

Does anyone know of a good way to transform a 640 x 480 image to 480 x 640 image.

I want to rotate a video stream(pixels) 90 degrees before I process it. Not just draw it using ofRotate()

Cheers

If you’re using ofxOpenCv you can just call rotate() on the image and that will actually just rotate the image itself. If you’re just using a regular webcam feed then you can get the pixel data with a call to getPixels() and then rotate the ofPixels objects with rotate90().

  
  
ofPixels p = grabber.getPixelsRef();  
p.rotate90();  
  

Hope that helps,

I’ve been doing a few tests using ofxOpenCv to this very thing, albeit with a 1280x720 video feed, feeding into ofxFaceTracker for some further processing. The issue I had with using OpenCv’s rotate() was that I couldn’t figure out how to end up with an uncropped 720x1280 image without doing a few resize() operations along the way.

int w = 1280;
int h = 720;
cvImage.setFromPixels( camera, w, h );
cvImage.resize(w, w );
cvImage.rotate(90, w/2, w/2 );
cvImage.resize(h, w);

which is obviously creating a lot of work and killing my framerate by about 20fps.

grabbing the pixels from the camera and using rotate90(-1) then feeding that to the cvImage is WAY better performance-wise, which is a tad confusing since rotate90() is allocating memory to do the rotation.

What is the best way to do this rotation every frame? It certainly feels as though I’m doing it wrong with ofxOpenCv…

(I found a stackoverflow post about the same thing, using cv::Transpose and cv::Flip, but am none too sure how to give that a try within oF.)

i usually do it using ofPixels:

video->getPixelsRef().rotate90To(rotatedInput,numInputRotation90);

rotateInput is an ofPixels with the rotated dimensions

3 Likes

Works great! thank you!

not sure how you would implement this, could you elaborate a little more?
Is this actual syntax or are you explaining something?
Sorry if it’s a dumb question, I’m really new to this.

is it:

mirror.getPixelsRef().rotate90To(vid.getPixelsRef(), 1);

my ofApp.h looks like this:

ofVideoGrabber vid;
ofxCvColorImage mirror;

I tried that line of code but it does not display any image on screen.

Since I could rely on ofRotate(), I am not using this code, an easy way to rotate the video in the display.

ofPushMatrix();
ofTranslate(0,0,0);
ofRotateZ(90); // angle you want to rotate
vid.draw(0, -horizCenter); // video feed from the webcam, horizCenter is just a little math to center the video on any size screen.
ofPopMatrix();

since my capture needs are very small, I am using the Code posted by nickhubben to create a rotated capture at 1/10th the size of the original video.

gifSize.setFromPixels(vid.getPixels(), frameW, frameH); // frameW, frameH - Video capture height and width.
gifSize.mirror(0, 1);

gifSize.resize(frameW, frameW);
gifSize.rotate(90, frameW/2, frameW/2);
gifSize.resize(gifW, gifH); // gifW = 2*frameH/10; (Rotated Size)

Quick update.
Noticed that my main video feed was no longer mirrored, so a simple negative width did the trick.

vid.draw(0, frameH-horizCenter, frameW, -frameH); // direct webcam feed.

Cheers!

i have used rotate90To and works, kind off.

video->getPixelsRef().rotate90To(rotatedInput,0);
video->getPixelsRef().rotate90To(rotatedInput,1);
video->getPixelsRef().rotate90To(rotatedInput,3);

but setting it to 2 does not work

video->getPixelsRef().rotate90To(rotatedInput,2);

the resulting image looks like it gets not allocate properly. something about rows and cols getting mixed up.

here my code: https://www.dropbox.com/s/2ser94ma3ztg5yh/videoGrabberExample.zip?dl=0