0% found this document useful (0 votes)
3 views

vertopal.com_Copy of CVPR1 (1)

Uploaded by

archit.muj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

vertopal.com_Copy of CVPR1 (1)

Uploaded by

archit.muj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

TANISHQ SANDEEP PATIL 229311050 AIML-D

!pip install opencv-python-headless


import cv2
import matplotlib.pyplot as plt
from google.colab.patches import cv2_imshow
#Had to use Matplotlib because colab didnt allow cv.imshow()

image = cv2.imread('MESSI.jpg')

if image is None:
print("Error: Could not read the image.")
else:
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(image_rgb)
plt.axis('off')
plt.show()
cv2.imwrite('output_image.jpg', image)
print("Image written to 'output_image.jpg'")

Requirement already satisfied: opencv-python-headless in


/usr/local/lib/python3.11/dist-packages (4.10.0.84)
Requirement already satisfied: numpy>=1.21.2 in
/usr/local/lib/python3.11/dist-packages (from opencv-python-headless)
(1.26.4)
Image written to 'output_image.jpg'

input_image_path = 'MESSI.jpg'
image = cv2.imread(input_image_path)

if image is None:
print("Error: Could not read the image.")
else:
output_image_path = 'output_image_1.png'
cv2.imwrite(output_image_path, image)
print(f"Image converted and saved as '{output_image_path}'")

Image converted and saved as 'output_image_1.png'

input_image_path = 'MESSI.jpg'
image = cv2.imread(input_image_path)

if image is None:
print("Error: Could not read the image.")
else:
original_height, original_width = image.shape[:2]
print(f"Original Dimensions: {original_width}x{original_height}")
new_width = 300
new_height = 200
new_dimensions = (new_width, new_height)
#Can put these dimensions as we please
resized_image = cv2.resize(image, new_dimensions) # For resizing ,
the rest is for saving the img
output_image_path = 'resized_image.jpg'
cv2.imwrite(output_image_path, resized_image)
print(f"Resized image saved as '{output_image_path}'")
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
resized_image_rgb = cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(image_rgb)
plt.title('Original Image')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(resized_image_rgb)
plt.title('Resized Image')
plt.axis('off')
plt.show()

Original Dimensions: 408x612


Resized image saved as 'resized_image.jpg'
input_image_path = 'MESSI.jpg'
image = cv2.imread(input_image_path)

if image is None:
print("Error: Could not read the image.")
else:
grayscale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # For
making image grayscale rest is for saving the image
output_image_path = 'grayscale_image.jpg'
cv2.imwrite(output_image_path, grayscale_image)
print(f"Grayscale image saved as '{output_image_path}'")
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(image_rgb)
plt.title('Original Image')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(grayscale_image, cmap='gray')
plt.title('Grayscale Image')
plt.axis('off')
plt.show()

Grayscale image saved as 'grayscale_image.jpg'


import numpy as np

input_image_path = 'MESSI.jpg'
image = cv2.imread(input_image_path)

if image is None:
print("Error: Could not read the image.")
else:
scale_factor = 0.5
scaled_image = cv2.resize(image, None, fx=scale_factor,
fy=scale_factor) # For Scaling
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
angle = 45
scale = 1.0
rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale)
rotated_image = cv2.warpAffine(image, rotation_matrix, (w, h)) #
For Rotation
translation_matrix = np.float32([[1, 0, 50], [0, 1, 30]])
shifted_image = cv2.warpAffine(image, translation_matrix, (w, h))
# For Shifting , the rest of the code is only to save the images
plt.figure(figsize=(15, 10))
plt.subplot(2, 2, 1)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title('Original Image')
plt.axis('off')
plt.subplot(2, 2, 2)
plt.imshow(cv2.cvtColor(scaled_image, cv2.COLOR_BGR2RGB))
plt.title('Scaled Image (50%)')
plt.axis('off')
plt.subplot(2, 2, 3)
plt.imshow(cv2.cvtColor(rotated_image, cv2.COLOR_BGR2RGB))
plt.title('Rotated Image (45 degrees)')
plt.axis('off')
plt.subplot(2, 2, 4)
plt.imshow(cv2.cvtColor(shifted_image, cv2.COLOR_BGR2RGB))
plt.title('Shifted Image (50 right, 30 down)')
plt.axis('off')
plt.show()

cap = cv2.VideoCapture('FISH.mp4')
if (cap.isOpened()== False):
print("Error opening video file")

while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
cv2.imshow('Frame', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()

----------------------------------------------------------------------
-----
DisabledFunctionError Traceback (most recent call
last)
<ipython-input-22-8d30034d6e4e> in <cell line: 0>()
7 ret, frame = cap.read()
8 if ret == True:
----> 9 cv2.imshow('Frame', frame)
10 if cv2.waitKey(25) & 0xFF == ord('q'):
11 break

/usr/local/lib/python3.11/dist-packages/google/colab/_import_hooks/
_cv2.py in wrapped(*args, **kwargs)
46 def wrapped(*args, **kwargs):
47 if not os.environ.get(env_var, False):
---> 48 raise DisabledFunctionError(message, name or
func.__name__)
49 return func(*args, **kwargs)
50

DisabledFunctionError: cv2.imshow() is disabled in Colab, because it


causes Jupyter sessions
to crash; see https://github.com/jupyter/notebook/issues/3935.
As a substitution, consider using
from google.colab.patches import cv2_imshow

def generate_image(frequency, width=256, height=256):


i = np.arange(height)
j = np.arange(width)
I, J = np.meshgrid(i, j, indexing='ij')
pixel_values = np.sin(2 * np.pi * frequency * (I + J))
pixel_values_normalized = (pixel_values - pixel_values.min()) /
(pixel_values.max() - pixel_values.min())
return pixel_values_normalized
def main():
frequency = float(input("Enter a frequency value (e.g., 0.1, 1,
5): "))
image = generate_image(frequency)
plt.imshow(image, cmap='gray')
plt.title(f'Sine Wave Pattern with Frequency: {frequency}')
plt.axis('off')
plt.show()

main()

Enter a frequency value (e.g., 0.1, 1, 5): 1

You might also like