From 46717ac657638e3dbcb2cb070088dcfa4e2752d5 Mon Sep 17 00:00:00 2001 From: devansh83 Date: Fri, 27 Oct 2023 19:14:25 +0530 Subject: [PATCH 1/2] Object Tracker based on HSV Values --- Tracking.py | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Tracking.py diff --git a/Tracking.py b/Tracking.py new file mode 100644 index 0000000..35373c8 --- /dev/null +++ b/Tracking.py @@ -0,0 +1,65 @@ +import cv2 +import numpy as np + +cam = cv2.VideoCapture(0) + +hueHigh = 0 +hueLow = 0 +satHigh = 0 +satLow = 0 +valHigh = 0 +valLow = 0 + +def hueh(val): + global hueHigh + hueHigh = val +def huel(val): + global hueLow + hueLow = val +def sath(val): + global satHigh + satHigh = val +def satl(val): + global satLow + satLow = val +def valh(val): + global valHigh + valHigh = val +def vall(val): + global valLow + valLow = val + +cv2.namedWindow('My frame') +cv2.createTrackbar('hueL','My frame',0,180,huel) +cv2.createTrackbar('hueH','My frame',0,180,hueh) + +cv2.createTrackbar('satL','My frame',0,255,satl) +cv2.createTrackbar('satH','My frame',0,255,sath) + +cv2.createTrackbar('valL','My frame',0,255,vall) +cv2.createTrackbar('valH','My frame',0,255,valh) + + +while True: + ignore,frame = cam.read() + frameHSV = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV) + + lowerBound = np.array([hueLow,satLow,valLow]) + upperBound = np.array([hueHigh,satHigh,valHigh]) + + myMask = cv2.inRange(frameHSV,lowerBound,upperBound) + + contours,junk = cv2.findContours(myMask,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) + for contour in contours: + area = cv2.contourArea(contour) + if area >=1000: + # cv2.drawContours(frame,[contour],0,(255,0,0),3) + x,y,w,h=cv2.boundingRect(contour) + cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),3) + + mySelection = cv2.bitwise_and(frame,frame,mask=myMask) + cv2.imshow("My Selection",mySelection) + cv2.imshow("Frame",frame) + if cv2.waitKey(1) & 0xff == ord('q'): + break +cam.release() From 91e5429f281e2d7882f901b02471a400d2d22d8b Mon Sep 17 00:00:00 2001 From: devansh83 Date: Fri, 27 Oct 2023 19:28:19 +0530 Subject: [PATCH 2/2] Stitches 2 images together to create a single combined image --- ImageStiching.py | 121 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 ImageStiching.py diff --git a/ImageStiching.py b/ImageStiching.py new file mode 100644 index 0000000..16df695 --- /dev/null +++ b/ImageStiching.py @@ -0,0 +1,121 @@ +import cv2 +import numpy as np + +img1 = input("Enter the path of the first image: ") +img2 = input("Enter the path of the second image: ") + +train = cv2.imread(r"{}".format(img1)) +query = cv2.imread(r"{}".format(img2)) +train_RGB = cv2.cvtColor(train,cv2.COLOR_BGR2RGB) +query_RGB = cv2.cvtColor(query,cv2.COLOR_BGR2RGB) +train_gray = cv2.cvtColor(train_RGB,cv2.COLOR_RGB2GRAY) +query_gray = cv2.cvtColor(query_RGB,cv2.COLOR_RGB2GRAY) + +query_gray = cv2.resize(query,(500,300)) +train_gray = cv2.resize(train,(500,300)) +query = cv2.resize(query,(500,300)) +train = cv2.resize(train,(500,300)) +feature_extraction_algo = 'sift' +feature_to_match = 'bf' + +def select_descriptor(image,method=None): + assert method is not None,"Please define a descriptor method. Accepted values are 'Sift','Surf','orb','brisk' " + + if method == 'sift': + descriptor = cv2.SIFT_create() + if method == 'surf': + descriptor = cv2.SURF_create() + if method == 'orb': + descriptor = cv2.ORB_create() + if method == 'brisk': + descriptor = cv2.BRISK_create() + (keypoints,features) = descriptor.detectAndCompute(image,None) + return (keypoints,features) + +keypoints_train,feature_train = select_descriptor(train_gray,feature_extraction_algo) +keypoints_query,feature_query = select_descriptor(query_gray,feature_extraction_algo) + +# print(keypoints_query) +# for keypoint in keypoints_query: +# x,y = keypoint.pt +# size = keypoint.size +# orientation = keypoint.angle +# response = keypoint.response +# octave = keypoint.octave +# class_id = keypoint.class_id +# print(x,y) +# cv2.imshow("Image1 ",cv2.drawKeypoints(train_gray,keypoints_train,None,color=(0,255,0))) +# cv2.imshow("Image2",cv2.drawKeypoints(query_gray,keypoints_query,None,color=(0,255,0))) # to draw key points +cv2.imshow("Image 1",train) +cv2.imshow("Image 2",query) + +def create_matching_object(method,crossCheck): + if method == 'sift' or method == 'surf': + bf = cv2.BFMatcher(cv2.NORM_L2,crossCheck=crossCheck) + if method == 'brisk' or method == 'orb': + bf = cv2.BFMatcher(cv2.NORM_HAMMING,crossCheck==crossCheck) + + return bf + +def keypoints_matching(feature_train,feature_query,method): + bf = create_matching_object(method,True) + best_matches = bf.match(feature_train,feature_query) + raw_matches = sorted(best_matches,key = lambda x:x.distance) + print("Raw Matches with Brute Force",len(raw_matches)) + return raw_matches + +def keypoints_matching_knn(feature_train,feature_query,ratio,method): + bf = create_matching_object(method,False) + raw_matches = bf.knnMatch(feature_train,feature_query,k=2) + print("Raw Matches with Knn",len(raw_matches)) + + knn_matches=[] + for m,n in raw_matches: + if m.distance4: + points_train = np.float32([keypoints_train_image[m.queryIdx] for m in matches]) + points_query= np.float32([keypoints_query_image[m.trainIdx] for m in matches]) + + (H,status)=cv2.findHomography(points_train,points_query,cv2.RANSAC,reprojthreshhold) + return (matches,H,status) + + else: + return None + +M = homography_Stiching(keypoints_train,keypoints_query,4) +if M is None: + print('Error') +(matches,Homography_Matrix,status) = M +print(Homography_Matrix) +width = query.shape[1]+train.shape[1] +print(width) +height = max(query.shape[0],train.shape[0]) +print(height) + +result = cv2.warpPerspective(train,Homography_Matrix,(width,height)) +print(result) + +result[0:query.shape[0],0:query.shape[1]] = query + +cv2.imshow("Stich",result) +cv2.waitKey(0) +cv2.destroyAllWindows() \ No newline at end of file