Python
Python
import cv2
img = cv2.imread('Independence-Day.jpg')
import cv2
img = cv2.imread("image.jpg")
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
See the difference in colors of images read by cv2 and matplotlib library.
Because cv2 uses BGR color format and matplotlib uses RGB color format.
(in google colab)
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('Independence-Day.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
#plt.axis('off')
plt.show()
cv2.imwrite('output.jpg', img)
We can see the shape , i.e., width and height and channels of the image
using shape attribute.
img.shape
cropped_img = img[100:500,400:900]
cv2.imwrite('output1.jpg', cropped_img)
Converting to Grayscale:Convert an image to grayscale using
cv2.cvtColor()
img = cv2.imread('Independence-Day.jpg')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imwrite('gray.jpg', gray_img)
import cv2
from matplotlib import pyplot as plt
welcome
great
learning
now
start
good
practice
Now as the vocabulary has only 7 words, we can use a fixed-length document-
representation of 7, with one position in the vector to score each word.
The scoring method we use here is the same as used in the previous example. For
sentence 1, the count of words is as follow:Word Frequency
welcome 1
great 1
learning 2
now 1
start 1
good 0
practice 0
Writing the above frequencies in the vector
Sentence 1 ➝ [ 1,1,2,1,1,0,0 ]
Now for sentence 2, the scoring would be like Word Frequency
welcome 0
great 0
learning 1
now 0
start 0
good 1
practice 1
Similarly, writing the above frequencies in the vector form
Sentence 2 ➝ [ 0,0,1,0,0,1,1 ]Sentence welcome great learning now
start good practice
Sentence1 1 1 2 1 1 0 0
Sentence2 0 0 1 0 0 1 1
Stopwords are common words like "is," "the," "and," etc., which are often removed
because they don't carry significant meaning for analysis. After removing them,
we create a dictionary of unique words.
Step 3: Create the Vector of Each Sentence
Finally, combine the vectors for all sentences into a structured vector format.
"love" appears once, "dogs" appears once, "cats" appears once, "running"
appears once, "hate" appears once.
Sentence 1: [1, 1, 1, 0, 0]
Sentence 2: [1, 1, 1, 1, 1]
"enjoy" appears once, "football" appears once, "play" appears once, "great,"
"sport," and "playing" do not appear.
Sentence 1: [1, 1, 1, 0, 0, 0]
Sentence 2: [0, 0, 1, 1, 1, 0]
Sentence 3: [1, 0, 1, 0, 0, 1]