# Color, shape, area

```
// Some code
import cv2
import rospy
import numpy as np
from cv_bridge import CvBridge
from sensor_msgs.msg import Image

bridge = CvBridge()

def image_colback_color(cv_image):
    img_hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV)
    return img_hsv

img = bridge.imgmsg_to_cv2(rospy.wait_for_message('main_camera/image_raw', Image), 'bgr8')
img_hsv = image_colback_color(img) # BGR to HSV

# masks = dict()
# masks["red"] = cv2.inRange(img_hsv, np.array([175, 0, 0]), np.array([255, 255, 255]))
# masks["yellow"] = cv2.inRange(img_hsv, np.array([28, 0, 0]), np.array([54, 255, 255]))
# masks["black"] = cv2.inRange(img_hsv, np.array([0, 0, 0]), np.array([255, 255, 107]))
# masks["blue"] = cv2.inRange(img_hsv, np.array([116, 0, 0]), np.array([149, 255, 255]))
# masks["green"] = cv2.inRange(img_hsv, np.array([68, 0, 0]), np.array([102, 255, 255]))
# masks["pink"] = cv2.inRange(img_hsv, np.array([167, 0, 0]), np.array([177, 255, 255]))
# masks["grey"] = cv2.inRange(img_hsv, np.array([0, 0, 56]), np.array([255, 255, 154]))
# masks["brown"] = cv2.inRange(img_hsv, np.array([10, 116, 0]), np.array([16, 186, 255]))
# masks["orange"] = cv2.inRange(img_hsv, np.array([10, 147, 0]), np.array([16, 224, 255]))
# masks["purple"] = cv2.inRange(img_hsv, np.array([149, 0, 0]), np.array([161, 255, 255]))

red_mask = cv2.inRange(img_hsv, np.array([175, 0, 0]), np.array([255, 255, 255])) # Красная маска

contours = cv2.findContours(red_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] # Все контуры (список)

for cnt in contours:
    epsilon = 0.01 * cv2.arcLength(cnt, True)
    approx = cv2.approxPolyDP(cnt, epsilon, True)
    corners = len(approx) # Углы фигуры
    shape_type = "" # Тип фигуры
    moment = cv2.moments(cnt)
    if moment['m00'] == 0:
        continue
    area = cv2.contourArea(cnt) # Площадь фигуры (в пикселях)
    x = int(moment['m10'] / moment['m00']) # Центр фигуры
    y = int(moment['m01'] / moment['m00']) # Центр фигуры
    if corners == 3:
        shape_type = "triangle"
    if corners == 4:
        shape_type = "square"
    if corners == 5:
        shape_type = "pentagon"
    if corners == 6:
        shape_type = "hexagon"
    if corners == 10:
        shape_type = "star"
    if corners > 10:
        shape_type = "circle"

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tangerine-2.gitbook.io/red_horizon/programmirovanie/koding-gy/color-shape-area.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
