Skip to content

Instantly share code, notes, and snippets.

@zudov
Last active October 21, 2018 14:46
Show Gist options
  • Save zudov/4967792 to your computer and use it in GitHub Desktop.
Save zudov/4967792 to your computer and use it in GitHub Desktop.
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);
}
}
@waldemarnt
Copy link

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();
rect.x(100);
rect.y(100);
rect.width(200);
rect.height(200);

Tnx for your job!

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