Practical Image-1
Practical Image-1
Practical Image-1
Practical
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.
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
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)
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))
Output
Pixel at (0, 0) - Red: 253, Green: 251, Blue: 251
Pixel at (0, 0) - Red: 0, Green: 255, Blue: 255
plt.imshow(pic)
cv2.waitKey(0)
cv2.destroyAllWindows()
plt.show()
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:
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:
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)
#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")
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')
# remove noise
img = cv2.GaussianBlur(gray,(3,3),0)
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)
# 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
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)