Practical Image-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Image processing

Practical

Eng. Sahar kamal


What’s a pixel?
Every image consists of a set of pixels. Pixels are the raw building blocks of an
image. There is no finer granularity than the pixel.

For example, let’s pretend we have an image with a resolution of 500x 300. This
means that our image is represented as a grid of pixels, with 500 rows and 300
columns. Overall, there are 500x 300 = 150, 000 pixels in our image.

Most pixels are represented in two ways: gray scale and color. In a gray scale
image, each pixel has a value between 0 and 255, where zero corresponds to
“black” and 255 corresponds to “white”. The values in between 0 and 255 are
varying shades of gray, where values closer to 0 are darker and values closer to
255 are lighter.

Color pixels are normally represented in the RGB color space – one value for
the Red component, one for Green, and one for Blue. Other color spaces exist,
but let’s start with the basics and move our way up from there.

Each of the three colors is represented by an integer in the range 0 to 255,


which indicates how “much” of the color there is. Given that the pixel value
only needs to be in the range [0, 255], we normally use an 8-bit unsigned
integer to represent each color intensity. We then combine these values into an
RGB tuple in the form (red, green, blue). This tuple represents our color.

For your reference, here are some common colors represented as RGB
tuples:
• Black: (0,0,0)
• White: (255,255,255)
• Red: (255,0,0)
• Green: (0,255,0)
• Blue: (0,0,255)
Important library
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import imageio
import cv2

how to read image as input /output


image = imageio.imread('photo.jpg')

1.Program how to load image and display dimension


import numpy as np
import matplotlib.image as mpimg
import cv2

# Load the image and show some basic information on it


image = mpimg.imread('photo.jpg')
print("width: {} pixels".format(image.shape[1]))
print("height: {} pixels".format(image.shape[0]))
print("channels: {}".format(image.shape[2]))

# Show the image and wait for a keypress


cv2.imshow("Image", image)
cv2.waitKey(0)

# Save the image -- OpenCV handles converting filetypes


# automatically

Output
width: 350 pixels
height: 228 pixels
channels: 3
2.Program how to Resize image and save it in another name

import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img= mpimg.imread('photo.jpg')
resized = cv2.resize(img_water, (400, 400))

#plt.figure(figsize = (10,10))
#plt.imshow(img)
plt.imshow(resized)
plt.title('Image')
plt.show()
cv2.imwrite("newimage.jpg", image)

3.Program how to convert image from color to gray level

