0% found this document useful (0 votes)
12 views5 pages

Python

very important guyz

Uploaded by

sb220224
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Python

very important guyz

Uploaded by

sb220224
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

nstallation: OpenCV can be installed using the command: pip install

opencv-python Importing OpenCV in Python: import cv2 cv2 is the module


used for OpenCV in Python.

import cv2

Basic Image Operations Reading an Image Use the cv2.imread() function


to load an image:

img = cv2.imread('Independence-Day.jpg')

Displaying an Image Use cv2.imshow() to display an image in a window


(using cv2 in idle/jupyter notebook/spyder)

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()

Save an image using cv2.imwrite()

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

Resizing an Image:Use cv2.resize() to change the size of an image

resized_img = cv2.resize(img, (180, 320))


cv2.imwrite('newpicture.jpg', resized_img)

Cropping an Image:Crop a section of an image using slicing

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)

Split image in RGB scale

import cv2
from matplotlib import pyplot as plt

image = cv2.imread('Independence-Day.jpg') # Read the image in BGR format

A,B,C = cv2.split(image) # Split the image into B, G, R channels

# Display the original image


image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert BGR to RGB
for correct display in matplotlib

plt.subplot(2, 2, 1) # Create a 2x2 grid, position 1


plt.imshow(image_rgb)
plt.title('Original')
plt.axis('off') # Hide axis

# Display the Blue channel


plt.subplot(2, 2, 2)
plt.imshow(A,cmap="gray") # Use grayscale color map to visualize the intensity
of the channel
plt.title('Blue Channel')
plt.axis('off')

# Display the Green channel


plt.subplot(2, 2, 3)
plt.imshow(B)
plt.title('Green Channel')
plt.axis('off')

# Display the Red channel


plt.subplot(2, 2, 4)
plt.imshow(C, cmap='gray')
plt.title('Red Channel')
plt.axis('off')

# Show all the plots together


plt.show()

Sentence 1: ”Welcome to Great Learning, Now start learning”


Sentence 2: “Learning is a good practice”
Step 1: Convert the above sentences in lower case as the case of the word does
not hold any information.
Step 2: Remove special characters and stopwords from the text. Stopwords are
the words that do not contain much information about text like ‘is’, ‘a’,’the and
many more’.
After applying the above steps, the sentences are changed to
Sentence 1: ”welcome great learning now start learning”
Sentence 2: “learning good practice”
Although the above sentences do not make much sense the maximum information
is contained in these words only.
Step 3: Go through all the words in the above text and make a list of all of the
words in our model vocabulary.

 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

Step 1: Convert Sentences to Lowercase

First, we convert all words to lowercase to avoid duplication due to different


capitalizations.
Step 2: Remove Stopwords and Create the Dictionary

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

Each sentence is represented as a vector where each position corresponds to a


word in the dictionary, and the value is the number of times that word appears in
the sentence.
Step 4: Create the Complete Vector Representation

Finally, combine the vectors for all sentences into a structured vector format.

 : "I love dogs and cats."


 : "Dogs love running but hate cats."
 Sentence 1: "i love dogs and cats."
 Sentence 2: "dogs love running but hate cats."

Stopwords like "and," "but," "the," "is," etc., are removed.

 : ["i," "and," "but"]


 Remaining words:
 Sentence 1: ["love," "dogs," "cats"]
 Sentence 2: ["dogs," "love," "running," "hate," "cats"]

Now, we create the dictionary (unique words):

 Dictionary: ["love," "dogs," "cats," "running," "hate"]

Based on the dictionary, create vectors for each sentence:

 ("love dogs cats"): [1, 1, 1, 0, 0]


 "love" appears once, "dogs" appears once, "cats" appears once, "running"
does not appear, "hate" does not appear.

("dogs love running hate cats"): [1, 1, 1, 1, 1]

 "love" appears once, "dogs" appears once, "cats" appears once, "running"
appears once, "hate" appears once.

The complete vector for both sentences is:

 Sentence 1: [1, 1, 1, 0, 0]
 Sentence 2: [1, 1, 1, 1, 1]

Example 2: Three Sentences

 : "I enjoy playing football."


 : "Football is a great sport."
 : "I play football and enjoy it."
 Sentence 1: "i enjoy playing football."
 Sentence 2: "football is a great sport."
 Sentence 3: "i play football and enjoy it."

Stopwords like "is," "a," "and," "it," etc., are removed.


 : ["i," "is," "a," "and," "it"]
 Remaining words:
 Sentence 1: ["enjoy," "playing," "football"]
 Sentence 2: ["football," "great," "sport"]
 Sentence 3: ["play," "football," "enjoy"]

Now, we create the dictionary (unique words):

 Dictionary: ["enjoy," "playing," "football," "great," "sport," "play"]

Based on the dictionary, create vectors for each sentence:

 ("enjoy playing football"): [1, 1, 1, 0, 0, 0]


 "enjoy" appears once, "playing" appears once, "football" appears once,
"great," "sport," and "play" do not appear.

("football great sport"): [0, 0, 1, 1, 1, 0]

 "football" appears once, "great" appears once, "sport" appears once,


"enjoy," "playing," and "play" do not appear.

("play football enjoy"): [1, 0, 1, 0, 0, 1]

 "enjoy" appears once, "football" appears once, "play" appears once, "great,"
"sport," and "playing" do not appear.

The complete vector for all three sentences is:

 Sentence 1: [1, 1, 1, 0, 0, 0]
 Sentence 2: [0, 0, 1, 1, 1, 0]
 Sentence 3: [1, 0, 1, 0, 0, 1]

: Convert all words to lowercase.: Remove common stopwords.: Represent each


sentence as a vector based on the remaining words.: Combine vectors for all
sentences to form the complete representation.

You might also like