Skip to content

Instantly share code, notes, and snippets.

@vivekpanchal
Created May 17, 2019 10:48
Show Gist options
  • Save vivekpanchal/dd8580ae4ae7fbe71f6007b57fae44ad to your computer and use it in GitHub Desktop.
Save vivekpanchal/dd8580ae4ae7fbe71f6007b57fae44ad to your computer and use it in GitHub Desktop.
demo to get rectangles -->>.
//1. Convert img1 into gray image
Imgproc.cvtColor(img1, imageGray1, Imgproc.COLOR_RGB2GRAY);
//2.blur the image ..ie smooth the image
Imgproc.GaussianBlur(imageGray1, imgGaussianBlur, new Size(3, 3), 0);
//3. edge detection
// Imgproc.Sobel(imgGaussianBlur, imgSobel, -1, 1, 0);
//4. Trhesholding the image
Imgproc.adaptiveThreshold(imgGaussianBlur, imgAdaptiveThreshold, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV, 99, 4);
//
// Imgproc.adaptiveThreshold(imageGray1, imgAdaptiveThreshold_forCrop, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 99, 4);
//
//Canny Edge Detection
Imgproc.Canny(imgAdaptiveThreshold, imageCny1, 20, 40, 3, true);
///////////////////////////////////////////////////////////////////
//////////////////Transform Canny to Bitmap/////////////////////////////////////////
imgContours = imageCny1.clone();
//
Imgproc.findContours(imgContours, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
MatOfPoint2f[] contoursPoly = new MatOfPoint2f[contours.size()];
Rect[] boundRect = new Rect[contours.size()];
for (int i = 0; i < contours.size(); i++) {
contoursPoly[i] = new MatOfPoint2f();
Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(i).toArray()), contoursPoly[i], 3, true);
boundRect[i] = Imgproc.boundingRect(new MatOfPoint(contoursPoly[i].toArray()));
}
Mat drawing = img1.clone();
Scalar color = new Scalar(255, 0, 0);
for (int i = 0; i < contours.size(); i++) {
// if (checkImageRatio(boundRect[i])) {
if (basicChecks(boundRect[i])) {
Imgproc.rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2);
if (checkIntensity(drawing)) {
imgDetectedPlateTrue = drawing.clone();
}
}
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment