ads_header

How to Change Perspective Image With Python and OpenCV



Hello friends, in this post I will share a little about how to apply the "Bird's Eye" method by using the OpenCV library in the Python programming language using the PyCharm IDE. So, "Bird's Eye" is a term in looking at an image with a Top-Down perspective from top to bottom.

This method is quite popularly used in "machine learning" for example in the case of "Lane Detection", "Measuring Car Distance" and of course many more.





Okay, just first open the PyCharm IDE then create a new file with the name index.py and include an image where the object is visible from the perspective from the bottom, more or less like in the image I use.

book.jpg

If the image is ready, then we enter the following code

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread("book.jpg")

cv2.circle(img, (215, 35), 5, (0, 0, 255), -1) # top left
cv2.circle(img, (550, 25), 5, (0, 0, 255), -1) # top right
cv2.circle(img, (130, 370), 5, (0, 0, 255), -1) # bottom left
cv2.circle(img, (630, 370), 5, (0, 0, 255), -1) # bottom right

cv2.imshow("Image", img)
cv2.waitKey(0)

The code above serves to save the picture "book.jpg" into the variable "img", then we put a red dot to adjust the location of the coordinates of the book picture corner. Actually this is not necessary, but it helps us in determining the location of the coordinates.





Note : Customize your picture.

If the above program is run it will appear like this




If it is suitable, then you just delete the code below

cv2.imshow("Image", img)
cv2.waitKey(0)

Continue with the next code


pts1 = np.float32(
        [[215,35], # top left
         [550,25], # top right
         [130,370], # bottom left
         [630,370]] # bottom right
)

pts2 = np.float32(
        [[0,0], # top left
         [500,0], # top right
         [0,600], # bottom left
         [500,600]] # bottom right
)

matrix = cv2.getPerspectiveTransform(pts1,pts2)
result = cv2.warpPerspective(img, matrix, (500,600)) # set size image based on pts2 (w = 500 h = 600)

pts1 is the coordinate point of the corner of the book, adjusting to the previous red dot.

pts2 is the coordinate point that we made to determine the width and height of the image to be produced.
Next we call the "cv2.getPerspectiveTransform" function based on the pts1 and pts2 variables stored in the matrix variable. Then in the result variable we call the "cv2.warpPerspective" function with parameters (original image, matrix and image size)





The last stage, we display the original image and the resulting image using the library "matplotlib.pyplot"


plt.subplot(1,2,1),
plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
plt.title('Original Image')

plt.subplot(1,2,2),
plt.imshow(cv2.cvtColor(result,cv2.COLOR_BGR2RGB))
plt.title('Result')

plt.show()
cv2.waitKey(0)

