Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- image = cv2.imread("car2.jpg")
- gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
- combined_image = np.hstack((image, cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR)))
- cv2.imshow('Original and Grayscale Image', combined_image)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- import numpy as np
- import cv2
- # Load the image
- img = cv2.imread('peter.jpg',0)
- # Display the original image
- cv2.imshow('Original image',img)
- # Reduce the image size by 50%
- Reduced_img = cv2.resize(img,(0,0),fx=0.5,fy=0.5)
- cv2.imshow('Reduced image',Reduced_img)
- # Upscale the image by 150%
- Upscaled_img = cv2.resize(img,(0,0),fx=1.5,fy=1.5)
- cv2.imshow('Upscaled image',Upscaled_img)
- # Wait for the user to close the windows
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- import numpy as np
- import cv2
- img = cv2.imread('peter.jpg',0)
- cv2.imshow('Original image',img)
- print(img.shape)
- new_width = 600
- dim_size = (new_width, img.shape[0])
- horizonStretchedImg = cv2.resize(img, dim_size, interpolation = cv2.INTER_AREA)
- cv2.imshow('Horizon stretched image',horizonStretchedImg)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- import numpy as np
- import cv2
- img = cv2.imread('peter.jpg',0)
- cv2.imshow('Original image',img)
- print(img.shape)
- new_height = 600
- dim_size = (img.shape[1], new_height)
- verticallyStretchedImg = cv2.resize(img, dim_size, interpolation = cv2.INTER_AREA)
- cv2.imshow('Vertically stretched image',verticallyStretchedImg)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- import numpy as np
- import cv2
- img = cv2.imread('peter.jpg',0)
- cv2.imshow('Original image',img)
- new_img1 = cv2.resize(img, None, fx=0.2, fy=0.2, interpolation=cv2.INTER_NEAREST)
- new_img2 = cv2.resize(img, None, fx=0.2, fy=0.2, interpolation=cv2.INTER_LINEAR)
- new_img3 = cv2.resize(img, None, fx=0.2, fy=0.2, interpolation=cv2.INTER_CUBIC)
- new_img4 = cv2.resize(img, None, fx=0.2, fy=0.2, interpolation=cv2.INTER_LANCZOS4)
- cv2.imshow('New image 1', new_img1)
- cv2.imshow('New image 2', new_img2)
- cv2.imshow('New image 3', new_img3)
- cv2.imshow('New image 4', new_img4)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- Filter.py
- import matplotlib.pyplot as plt
- from skimage import data, filters
- image = data.coins()
- edges = filters.sobel(image)
- plt.imshow(edges, cmap='gray')
- plt.show()
- FilterCanny.py
- import numpy as np
- import cv2
- img = cv2.imread('peter.jpg')
- t_lower = 50
- t_upper = 100
- apenture_size= 5
- edge = cv2.Canny(img,t_lower,t_upper,apenture_size)
- cv2.imshow('Original image',img)
- cv2.imshow('Edge image',edge)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- Prewitt / Robert is Homework
- import numpy as np
- import cv2
- from skimage.filters import prewitt, roberts
- from skimage import io, color
- # Load the image
- img = cv2.imread('peter.png')
- # Convert the image to grayscale (Prewitt and Roberts work on grayscale)
- gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- # Perform Prewitt edge detection
- prewitt_edges = prewitt(gray_img)
- # Perform Roberts edge detection
- roberts_edges = roberts(gray_img)
- # Display the results
- cv2.imshow('Original Image', gray_img)
- cv2.imshow('Prewitt Edge Detection', prewitt_edges)
- cv2.imshow('Roberts Edge Detection', roberts_edges)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- import cv2
- import numpy as np
- import matplotlib.pyplot as plt
- img = cv2.imread('peter.jpg')
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- blurred_image = cv2.GaussianBlur(gray, (5, 5), 1)
- canny = cv2.Canny(blurred_image, 50, 100)
- plt.figure(figsize=(10,5))
- plt.subplot(1,2,1)
- plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
- plt.title('Original image')
- plt.axis('off')
- plt.subplot(1,2,2)
- plt.imshow(canny, cmap='gray')
- plt.title('Canny image')
- plt.axis('off')
- plt.show()
- ------------HOPE THIS DOESN'T COME------------
- from collections import Counter
- import heapq
- class Node:
- def __init__(self, symbol=None, freq=0, left=None, right=None):
- self.freq = freq
- self.symbol = symbol
- self.left = left
- self.right = right
- def __lt__(self, other):
- return self.freq < other.freq
- def build_huffman_tree(data):
- # Count the frequency of each symbol in the data
- frequency = Counter(data)
- # Create a priority queue (min-heap) for the nodes
- heap = [Node(symbol, freq) for symbol, freq in frequency.items()]
- heapq.heapify(heap)
- # Build the Huffman tree
- while len(heap) > 1:
- left = heapq.heappop(heap)
- right = heapq.heappop(heap)
- merged = Node(freq=left.freq + right.freq, left=left, right=right)
- heapq.heappush(heap, merged)
- # The remaining node is the root of the tree
- return heap[0]
- def generate_huffman_codes(node, prefix="", codelook={}):
- if node.symbol is not None:
- codelook[node.symbol] = prefix
- else:
- # Traverse left (append '0') and right (append '1')
- generate_huffman_codes(node.left, prefix + '0', codelook)
- generate_huffman_codes(node.right, prefix + '1', codelook)
- return codelook
- def huffman_encoding(data, codelook):
- # Generate the encoded string based on the Huffman codes
- return ''.join(codelook[symbol] for symbol in data)
- def huffman_decoding(encoded_data, root):
- # Decode the encoded data back to the original string
- decoded_data = []
- node = root
- for bit in encoded_data:
- node = node.left if bit == '0' else node.right
- if node.symbol is not None:
- decoded_data.append(node.symbol)
- node = root
- return ''.join(decoded_data)
- # Test with sample data
- data = "We are the student of amity"
- root = build_huffman_tree(data)
- codelook = generate_huffman_codes(root)
- encoded_data = huffman_encoding(data, codelook)
- decoded_data = huffman_decoding(encoded_data, root)
- print("Encoded Data:", encoded_data)
- print("Decoded Data:", decoded_data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement