Page 1 of 1

Overlay license plate

Posted: 2019-08-12T09:37:57-07:00
by mediafc
Hi all,

I'm currently involved in a project that requires that a license plate is overlayed with the company's logo before being posted online due to privacy concerns.
We already have license plate recognition, which gives us the coordinates of the 4 edges of the plate within the photo.
Now, what is missing is overlaying the logo over the photo with the provided coordinates, but first we will also need to distort the logo image so it can give the impression of perspective.
Can this be done with IM? How? First distort then overlay?
We were thinking of using IM command line because this process has to be automatic.

Thanks in advance.

Re: Overlay license plate

Posted: 2019-08-12T10:04:06-07:00
by snibgo
You know the four corners of your logo, and the four corners where you want them to move to. See https://www.imagemagick.org/discourse-s ... 08#p167933

Re: Overlay license plate

Posted: 2019-08-12T10:08:34-07:00
by fmw42
Yes, it can be done in imagemagick. You would get the 4 corners of the license (or region where you want to put the logo) and the 4 corners of the logo, then use distort perspective. See https://imagemagick.org/Usage/distorts/#perspective. Once you have distorted it, then use -geometry +X+Y -compose over -composite to place the logo over the license. See https://imagemagick.org/Usage/compose/#compose and https://imagemagick.org/Usage/layers/#convert.

If you can post an example or a diagram of where you want the logo to be placed, then we can help further with the command line code.

Please clarify if you want to the logo over the whole license plate or just in some region. If the latter, where?

Re: Overlay license plate

Posted: 2019-08-12T14:30:08-07:00
by mediafc
First of all, thank you both for the quick reply.

So here it is a typical photo with the license plates visible. Through our automatic license plate recognition software, besides the OCR, I also get the 4 corners of the license plate
Image
https://www.dropbox.com/s/jya76ofu89lu5 ... e.jpg?dl=0

In this second picture using photoshop is what we wanted to get as result:
Image
https://www.dropbox.com/s/c3ynic78yuru1 ... e.jpg?dl=0

Thanks.

Re: Overlay license plate

Posted: 2019-08-12T14:39:15-07:00
by fmw42
You did not mention your ImageMagick version or platform/OS? So the following is Unix syntax with IM 6.9.10.60 Q16 Mac OSX. If on IM 7, then replace convert with magick. If on an ancient version of ImageMagick 6, then you may need to list the control points differently. See https://imagemagick.org/Usage/distorts/#control_points

Logo:
Image

License:
Image



Here is what to do. Get the points for the corners of the logo and the license in the image.

Here I put them into arrays and then alternate the points as: logo1 license1 logo2 license2 logo3 license3 logo4 license4.

This is Unix. If on Windows or Unix, you can just make the list and put the list of points into the perspective rather than the variable.

Here I have listed the points clockwise from the top left. The order does not matter so long as the pairs of points correspond.

Code: Select all

logoArr=(0,0 290,0 290,86 0,86)
carArr=(149,809 330,812 337,850 156,845)
list=`echo "${logoArr[0]} ${carArr[0]}  ${logoArr[1]} ${carArr[1]}  ${logoArr[2]} ${carArr[2]}  ${logoArr[3]} ${carArr[3]}"`
echo $list
0,0 149,809 290,0 330,812 290,86 337,850 0,86 156,845

Code: Select all

convert mylogo.png -virtual-pixel none +distort perspective "$list" \
photoLicense.jpg +swap -layers merge +repage license_logo.jpg

or

convert mylogo.png -virtual-pixel none +distort perspective \
"0,0 149,809 290,0 330,812 290,86 337,850 0,86 156,845" \
photoLicense.jpg +swap -layers merge +repage license_logo.jpg
(In Windows replace the end of line \ with ^ or make one long command line by removing the \).

Image

Re: Overlay license plate

Posted: 2019-08-13T07:13:14-07:00
by mediafc
Wow!! It was exactly this thank you :)

Just one more question, when there are two licenses in the same photo I have to do it within a cycle (or pipe in bash) right?
There's is no way to do it in a single pass?

Thanks again.

Re: Overlay license plate

Posted: 2019-08-13T09:50:30-07:00
by fmw42
You can do it in a single pass, by issuing two distort commands chained in the same command line

Code: Select all

convert photoLicense.jpg \
\( mylogo.png -write mpr:logo +delete \) \
-virtual-pixel none \
\( mpr:logo +distort perspective "$list1" \) \
-layers merge +repage \
\( mpr:logo +distort perspective "$list2" \) \
-layers merge +repage \
license_logo.jpg
mpr:xxx is an in memory format. I use that to avoid reading the logo more than once. I read the logo into mpr: format and then delete the original. Then use the mpr: version for each distort.