How to calculate the actual length of the black portion in image attached after getting actual contour of that. [closed]

I got a middle portion of laser which is captured by angled camera. How to calculate the actual length of the black portion in image attached after getting actual contour of that

import cv2
import numpy as np
from skimage import morphology, color
import matplotlib.pyplot as plt
from scipy.spatial import distance as dist
from imutils import perspective
from imutils import contours
import argparse
import imutils
def midpoint(ptA, ptB):
return ((ptA + ptB) * 0.5, (ptA + ptB) * 0.5)

lowerb = np.array([0, 0, 120])
upperb = np.array([200, 100, 255])
red_line = cv2.inRange(img, lowerb, upperb)

red_line = cv2.GaussianBlur(red_line, (5, 5), 0)
ret, red_line = cv2.threshold(red_line, 45, 255, cv2.THRESH_BINARY)

red_line = cv2.dilate(red_line, None, iterations=1)
kernel = np.ones((10,10),np.uint8)
red_line = cv2.erode(red_line, kernel, iterations=1)

nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(red_line, connectivity=8)
sizes = stats[1:, -1]; nb_components = nb_components - 1

min_size = 1800

img2 = np.zeros((output.shape))

for i in range(0, nb_components):
if sizes[i] >= min_size:
img2[output == i + 1] = 255

cv2.imshow('red', img2)
cv2.waitKey(0)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)

f,thresh = cv2.threshold(gray, 70, 255, cv2.THRESH_BINARY)

thresh = cv2.erode(thresh, None, iterations=1)
thresh = cv2.dilate(thresh, None, iterations=1)

cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

c = max(cnts, key=cv2.contourArea)
for var in c:
with open('c:\\your_file.txt', 'a') as f:
f.write(str(var) + "\n")
print(var)

for contour in cnts:
perimeter = cv2.arcLength(contour, True)
print(perimeter/2)

# determine the most extreme points along the contour
extLeft = tuple(c[c[:, :, 0].argmin()])
extRight = tuple(c[c[:, :, 0].argmax()])
extTop = tuple(c[c[:, :, 1].argmin()])
extBot = tuple(c[c[:, :, 1].argmax()])

green, top-most is blue, and bottom-most is teal
cv2.drawContours(image, [c], -1, (0, 255, 255), 2)

cv2.imshow("Image", image)
cv2.waitKey(0)
edit retag reopen merge delete