Quantcast
Channel: OpenCV Q&A Forum - Latest question feed
Viewing all articles
Browse latest Browse all 19555

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

$
0
0
I asked this question : [Question about second momentos](http://answers.opencv.org/question/96733/calc-the-seconds-moments-of-area-of-huge-number-of-countours/), which @Tetragramm helped me a lot . For me it's not clear how can I get the [Second Moments of Area](https://en.wikipedia.org/wiki/Second_moment_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](https://dl.dropboxusercontent.com/u/710615/bwImlayer.jpg) [OutputCountoursConnected](https://dl.dropboxusercontent.com/u/710615/tstCountoursConnected_3.jpg) [Output Text](https://dl.dropboxusercontent.com/u/710615/moments_.txt) Inputs : [source Imesh](https://dl.dropboxusercontent.com/u/710615/new_refined2__DXF.png) [Layer image](https://dl.dropboxusercontent.com/u/710615/Layer1.png) I think this peace of code: cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) calculates the [centroids](https://en.wikipedia.org/wiki/Centroid), 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

Viewing all articles
Browse latest Browse all 19555

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>