import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread('photo.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow('Original image',img)
cv2.imshow('Gray image', gray)
cv2.imwrite("newimage.png", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
4.Program how to change image pixel value and display
red,green and blue

import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image= mpimg.imread('photo.jpg')
#cv2.imshow("Original", image)
(b, g, r) = image[0, 0]
print("Pixel at (0, 0) - Red: {}, Green: {}, Blue: {}".format(r, g, b))

(b, g, r) = image[200, 200]


print("Pixel at (0, 0) - Red: {}, Green: {}, Blue: {}".format(r, g, b))

Output
Pixel at (0, 0) - Red: 253, Green: 251, Blue: 251
Pixel at (0, 0) - Red: 0, Green: 255, Blue: 255

5.Program how to display corner


import numpy as np
import imageio
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
pic = imageio.imread('image.jpg')
corner = pic[30:120, 250:350]
cv2.imshow("Corner", corner)

#pic[0:100, 0:100] = (255, 0, 100)


##cv2.imshow("Updated", pic)

#pic[150:200 ,: , 2] = 255 # full intensity to those pixel's R channel


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

plt.imshow(pic)
cv2.waitKey(0)
cv2.destroyAllWindows()
plt.show()

6.Program how to display image in different channel


import numpy as np
import imageio
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
pic= mpimg.imread('image.jpg')
plt.title(' channel')
plt.ylabel('Height {}'.format(pic.shape[0]))
plt.xlabel('Width {}'.format(pic.shape[1]))

plt.imshow(pic[ : , : , ])
plt.show()
# assing each channel
import numpy as np
pic = imageio.imread('photo.jpg')
fig, ax = plt.subplots(nrows = 1, ncols=3, figsize=(15,5))
for c, ax in zip(range(3), ax):
# create zero matrix
split_img = np.zeros(pic.shape, dtype="uint8") # 'dtype' by default: 'numpy.float64'
# assing each channel
split_img[ :, :, c] = pic[ :, :, c]
# display each channel
ax.imshow(split_img)
Smoothing Images
Goals
Learn to:

• Blur imagess with various low pass filters


• Apply custom-made filters to images (2D convolution)

2D Convolution ( Image Filtering )


As for one-dimensional signals, images also can be filtered with various low-pass filters
(LPF), high-pass filters (HPF), etc. A LPF helps in removing noise, or blurring the image. A
HPF filters helps in finding edges in an image.

OpenCV provides a function, cv2.filter2D(), to convolve a kernel with an image. As an


example, we will try an averaging filter on an image. A 5x5 averaging filter kernel can be
defined as follows:

Filtering with the above kernel results in the following being performed: for each pixel, a
5x5 window is centered on this pixel, all pixels falling within this window are summed up,
and the result is then divided by 25. This equates to computing the average of the pixel
values inside that window. This operation is performed for all the pixels in the image to
produce the output filtered image. Try this code and check the result:

Image filtering is used to:


Remove noise
Sharpen contrast
Highlight contours
Detect edges
1. Averaging
Average (or mean) filtering is a method of ‘smoothing’ images by

Reducing the amount of intensity variation between neighboring pixels. The


average filter works by moving through the image pixel by pixel, replacing
each value with the average value of neighboring pixels, including it.
There are some potential problems:
A single pixel with a very unrepresentative value can significantly affect the
average value of all the pixels in its neighborhood.
When the filter neighborhood straddles an edge, the filter will interpolate new
values for pixels on the edge and so will blur that edge.

import cv2
import numpy as np
from matplotlib import pyplot as plt
image= plt.imread('image.jpg')
img=cv2.blur(image, (3, 3))
img1=cv2.blur(image, (5, 5))
img2=cv2.blur(image, (7, 7))
cv2.imshow("Averaged", img)
cv2.imshow("Averaged1", img1)
cv2.imshow("Averaged2", img2)

cv2.waitKey(0)
2. Median Filtering
Median filtering is a nonlinear method used to remove noise from images.
It is widely used as it is very effective at removing noise while preserving
edges.
It is particularly effective at removing ‘salt and pepper’ type noise.
The median filter works by moving through the image pixel by pixel, replacing
each value with the median value of neighboring pixels.
The median is calculated by first sorting all the pixel values from the window
into numerical order, and then replacing the pixel being considered with the
middle (median) pixel value

import cv2
import numpy as np
from matplotlib import pyplot as plt
image= plt.imread('image.jpg')

blurred = np.hstack([
cv2.medianBlur(image, 3),
cv2.medianBlur(image, 5),
cv2.medianBlur(image, 7)])
cv2.imshow("Median", blurred)
cv2.waitKey(0)

import cv2
# Gaussian filter
import numpy as np
from matplotlib import pyplot as plt
image= plt.imread('image.jpg')

blurred = np.hstack([
cv2.GaussianBlur(image, (3, 3), 0),
cv2.GaussianBlur(image, (5, 5), 0),
cv2.GaussianBlur(image, (7, 7), 0)])
cv2.imshow("Gaussian", blurred)
cv2.waitKey(0)

Image transformations
1.Translation
The first method we are going to explore is translation. Translation is the shifting of
an image along the x and y axis. Using translation, we can shift an image up, down,
left, or right, along with any combination of the above

2.Flipping
Next up on our image transformations to explore is flipping an image. We can flip an
image around either the x or y axis, or even both.

3. Rotation
Rotation is exactly what it sounds like: rotating an image by some angle q. In this
section, we’ll explore how to rotate an image. We’ll use q to represent by how many
degrees we are rotating the image. Later, I’ll provide another convenience method,
rotate, to make performing rotations on images easier.

import cv2
import numpy as np
from matplotlib import pyplot as plt
image= plt.imread('image.jpg')

#shifting
M = np.float32([[1, 0, 25], [0, 1, 50]])
shifted = cv2.warpAffine(image, M, (image.shape[1], image.shape [0]))
cv2.imshow("Shifted Down and Right", shifted)

M = np.float32([[1, 0, -50], [0, 1, -90]])


shifted = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
cv2.imshow("Shifted Up and Left", shifted)

#flipping
flipped = cv2.flip(image, 1)
cv2.imshow("Flipped Horizontally", flipped)
# Rotate
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, 45, 1.0)
rotated = cv2.warpAffine(image, M, (w, h))
cv2.imshow("Rotated by 45 Degrees", rotated)
cv2.waitKey(0)
4.Adding & subtraction
import cv2
import numpy as np
from matplotlib import pyplot as plt
image= plt.imread('image.jpg')
#add
M = np.ones(image.shape,dtype = "uint8") * 150
added = cv2.add(image, M)
cv2.imshow("Added", added)

#subtract
M = np.ones(image.shape, dtype = "uint8") *150
subtracted = cv2.subtract(image, M)
cv2.imshow("Subtracted", subtracted)
cv2.waitKey(0)
4. Gray image histograms
Now that we have an understanding of the cv2.calcHist function, let’s write some
actual code.

import cv2
import numpy as np
from matplotlib import pyplot as plt
image= plt.imread('image.jpg')
hist = cv2.calcHist([image], [0], None, [256], [0, 256])

plt.figure()
plt.title("Grayscale Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")
plt.plot(hist)
plt.xlim([0, 256])
plt.show()
cv2.waitKey(0)
2 .Color image histograms
import cv2
import numpy as np
from matplotlib import pyplot as plt
image= plt.imread('image.jpg')
chans = cv2.split(image)
colors = ("b", "g", "r")
plt.figure()
plt.title("’Flattened’ Color Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")

for (chan, color) in zip(chans, colors):


hist = cv2.calcHist([chan], [0], None, [256], [0, 256])
plt.plot(hist, color = color)
plt.xlim([0, 256])
3. Histogram equalization
Histogram equalization improves the contrast of an image by “stretching” the
distribution of pixels. Consider a histogram with a large peak at the center of it.
Applying histogram equalization will stretch the peak out towards the corner of the
image, thus improving the global contrast of the image. Histogram equalization is
applied to gray scale images.

import cv2
import numpy as np
from matplotlib import pyplot as plt
image= plt.imread('image.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
eq = cv2.equalizeHist(image)
cv2.imshow("Histogram Equalization", np.hstack([image, eq]))
cv2.waitKey(0)
4. edge detection( sobel ,Laplacian)
import cv2
import numpy as np
from matplotlib import pyplot as plt

# loading image
#img0 = cv2.imread('SanFrancisco.jpg',)
img = cv2.imread('image.jpg')

# converting to gray scale


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

# remove noise
img = cv2.GaussianBlur(gray,(3,3),0)

# convolute with proper kernels


laplacian = cv2.Laplacian(img,cv2.CV_64F)
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5) # x
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5) # y

