Ivalab
Ivalab
Program:
import cv2
import numpy as np
pyramid = [image]
for i in range(levels):
image = cv2.pyrDown(image)
pyramid.append(image)
return pyramid
image = cv2.imread(r'D:\meenu_rs\img1.jpg')
if image is None:
exit()
levels = 4
for i in range(len(pyramid)):
cv2.waitKey(0)
cv2.destroyAllWindows()
Program:
import cv2
import numpy as np
img = cv2.imread(r'D:\iva\tree.webp')
def split4(image):
top_bottom = np.array_split(image, 2)
def flatten_split(parts):
def mean_color(image):
def is_uniform(image):
class QuadTree:
self.level = level
self.mean = mean_color(image)
self.size = image.shape[:2]
self.leaf = is_uniform(image)
if not self.leaf:
parts = flatten_split(split4(image))
return self
def get_image(self, target_level):
return combine4(
self.nw.get_image(target_level),
self.ne.get_image(target_level),
self.sw.get_image(target_level),
self.se.get_image(target_level)
parts = flatten_split(split4(img))
for i, ax in enumerate(axs.flat):
ax.imshow(parts[i])
plt.show()
combined = combine4(*parts)
plt.imshow(combined)
plt.show()
print(means)
plt.imshow(means)
plt.show()
qt = QuadTree().insert(img)
plt.imshow(qt.get_image(level))
plt.show()
Meaning:
1. Image Load Panradhu:
Computer-la irukra oru image file-ai eduthu appadiye use panna ready-aakkiradhu.
12. QuadTree-a Use Panni Different Levels-ku Image Generate Panni Display Panradhu:
QuadTree structure-a use panni, levels 1, 3, 7, 10-ku average color patches use
pannitu images generate pannuvom, adha display pannuvom. Level koranda podhu
image blurry, level perunda detail clear.
Image Rotation:
The image is rotated by 90 degrees. It calculates the center of the image and
rotates it around that center. The rotation is done with a specific scale (in this
case, 0.6), which reduces the image size while rotating.
Resize Pannuthu:
Both the original image and the rotated image are resized to a fixed size of
800x600 pixels to ensure that they are displayed clearly and consistently.
Scaling the Image: The image is then scaled (resized) to 50% of its original
size in both the x (horizontal) and y (vertical) directions using the cv2.resize
function. The fx parameter controls the scaling factor for the x-axis (width),
and fy controls the scaling factor for the y-axis (height). Here, both are set to
0.5, meaning the image will be resized to half its original width and height.
Displaying Images: After scaling, both the original and scaled images are
shown using cv2.imshow. The first window will display the original image,
and the second window will display the scaled image.
Waiting and Closing: The cv2.waitKey(0) function waits for any key to be
pressed, and once a key is pressed, the cv2.destroyAllWindows() function will
close the windows displaying the images.
Program:
import cv2
import numpy as np
img = cv2.imread(r'D:\meenu_rs\img1.jpg')
pts1 = np.float32([[cols * 0.45, rows * 0.95], [cols * 0.90, rows * 0.95], [cols *
0.10, 0], [cols, 0]])
pts2 = np.float32([[cols * 0.1, rows], [cols, rows], [0, 0], [cols, 0]])
M = cv2.getPerspectiveTransform(pts1, pts2)
cv2.imshow('Original', small_img)
cv2.imshow('Transformed', small_dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
Meaning:
First, oru image-ah read pannirukaanga system-la irundhu.
Ithu rendu image-ai oru window-la kaattum, and then user-oda key press-a
wait pannum.
import numpy as np
img = cv2.imread(r'D:\meenu_rs\img1.jpg')
M = cv2.getAffineTransform(pts1, pts2)
plt.subplot(121)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title('Input')
plt.subplot(122)
plt.imshow(cv2.cvtColor(dst, cv2.COLOR_BGR2RGB))
plt.title('Output')
plt.show()
cv2.destroyAllWindows()
Meaning:
Next, moonu point pairs define panrom — original image-la irundhu three
points (source) and athukku correspond aagura three points (destination).
import numpy as np
img = cv2.imread(r'D:\meenu_rs\tree.jpg')
if img is None:
exit()
x2, y2 = x1 + 1, y1 + 1
dx, dy = x - x1, y - y1
tl = img[y1, x1].astype(np.float32)
tr = img[y1, x2].astype(np.float32)
bl = img[y2, x1].astype(np.float32)
br = img[y2, x2].astype(np.float32)
for x, y in points:
val = bilinear_interp(img, x, y)
cv2.waitKey(0)
cv2.destroyAllWindows()
Meaning:
Four points are defined on the image where we want to calculate the pixel
value.
For each of these points, it checks the neighboring pixels and calculates the
pixel value at that point using bilinear interpolation (based on four
surrounding pixels).
A red circle is drawn at each of the four points to mark them on the image.
Finally, the image with the points marked is shown on the screen.
img = cv2.imread(r"D:\iva\cat.webp")
if img is None:
exit()
if cascade.empty():
exit()
plt.imshow(rgb)
plt.axis('off')
plt.show()
Meaning:
1. import cv2 and from matplotlib import pyplot as plt — Ithu OpenCV mathiri image
processing library-um, matplotlib-la image display panna use panra tool-um load
panrathu.
3. if img is None: ... — Image file correct-a load aagala na error message solli program-
a stop pannudhu.
7. if cascade.empty(): ... — Haar cascade file load aagala error solli program-a stop
pannudhu.
9. for (x, y, w, h) in found: — Detect panna cat face ellam rectangle draw pannudhu.
11. print(...) — Cat face detect aana message illa na detect aagala message print
pannudhu.
def motion_analysis(path):
cap = cv2.VideoCapture(path)
while True:
ret, frame = cap.read()
cv2.imshow("Motion", frame)
prev = gray
cap.release(), cv2.destroyAllWindows()
if __name__ == "__main__":
Meanings:
3. Apparam, video open pannitu first frame ah read pannum. Adha gray
(black & white) color la maathidum. Ithu motion detect panna easy
aagum.
4. Ippo video la oru oru frame read pannum. Frame illa na video
mudinchu nu artham, so loop stop aagum.
5. Every new frame-um black & white la maathidum. Appo previous frame
kooda compare pannum to see edhavadhu maari irukka (like car move
aagiruchaa nu paakardhukku).
9. Video stop aana udane, program clean ah video file close pannidum,
screen um close aagum.
Program:
import cv2, numpy as np
f=cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')
kf=cv2.imread(r"D:\iva\face.jpg",0)
x,y,w,h=f.detectMultiScale(kf,1.1,5)[0]
kr=kf[y:y+h,x:x+w]
cap=cv2.VideoCapture(0)
while 1:
ret,frame=cap.read()
g=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
roi=cv2.resize(g[y:y+h,x:x+w],(w,h))
mse=np.mean(cv2.absdiff(roi,known_resized))
cv2.rectangle(frame,(x,y),(x+w,y+h),c,2)
cv2.putText(frame,t,(x,y-10),0,0.7,c,2)
cv2.imshow('Face',frame)
if cv2.waitKey(1)&0xFF==ord('q'): break
cap.release()
cv2.destroyAllWindows()
Meaning:
1. Import Libraries
• Load OpenCV’s pre-trained face detector using Haar Cascade XML file.
• This detector helps find faces in images or video frames.
• Check if the image was loaded successfully, else stop the program.
• Crop the face region from the known image to use as a reference.
▪ Resize the known face crop to the same size as the detected
face region.
6. Open Camera
• Loop continuously:
8. Cleanup
video = cv2.VideoCapture('D:\\meenu_rs\\event.mp4')
bg = cv2.createBackgroundSubtractorMOG2()
while True:
if not ret:
break
mask = bg.apply(frame)
for c in contours:
x, y, w, h = cv2.boundingRect(c)
cv2.imshow("Video", frame)
cv2.imshow("Motion", mask)
break
video.release()
cv2.destroyAllWindows()
Meaning:
Program:
import cv2
import numpy as np
img = cv2.imread(r"D:\meenu_rs\gamma.webp")
gamma = 0.6
scale = 0.5
cv2.imshow("Original", small(img))
cv2.waitKey(0)
cv2.destroyAllWindows()
Meaning:
img = cv2.imread(r"D:\meenu_rs\gamma.webp")
Ithu image-ai load panrathu from the given path. File path correct illa na img will be None.
gamma = 0.6
Gamma value set pannirukkom. Ithu brightness and contrast adjust panrathuku use aagum.
scale = 0.5
Ithu scale factor. Image size 50% ku reduce panna use pannuvom.
cv2.imshow("Original", small(img))
Original image size-a reduce panni display pannum.
cv2.waitKey(0)
User oru key press pannum varai window open-a irukkum.
cv2.destroyAllWindows()
Display pannra windows ellam close pannum