SHARE
TWEET

exercise_6.py

a guest Feb 20th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. import argparse
  3. import re
  4. import sys
  5. import cv2
  6. import os
  7. import numpy
  8.  
  9. parser = argparse.ArgumentParser(
  10.     description="Execute exercise 1 on an image. Will show image normally, in grayscale, thresholded at 50%% and saves both modified images"
  11. )
  12. parser.add_argument('file', nargs=1, type=argparse.FileType('r'))
  13. file = (parser.parse_args().file)[0]
  14.  
  15. if (re.match("^.*.png$", file.name) == None):
  16.     sys.exit("File needs to be a .png image file")
  17.  
  18. # Create two seperate windows to compare original and modified image
  19. cv2.namedWindow("filter2D", cv2.WINDOW_AUTOSIZE)
  20.  
  21. # Load image and modified image
  22. original = cv2.imread(file.name)
  23.  
  24. # Create the kernel
  25. kernel = numpy.zeros((15, 15))
  26.  
  27. for i in range(7):
  28.     kernel[i][i] = 1.0/7
  29.  
  30. # Create 2D filtered version
  31. filter2D = cv2.filter2D(original, cv2.CV_32F, kernel, anchor=(14, 14))
  32.  
  33. # Because the CV_32F color space expects values bewteen 0 and 1 (0 = white & 1 = black), need to reduce the all values to values in this range
  34. filter2D = filter2D / 255
  35.  
  36. combined = numpy.concatenate((original, filter2D), axis=1)
  37.  
  38. # Show images until a key is pressed
  39. cv2.imshow("filter2D", combined)
  40. cv2.waitKey(0)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top