Crop video capture


I’m trying to ‘mask’ off a portion of my video input feed - before I pass it to a blob detection routine, in order to eliminate some noise…

I could just ask for centroids within a certain rectangle / polygon, but I’d rather do it with the camera…

I’ve done this kind of thing before in Processing, but how would I go about in oF ?

(just getting into this, but thought I’d post the question first (ie not just being lazy!))


~ J

Sounds like you are using OpenCv so you may just want to use ROI (region of interest)
One watchout is to make sure you reset it after you are done using it

Hi, this question comes up a lot, depending on what sort of cropping you need, ther eare different ways of doing it. If you need a simple rectangular crop, you can use the ofPixels::crop methods or jcleave’s suggestion to use openCV functionality. If you need non rectangular cropping (to mitigate perspective distortion for example) you might look at this-thread which provides some other solutions for quad warping including an openCV approach warpIntoMe() or a routine I wrote called getQuadSubImage().

Good luck!

I’ve been looking for an example implementation of the simple rectangular crop but I’ve come up empty-handed. Found some really old implementation from before the ofPixels::crop existed in the forum, so maybe someone could post an example of how to crop a video using ofPixels() and crop()?

Thanks in advance!

You can easily crop the video by calling the
cvSetMouseCallback("image", mouseHandler, NULL);

the mouseHandler function is given below:

 void mouseHandler(int event, int x, int y, int flags, void* param)
            if (event == CV_EVENT_LBUTTONDOWN && !drag)
                /* left button clicked. ROI selection begins */
                point1 = Point(x, y);
                drag = 1;

    if (event == CV_EVENT_MOUSEMOVE && drag)
        /* mouse dragged. ROI being selected */
        Mat img1 = img.clone();
        point2 = Point(x, y);
        rectangle(img1, point1, point2, CV_RGB(255, 0, 0), 3, 8, 0);
        imshow("image", img1);

    if (event == CV_EVENT_LBUTTONUP && drag)
        point2 = Point(x, y);
        rect = Rect(point1.x,point1.y,x-point1.x,y-point1.y);
        drag = 0;
        roiImg = img(rect);

    if (event == CV_EVENT_LBUTTONUP)
       /* ROI selected */
        select_flag = 1;
        drag = 0;

The details can be found on this link:How to Crop Video from Webcam using OpenCV

create a sprite. render the video there. and scale the video in that specific “sprite”. or within its boundaries