The following explanation above code :

  1. Define a plot consisting of (1 row, 2 columns, placement at position 1)
  2. Ready to display the original image "img" in its original color
  3. Set image title
  4. Define a plot consisting of (1 row, 2 columns, placement at position 2)
  5. Ready to display image "result"
  6. Set image title
  7. Show plot
  8. Hold the window to keep it appearing


    27 comments:

    1. I was looking at a portion of your posts on this site and I consider this site is really enlightening! Keep setting up..
      360DigiTMG supply chain analytics course

      ReplyDelete
    2. This knowledge.Excellently composed article, if just all bloggers offered a similar degree of substance as you, the web would be a greatly improved spot. If you don't mind keep it up.

      360DigiTMG data science bootcamp malaysia

      ReplyDelete
    3. an extremely wonderful post this is. Genuinely, perhaps the best post I've at any point seen to find in as long as I can remember. Goodness, simply keep it up.
      what is hrdf claimable

      ReplyDelete
    4. I truly like your style of blogging. I added it to my preferred's blog webpage list and will return soon…
      data scientist course in noida

      ReplyDelete
    5. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
      cyber security certification malaysia

      ReplyDelete
    6. This blog is really good... keep sharing such amazing posts with us.

      If you guys are looking for the best and most cost-effective data migration services, master data management services, data integration services, and need advice for sap data management, you can contact our data analysis expert team. They will help you out with all your data concerns. Get in touch with them from - https://gluedata.com/

      ReplyDelete
    7. Dear Sir,
      Thanks for sharing such a nice article. In this blog i learn lot of things which is very useful for me. If you want to shop for skin care product so please visit my website https://weliv.online/find-yourself-a-better-hair-and-skin-care-regime-on-wow-online-platform/

      ReplyDelete
    8. Thanks for complete information of "How to Change Perspective Image With Python and OpenCV"

      Admission Open - Manthena American School Sharjah

      ReplyDelete
    9. When you or your company need help with QuickBooks or any other aspect of your business, dial Quickbooks Support Phone Number +1 888-471-2380.

      ReplyDelete
    10. What if i told you that you can be your own boss ii you chai franchise in india just
      Apply for cafe frachise and forget the rest

      ReplyDelete
    11. Beautiful post on how to use and importance of platform events. Do try our GPS Tracking Device for Bikes

      ReplyDelete

    12. An intriguing discussion is worth comment. I think that you ought to publish more on this subject matter, it might not be a taboo matter but generally people don't discuss these issues. To the next! Kind regards!!

      https://www.webranksindia.com/

      ReplyDelete
    13. ASPECT Billing Solutions is a multi-specialty medical billing services company. Our mission is to cover all aspects before submission making sure the healthcare providers get maximum reimbursement of their time from insurance carriers. We cover minor details of each claim before submission, i.e. each patient eligibility verification before claim submission, correct coding, CPT and DX verification as per CMS guidelines hence making sure your practice is error free. While you are focusing on patients we take care of your revenue cycle management.
      Chiropractor Billing

      ReplyDelete
    14. Best Vastu consultant of World promising Health, Wealth & Prosperity. vastu for home plan

      ReplyDelete
    15. Awesome and interesting article. Great things you've always shared with us. Thanks. Just continue composing this kind of post.
      The top things every website needs

      ReplyDelete
    16. You make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers.
      scoopearth.com/domainnetworks-best-seo-practices

      ReplyDelete
    17. Hotlivetv.net is a social networking platform & Entertaining connection space. Everyone can share information, Watch hot girl live streams, play batting Games, Watch Movies, Chat with star girls hotlive app.net

      ReplyDelete
    18. Chichlive.biz is an entertaining social platform where Everyone can share information, Watch live girl streams, and Chat with hot girls. In addition, everyone can also participate in attractive, lucrative entertainment games such as fortune and fainting, lottery, football betting, gambling, etc.

      In addition, our website has a lot of features that will help you to make an easy chat with these girls:

      - Unlimited instant messaging without any limits

      - No registration is required

      - You can send messages directly from this site without using any other program or application

      - You can see what other people think about the girl and their status on chichlive

      ReplyDelete
    19. Sell Verified Accredited Accounts and Followers
      Intro: Virtual Card Accounts is a company that offers digital accounts and followers, which is also known as dotparadox. You can purchase verified accounts and followers from our website at a reasonable price. We offer various packages with different numbers of followers in order to meet your needs. If you need an account or follower, please contact us today.

      ReplyDelete
    20. Very nice post an
      d right to the point. also, see other posts really good content I find here. Thank you. Also, Visit my website
      office furniture dubai
      Visit my website
      office furniture dubai
      office furniture
      Meeting Table dubai

      ReplyDelete
    21. this blog is too good. Please do share more blogs like this.

      https://innosoft-group.com/solana-blockchain-development-company/

      ReplyDelete
    22. Excellent Blog! Thanks for sharing this post. It is nice post. When I read about this post then got more information. I liked it very much.
      Visit successfulltime

      ReplyDelete
    23. The more pixels the better the lucidity and wealth of the picture. https://jpg-compress.com

      ReplyDelete
    24. easyapprovallending is a blog site which gives you complete information about the Worst acquisition and our team has only one goal, to help you go through your journey. We have all the knowledge related to this topic and we are here to help you get updated content related to it.

      ReplyDelete
    25. Để luôn cập nhật những tin tức, thông tin và phân tích mới nhất về World Cup sắp tới của Qatar vào năm 2022 — lần đầu tiên nó được tổ chức tại một quốc gia Ả Rập — hãy xem trang web của chúng tôi để biết mọi điều về WorldCup2022.vn! Độc giả của chúng tôi đã mong đợi rằng chúng tôi xuất bản thông tin mới mỗi ngày. Chúng tôi hy vọng báo cáo của chúng tôi về soi kèo World Cup 2022 , sẽ được tổ chức tại Qatar, giúp mọi người hiểu rõ hơn về khả năng ảnh hưởng của sự kiện này đối với cuộc sống ở đó.

      ReplyDelete

    Powered by Blogger.