How to crop image using contour not in Rectangle shape in OpenCV Java? -
i learn opencv 2 weeks ago , planning use on spring project java wrapper. want crop image based on shape , embed pure white background. tried several method internet (hsv average comparison, cropping using standard roi, masking, etc) , decided use these steps got forum :
- convert img grayscale
- threshold binary image
- use findcontours actual outline
- crop image based on contour result
i hope can me on matter or maybe guide using method, thank much.
example image source : image source
current extracted result using rectangle roi (the code not contain on below section because i'm trying change method) : cropped image
here tried (using opencv 3.2.0) :
system.loadlibrary(core.native_library_name); // reading image file , storing in matrix object string file ="c:/examples/opencv/smartphone.jpg"; mat src = imgcodecs.imread(file); mat srcclone = new mat(); // clone won't affect original image imgproc.cvtcolor(src, srcclone, imgproc.color_bgr2gray); // convert color gray double threshold = imgproc.threshold(srcclone, srcclone, 50, 255, imgproc.thresh_binary); // add gaussian blur effect reduce noises imgproc.gaussianblur(srcclone, srcclone, new size(5,5), 0); //find edges using canny mat edges = new mat(); imgproc.canny(srcclone, edges, 100, 200); //find contour based on canny result list<matofpoint> contours = new arraylist<matofpoint>(); final mat hierarchy = new mat(); imgproc.findcontours(edges, contours, hierarchy, imgproc.retr_external , imgproc.chain_approx_simple); //don't know next step if not using rectangle roi ...
Comments
Post a Comment