# Detect Contours and calculate area

Hello, I am looking for help. I would like to have a code that can detects the white material contours between the metal plates and be able to calculate the are of each side. Thank you.

hello, we won’t write your prog,
please take a nice tour through the tutorials,
try to come up with your own solution, then maybe we can help you improve it !

besides that, try to improve your imaging, – use ambient light, not frontal, avoid those nasty shadows, make sure your ‘white material’ is actually white, etc…

Hi have this code, but it is not performing what I want

``````import cv2
import math
import matplotlib.pyplot as plt
import numpy as np

#read in the image

image = cv2.imread('inserttest.jpg')

# converting the image to hsv
img_rgb = cv2.cvtColor (image, cv2.COLOR_BGR2RGB)
hsv_img = cv2.cvtColor (img_rgb, cv2.COLOR_RGB2HSV)

#segmenting color of choice on the hsv image first by specifying color range
#mask is always in binary 1s and 0s, so cant be worked on, with the color info hidden in binary
color_value1 = (10, 10, 44)    #light color --light white (0, 0, 200) light blue (90, 150, 50)
color_value2 = (145, 60, 255)   #dark color -- dark white (145, 60, 255) dark blue (148, 255, 255)
mask = cv2.inRange(hsv_img, color_value1, color_value2 )
#imposing the mask on the original image // converting mask to pixel image
segmented = cv2.bitwise_and(img_rgb, img_rgb, mask = mask)

# Find contours in the image
contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

#getting the x,y coordinates

cnt = contours[0]
# Find the minimum area rectangle for the largest contour
rect = cv2.minAreaRect(cnt)
#to obtain 4 points of the rectangle
box = cv2.boxPoints(rect)
box = np.int0(box)

#drawing a box around the object
#cv2.drawContours(segmented,[box],0,(0,0,255),2)
angle = rect[-1]
print("tilt angle is: ", angle)

# Draw contours around the object
cv2.drawContours(segmented, contours, 0, (0, 255, 0), 2)

# Show image:
cv2.imshow("outline contour & centroid", segmented)

# Wait until a key is pressed:
cv2.waitKey(0)

# Destroy all created windows:
cv2.destroyAllWindows()
``````