To write a python program using OpenCV to do the following image manipulations. i) Read, display, and write an image. ii) Access the rows and columns in an image. iii) Cut and paste a small portion of the image.
Anaconda - Python 3.7
Choose an image and save it as a filename.jpg
Use imread(filename, flags) to read the file.
Use imshow(window_name, image) to display the image.
Use imwrite(filename, image) to write the image.
End the program and close the output image windows.
import cv2
color_image=cv2.imread('pic23.jpg',1)
cv2.imshow('color_image',color_image)
cv2.waitKey(0)
gray_image=cv2.imread('pic23.jpg',0)
cv2.imshow('gray_image',gray_image)
cv2.waitKey(0)
import cv2
colorim = cv2.imread('pic23.jpg',1)
cv2.imwrite('pic23.jpg',colorim)
cv2.imshow('pic23.jpg',colorim)
cv2.waitKey(0)
color_image=cv2.imread('pic23.jpg',1)
print(color_image.shape)
import random
import cv2
A=cv2.imread("pic23.jpg",1)
for i in range(100):
for j in range(A.shape[1]):
A[i][j]=[random.randint(0,255),random.randint(0,255),random.randint(0,255)]
cv2.imshow("color_image",A)
cv2.waitKey(0)
import cv2
A=cv2.imread("pic23.jpg",1)
tag=A[140:240,165:180]
A[25:125,50:65]=tag
cv2.imshow("color_image",A)
cv2.waitKey(0)
Thus the images are read, displayed, and written successfully using the python program.