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.
i) #To Read,display the image
image=cv2.imread("nature.jpg",1)
cv2_imshow(image)
ii) #To write the image
image = cv2.imread("nature.jpg",-1)
cv2.imwrite("pain.jpg",image)
iii) #Find the shape of the Image
print(image.shape)
iv) #To access rows and columns
for i in range(150):
for j in range(image.shape[1]):
image[i][j] = [random.randint(0,255),random.randint(0,255),random.randint(0,255)]
cv2_imshow(image)
cv2.waitKey(0)
v) #To cut and paste portion of image
image= cv2.imread('pranav.jpg',-1)
new = image[200:450,200:450]
image[150:400,150:400] = new
cv2_imshow(image)
cv2.waitKey(0)
Thus the images are read, displayed, and written successfully using the python program.