0% found this document useful (0 votes)
51 views4 pages

Esponilla, Jonnel Lopez, Joshua Albert Templonuevo, Sofia Coleen Valerio, Mark Kenneth

The document is an assignment that converts an image to greyscale or black and white using OpenCV in Python. The code imports necessary libraries, prompts the user to select an image, and allows them to choose converting it to greyscale or black and white using Otsu's algorithm. If greyscale is chosen, it converts the image to greyscale and displays both images side by side. If black and white is chosen, it uses Otsu's algorithm to threshold the image into a binary black and white one and also displays both images side by side. Screenshots are included showing the user interface and output images.
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)
51 views4 pages

Esponilla, Jonnel Lopez, Joshua Albert Templonuevo, Sofia Coleen Valerio, Mark Kenneth

The document is an assignment that converts an image to greyscale or black and white using OpenCV in Python. The code imports necessary libraries, prompts the user to select an image, and allows them to choose converting it to greyscale or black and white using Otsu's algorithm. If greyscale is chosen, it converts the image to greyscale and displays both images side by side. If black and white is chosen, it uses Otsu's algorithm to threshold the image into a binary black and white one and also displays both images side by side. Screenshots are included showing the user interface and output images.
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/ 4

Assignment No.

3
Greyscale
Course Code: CPE102 Program: BSCPE
Course Title: Machine Perception Date Performed:12/18/20
Section: CPE31S3 Date Submitted:12/18/20
Names: Balunes, Prince Patrick Instructor: Engr. Gabriel Sampedro
Esponilla, Jonnel
Lopez, Joshua Albert
Templonuevo, Sofia Coleen
Valerio, Mark Kenneth
Source Code:

import cv2
import numpy as np
import tkinter as tk
from tkinter import filedialog

input ("You will be asked to open an image to edit. Press ENTER to


continue.")

root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()

image = cv2.imread(file_path)

fxn = input("""What do you want to do with the selected image?


[1] greyscale
[2] black and white (using Otsu's Algorithm)
[other keys] exit the program
Your answer: """)

if fxn == '1':

grey = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)


grey_3_channel = cv2.cvtColor(grey, cv2.COLOR_GRAY2BGR)
stack = np.hstack((image, grey_3_channel))

cv2.imshow('Grayscale Image', stack)

cv2.waitKey(0)
cv2.destroyAllWindows()

elif fxn == '2':

grey = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)


ret, thresh = cv2.threshold(grey, 120, 255, cv2.THRESH_BINARY +
cv2.THRESH_OTSU)
grey_3_channel = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)

stack = np.hstack((image, grey_3_channel))

cv2.imshow('Black&White Image', stack)


cv2.waitKey(0)
cv2.destroyAllWindows()

else:
pass

The code consists of programming that is to be displayed the greyscale of an image and to display a
black and white version of the image using Otsu’s algorithm besides from the original image

Output:

Figure 2.1
This figure shows the user to open an image.
Figure 2.2
This figure shows the user prompt to pick an image.

Figure 2.3
Figure 2.3 shows the selected image and how it transformed in a grayscale image.
Figure 2.4
As you can see on the figure 2.4, the selected image transformed into black and white image in another window

You might also like