PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 27, 2022

[FIXED] How do I crop an image given the coordinates of the four corners to crop

 July 27, 2022     crop, image-processing, matplotlib, opencv, python     No comments   

Issue

I need to crop the license plate out of a car's image with python given the coordinates for bounding boxes of the plate. (4 coordinates) . Any tips on how I could do this ?

I have the following code , but does not work as expected.

> x1, y1: 1112 711 
> x2, y2: 1328 698 
> x3, y3: 1330 749 
> x4, y4: 1115 761
image = cv2.imread(IMAGE_PATH)
fixed_image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

new_img = cv2.rectangle(fixed_image, (x3,y3), (x1,y1), (0, 255, 0), 5) 

plt.figure(figsize=(12,13))
plt.imshow(new_img)

Image for reference

Cropped image

Thank you.


Solution

since the coords you are getting are a POLYGON and not a RECTANGLE, you will have to make some adjustments in your slicing, the simplest to do is adjust your rectangle:

x1, y1: 1112 711
x2, y2: 1328 698
x3, y3: 1330 749
x4, y4: 1115 761

top_left_x = min([x1,x2,x3,x4])
top_left_y = min([y1,y2,y3,y4])
bot_right_x = max([x1,x2,x3,x4])
bot_right_y = max([y1,y2,y3,y4])

now you can do

img[top_left_y:bot_right_y, top_left_x:bot_right_x]

please note that slicing does NOT include the end point, so you might want to do

img[top_left_y:bot_right_y+1, top_left_x:bot_right_x+1]


Answered By - Nullman
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing