Print pixels after segmentation

Hello, how to get the number of pixels from the first to the last pixel after object segmentation?
Knowing that in 1 mm 3.8 RGB pixels I want to get the number of pixels from the segmentation object and then convert to millimeters and divide by the magnification scale under the microscope.
Please enter new lines)


import numpy as np
import cv2

cap = cv2.VideoCapture ( 1 )
kernel = np.ones ( (2 , 2) , np.uint8 )
while (True):
    # Capture frame-by-frame
    ret , frame = cap.read ()

    # Our operations on the frame come here
    gray = cv2.cvtColor ( frame , cv2.COLOR_BGR2GRAY )
    gray = cv2.GaussianBlur ( gray , (7 , 7) , 0 )
    gray = cv2.medianBlur ( gray , 5 )  # to remove salt and paper noise
    # to binary
    ret , thresh = cv2.threshold ( gray , 128 , 128 , 128 )  # to detect white objects
    # to get outer boundery only
    thresh = cv2.morphologyEx ( thresh , cv2.MORPH_GRADIENT , kernel )
    # to strength week pixels
    thresh = cv2.dilate ( thresh , kernel , iterations = 1 )
    im2 = contours , hierarchy = cv2.findContours ( thresh , cv2.RETR_EXTERNAL , cv2.CHAIN_APPROX_SIMPLE )
    if len ( contours ) > 0:
        cv2.drawContours ( frame , contours , -1 , (0 , 255 , 0) , 3 )
        # find the biggest countour (c) by the area
        c = max ( contours , key = cv2.contourArea )
        x , y , w , h = cv2.boundingRect ( c )



        # draw the biggest contour (c) in green
        cv2.rectangle ( frame , (x , y) , (x + w , y + h) , (255 , 0 , 0) , 2 )
    # Display the resulting frame
    cv2.imshow ( 'frame' , frame )
    if cv2.waitKey ( 27 ) & 0xFF == ord ( 'q' ):
        break

# When everything done, release the capture
cap.release ()
cv2.destroyAllWindows ()

you have the bounding rectangle. that is what you’re asking for.

how to get size in mm from this rectangle?

did you determine that? I’m not sure what exactly you’re saying because of your language barrier.

since you have that number, you have everything you need to convert pixels to millimeters.

I am a beginner and I would like to know what function is needed, and in which line to write a function that will show the size of the frame in mm

the function is multiplication or division. you use the * and / operators to calculate.

if 3.8 pixels are 1 mm, and you have x pixels, you calculate x / 3.8 and you have that in millimeters.