akniazi
akniazi

Reputation: 732

Cropping image in Android using opencv

I am using OpenCV 2.3.1 in Android. I need to crop the image into half. What I am doing is:

    Mat mIntermediateMat2 = new Mat(frame_height,frame_width,rgba.type);
    mIntermediateMat2 = rgba.clone();
    mIntermediateMat2 = mIntermediateMat2.rowRange(0,frame_height/2);

Will the third step do the job or I have to add something more? I saw Mat::operator() in opencv 2.3 documentation but unfortunately not able to find in the opencv Android package.

Upvotes: 15

Views: 19493

Answers (3)

Sam Uong
Sam Uong

Reputation: 286

There are a few constructors for the Mat class, one of which takes a Mat and an ROI (region of interest).

Here's how to do it in Android/Java:

Mat uncropped = getUncroppedImage();
Rect roi = new Rect(x, y, width, height);
Mat cropped = new Mat(uncropped, roi);

Upvotes: 27

Matteo Mannino
Matteo Mannino

Reputation: 399

Seems cv::getRectSubPix does what you want. Plus you don't have to allocate more space than you need. Also it does necessary interpolations if the cropped area is not aligned over an exact pixel.

This should do what you want. Input type will be the output type.

Mat dst;
getRectSubPix(src, Size(src.rows()/2,src.cols()), Point2f(src.rows()/4, src.cols()/2), dst);

Upvotes: 0

mevatron
mevatron

Reputation: 14011

I have always done cropping this way:

Mat image = ...; // fill it however you want to
Mat crop(image, Rect(0, 0, image.cols, image.rows / 2)); // NOTE: this will only give you a reference to the ROI of the original data

// if you want a copy of the crop do this:
Mat output = crop.clone();

Hope that helps!

Upvotes: 2

Related Questions