Advertisement
brandblox

Common_people

Nov 19th, 2024 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.89 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. image = cv2.imread("car2.jpg")
  5. gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  6. combined_image = np.hstack((image, cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR)))
  7. cv2.imshow('Original and Grayscale Image', combined_image)
  8. cv2.waitKey(0)
  9. cv2.destroyAllWindows()
  10.  
  11.  
  12.  
  13. import numpy as np
  14. import cv2
  15.  
  16. # Load the image
  17. img = cv2.imread('peter.jpg',0)
  18.  
  19. # Display the original image
  20. cv2.imshow('Original image',img)
  21.  
  22. # Reduce the image size by 50%
  23. Reduced_img = cv2.resize(img,(0,0),fx=0.5,fy=0.5)
  24. cv2.imshow('Reduced image',Reduced_img)
  25.  
  26. # Upscale the image by 150%
  27. Upscaled_img = cv2.resize(img,(0,0),fx=1.5,fy=1.5)
  28. cv2.imshow('Upscaled image',Upscaled_img)
  29.  
  30. # Wait for the user to close the windows
  31. cv2.waitKey(0)
  32. cv2.destroyAllWindows()
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. import numpy as np
  40. import cv2
  41.  
  42. img = cv2.imread('peter.jpg',0)
  43. cv2.imshow('Original image',img)
  44. print(img.shape)
  45. new_width = 600
  46. dim_size = (new_width, img.shape[0])
  47. horizonStretchedImg = cv2.resize(img, dim_size, interpolation = cv2.INTER_AREA)
  48.  
  49. cv2.imshow('Horizon stretched image',horizonStretchedImg)
  50. cv2.waitKey(0)
  51. cv2.destroyAllWindows()
  52.  
  53.  
  54.  
  55.  
  56. import numpy as np
  57. import cv2
  58.  
  59. img = cv2.imread('peter.jpg',0)
  60. cv2.imshow('Original image',img)
  61. print(img.shape)
  62. new_height = 600
  63. dim_size = (img.shape[1], new_height)
  64. verticallyStretchedImg = cv2.resize(img, dim_size, interpolation = cv2.INTER_AREA)
  65.  
  66. cv2.imshow('Vertically stretched image',verticallyStretchedImg)
  67. cv2.waitKey(0)
  68. cv2.destroyAllWindows()
  69.  
  70.  
  71.  
  72.  
  73.  
  74. import numpy as np
  75. import cv2
  76.  
  77. img = cv2.imread('peter.jpg',0)
  78. cv2.imshow('Original image',img)
  79.  
  80. new_img1 = cv2.resize(img, None, fx=0.2, fy=0.2, interpolation=cv2.INTER_NEAREST)
  81. new_img2 = cv2.resize(img, None, fx=0.2, fy=0.2, interpolation=cv2.INTER_LINEAR)
  82. new_img3 = cv2.resize(img, None, fx=0.2, fy=0.2, interpolation=cv2.INTER_CUBIC)
  83. new_img4 = cv2.resize(img, None, fx=0.2, fy=0.2, interpolation=cv2.INTER_LANCZOS4)
  84.  
  85. cv2.imshow('New image 1', new_img1)
  86. cv2.imshow('New image 2', new_img2)
  87. cv2.imshow('New image 3', new_img3)
  88. cv2.imshow('New image 4', new_img4)
  89.  
  90. cv2.waitKey(0)
  91. cv2.destroyAllWindows()
  92.  
  93.  
  94.  
  95.  
  96.  
  97. Filter.py
  98. import matplotlib.pyplot as plt
  99. from skimage import data, filters
  100.  
  101.  
  102. image = data.coins()
  103.  
  104. edges = filters.sobel(image)
  105. plt.imshow(edges, cmap='gray')
  106. plt.show()
  107.  
  108.  
  109.  
  110. FilterCanny.py
  111. import numpy as np
  112. import cv2
  113. img = cv2.imread('peter.jpg')
  114. t_lower = 50
  115. t_upper = 100
  116. apenture_size= 5
  117. edge = cv2.Canny(img,t_lower,t_upper,apenture_size)
  118.  
  119. cv2.imshow('Original image',img)
  120. cv2.imshow('Edge image',edge)
  121. cv2.waitKey(0)
  122. cv2.destroyAllWindows()
  123.  
  124. Prewitt / Robert is Homework
  125. import numpy as np
  126. import cv2
  127. from skimage.filters import prewitt, roberts
  128. from skimage import io, color
  129.  
  130. # Load the image
  131. img = cv2.imread('peter.png')
  132.  
  133. # Convert the image to grayscale (Prewitt and Roberts work on grayscale)
  134. gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  135.  
  136. # Perform Prewitt edge detection
  137. prewitt_edges = prewitt(gray_img)
  138.  
  139. # Perform Roberts edge detection
  140. roberts_edges = roberts(gray_img)
  141.  
  142. # Display the results
  143. cv2.imshow('Original Image', gray_img)
  144. cv2.imshow('Prewitt Edge Detection', prewitt_edges)
  145. cv2.imshow('Roberts Edge Detection', roberts_edges)
  146.  
  147. cv2.waitKey(0)
  148. cv2.destroyAllWindows()
  149.  
  150.  
  151.  
  152.  
  153. import cv2
  154. import numpy as np
  155. import matplotlib.pyplot as plt
  156. img = cv2.imread('peter.jpg')
  157. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  158. blurred_image = cv2.GaussianBlur(gray, (5, 5), 1)
  159. canny = cv2.Canny(blurred_image, 50, 100)
  160. plt.figure(figsize=(10,5))
  161. plt.subplot(1,2,1)
  162. plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
  163. plt.title('Original image')
  164. plt.axis('off')
  165. plt.subplot(1,2,2)
  166. plt.imshow(canny, cmap='gray')
  167. plt.title('Canny image')
  168. plt.axis('off')
  169. plt.show()
  170.  
  171.  
  172.  
  173.  
  174.  
  175. ------------HOPE THIS DOESN'T COME------------
  176. from collections import Counter
  177. import heapq
  178.  
  179.  
  180.  
  181. class Node:
  182.    def __init__(self, symbol=None, freq=0, left=None, right=None):
  183.        self.freq = freq
  184.        self.symbol = symbol
  185.        self.left = left
  186.        self.right = right
  187.  
  188.    def __lt__(self, other):
  189.        return self.freq < other.freq
  190.  
  191.  
  192. def build_huffman_tree(data):
  193.    # Count the frequency of each symbol in the data
  194.    frequency = Counter(data)
  195.  
  196.    # Create a priority queue (min-heap) for the nodes
  197.    heap = [Node(symbol, freq) for symbol, freq in frequency.items()]
  198.    heapq.heapify(heap)
  199.  
  200.    # Build the Huffman tree
  201.    while len(heap) > 1:
  202.        left = heapq.heappop(heap)
  203.        right = heapq.heappop(heap)
  204.        merged = Node(freq=left.freq + right.freq, left=left, right=right)
  205.        heapq.heappush(heap, merged)
  206.  
  207.    # The remaining node is the root of the tree
  208.    return heap[0]
  209.  
  210.  
  211. def generate_huffman_codes(node, prefix="", codelook={}):
  212.    if node.symbol is not None:
  213.        codelook[node.symbol] = prefix
  214.    else:
  215.        # Traverse left (append '0') and right (append '1')
  216.        generate_huffman_codes(node.left, prefix + '0', codelook)
  217.        generate_huffman_codes(node.right, prefix + '1', codelook)
  218.    return codelook
  219.  
  220.  
  221. def huffman_encoding(data, codelook):
  222.    # Generate the encoded string based on the Huffman codes
  223.    return ''.join(codelook[symbol] for symbol in data)
  224.  
  225.  
  226. def huffman_decoding(encoded_data, root):
  227.    # Decode the encoded data back to the original string
  228.    decoded_data = []
  229.    node = root
  230.    for bit in encoded_data:
  231.        node = node.left if bit == '0' else node.right
  232.        if node.symbol is not None:
  233.            decoded_data.append(node.symbol)
  234.            node = root
  235.    return ''.join(decoded_data)
  236.  
  237.  
  238. # Test with sample data
  239. data = "We are the student of amity"
  240. root = build_huffman_tree(data)
  241. codelook = generate_huffman_codes(root)
  242. encoded_data = huffman_encoding(data, codelook)
  243. decoded_data = huffman_decoding(encoded_data, root)
  244.  
  245. print("Encoded Data:", encoded_data)
  246. print("Decoded Data:", decoded_data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement