Advertisement
AntoniiaG

Untitled

Jun 7th, 2024
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. from __future__ import print_function
  2. import cv2 as cv
  3. import numpy as np
  4. import argparse
  5. import random as rng
  6.  
  7. rng.seed(12345)
  8.  
  9.  
  10. def calculate_parameters(contour, gray_image):
  11.     area = cv.contourArea(contour)
  12.     perimeter = cv.arcLength(contour, True)
  13.     x, y, w, h = cv.boundingRect(contour)
  14.     aspect_ratio = float(w) / h
  15.     rect_area = w * h
  16.     extent = float(area) / rect_area
  17.     hull = cv.convexHull(contour)
  18.     hull_area = cv.contourArea(hull)
  19.     solidity = float(area) / hull_area
  20.     equi_diameter = np.sqrt(4 * area / np.pi)
  21.     if len(contour) >= 5:
  22.         (x, y), (MA, ma), angle = cv.fitEllipse(contour)
  23.     else:
  24.         angle = 0
  25.     mask = np.zeros_like(gray_image)
  26.     cv.drawContours(mask, [contour], -1, 255, -1)
  27.     mean_val = cv.mean(gray_image, mask=mask)[0]
  28.     return {
  29.         "Area": area,
  30.         "Perimeter": perimeter,
  31.         "Aspect Ratio": aspect_ratio,
  32.         "Extent": extent,
  33.         "Solidity": solidity,
  34.         "Equivalent Diameter": equi_diameter,
  35.         "Orientation": angle,
  36.         "Mean Intensity": mean_val
  37.     }
  38.  
  39.  
  40. def thresh_callback(val):
  41.     threshold = val
  42.     canny_output = cv.Canny(src_gray, threshold, threshold * 2)
  43.     contours, _ = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
  44.  
  45.     drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3), dtype=np.uint8)
  46.  
  47.     for i, contour in enumerate(contours):
  48.         color = (rng.randint(0, 256), rng.randint(0, 256), rng.randint(0, 256))
  49.         cv.drawContours(drawing, contours, i, color)
  50.  
  51.         # Compute and print parameters for each contour
  52.         params = calculate_parameters(contour, src_gray)
  53.         print(f"Contour #{i + 1}:")
  54.         for param_name, param_value in params.items():
  55.             print(f"  {param_name}: {param_value}")
  56.  
  57.     cv.imshow('Contours', drawing)
  58.  
  59.  
  60. parser = argparse.ArgumentParser(description='Code for Creating Bounding boxes and circles for contours tutorial.')
  61. parser.add_argument('--input', help='Path to input image.', default='water_coins.jpg')
  62. args = parser.parse_args()
  63.  
  64. src = cv.imread(cv.samples.findFile(args.input))
  65. if src is None:
  66.     print('Could not open or find the image:', args.input)
  67.     exit(0)
  68.  
  69. src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
  70. src_gray = cv.blur(src_gray, (3, 3))
  71.  
  72. source_window = 'Source'
  73. cv.namedWindow(source_window)
  74. cv.imshow(source_window, src)
  75.  
  76. max_thresh = 255
  77. thresh = 100
  78. cv.createTrackbar('Canny thresh:', source_window, thresh, max_thresh, thresh_callback)
  79. thresh_callback(thresh)
  80.  
  81. cv.waitKey()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement