Skip to content

Instantly share code, notes, and snippets.

Last active Oct 21, 2018
What would you like to do?
Crop image using javacv.
import static com.googlecode.javacv.cpp.opencv_core.cvCopy;
import static com.googlecode.javacv.cpp.opencv_core.cvCreateImage;
import static com.googlecode.javacv.cpp.opencv_core.cvGetSize;
import static com.googlecode.javacv.cpp.opencv_core.cvSetImageROI;
import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage;
import com.googlecode.javacv.cpp.opencv_core.CvRect;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
public class Cropper {
public static void main(String[] args) {
IplImage orig = cvLoadImage("orig.png");
// Creating rectangle by which bounds image will be cropped
CvRect r = new CvRect(100, 100, 200, 200);
// After setting ROI (Region-Of-Interest) all processing will only be done on the ROI
cvSetImageROI(orig, r);
IplImage cropped = cvCreateImage(cvGetSize(orig), orig.depth(), orig.nChannels());
// Copy original image (only ROI) to the cropped image
cvCopy(orig, cropped);
cvSaveImage("cropped.png", cropped);
Copy link

waldemarnt commented Dec 8, 2014

in the last changes of the javacv lib, the Rect constructor not accecpt int,int,int,int, you need set manually like this
CvRect rect = new CvRect();

Tnx for your job!


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment