title: Measuring the width of several points in a mask image based on another mask image
body: <p>I have these two mask images:</p>
  <p>Image1</p>
  <p><a href="https://i.stack.imgur.com/wbQAL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wbQAL.png" alt="enter image description here" /></a></p>
  <p>Image2</p>
  <p><a href="https://i.stack.imgur.com/8T7RB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8T7RB.png" alt="enter image description here" /></a></p>
  <p>The first image I have the real width. And I would like to measure the width of several points, like this:</p>
  <p><a href="https://i.stack.imgur.com/4SFys.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4SFys.png" alt="enter image description here" /></a></p>
  <p>I made this code based on this (<a href="https://www.pyimagesearch.com/2016/03/28/measuring-size-of-objects-in-an-image-with-opencv/" rel="nofollow noreferrer">https://www.pyimagesearch.com/2016/03/28/measuring-size-of-objects-in-an-image-with-opencv/</a>), and I got just the total width:</p>
  <p><a href="https://i.stack.imgur.com/gZtfU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gZtfU.png" alt="enter image description here" /></a></p>
  <p>Follow my adapted code:</p>
  <pre><code>from scipy.spatial import distance as dist
  from imutils import perspective
  from imutils import contours
  import numpy as np
  import argparse
  import imutils
  import cv2
  
  def midpoint(ptA, ptB):
      return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)
  # Read image
  mask2 = cv2.imread('Image1.jpg')
  mask4 = cv2.imread('Image2.jpg')
  cv2.imshow(&quot;Image&quot;, mask2)
  cv2.waitKey(0)
  
  ########### Mask 2 - Oject
  gray = cv2.cvtColor(mask2, cv2.COLOR_BGR2GRAY)
  gray = cv2.GaussianBlur(gray, (7, 7), 0)
  # perform edge detection, then perform a dilation + erosion to
  # close gaps in between object edges
  edged = cv2.Canny(gray, 50, 100)
  edged = cv2.dilate(edged, None, iterations=1)
  edged = cv2.erode(edged, None, iterations=1)
  # find contours in the edge map
  cnts_1 = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
      cv2.CHAIN_APPROX_SIMPLE)
  cnts_1 = imutils.grab_contours(cnts_1)
  # sort the contours from left-to-right and initialize the
  # 'pixels per metric' calibration variable
  (cnts_1, _) = contours.sort_contours(cnts_1)
  pixelsPerMetric = None
  contorno=cv2.drawContours(mask2,cnts_1, -1, (0, 0, 255), 1)
  cv2.imshow(&quot;Chaveiro&quot;, contorno)
  cv2.waitKey()
  
  ##### Mask 4 - EGS
  gray = cv2.cvtColor(mask4, cv2.COLOR_BGR2GRAY)
  gray = cv2.GaussianBlur(gray, (7, 7), 0)
  # perform edge detection, then perform a dilation + erosion to
  # close gaps in between object edges
  edged = cv2.Canny(gray, 50, 100)
  edged = cv2.dilate(edged, None, iterations=1)
  edged = cv2.erode(edged, None, iterations=1)
  # find contours in the edge map
  cnts_2 = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
      cv2.CHAIN_APPROX_SIMPLE)
  cnts_2 = imutils.grab_contours(cnts_2)
  # sort the contours from left-to-right and initialize the
  # 'pixels per metric' calibration variable
  (cnts_2, _) = contours.sort_contours(cnts_2)
  pixelsPerMetric = None
  contorno2=cv2.drawContours(mask4,cnts_2, -1, (0, 255, 0), 1)
  cv2.imshow(&quot;EGS&quot;, mask4)
  cv2.waitKey()
  
  ###### Total Contourn
  cnts=(cnts_1 + cnts_2)
  
  # loop over the contours individually
  for c in cnts:
      # if the contour is not sufficiently large, ignore it
      if cv2.contourArea(c) &lt; 100:
          continue
      # compute the rotated bounding box of the contour
      orig = (mask2 + mask4)
      box = cv2.minAreaRect(c)
      box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
      box = np.array(box, dtype=&quot;int&quot;)
      # order the points in the contour such that they appear
      # in top-left, top-right, bottom-right, and bottom-left
      # order, then draw the outline of the rotated bounding
      # box
      box = perspective.order_points(box)
      cv2.drawContours(orig, [box.astype(&quot;int&quot;)], -1, (0, 255, 0), 2)
      # loop over the original points and draw them
      for (x, y) in box:
          cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1)
  
          # unpack the ordered bounding box, then compute the midpoint
          # between the top-left and top-right coordinates, followed by
          # the midpoint between bottom-left and bottom-right coordinates
          (tl, tr, br, bl) = box
          (tltrX, tltrY) = midpoint(tl, tr)
          (blbrX, blbrY) = midpoint(bl, br)
          # compute the midpoint between the top-left and top-right points,
          # followed by the midpoint between the top-righ and bottom-right
          (tlblX, tlblY) = midpoint(tl, bl)
          (trbrX, trbrY) = midpoint(tr, br)
          # draw the midpoints on the image
          cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1)
          cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1)
          cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1)
          cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)
          # draw lines between the midpoints
          cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),
                   (255, 0, 255), 2)
          cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),
                   (255, 0, 255), 2)
  # compute the Euclidean distance between the midpoints
      dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
      dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))
  
      # if the pixels per metric has not been initialized, then
      # compute it as the ratio of pixels to supplied metric
      # (in this case, mm)
      if pixelsPerMetric is None:
          pixelsPerMetric = dB / 5.6
  
  # compute the size of the object
      dimA = dA / pixelsPerMetric
      print(dimA)
      dimB = dB / pixelsPerMetric
      print(dimB)
      # draw the object sizes on the image
      cv2.putText(orig, &quot;{:.1f}mm&quot;.format(dimA),
          (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX,
          0.65, (0, 255, 255), 2)
      cv2.putText(orig, &quot;{:.1f}mm&quot;.format(dimB),
          (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,
          0.65, (0, 255, 255), 2)
      # show the output image
      cv2.imshow(&quot;Image&quot;, orig)
      cv2.waitKey(0)
  </code></pre>
  <p>Some sugestion?</p>