Background
Image processing can be applied to car license plate detection problems see Wikipedia, segmenting numbers, belisarius's old answer. Also, on this site once can use some techniques explained in these answers 4, 5, 6 in order to find the license plate. I want to apply image processing techniques to 'Car front detection' problem.
The main idea is to find the license plate rectangle and then use the center to chop the car front with a given length and width. Same sample images are:
The desired output is the red clipped rectangle the size that might be adjusted once, but should use the center of the license plate vertically so that if needed I can mirror the half of the car.
I have two main problems:
- The license plate rectangle has to be corrected due to the camera
angle. (by using e.g
PerspectiveTransform
) - To find the car front I have to clean the shadow of the car.
The code up to now
To find license plate:
img = Import@"https://i.stack.imgur.com/pmONo.png";
bin = FillingTransform[ MorphologicalBinarize[ColorNegate@imgc, {.3, .5}], 1];
(* License Plate method 1*)
Thinning@Dilation[ Binarize[DeleteSmallComponents[
LaplacianGaussianFilter[img, 3], Method -> "Cluster"], 0.05], 4]
(* License Plate method 2*)
c = Closing[img, 5];
lic = DeleteSmallComponents@Binarize[TopHatTransform[c, 10], 0.25]
Taken from @nikie's nice answer to this Q
rects = ComponentMeasurements[lic, "BoundingBox"];
growRect[{{x1_, y1_}, {x2_, y2_}}] :=
Module[{checkRectEmpty, growSingleDirection, growSingleStep, cx, cy,
left, top, right, bottom, sizeEstimate,
size}, ({cx, cy} = Round[{x1 + x2, y1 + y2}/2];
checkRectEmpty[{left_, top_, right_, bottom_}] :=
Max[ImageValue[
m, {cx - left ;; cx + right, cy - top ;; cy + bottom}]] == 0;
growSingleDirection[size_, grow_] :=
If[checkRectEmpty[size + grow], size + grow, size];
growSingleStep[size_] :=
Fold[growSingleDirection, size, IdentityMatrix[4]];
sizeEstimate =
Abs[Round[{x2 - x1, y2 - y1, x2 - x1, y2 - y1}/2 - 20]];
{left, top, right, bottom} =
FixedPoint[growSingleStep, sizeEstimate, 20];
Rectangle[{cx - left, cy - top}, {cx + right, cy + bottom}])]
finalRectangles = growRect /@ rects[[All, 2]]
Show[lic, Graphics[{Red, finalRectangles[[ ;; ]]}]]
{Rectangle[{348, 230}, {420, 246}], Rectangle[{278, 193}, {478, 205}]}
Edit 1: Camera angle correction
(*Select license plate component*)
c = SelectComponents[MorphologicalComponents[lic] , "Rectangularity", # > 0.5 &];
ang = ComponentMeasurements[c, "Orientation"][[1, 2]];
ImagePerspectiveTransformation[img,
ShearingTransform[ Pi + ang, {1, 0}, {0, 1}], PlotRange -> All]
Edit 1 end.
To find shadow:
(*finding shadow*)
comp = MorphologicalComponents[DeleteBorderComponents@bin] ;
s = SelectComponents[comp, {"Centroid", "Area"}, #2 > 31000 &] // Colorize
ComponentMeasurements[DeleteBorderComponents@bin, {"Centroid", "Area"}, #2 > 31000 &]
{11 -> {{431.823, 165.048}, 32052.3}}
My questions are:
- How to find car plate rectangle and correct the image perspective?
- How to remove the car shadow from the front view?
Any help and suggestions are welcome.