DIP Lab Manual No 03
DIP Lab Manual No 03
Lab Manual No 03
Learning Outcomes:-
To read and display image using OpenCV Python, you could use cv2.imread() for reading image
to a variable and cv2.imshow() to display the image in a separate window.
Syntax – cv2.imread():
cv2.imread(/complete/path/to/image,flag)
• First argument is complete path to the image along with the extension.
• Second argument is an optional flag which could be any one of the following:
cv2.IMREAD_COLOR : Loads a color image. Any transparency of image will
be neglected. It is the default flag.
cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode
Returns numpy array, containing the pixel values. For colored images, each pixel is represented as
an array containing Red, Green and Blue channels. Note that the default flag is
cv2.IMREAD_COLOR.
Syntax – cv2.imshow():
cv2.imshow(window_name, image)
• First argument is name of the window that is displayed when image is shown in.
• Second argument is the image to be shown in the window.
When working with colab, there is a slight change in syntax. Add these two lines of codes
import cv2
img = cv2.imread('opencv_screenshot.jpg')
cv2_imshow(img)
When working with OpenCV Python, images are stored in numpy ndarray. To save an image to
the local file system, use cv2.imwrite() function of opencv python library.
Syntax of cv2.imwrite():
cv2.imwrite('/path/to/destination/image.png',image)
where,
• First Argument is Path to the destination on file system, where image is ought to be
saved.
• Second Argument is ndarray containing image
Example:
import cv2
img = cv2.imread('opencv_screenshot.jpg')
cv2.imwrite('image.png',img)
When working with OpenCV Python, images are stored in numpy ndarray. To get the image shape
or size, use ndarray.shape to get the dimensions of the image. Then, you can use index on the
dimensions variable to get width, height and number of channels for each pixel.
Example:
img = cv2.imread('/path/to/image.png')
dimensions = img.shape
print(dimensions)
where
• Height represents the number of pixel rows in the image or the number of pixels in each
column of the image array.
• Width represents the number of pixel columns in the image or the number of pixels in
each row of the image array.
• Number of Channels represents the number of components used to represent each pixel.
Resizing an image means changing the dimensions of it, be it width alone, height alone or both.
Also, the aspect ratio of the original image could be preserved in the resized image. To resize an
image, OpenCV provides cv2.resize() function.
Syntax of cv2.resize():
may be a preferred method for image decimation, as it gives moire’-free results. But when
the image is zoomed, it is similar to the INTER_NEAREST method. INTER_CUBIC – a
bicubic interpolation over 4×4 pixel neighborhood INTER_LANCZOS4 – a Lanczos
interpolation over 8×8 pixel neighborhood
import cv2
img = cv2.imread('opencv_screenshot.jpg', 0)
print(img)
cv2_imshow(img)
k = cv2.waitKey(5000)
if k == 27:
cv2.destroyAllWindows()
elif k == ord('s'):
cv2.imwrite('my_image.jpg', img)
cv2.destroyAllWindows()
dimensions = img.shape
height = img.shape[0]
width = img.shape[1]
#channels = img.shape[2]
# resize image
cv2_imshow(resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
# resize image
cv2_imshow(resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
width = 440
# resize image
cv2_imshow(resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
height = 440
# resize image
cv2_imshow(resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
width = 350
height = 450
# resize image
cv2_imshow(resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
Lab Tasks:
THINK!!
1. What will be the number of dimensions of a grayscale image if
opened as colored?
2. Is image a list, tuple or an array?
3. A black and white image can only have what gray levels?
4. Can we flip the image using just one line python code?
5. What is the difference between cv.write and printing an image?
8. Read any image that you want and save it in gray scale. Now mirror
the image that you have read at center i.e., the upper half of the
image should be the copy of the lower half but mirrored.
Write the image to the disk. See the image below.