JustPaste
HomeCategoriesAboutDonateContactTerms of UsePrivacy Policy
JustPaste

Free online notepad — write and share instantly

Navigate

  • Home
  • Timeline
  • Categories

Info

  • About
  • Donate
  • Contact

Legal

  • Terms of Use
  • Privacy Policy

© 2026 JustPaste.app. All rights reserved.

Made with ♥ by JustPaste

Untitled Page | JustPaste.app
about 12 hours ago1 views
👨‍💻Programming

Untitled Page

import cv2
import numpy as np
import matplotlib.pyplot as plt


image = cv2.imread("images.jpg")

if image is None:
    print("Error: Image not found.")
    exit()


gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

while True:

    print("\n========== EDGE DETECTION ==========")
    print("1. Sobel Edge Detection")
    print("2. Prewitt Edge Detection")
    print("3. Roberts Edge Detection")
    print("4. Laplacian Edge Detection")
    print("5. Display Original and Grayscale Image")
    print("6. Exit")

    choice = input("Enter your choice: ")

    if choice == "1":
        sobel_x = cv2.Sobel(
            gray, cv2.CV_64F, 1, 0, ksize=3
        )

        sobel_y = cv2.Sobel(
            gray, cv2.CV_64F, 0, 1, ksize=3
        )

        sobel_x = cv2.convertScaleAbs(sobel_x)
        sobel_y = cv2.convertScaleAbs(sobel_y)

        sobel = cv2.addWeighted(
            sobel_x, 0.5,
            sobel_y, 0.5,
            0
        )

        print("\nDisplaying Sobel Edge Detection...")

        plt.imshow(sobel, cmap="gray")
        plt.title("Sobel Edge Detection")
        plt.axis("off")
        plt.show()

    elif choice == "2":

        prewitt_x_kernel = np.array([
            [-1, 0, 1],
            [-1, 0, 1],
            [-1, 0, 1]
        ], dtype=np.float32)

        prewitt_y_kernel = np.array([
            [-1, -1, -1],
            [0, 0, 0],
            [1, 1, 1]
        ], dtype=np.float32)

        prewitt_x = cv2.filter2D(
            gray, cv2.CV_32F,
            prewitt_x_kernel
        )

        prewitt_y = cv2.filter2D(
            gray, cv2.CV_32F,
            prewitt_y_kernel
        )

        prewitt_x = cv2.convertScaleAbs(prewitt_x)
        prewitt_y = cv2.convertScaleAbs(prewitt_y)

        prewitt = cv2.addWeighted(
            prewitt_x, 0.5,
            prewitt_y, 0.5,
            0
        )

        print("\nDisplaying Prewitt Edge Detection...")

        plt.imshow(prewitt, cmap="gray")
        plt.title("Prewitt Edge Detection")
        plt.axis("off")
        plt.show()

    elif choice == "3":

        roberts_x_kernel = np.array([
            [1, 0],
            [0, -1]
        ], dtype=np.float32)

        roberts_y_kernel = np.array([
            [0, 1],
            [-1, 0]
        ], dtype=np.float32)

        roberts_x = cv2.filter2D(
            gray, cv2.CV_32F,
            roberts_x_kernel
        )

        roberts_y = cv2.filter2D(
            gray, cv2.CV_32F,
            roberts_y_kernel
        )

        roberts_x = cv2.convertScaleAbs(roberts_x)
        roberts_y = cv2.convertScaleAbs(roberts_y)

        roberts = cv2.addWeighted(
            roberts_x, 0.5,
            roberts_y, 0.5,
            0
        )

        print("\nDisplaying Roberts Edge Detection...")

        plt.imshow(roberts, cmap="gray")
        plt.title("Roberts Edge Detection")
        plt.axis("off")
        plt.show()

    elif choice == "4":

        laplacian = cv2.Laplacian(
            gray,
            cv2.CV_64F
        )

        laplacian = cv2.convertScaleAbs(
            laplacian
        )

        print("\nDisplaying Laplacian Edge Detection...")

        plt.imshow(laplacian, cmap="gray")
        plt.title("Laplacian Edge Detection")
        plt.axis("off")
        plt.show()

    elif choice == "5":

        plt.figure(figsize=(10, 4))

        plt.subplot(1, 2, 1)
        plt.imshow(
            cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        )
        plt.title("Original Image")
        plt.axis("off")

        plt.subplot(1, 2, 2)
        plt.imshow(gray, cmap="gray")
        plt.title("Grayscale Image")
        plt.axis("off")

        plt.show()

    elif choice == "6":

        print("\nProgram Terminated.")
        break

    else:

        print("\nInvalid choice. Please try again.")

⚠️Content was pasted as plain text and auto-formatted as a code block. Use the Code Block button in the editor for proper formatting.

← Back to timeline