0% found this document useful (0 votes)
30 views5 pages

Adjusting Brightness and Contrast

The document discusses several image processing techniques including adjusting brightness and contrast, sharpening images, removing noise, enhancing color, resizing and scaling images, inverting colors, and equalizing histograms. Each technique is demonstrated by importing libraries, loading an image, applying the technique using OpenCV functions, saving the resulting image, and plotting the original and processed images for comparison.

Uploaded by

Galatom Yadeta
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)
30 views5 pages

Adjusting Brightness and Contrast

The document discusses several image processing techniques including adjusting brightness and contrast, sharpening images, removing noise, enhancing color, resizing and scaling images, inverting colors, and equalizing histograms. Each technique is demonstrated by importing libraries, loading an image, applying the technique using OpenCV functions, saving the resulting image, and plotting the original and processed images for comparison.

Uploaded by

Galatom Yadeta
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
You are on page 1/ 5

Adjusting brightness and contrast

#Import the necessary libraries


import cv2
import matplotlib.pyplot as plt
import numpy as np
# Load the image
image = cv2.imread('GFG.jpeg')
#Plot the original image
plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)
# Adjust the brightness and contrast
# Adjusts the brightness by adding 10 to each pixel value
brightness = 10
# Adjusts the contrast by scaling the pixel values by 2.3
contrast = 2.3
image2 = cv2.addWeighted(image, contrast, np.zeros(image.shape, image.dtype), 0, brightness)
#Save the image
cv2.imwrite('modified_image.jpg', image2)
#Plot the contrast image
plt.subplot(1, 2, 2)
plt.title("Brightness & contrast")
plt.imshow(image2)
plt.show()

Sharpening images
#Import the necessary libraries
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Load the image
image = cv2.imread('GFG.jpeg')
#Plot the original image
plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)
# Create the sharpening kernel
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
# Sharpen the image
sharpened_image = cv2.filter2D(image, -1, kernel)
#Save the image
cv2.imwrite('sharpened_image.jpg', sharpened_image)
#Plot the sharpened image
plt.subplot(1, 2, 2)
plt.title("Sharpening")
plt.imshow(sharpened_image)
plt.show()

Removing noise from images


Median Blur
#Import the necessary libraries
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Load the image
image = cv2.imread('GFG.jpeg')
#Plot the original image
plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)
# Remove noise using a median filter
filtered_image = cv2.medianBlur(image, 11)
#Save the image
cv2.imwrite('Median Blur.jpg', filtered_image)
#Plot the blured image
plt.subplot(1, 2, 2)
plt.title("Median Blur")
plt.imshow(filtered_image)
plt.show()

GaussianBlur
#Import the necessary libraries
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Load the image
image = cv2.imread('GFG.jpeg')
#Plot the original image
plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)
# Remove noise using a Gaussian filter
filtered_image2 = cv2.GaussianBlur(image, (7, 7), 0)
#Save the image
cv2.imwrite('Gaussian Blur.jpg', filtered_image2)
#Plot the blured image
plt.subplot(1, 2, 2)
plt.title("Gaussian Blur")
plt.imshow(filtered_image2)
plt.show()

Enhancing color in images


#Import the necessary libraries
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Load the image
image = cv2.imread('GFG.jpeg')
#Plot the original image
plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)
# Convert the image from BGR to HSV color space
image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
# Adjust the hue, saturation, and value of the image
# Adjusts the hue by multiplying it by 0.7
image[:, :, 0] = image[:, :, 0] * 0.7
# Adjusts the saturation by multiplying it by 1.5
image[:, :, 1] = image[:, :, 1] * 1.5
# Adjusts the value by multiplying it by 0.5
image[:, :, 2] = image[:, :, 2] * 0.5
# Convert the image back to BGR color space
image2 = cv2.cvtColor(image, cv2.COLOR_HSV2BGR)
#Save the image
cv2.imwrite('enhanced coloured.jpg', image2)
#Plot the enhanced image
plt.subplot(1, 2, 2)
plt.title("enhanced coloured")
plt.imshow(image2)
plt.show()

Image resizing and scaling


Resized Image
#Import the necessary libraries
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Load the image
image = cv2.imread('GFG.jpeg')
#Plot the original image
plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)
# Resize the image to a specific width and height
resized_image = cv2.resize(image, (2100, 1500))
#Save the Resized image
cv2.imwrite('Resized image.jpg', resized_image)
#Plot the Resized image
plt.subplot(1, 2, 2)
plt.title("Resized")
plt.imshow(resized_image)
plt.show()

Scaled Image
#Import the necessary libraries
import cv2
import matplotlib.pyplot as plt
import numpy as np

# Load the image


image = cv2.imread('GFG.jpeg')
#Plot the original image
plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)
# Scale the image by a factor of 2 along both axes
scaled_image = cv2.resize(image, None, fx=2, fy=2)
#Save the image
cv2.imwrite('Scaled.jpg', scaled_image)
#Plot the Scaled image
plt.subplot(1, 2, 2)
plt.title("Scaled")
plt.imshow(scaled_image)
plt.show()
Inverse Transform
#Import the necessary libraries
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Load the image
image = cv2.imread('GFG.jpeg')

#Plot the original image


plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)
# Inverse by subtracting from 255
inverse_image = 255 - image
#Save the image
cv2.imwrite('inverse_image.jpg', inverse_image)
#Plot the Inverse image
plt.subplot(1, 2, 2)
plt.title("Inverse color")
plt.imshow(inverse_image)
plt.show()

Equalized

#Import the necessary libraries


import cv2
import matplotlib.pyplot as plt
import numpy as np
# Load the image
image = cv2.imread('GFG.jpeg')

#Plot the original image


plt.subplot(1, 2, 1)
plt.title("Original")
plt.imshow(image)
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Equalize the histogram
equalized_image = cv2.equalizeHist(gray_image)
#Save the equalized image
cv2.imwrite('equalized.jpg', equalized_image)
#Plot the equalized image
plt.subplot(1, 2, 2)
plt.title("equalized")
plt.imshow(equalized_image)
plt.show()

You might also like