What are the "moments" in opencv ? Ratios, eigenvalues and eigenvectors [closed]

asked 2016-09-08 07:27:36 -0500

marcoE gravatar image

I asked this question : Question about second momentos, which @Tetragramm helped me a lot .

For me it's not clear how can I get the Second Moments of Area, I'm not sure what cv:moments returns, what "moments" .

#!/usr/bin/python
import cv2
import numpy as np

# mesh image
imMesh = cv2.imread('new_refined2__DXF.png', -1)
# layer image
imLayer1 = cv2.imread('Layer1.png', -1)
"""
binary conversions
"""
imMeshGray = cv2.cvtColor(imMesh, cv2.COLOR_BGR2GRAY)
ret, imMeshBw = cv2.threshold(imMeshGray, 250, 255, cv2.THRESH_BINARY)

imLayer1Gray = cv2.cvtColor(imLayer1, cv2.COLOR_BGR2GRAY)
(thresh, imLayerBw) = cv2.threshold(imLayer1Gray, 128, 255,
                                cv2.THRESH_BINARY | cv2.THRESH_OTSU)

cv2.imwrite('bwImlayer.jpg', imLayerBw)
connectivity = 4
output = cv2.connectedComponentsWithStats(imMeshBw, connectivity, cv2.CV_32S)
num_labels = output[0]
labels = output[1]
stats = output[2]
centroids = output[3]
file = open('moments_.txt', 'w+')
# if I want for each label
for label in np.unique(labels):
   if label == 0:
       continue
   mask = np.zeros(imMeshGray.shape, dtype="uint8")
   mask[labels == label] = 255
   maskedLayer = cv2.bitwise_and(mask, imLayerBw)

   M = cv2.moments(maskedLayer)
   m2 = cv2.moments(mask)
   Vr = float(M["m00"] / m2["m00"])

   if M["m00"] != 0:
       cX = int(M["m10"] / M["m00"])
       cY = int(M["m01"] / M["m00"])
   else:
        cX, cY = 0, 0

    file.write('{} {} {} {} {} \n'.format(Vr, M["m10"], M["m00"], cX, cY))
    cv2.putText(imMesh, "*",
                (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
file.close()
cv2.imwrite('tstCountoursConnected_3.jpg', imMesh)

The outputs: BwImLayer

OutputCountoursConnected

Output Text

Inputs :

source Imesh

Layer image

I think this peace of code:

cX = int(M["m10"] / M["m00"])

cY = int(M["m01"] / M["m00"])

calculates the centroids, because they are related to the masked layer, so the "centers" are not the center of the "triangles". I'm not sure !

I want to calculate eigenvalues and eigenvector of the second moments of area from the center of triangle. With this result I'll have the magnitude and the direction of the "piece of the layer" in each triangle which has something.

I think with this piece of code :

Vr = float(M["m00"] / m2["m00"])

I'm computing the ration between the "white" and "black". I've followed the @Tetragramm tip

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-10-06 11:58:49.246816