How to detect colored rectangle from image in android? [closed]
hello all, i want detect specific colored rectangle from paper sheet which indicate border of paper.so i want detect that rectangle and crop it so i can do further operation on sheet.
i tried this code..
Mat rgbMat = new Mat();
Utils.bitmapToMat(resultBitmap1, rgbMat);
Mat grayMat = new Mat(resultBitmap1.getHeight(), resultBitmap1.getWidth(),
CvType.CV_8U, new Scalar(1));
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat hierarchy = new Mat();
Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY, 2);
Imgproc.threshold(grayMat, grayMat, 100, 255, Imgproc.THRESH_BINARY);
Core.bitwise_not(grayMat, grayMat);
Imgproc.findContours(grayMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
pointList=new ArrayList<Point>();
for(int i=0; i<contours.size(); i++)
{
Rect rect = Imgproc.boundingRect(contours.get(i));
double k = (rect.height+0.0)/rect.width;
if (0.9<k && k<1.1)
{
Point tmppnt=new Point(rect.x,rect.y);
pointList.add(tmppnt);
Imgproc.drawContours(rgbMat, contours, i, new Scalar(255, 0, 0),3);
}
}
Utils.matToBitmap(rgbMat, resultBitmap1);
sourceImageView.setImageBitmap(resultBitmap1);
please help me..
thanks
take a look at this. maybe it will be helpful for your next step.