Advertisement
furas

Python - OpenCV - detect red line

Sep 4th, 2023 (edited)
967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. # Code for answer to question on Stackoverflow:
  2. # "opencv - find a red line in image by python cv2 - Stack Overflow"
  3. # https://stackoverflow.com/questions/77037396/find-a-red-line-in-image-by-python-cv2
  4.  
  5.  
  6. import cv2
  7.  
  8.  
  9. class RedLineDetector:
  10.  
  11.     def __init__(self):
  12.         self.image = None
  13.    
  14.     def load_image(self, image_path):
  15.         self.image = cv2.imread(image_path)
  16.    
  17.     def detect_line(self):
  18.         if self.image is None:
  19.             raise ValueError("No image loaded. Please load an image using 'load_image' method.")
  20.        
  21.         height, width, _ = self.image.shape
  22.         print(height, width)
  23.        
  24.         min_width = 300  # Minimum width of the red line
  25.         min_height = 10  # Minimum height of the red line
  26.         max_height = 50  # Maximum height of the red line
  27.  
  28.         ### I reduce size to see full image on screen ###
  29.         preview_image = cv2.resize(self.image, (width//4, height//4))
  30.         cv2.imshow('image', preview_image)
  31.         #cv2.waitKey(0)
  32.  
  33.         # find red pixels in some range                
  34.         thresh = cv2.inRange(self.image, (50,50,160), (120, 120, 200))
  35.         #print(thresh)
  36.        
  37.         ### I reduce size to see full image on screen ###
  38.         preview_thresh = cv2.resize(thresh, (width//4, height//4))
  39.         cv2.imshow('thresh', preview_thresh)
  40.         #cv2.waitKey(0)
  41.  
  42.         # find contours with red pixels
  43.         contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  44.         contours = contours[0] if len(contours) == 2 else contours[1]
  45.         print('len(contours)', len(contours))
  46.         #print(contours)
  47.        
  48.         result = self.image.copy()
  49.         bounding_box = None
  50.        
  51.         for c in contours:
  52.             #area = cv2.contourArea(c)
  53.             x, y, w, h = cv2.boundingRect(c)
  54.            
  55.             if  min_width < w and min_height < h < max_height:
  56.                 bounding_box = x, y, w, h
  57.                 cv2.drawContours(result, [c], -1, (0, 255, 0), 2)
  58.                 print('w, h:', w, h)
  59.                 break
  60.                            
  61.         ### I reduce size to see full image on screen ###
  62.         preview_result = cv2.resize(result, (width//4, height//4))
  63.         cv2.imshow('result', preview_result)
  64.         cv2.waitKey(0)                
  65.  
  66.         return bounding_box
  67.  
  68. # Example usage
  69. detector = RedLineDetector()
  70. detector.load_image('frame3.jpg')
  71. bounding_box = detector.detect_line()
  72. if bounding_box is not None:
  73.     print("Bounding Box Coordinates:", bounding_box)
  74. else:
  75.     print("No red line detected.")
Tags: python OpenCV
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement