0% found this document useful (0 votes)
4 views7 pages

CV Lab

The document contains Python code that performs various image processing tasks using OpenCV and Matplotlib. It includes loading an image, adjusting contrast, performing histogram equalization in both grayscale and color, applying a low-pass filter using Fourier Transform, and displaying the results. Additionally, it demonstrates converting images to grayscale, binary, and negative formats, as well as splitting the color channels of an image.

Uploaded by

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

CV Lab

The document contains Python code that performs various image processing tasks using OpenCV and Matplotlib. It includes loading an image, adjusting contrast, performing histogram equalization in both grayscale and color, applying a low-pass filter using Fourier Transform, and displaying the results. Additionally, it demonstrates converting images to grayscale, binary, and negative formats, as well as splitting the color channels of an image.

Uploaded by

Nagaraju K
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import cv2

import [Link] as plt

# ---------------------- LOAD IMAGE ----------------------

img = [Link](r"D:\Aditya University\[Link]")

if img is None:

print("Image not found!")

exit()

else:

print("Image loaded successfully:", [Link])

[Link](“myimage”,img);

[Link](0);

[Link]();

# ---------------------- CONTRAST ADJUSTMENT ----------------------

alpha = 1.5 # contrast

beta = 0 # brightness

contrast_img = [Link](img, alpha=alpha, beta=beta)

[Link]("contrast_output.jpg", contrast_img)
# ---------------------- HISTOGRAM PROCESSING ----------------------

colors = ('b', 'g', 'r')


[Link]()
[Link]("Histogram for Original Image")
[Link]("Pixel value")
[Link]("Frequency")
for i, col in enumerate(colors):
hist = [Link]([img], [i], None, [256], [0, 256])
[Link](hist, color=col)
[Link]([0, 256])
[Link]()

# ---------------------- HISTOGRAM EQUALIZATION (GRAY) ----------------------

gray = [Link](img, cv2.COLOR_BGR2GRAY)

equalized_gray = [Link](gray)

[Link]("equalized_gray.jpg", equalized_gray)

[Link]()

[Link]("Grayscale Histogram Equalization")

[Link](1, 2, 1)

[Link]("Original Gray")

[Link](gray, cmap='gray')

[Link]("off")

[Link](1, 2, 2)

[Link]("Equalized Gray")

[Link](equalized_gray, cmap='gray')

[Link]("off")

[Link]()
# ---------------------- HISTOGRAM EQUALIZATION (COLOR: LAB) ----------------------

lab = [Link](img, cv2.COLOR_BGR2LAB)

l, a, b = [Link](lab)

# equalize only L channel

l_eq = [Link](l)

lab_eq = [Link]((l_eq, a, b))

color_equalized = [Link](lab_eq, cv2.COLOR_LAB2BGR)

[Link]("equalized_color.jpg", color_equalized)

# ---------------------- DISPLAY RESULTS ----------------------

[Link]("Original Image", img)

[Link]("Contrast Adjusted", contrast_img)

[Link]("Equalized Gray", equalized_gray)

[Link]("Color Equalized Image", color_equalized)

[Link](0)

[Link]()

import cv2

import numpy as np
import [Link] as plt

# Step 1: Read the image in grayscale

img = [Link]("D:\II-Sem\ComputerVision\[Link]",
cv2.IMREAD_GRAYSCALE)

# Step 2: Perform Fourier Transform

dft = [Link].fft2(img)

dft_shift = [Link](dft)

# Step 3: Create a Low-Pass Filter mask

rows, cols = [Link]

crow, ccol = rows // 2, cols // 2

mask = [Link]((rows, cols), np.uint8)

radius = 50 # cutoff frequency

[Link](mask, (ccol, crow), radius, 1,-1)

# Step 4: Apply mask

filtered_dft = dft_shift * mask

# Step 5: Inverse Fourier Transform

ifft_shift = [Link](filtered_dft)

filtered_img = [Link].ifft2(ifft_shift)

filtered_img = [Link](filtered_img)

# Step 6: Display results

[Link](figsize=(10, 8))

[Link](2, 2, 1)
[Link]("Original Image")

[Link](img, cmap='gray')

[Link]('off')

[Link](2, 2, 2)

[Link]("Magnitude Spectrum")

[Link]([Link](1 + [Link](dft_shift)), cmap='gray')

[Link]('off')

[Link](2, 2, 3)

[Link]("Low-Pass Filter Mask")

[Link](mask, cmap='gray')

[Link]('off')

[Link](2, 2, 4)

[Link]("Filtered Image")

[Link](filtered_img, cmap='gray')

[Link]('off')

plt.tight_layout()

[Link]()

import cv2
img=[Link]("D:\Aditya University\[Link]")
gray=[Link](img,cv2.COLOR_BGR2GRAY);
ret,binary=[Link](gray,120,255,[Link]
H_BINARY)
negative=255-gray
[Link]("[Link]",gray);
[Link]("[Link]",binary);
[Link]("[Link]",negative)
[Link]("myimage",img)
[Link]("grayimage",gray)
[Link]("Binary image",binary)
[Link]("Negative",negative);
[Link](0)
[Link]()

import cv2

img=[Link]("D:\Aditya University\[Link]")

[Link]("my image",img)

b,g,r=[Link](img);

[Link]("my image",b)
[Link]("my image",g)

[Link]("my image",r)

[Link](0)

[Link]();

You might also like