From cbf2603c1b32385772efdca98551ae8599dc64da Mon Sep 17 00:00:00 2001 From: ryan-preble Date: Mon, 27 Jul 2026 13:16:20 -0400 Subject: [PATCH 1/2] Migrate to OpenCV 5 --- GetLargestContour.py | 4 ++-- ImageProcess/CalculatePhenotypeSift.py | 2 +- ImageProcess/MatchAndAlignImages.py | 2 +- ImageProcess/MergeChannels.py | 2 +- ImageProcess/RemoveBackground.py | 3 ++- ImageProcess/RemoveBackgroundPercentage.py | 12 ++++++++---- ImageStitching/Panorama/Panorama.py | 2 +- 7 files changed, 16 insertions(+), 11 deletions(-) diff --git a/GetLargestContour.py b/GetLargestContour.py index 1ae7da3..1435820 100644 --- a/GetLargestContour.py +++ b/GetLargestContour.py @@ -62,8 +62,8 @@ cv2.drawContours(output, hull, -1, (0,255,0), 2) largestcontourarea = cv2.contourArea(hull[0]) - for p in hull[0]: - polygon.append({'x':p[0][0], 'y':p[0][1]}) + for p in np.atleast_2d(np.squeeze(hull[0])): + polygon.append({'x':p[0], 'y':p[1]}) #cv2.rectangle(output,(x,y),(x+w,y+h),(0,255,0),2) sd = CropPolygonsToSingleImage() diff --git a/ImageProcess/CalculatePhenotypeSift.py b/ImageProcess/CalculatePhenotypeSift.py index 117c00e..f704374 100644 --- a/ImageProcess/CalculatePhenotypeSift.py +++ b/ImageProcess/CalculatePhenotypeSift.py @@ -27,7 +27,7 @@ result_file_lines = [] for image in images: img = cv2.imread(image) - descriptor = cv2.xfeatures2d.SIFT_create() + descriptor = cv2.SIFT_create() (kps, features) = descriptor.detectAndCompute(img, None) print(features) result_file_lines.append([len(kps)]) diff --git a/ImageProcess/MatchAndAlignImages.py b/ImageProcess/MatchAndAlignImages.py index 2077c8e..86ce155 100644 --- a/ImageProcess/MatchAndAlignImages.py +++ b/ImageProcess/MatchAndAlignImages.py @@ -34,7 +34,7 @@ img2 = cv2.imread(input_image1, cv2.IMREAD_UNCHANGED) # orb = cv2.ORB_create(MAX_FEATURES) -orb = cv2.xfeatures2d.SIFT_create(MAX_FEATURES) +orb = cv2.SIFT_create(MAX_FEATURES) keypoints1, descriptors1 = orb.detectAndCompute(img1, None) keypoints2, descriptors2 = orb.detectAndCompute(img2, None) diff --git a/ImageProcess/MergeChannels.py b/ImageProcess/MergeChannels.py index 7861821..f9babd4 100644 --- a/ImageProcess/MergeChannels.py +++ b/ImageProcess/MergeChannels.py @@ -21,7 +21,7 @@ def align_images(moving, fixed_im): moving_im = b # Initiate SIFT detector - sift = cv2.xfeatures2d.SIFT_create() + sift = cv2.SIFT_create() print("GET SIFT") # find the keypoints and descriptors with SIFT diff --git a/ImageProcess/RemoveBackground.py b/ImageProcess/RemoveBackground.py index 10c9c94..68dc92d 100644 --- a/ImageProcess/RemoveBackground.py +++ b/ImageProcess/RemoveBackground.py @@ -23,7 +23,8 @@ src = cv2.imread(input_image, cv2.IMREAD_GRAYSCALE) -th, dst = cv2.threshold(src, int(float(lower_thresh)), int(float(upper_thresh)), cv2.THRESH_TOZERO) +th, dst = cv2.threshold(src, int(float(lower_thresh)), 0, cv2.THRESH_TOZERO) # zero out pixels darker than lower_thresh +th, dst = cv2.threshold(dst, int(float(upper_thresh)), 0, cv2.THRESH_TOZERO_INV) # zero out pixels brighter than upper_thresh #cv2.imshow("Result", dst) cv2.imwrite(outfile_path, dst) diff --git a/ImageProcess/RemoveBackgroundPercentage.py b/ImageProcess/RemoveBackgroundPercentage.py index 10320df..813f5f2 100644 --- a/ImageProcess/RemoveBackgroundPercentage.py +++ b/ImageProcess/RemoveBackgroundPercentage.py @@ -29,6 +29,9 @@ img_shape = img.shape if len(img_shape) == 3: + if img_shape[2] == 4: + img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) # drop the alpha channel so it is not thresholded along with the color channels + img_shape = img.shape if img_shape[2] == 3: b,g,r = cv2.split(img) if image_band_index is not None: @@ -41,7 +44,7 @@ img = r -hist = cv2.calcHist([img],[0],None,histSize,[0,255]) +hist = cv2.calcHist([img],[0],None,histSize,[0,256]).flatten() #print(hist) #print(img.shape) @@ -51,7 +54,7 @@ drone_imagery_remove_background_lower_percentage_threshold = 0 drone_imagery_remove_background_upper_percentage_threshold = 0 for i in range(0, histSize[0]): - binVal = hist[i][0] + binVal = hist[i] summing = summing + binVal percentage = summing / total_pixels if percentage >= lower_percentage: @@ -60,7 +63,7 @@ summing = 0; for i in range(0, histSize[0]): - binVal = hist[i][0] + binVal = hist[i] summing = summing + binVal percentage = summing / total_pixels if percentage >= 1-upper_percentage: @@ -69,7 +72,8 @@ lower_thresh = int(float(drone_imagery_remove_background_lower_percentage_threshold)) upper_thresh = int(float(drone_imagery_remove_background_upper_percentage_threshold)) -th, dst = cv2.threshold(img, lower_thresh, upper_thresh, cv2.THRESH_TOZERO) +th, dst = cv2.threshold(img, lower_thresh, 0, cv2.THRESH_TOZERO) # zero out the darkest lower_percentage of pixels +th, dst = cv2.threshold(dst, upper_thresh, 0, cv2.THRESH_TOZERO_INV) # zero out the brightest upper_percentage of pixels cv2.imwrite(outfile_path, dst) #cv2.waitKey(0) diff --git a/ImageStitching/Panorama/Panorama.py b/ImageStitching/Panorama/Panorama.py index 87d4989..af07b00 100644 --- a/ImageStitching/Panorama/Panorama.py +++ b/ImageStitching/Panorama/Panorama.py @@ -82,7 +82,7 @@ def detectAndDescribe(self, image): # Only works in OpenCV 3.X # detect and extract features from the image - descriptor = cv2.xfeatures2d.SIFT_create() + descriptor = cv2.SIFT_create() (kps, features) = descriptor.detectAndCompute(image, None) # convert the keypoints from KeyPoint objects to NumPy arrays From cf04440031d8c365b1f77052f8f97308e1a46ed3 Mon Sep 17 00:00:00 2001 From: ryan-preble Date: Tue, 28 Jul 2026 10:56:43 -0400 Subject: [PATCH 2/2] Remove mysterious semicolon --- ImageProcess/RemoveBackgroundPercentage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ImageProcess/RemoveBackgroundPercentage.py b/ImageProcess/RemoveBackgroundPercentage.py index 813f5f2..58d052b 100644 --- a/ImageProcess/RemoveBackgroundPercentage.py +++ b/ImageProcess/RemoveBackgroundPercentage.py @@ -61,7 +61,7 @@ drone_imagery_remove_background_lower_percentage_threshold = i break -summing = 0; +summing = 0 for i in range(0, histSize[0]): binVal = hist[i] summing = summing + binVal