furas

Python - cv2 - imshow and waitKey (Stackoverflow)

Dec 29th, 2025 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.22 KB | None | 0 0
  1. # date: 2025.12.29
  2.  
  3. # [opencv - How to fix this code so it shows the image with python and cv? - Stack Overflow](https://stackoverflow.com/questions/79856626/how-to-fix-this-code-so-it-shows-the-image-with-python-and-cv)
  4.  
  5. import cv2
  6. import numpy as np
  7. import time
  8.  
  9.  
  10. # ---------------- CONFIG ----------------
  11. BASE_IMAGE_PATH = "Input/RIMG_20250222_074457.jpg"
  12. OVERLAY_IMAGE_PATH = "Input/RIMG_20250308_072815.jpg"
  13. # BASE_IMAGE_PATH = "image1.jpg"
  14. # OVERLAY_IMAGE_PATH = "image2.jpg"
  15. OUTPUT_IMAGE_PATH = "overlay_aligned.png"
  16.  
  17. MOVE_STEP = 5  # pixels per key press
  18. ROTATE_STEP = 1.0  # degrees per key press
  19. ZOOM_STEP = 0.02  # scale per key press
  20. # ---------------------------------------
  21.  
  22.  
  23. def transform_overlay(img, tx, ty, angle, scale):
  24.     print("rendering ...")
  25.     h, w = img.shape[:2]
  26.  
  27.     # Center of the image
  28.     center = (w // 2, h // 2)
  29.  
  30.     # Rotation + scaling
  31.     M = cv2.getRotationMatrix2D(center, angle, scale)
  32.  
  33.     # Translation
  34.     M[0, 2] += tx
  35.     M[1, 2] += ty
  36.  
  37.     transformed = cv2.warpAffine(
  38.         img,
  39.         M,
  40.         (w, h),
  41.         flags=cv2.INTER_LINEAR,
  42.         borderMode=cv2.BORDER_CONSTANT,
  43.         borderValue=(0, 0, 0),
  44.     )
  45.  
  46.     return transformed, M
  47.  
  48.  
  49. def main():
  50.     base = cv2.imread(BASE_IMAGE_PATH)
  51.     overlay = cv2.imread(OVERLAY_IMAGE_PATH)
  52.  
  53.     if base is None or overlay is None:
  54.         raise FileNotFoundError("Could not load images")
  55.  
  56.     # Resize overlay to match base if needed
  57.     overlay = cv2.resize(overlay, (base.shape[1], base.shape[0]))
  58.  
  59.     tx, ty = 0, 0
  60.     angle = 0.0
  61.     scale = 1.0
  62.  
  63.     print("Controls:")
  64.     print("Arrow keys: move overlay")
  65.     print("A / D: rotate")
  66.     print("W / S: zoom in / out")
  67.     print("R: reset")
  68.     print("SPACE: save aligned overlay")
  69.     print("ESC: exit")
  70.  
  71.     cv2.namedWindow("Alignment Tool", cv2.WINDOW_NORMAL)
  72.  
  73.     while True:
  74.         transformed, M = transform_overlay(overlay, tx, ty, angle, scale)
  75.         # Blend images for visualization
  76.         blended = cv2.addWeighted(base, 0.6, transformed, 0.4, 0)
  77.  
  78.         cv2.imshow("Alignment Tool", blended)
  79.  
  80.         key = cv2.waitKey(100)  # 100ms = 0.1s
  81.  
  82.         print(key)
  83.  
  84.         if key != -1:
  85.             if key == 27:  # ESC
  86.                 break
  87.  
  88.             elif key == ord("r"):
  89.                 tx, ty, angle, scale = 0, 0, 0.0, 1.0
  90.  
  91.             elif key == ord("a"):
  92.                 angle -= ROTATE_STEP
  93.             elif key == ord("d"):
  94.                 angle += ROTATE_STEP
  95.  
  96.             elif key == ord("w"):
  97.                 scale += ZOOM_STEP
  98.             elif key == ord("s"):
  99.                 scale = max(0.1, scale - ZOOM_STEP)
  100.  
  101.             elif key == 32:  # SPACE
  102.                 cv2.imwrite(OUTPUT_IMAGE_PATH, transformed)
  103.                 print(f"Saved aligned overlay to {OUTPUT_IMAGE_PATH}")
  104.  
  105.             # Arrow keys (OpenCV key codes)
  106.             elif key == ord("j"):  # left
  107.                 tx -= MOVE_STEP
  108.             elif key == ord("k"):  # right
  109.                 tx += MOVE_STEP
  110.             elif key == ord("i"):  # up
  111.                 ty -= MOVE_STEP
  112.             elif key == ord("m"):  # down
  113.                 ty += MOVE_STEP
  114.  
  115.     cv2.destroyAllWindows()
  116.  
  117.  
  118. if __name__ == "__main__":
  119.     main()
Advertisement
Add Comment
Please, Sign In to add comment