Getting the x and y coordinates from cv2.contour in Open CV Python and store it to different variables

Getting the x and y coordinates from cv2.contour in Open CV Python and store it to different variables

In OpenCV with Python, when you find contours using cv2.findContours, you get a list of contours, where each contour is represented as an array of points. You can access individual contour points and store their x and y coordinates in separate variables. Here's an example:

import cv2 # Read the image image = cv2.imread('your_image_path.jpg') # Convert the image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Find contours contours, _ = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Iterate through contours for contour in contours: # Iterate through points in the contour for point in contour: # Extract x and y coordinates x, y = point[0] # Print or store the x and y coordinates in separate variables print(f'X: {x}, Y: {y}') # Display the image with contours cv2.drawContours(image, contours, -1, (0, 255, 0), 2) cv2.imshow('Contours', image) cv2.waitKey(0) cv2.destroyAllWindows() 

In this example:

  • contours is a list of contours found in the image.
  • contour represents a single contour in the list.
  • point is an individual point in the contour.
  • point[0] contains the x and y coordinates of the point.

You can print or store the x and y coordinates as needed within your application. Adjust the code according to your specific requirements.

Note: The contour points are integer values, so if you need floating-point precision, you may need to convert them accordingly.

Examples

  1. "OpenCV Python get x and y coordinates from contour"

    • Code Implement:
      # Assuming 'contour' is the contour obtained using cv2.findContours for point in contour: x, y = point[0] # Your logic with x and y 
    • Description: Iterate through the points of the contour and extract x and y coordinates.
  2. "OpenCV Python extract x and y coordinates from contour array"

    • Code Implement:
      # Assuming 'contour' is the contour obtained using cv2.findContours x, y = contour[:, 0, 0], contour[:, 0, 1] # Your logic with x and y 
    • Description: Extract x and y coordinates directly from the contour array using slicing.
  3. "OpenCV Python iterate through contour points and store x and y"

    • Code Implement:
      # Assuming 'contour' is the contour obtained using cv2.findContours x_coords, y_coords = [], [] for point in contour: x, y = point[0] x_coords.append(x) y_coords.append(y) # Your logic with x_coords and y_coords 
    • Description: Iterate through the contour points, store x and y coordinates in separate lists.
  4. "OpenCV Python find x and y coordinates of contour centroid"

    • Code Implement:
      # Assuming 'contour' is the contour obtained using cv2.findContours M = cv2.moments(contour) centroid_x = int(M["m10"] / M["m00"]) centroid_y = int(M["m01"] / M["m00"]) # Your logic with centroid_x and centroid_y 
    • Description: Calculate the centroid of the contour and obtain x and y coordinates.
  5. "OpenCV Python store contour x and y coordinates in a numpy array"

    • Code Implement:
      # Assuming 'contour' is the contour obtained using cv2.findContours points = contour[:, 0, :] # Now 'points' is a numpy array containing x and y coordinates 
    • Description: Extract x and y coordinates and store them in a numpy array for further processing.
  6. "OpenCV Python get bounding box x and y coordinates from contour"

    • Code Implement:
      # Assuming 'contour' is the contour obtained using cv2.findContours x, y, w, h = cv2.boundingRect(contour) # Your logic with x and y 
    • Description: Obtain the bounding box coordinates (x, y) from the contour using cv2.boundingRect.
  7. "OpenCV Python store x and y coordinates in a list from multiple contours"

    • Code Implement:
      # Assuming 'contours' is a list of contours obtained using cv2.findContours all_x_coords, all_y_coords = [], [] for contour in contours: x, y = contour[:, 0, 0], contour[:, 0, 1] all_x_coords.extend(x) all_y_coords.extend(y) # Your logic with all_x_coords and all_y_coords 
    • Description: Iterate through multiple contours and store x and y coordinates in separate lists.
  8. "OpenCV Python find x and y coordinates of contour vertices"

    • Code Implement:
      # Assuming 'contour' is the contour obtained using cv2.findContours epsilon = 0.02 * cv2.arcLength(contour, True) vertices = cv2.approxPolyDP(contour, epsilon, True) x, y = vertices[:, 0, 0], vertices[:, 0, 1] # Your logic with x and y 
    • Description: Approximate the contour with vertices and obtain x and y coordinates.
  9. "OpenCV Python find extreme points of contour and get x and y coordinates"

    • Code Implement:
      # Assuming 'contour' is the contour obtained using cv2.findContours leftmost = tuple(contour[contour[:, :, 0].argmin()][0]) rightmost = tuple(contour[contour[:, :, 0].argmax()][0]) topmost = tuple(contour[contour[:, :, 1].argmin()][0]) bottommost = tuple(contour[contour[:, :, 1].argmax()][0]) left_x, left_y = leftmost right_x, right_y = rightmost top_x, top_y = topmost bottom_x, bottom_y = bottommost # Your logic with left_x, left_y, right_x, right_y, top_x, top_y, bottom_x, bottom_y 
    • Description: Find extreme points of the contour and obtain x and y coordinates for each.
  10. "OpenCV Python find x and y coordinates using moments and Hu moments"

    • Code Implement:
      # Assuming 'contour' is the contour obtained using cv2.findContours M = cv2.moments(contour) hu_moments = cv2.HuMoments(M).flatten() x_cen = int(M["m10"] / M["m00"]) y_cen = int(M["m01"] / M["m00"]) # Your logic with x_cen, y_cen, and hu_moments 
    • Description: Use moments and Hu moments to find x and y coordinates along with other shape descriptors.

More Tags

mnist dynamicobject karma-jasmine instagram visual-studio-addins android-source uft14 hybrid-mobile-app azure-aks google-cdn

More Programming Questions

More Weather Calculators

More Other animals Calculators

More Geometry Calculators

More Fitness Calculators