All Python Codes:
9:- Color Histogram; 10:- Histogram enhance open CV
from PIL import Image import cv2
import matplotlib.pyplot as plt import numpy as np
im = Image.open("image.jpg") import matplotlib.pyplot as plt
im.show()
r, g, b = im.split() image=cv2.imread('image.jpg',cv2.IMREAD_GR
AYSCALE)
if image is None:
plt.subplot(3, 1, 1)
print("Error: Image not found!")
plt.xlabel("Levels")
exit()
plt.ylabel("Number of red pixels")
equalized_image = cv2.equalizeHist(image)
plt.plot(im.histogram(r),c='red')
plt.figure(figsize=(10,5))
plt.subplot(1,2,1)
plt.subplot(3, 1, 2)
plt.imshow(image, cmap='gray')
plt.xlabel("Levels")
plt.title('original image')
plt.ylabel("Number of green pixels")
plt.axis('off')
plt.plot(im.histogram(r),c='green')
plt.subplot(1,2,2)
plt.subplot(3, 1, 3)
plt.imshow(equalized_image,cmap='gray')
plt.title('equalized image')
plt.axis('off')
plt.xlabel("Levels")
plt.show()
plt.ylabel("Number of blue pixels")
cv2.imwrite('image_c2.jpg',equalized_image)
plt.plot(im.histogram(r),c='blue')
plt.show()
11:- Linear Regression; 12:- Logistic regression;
import matplotlib.pyplot as plt import numpy as np
from scipy import stats import matplotlib.pyplot as plt
x = [1, 3, 5, 7, 9, 11, 34, 23, 32, 45] def sigmoid(z):
y = [20, 22, 24, 26, 28, 30, 32, 34, 36, 38] return 1 / (1 + np.exp(-z))
slope, intercept, r, p, std_err = stats.linregress(x, y) z = np.arange(-5, 5, 0.1)
def myfun(x): phi_z = sigmoid(z)
return slope * x + intercept plt.plot(z, phi_z)
mymodel = list(map(myfun, x)) plt.axvline(0.0, color='k')
plt.scatter(x, y) plt.xlabel('z')
plt.plot(x, mymodel) plt.ylabel('$\phi(z)$')
plt.show() plt.yticks([0.0, 0.5, 1.0])
ax = plt.gca()
import matplotlib.pyplot as plt ax.yaxis.grid(True)
from scipy import stats plt.tight_layout()
x = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] plt.show()
y = [19, 17, 15, 13, 11, 9, 7, 5, 3, 1]
slope, intercept, r, p, std_err = stats.linregress(x, y)
def myfun(x):
return slope * x + intercept
mymodel = list(map(myfun, x))
plt.scatter(x, y)
plt.plot(x, mymodel)
plt.show()
8:- File handling Operations;
#for read operation
import os
import os
cwd = os.getcwd()
os.getcwd()
print("Working Directory:", cwd)
f=open('file4.txt','r')
if not os.path.exists('file4.txt'):
call=f.read()
f = open('file4.txt', 'x')
print(call)
print("File Created Successfully")
f.close()
else:
print("File already exists.")
f = open('file4.txt', 'w')
f.write('Project Presentation')
print("Data written successfully")
f.close()
#append
import os
ced=os.getcwd()
f=open("file4.txt",'a')
f.write('trying to append')
print("Data appended successfully")
f.close()
#for writing lines from a list
import os
cwd=os.getcwd()
f=open("file4.txt",'w')
list=['My roll number is 64',' I am a second year Engineering student']
f.writelines(list)
print('list values written successfully')
f.close()
7:-Recursion function, sum of digit of 5 digit no.; 6:- Vowels, Consonents, White Space;
def sum_of_digits(n): input_str = input("Enter a string: ")
if n == 0: vowel = consonant = whitespace = digit = 0
return 0 input_str = input_str.lower() # Convert string to
lowercase
else:
for i in range(len(input_str)):
return (n % 10) + sum_of_digits(int(n / 10))
if input_str[i] in ("a", "e", "i", "o", "u"):
num = int(input("Enter the Number= "))
vowel += 1
result = sum_of_digits(num)
elif 'a' <= input_str[i] <= 'z':
print("Sum of Digits= ", result)
consonant += 1
elif '0' <= input_str[i] <= '9':
digit += 1
def add(n):
elif input_str[i] == " ":
total = 0
whitespace += 1
while n != 0:
print("Vowels:", vowel)
digit = n % 10
print("Consonants:", consonant)
total = total + digit
print("Digits:", digit)
n = n // 10
print("Whitespace:", whitespace)
return total
num = int(input("Enter the Number= "))
result = add(num)
print("Sum of digits= ", result)
5:- To calculate Factorial number; 4:- Largest and Smallest among 3 numbers;
num = int(input("Enter a Number: ")) a = 23
factorial = 1 b=7
if num < 0: c = 54
print("Factorial of a negative number does not if (a >= b) and (a >= c):
exist")
print("Largest number is:", a)
elif num == 0:
elif (b >= a) and (b >= c):
print("Factorial of 0 is 1")
print("Largest number is:", b)
else:
else:
for i in range(1, num + 1):
print("Largest number is:", c)
factorial = factorial * i
print("The factorial of", num, "is", factorial)
a = int(input("Enter Value of a: "))
b = int(input("Enter Value of b: "))
def fact(num):
c = int(input("Enter Value of c: "))
factorial = 1
if (a <= b) and (a <= c):
for i in range(1, num + 1):
print("Smallest number is:", a)
factorial = factorial * i
elif (b <= a) and (b <= c):
return factorial
print("Smallest number is:", b)
number = int(input("Enter number to find factorial:
")) else:
print(fact(number)) print("Smallest number is:", c)
def recursive_factorial(num):
if num == 1:
return 1
else:
return num * recursive_factorial(num - 1)
number = int(input("Enter Number: "))
print("The factorial of", number, "is",
recursive_factorial(number))
3:- To calculate length of a string;
string = input("give the input")
count=0
for i in string:
count = count + 1
print("length of the string is:")
print(count)
num=int(input("Enter an Integer:"))
mod=num%2
if mod>0:
print("Odd Number")
else:
print("Even Number")