Overlay license plate

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
mediafc
Posts: 3
Joined: 2019-08-12T09:30:45-07:00
Authentication code: 1152

Overlay license plate

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Overlay license plate

Post 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
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Overlay license plate

Post 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?
mediafc
Posts: 3
Joined: 2019-08-12T09:30:45-07:00
Authentication code: 1152

Re: Overlay license plate

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Overlay license plate

Post 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
mediafc
Posts: 3
Joined: 2019-08-12T09:30:45-07:00
Authentication code: 1152

Re: Overlay license plate

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Overlay license plate

Post 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.
Post Reply