ok i am working on how to detect the plate on different image sizes so i have two images when i use the following code the first image provide a rectangle around the plate and in the second it doesn't i believe read from this link that there is because different distance or image size my questions is why? and how to detect my region of interest which is the plate (what to do?)technique as follow
link
public void run() throws IOException {
1.Rectangle check
Checking that the candidate regions for plate had rectangle shape by compare white pixels count of these regions to their areas with ±5% tolerance
If count of white pixels = ±5% area of these region
This region may be a plate
Else This region not a plate
2. Plate dimension check
The Egyptian plate had a fixed dimension with height equal to 17 cm and width equal to 32 cm, so the ratio between heights to width approximately 1:2. After rectangle check, the dimension check applied of the succeed regions of rectangle shapes,
If 1.5 < width/height of the succeed region <2.5
This region may be a plate
Else This region not a plate
so after findcontours such if conditions should be applies and here is my code
Mat image = Imgcodecs.imread("/home/tomna/NetBeansProjects/MainClass/src/mainclass/cars/1.jpg");
Mat image_gray hierarchy = new Mat();
Mat image_bilateral = new Mat();
Mat image_sobel = new Mat();
Mat image_threshold = new Mat();
Mat image_morph = new Mat();
Mat strel = getStructuringElement(MORPH_RECT, new Size(35, 35));
Mat Kernel = Mat.ones(new Size(3, 3), MORPH_RECT);
Imgproc.cvtColor(image, image_gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.bilateralFilter(image_gray, image_bilateral, 15, 10, 14);
Imgproc.equalizeHist(image_bilateral, image_bilateral);
Imgproc.morphologyEx(image_bilateral, image_morph, Imgproc.MORPH_CLOSE, strel);
Core.subtract(image_morph, image_bilateral, image_threshold);
Imgproc.threshold(image_threshold, image_threshold, 0, 255, Imgproc.THRESH_OTSU + Imgproc.THRESH_BINARY);
Imgproc.Canny(image_threshold, strel, 250, 225);
Imgproc.dilate(image_threshold, image_threshold, Kernel);
Imgproc.dilate(image_threshold, image_threshold, Kernel);
Vector<MatOfPoint> contours = new Vector<MatOfPoint>();
Mat hierarchy = new Mat();
Mat sorted = new Mat();
Imgproc.findContours(image_threshold.clone(), contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);
// Rect rectCrop = new Rect();
Mat m = new Mat();
for (MatOfPoint contour : contours) {
Rect rec = Imgproc.boundingRect(contour);
if (hierarchy.size().height > 0 && hierarchy.size().width > 0) {
for (int i = 0; i < contours.size(); i++) {
if (verifysize(rec)) {
System.out.println("first test");
Core.extractChannel(contour, m, 0);
int n = Core.countNonZero(m);
float area = (float) (rec.size().height * (float) rec.size().width);
int minarea = (int) area - (int) (area / 100 * 6);
int maxarea = (int) area + (int) (area / 100 * 6);
if (minarea < n || n < maxarea) {
System.err.println("second test");
// Imgproc.drawContours(image, contours, i, new Scalar(0, 0, 255), -1, 8, hierarchy, 0, new Point());
Imgproc.rectangle(image, rec.tl(), rec.br(), new Scalar(0, 0, 255), 3);
// rectCrop = new Rect(rec.x, rec.y, rec.width, rec.height);
}
}
}
Imgcodecs.imwrite("/home/tomna/NetBeansProjects/MainClass/src/mainclass/Dadcar2.jpg",
}
Imgcodecs.imwrite("/home/tomna/NetBeansProjects/MainClass/src/mainclass/121.jpg", image);
}
}
public static boolean verifysize(Rect re) {
float region ratio = (float) (re.size().width re.size().width / (float)re.size().height);
(float) re.size().height;
if (1.5 (2.0 > region && region ratio || ratio > 2.5) 2.3) {
return false;
} else {
return true;
}
}
UPDATE
so i decided to resize any image to (720,1280) according to the image that detected the plate (first pic) so the output i think is better but still not sure the right decision because it worked for the just two pictures
here is the code
Size s = new Size(720,1280);
Imgproc.resize(image,image,s);
System.err.println("resizing...");
output images