You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

76 lines
1.9 KiB

  1. # Importing the dependencies
  2. import cv2
  3. import numpy as np
  4. # Defining variables to store coordinates where the second image has to be placed
  5. positions = []
  6. positions2 = []
  7. count = 0
  8. # Mouse callback function
  9. def draw_circle(event, x, y, flags, param):
  10. global positions, count
  11. # If event is Left Button Click then store the coordinate in the lists, positions and positions2
  12. if event == cv2.EVENT_LBUTTONUP:
  13. cv2.circle(room_draw, (x, y), 2, (255, 0, 0), -1)
  14. positions.append([x, y])
  15. if count != 3:
  16. positions2.append([x, y])
  17. elif count == 3:
  18. positions2.insert(2, [x, y])
  19. count += 1
  20. # Reading the two images and storing it in variables room and dp
  21. room = cv2.imread("./data/room2.jpg")
  22. room_draw = room.copy()
  23. dp = cv2.imread("./data/carpet.jpeg")
  24. # Defing a window named 'image' and set the callback to define the destination points
  25. cv2.namedWindow("image")
  26. cv2.setMouseCallback("image", draw_circle)
  27. while True:
  28. cv2.imshow("image", room_draw)
  29. k = cv2.waitKey(20) & 0xFF
  30. if k == 27 or count > 3:
  31. break
  32. cv2.destroyAllWindows()
  33. height, width = room.shape[:2]
  34. h1, w1 = dp.shape[:2]
  35. pts1 = np.float32([[0, 0], [w1, 0], [0, h1], [w1, h1]])
  36. pts2 = np.float32(positions)
  37. # Calculate homography
  38. h, mask = cv2.findHomography(pts1, pts2, cv2.RANSAC, 5.0)
  39. height, width, channels = room.shape
  40. im1Reg = cv2.warpPerspective(dp, h, (width, height))
  41. # Masking and blending
  42. mask2 = np.zeros(room.shape, dtype=np.uint8)
  43. roi_corners2 = np.int32(positions2)
  44. channel_count2 = room.shape[2]
  45. ignore_mask_color2 = (255,) * channel_count2
  46. cv2.fillConvexPoly(mask2, roi_corners2, ignore_mask_color2)
  47. mask2 = cv2.bitwise_not(mask2)
  48. masked_image2 = cv2.bitwise_and(room, mask2)
  49. # Using Bitwise or to merge the two images
  50. final = cv2.bitwise_or(im1Reg, masked_image2)
  51. # Show results and write to file
  52. cv2.imshow("Final", final)
  53. cv2.imwrite("Final2.jpg", final)
  54. cv2.waitKey(0) & 0xFF
  55. cv2.destroyAllWindows()