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

Computer Vision Assignment

This document contains answers to questions about computer vision tasks. It shows code to open, display, convert to grayscale, check dimensions, extract channels of an image. It also shows code to extract pixels, create a new image by sampling pixels, flip an image vertically, and create a histogram.

Uploaded by

Aryaman Sood
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)
43 views

Computer Vision Assignment

This document contains answers to questions about computer vision tasks. It shows code to open, display, convert to grayscale, check dimensions, extract channels of an image. It also shows code to extract pixels, create a new image by sampling pixels, flip an image vertically, and create a histogram.

Uploaded by

Aryaman Sood
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

Computer vision assignment

Submitted by – Yash Chauhan


Roll No. – 2020UCO1657
Branch – COE 3
Q1. Read an image into a variable
Ans. from PIL import Image
# Open an image file
img = Image.open("image.jpg")
# Access the pixel data
pixels = img.load()

Q2. Display that image


Ans. from PIL import Image
import matplotlib.pyplot as plt
# Open an image file
img = Image.open("image.jpg")
# Display the image
plt.imshow(img)
plt.show()
This code uses the Image.open() function from the Pillow library to open an image file
and store it in the img variable. Then, it uses the plt.imshow() function from the
Matplotlib library to display the image stored in the img variable. Finally, the plt.show()
function is used to show the plot.

Q3. Convert an image into a gray scale image.


Ans. from PIL import Image
# Open an image file
img = Image.open("image.jpg").convert('L')
# Save the grayscale image
img.save("gray_image.jpg")
Q4. Check the height and width of that image.
Ans. import cv2
# Load the image
img = cv2.imread("image.jpg")
# Get the image size
height, width, channels = img.shape
# Print the size
print("Width:", width)
print("Height:", height)
Q5. Extract the RGB channels of a given color image
Ans. import cv2
# Load the image
img = cv2.imread("image.jpg")
# Split the image into its RGB channels
r, g, b = cv2.split(img)
# Display the channels
cv2.imshow("Red", r)
cv2.imshow("Green", g)
cv2.imshow("Blue", b)
# Wait for key press and then close the windows
cv2.waitKey(0)
cv2.destroyAllWindows()

Q6. To extract given 100 pixels from a gray scale image


Ans. import cv2
# Load the image
img = cv2.imread("image.jpg", cv2.IMREAD_GRAYSCALE)
# Get the height and width of the image
height, width = img.shape
# Calculate the starting and ending row and column
indices
start_row = height // 2 - 50
end_row = height // 2 + 50
start_col = width // 2 - 50
end_col = width // 2 + 50
# Extract the middle 100 pixels
middle_100 = img[start_row:end_row, start_col:end_col]
# Display the extracted pixels
cv2.imshow("Middle 100 Pixels", middle_100)
# Wait for key press and then close the window
cv2.waitKey(0)
cv2.destroyAllWindows()
Q7. Create a new image with every 10th pixel
horizontally and 20th pixel vertically
Ans. import cv2
# Load the image
img = cv2.imread("image.jpg", cv2.IMREAD_GRAYSCALE)
# Get the height and width of the image
height, width = img.shape
# Initialize an empty image with reduced size
reduced_img = np.zeros((height//20, width//10),
dtype=np.uint8)
# Fill the reduced image with every 10th pixel
horizontally and 20th pixel vertically
for row in range(0, height, 20):
for col in range(0, width, 10):
reduced_img[row//20, col//10] = img[row, col]
# Display the reduced image
cv2.imshow("Reduced Image", reduced_img)
# Wait for key press and then close the window
cv2.waitKey(0)
cv2.destroyAllWindows()
Q8. Flip the image vertically
Ans. import cv2
# Load the image
img = cv2.imread("image.jpg")
# Flip the image vertically
flipped_img = cv2.flip(img, 0)
# Display the flipped image
cv2.imshow("Flipped Image", flipped_img)
# Wait for key press and then close the window
cv2.waitKey(0)
cv2.destroyAllWindows()
Q9. Create a histogram graph and identify number of
pixels with same intensity
Ans. import cv2
import matplotlib.pyplot as plt
# Load the image
img = cv2.imread("image.jpg", cv2.IMREAD_GRAYSCALE)
# Calculate the histogram of the image
hist = cv2.calcHist([img], [0], None, [256], [0, 256])
# Plot the histogram
plt.hist(img.ravel(), 256, [0, 256])
plt.xlim([0, 256])
plt.show()

You might also like