plt.subplot(2,2,1),plt.imshow(img,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,2),plt.imshow(laplacian,cmap = 'gray')
plt.title('Laplacian'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,3),plt.imshow(sobelx,cmap = 'gray')
plt.title('Sobel X'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,4),plt.imshow(sobelx+sobely,cmap = 'gray')
plt.title('Sobel Y+y'), plt.xticks([]), plt.yticks([])

cv2.waitKey(0)
6.prewitt
import cv2
import numpy as np
from matplotlib import pyplot as plt

# loading image
#img0 = cv2.imread('SanFrancisco.jpg',)
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gaussian = cv2.GaussianBlur(gray,(3,3),0)
#prewitt
kernely = np.array([[1,1,1],[0,0,0],[-1,-1,-1]])
kernelx = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])
img_prewittx = cv2.filter2D(img_gaussian, -1, kernelx)
img_prewitty = cv2.filter2D(img_gaussian, -1, kernely)

cv2.imshow("Prewitt X", img_prewittx)


cv2.imshow("Prewitt Y", img_prewitty)
cv2.imshow("Prewitt", img_prewittx + img_prewitty)
cv2.waitKey(0)
# Another method sobel
import cv2
import numpy as np
from matplotlib import pyplot as plt

# loading image
#img0 = cv2.imread('SanFrancisco.jpg',)
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gaussian = cv2.GaussianBlur(gray,(3,3),0)
#sobel
img_sobelx = cv2.Sobel(img_gaussian,cv2.CV_8U,1,0,ksize=5)
img_sobely = cv2.Sobel(img_gaussian,cv2.CV_8U,0,1,ksize=5)
img_sobel = img_sobelx + img_sobely
cv2.imshow("Sobel X", img_sobelx)
cv2.imshow("Sobel Y", img_sobely)
cv2.imshow("Sobelxy", img_sobel)
cv2.waitKey(0)

# threshold
import cv2

from matplotlib import pyplot as plt


image= plt.imread('1.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(image, (5, 5), 0)
cv2.imshow("Image", image)
(T, thresh) = cv2.threshold(blurred, 225, 255, cv2.THRESH_BINARY)
cv2.imshow("Threshold Binary", thresh)

(T, threshInv) = cv2.threshold(blurred,225, 255, cv2.THRESH_BINARY_INV)


cv2.imshow("Threshold Binary Inverse", threshInv)

cv2.imshow("image", cv2.bitwise_and(image, image, mask =threshInv))

cv2.waitKey(0)

#Otsu
import cv2
import numpy as np
from matplotlib import pyplot as plt
import mahotas
# loading image
#img0 = cv2.imread('SanFrancisco.jpg',)
img = cv2.imread('coins.png')
image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(image,(5,5),0)
T = mahotas.thresholding.otsu(blurred)
print("Otsu’s threshold: {}".format(T))
thresh = image.copy()
thresh[thresh > T] = 255
thresh[thresh < 255] = 0
thresh = cv2.bitwise_not(thresh)
cv2.imshow("Otsu", thresh)

T = mahotas.thresholding.rc(blurred)
print("Riddler-Calvard: {}".format(T))
thresh = image.copy()
thresh[thresh > T] = 255
thresh[thresh < 255] = 0
thresh = cv2.bitwise_not(thresh)
cv2.imshow("Riddler-Calvard", thresh)
cv2.waitKey(0)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy