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.
iv) To perform the color conversion between RGB, BGR, HSV, and YCbCr color models.
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.
Convert BGR and RGB to HSV and GRAY
Convert HSV to RGB and BGR
Convert RGB and BGR to YCrCb
Split and Merge RGB Image
Split and merge HSV Image
Developed By: Shriram S
Register Number: 212222240098
#Read and display the image
import cv2
image=cv2.imread('city.jpg',1)
image=cv2.resize(image,(200,325))
cv2.imshow('Shriram',image)
cv2.waitKey(0)
cv2.destroyAllWindows()
#Write the image
import cv2
image=cv2.imread('RGB.jpg',0)
cv2.imwrite('demos.jpg',image)
#Shape of the Image
import cv2
image=cv2.imread('RGB.jpg',1)
print(image.shape)
#Access rows and columns
import random
import cv2
image=cv2.imread('RGB.jpg',1)
image=cv2.resize(image,(400,400))
for i in range (150,200):
for j in range(image.shape[1]):
image[i][j]=[random.randint(0,255),
random.randint(0,255),
random.randint(0,255)]
cv2.imshow('part image',image)
cv2.waitKey(0)
cv2.destroyAllWindows()
#Cut and paste portion of image
import cv2
image=cv2.imread('RGB.jpg',1)
image=cv2.resize(image,(400,400))
tag =image[150:200,110:160]
image[110:160,150:200] = tag
cv2.imshow('partimage1',image)
cv2.waitKey(0)
cv2.destroyAllWindows()
#BGR and RGB to HSV and GRAY
import cv2
img = cv2.imread('RGB.jpg',1)
img = cv2.resize(img,(300,200))
cv2.imshow('Original Image',img)
hsv1 = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow('BGR2HSV',hsv1)
hsv2 = cv2.cvtColor(img,cv2.COLOR_RGB2HSV)
cv2.imshow('RGB2HSV',hsv2)
gray1 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('BGR2GRAY',gray1)
gray2 = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
cv2.imshow('RGB2GRAY',gray2)
cv2.waitKey(0)
cv2.destroyAllWindows()
#HSV to RGB and BGR
import cv2
img = cv2.imread('RGB.jpg')
img = cv2.resize(img,(300,200))
img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow('Original HSV Image',img)
RGB = cv2.cvtColor(img,cv2.COLOR_HSV2RGB)
cv2.imshow('2HSV2BGR',RGB)
BGR = cv2.cvtColor(img,cv2.COLOR_HSV2BGR)
cv2.imshow('HSV2RGB',BGR)
cv2.waitKey(0)
cv2.destroyAllWindows()
#RGB and BGR to YCrCb
import cv2
img = cv2.imread('RGB.jpg')
img = cv2.resize(img,(300,200))
cv2.imshow('Original RGB Image',img)
YCrCb1 = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
cv2.imshow('RGB-2-YCrCb',YCrCb1)
YCrCb2 = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb)
cv2.imshow('BGR-2-YCrCb',YCrCb2)
cv2.waitKey(0)
cv2.destroyAllWindows()
#Split and merge RGB Image
import cv2
img = cv2.imread('RGB.jpg',1)
img = cv2.resize(img,(300,200))
R = img[:,:,2]
G = img[:,:,1]
B = img[:,:,0]
cv2.imshow('R-Channel',R)
cv2.imshow('G-Channel',G)
cv2.imshow('B-Channel',B)
merged = cv2.merge((B,G,R))
cv2.imshow('Merged RGB image',merged)
cv2.waitKey(0)
cv2.destroyAllWindows()
#Split and merge HSV Image
import cv2
img = cv2.imread("RGB.jpg",1)
img = cv2.resize(img,(300,200))
img=cv2.cvtColor(img,cv2.COLOR_RGB2HSV)
H,S,V=cv2.split(img)
cv2.imshow('Hue',H)
cv2.imshow('Saturation',S)
cv2.imshow('Value',V)
merged = cv2.merge((H,S,V))
cv2.imshow('Merged',merged)
cv2.waitKey(0)
cv2.destroyAllWindows()