Hi there. I'm trying to crop just the form from this image without the content outside of the box. C:\fakepath\img.jpg
So far I've been able to get just the just content.
C:\fakepath\img2.PNG
Can someone please show me how to do this in code? OpenCv has been a challenge for me. I really need some hand holding. Thanks in advanced. This is the code i used to get my results
public doOpenCv(BufferedImage image){
Mat frameImg = bufferedImageToMat(image);
Mat colorImg = bufferedImageToMat(image);
frameImg = this.doCanny(frameImg);
try{
image = mat2BufferedImage(frameImg);
/**PROCESS**/
frameImg = processForm(frameImg,colorImg);
Imgproc.threshold(frameImg,frameImg,120,255,Imgproc.THRESH_BINARY);
image = mat2BufferedImage(frameImg);
}catch(Exception e){
......
}
}
public Mat processForm(Mat frameImg, Mat colorImg){
try{
frameImg = this.doBackgroundRemoval(frameImg);
List<MatOfPoint> contours = doFindContours(frameImg);
Rect cropCoordinates = returnCropCoordinates(contours);
Mat i = drawContours(frameImg,contours);
BufferedImage bi = mat2BufferedImage(i);
//Extract the colored image from the background
frameImg = doCrop(colorImg, cropCoordinates);
frameImg = doGrayScale(frameImg);
Size sz = new Size(3000,1500);
Imgproc.resize(frameImg,frameImg,sz);
//Imgproc.equalizeHist(frameImg,frameImg);
contours.clear();
return frameImg;
}catch(Exception e){
return frameImg;
}
}
public List<MatOfPoint> doFindContours(final Mat src) throws Exception {
Mat dst = new Mat(src.rows(), src.cols(), src.type());
src.copyTo(dst);
Imgproc.cvtColor(dst, dst, Imgproc.COLOR_BGR2GRAY);
final List<MatOfPoint> contours = new ArrayList<>();
final Mat hierarchy = new Mat();
Imgproc.findContours(dst, contours, hierarchy, Imgproc.CV_SHAPE_RECT, Imgproc.CHAIN_APPROX_SIMPLE);
Imgproc.cvtColor(dst, dst, Imgproc.COLOR_GRAY2BGR);
return contours;
}
public Rect returnCropCoordinates(List<MatOfPoint>contours){
int height=0;
int width=0;
int x=0;
int y=0;
for(int i=0; i < contours.size(); i++){
//if(Imgproc.contourArea(contours.get(i)) > 50){
MatOfPoint c1 = contours.get(i);
Rect rect = Imgproc.boundingRect(contours.get(i));
Mat contour = contours.get(i);
if(rect.height>height){
height = rect.height;
y = rect.y;
x = rect.x;
}
if(rect.width > width){
width = rect.width;
}
}
}
Rect rectCrop= new Rect(x,y,width,height);
return rectCrop;
}
public Mat doCrop(Mat img,Rect roi){
if(roi.x >=0 && roi.y >=0 && roi.width+roi.x < img.cols() && roi.height+ roi.y < img.rows() ) {
return img.submat(roi);
}else{
.......
}
}