1

I have an image that contains ~400 dots. I've been using the Simpleblob detector to find the keypoints. I then for loop over all keypoints to find the center of each of the keypoints (code below). This works well, but I'm also interested in the moment information, as I would imagine .pt is only averaging the position of all of the pixels associated with the keypoints.

import cv2
import numpy as np
import csv

im = cv2.imread("8f3secshim.bmp", cv2.IMREAD_GRAYSCALE)

detector = cv2.SimpleBlobDetector_create()

keypoints = detector.detect(im)

im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), 
(0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

cv2.imshow("Keypoints", im_with_keypoints)

x = np.empty([len(keypoints), 2])

for i in range(len(keypoints)):

    x[i] = keypoints[i].pt

I wanted to do something along the lines of this:

M = np.empty([lens(keypoints), 1])

for j in range(len(keypoints)):
    M[j] = cv2.moments(keypoints[j])

but it fails.

I've tried abandoning the Simpledetector and using a treatment for moments listed here http://docs.opencv.org/trunk/dd/d49/tutorial_py_contour_features.html , but that has failed as well.

If anyone has any suggestions they would be much appreciated.

D Palo
  • 21
  • 5
  • "_I then for loop over all keypoints to find the center of each of the keypoints_" I think you misunderstand the concept of [KeyPoints](http://docs.opencv.org/3.2.0/d2/d29/classcv_1_1KeyPoint.html). A keypoint is basically **a** location on a image where lies a good feature. The location can be found like so : `keypoint.pt`. Moreover, computing a _moment_ only makes sense on a _list of Points_. – Elouarn Laine Aug 02 '17 at 10:03
  • While that is true, in the directory of KeyPoints there is an attribute labelled 'size'. This finds the meaningful area in the neighborhood of the KeyPoint. Clearly you cannot find a moment of a single point. I'm interested in the area of the object that the KeyPoint is tied to. I would like to be able to find the moment of that area. In any case I figured out a way to do this using cv2.Threshold instead of the cv2.SimpleBlobDetector. – D Palo Aug 02 '17 at 17:15

1 Answers1

0

I seem to have incorrectly worded my question. If anyone is looking to find the moments associated with individual objects found in a given image, the following code can be used.

import cv2
import numpy as np

img = cv2.imread('8f3secshim.bmp',0)
ret,thresh = cv2.threshold(img,127,255,0)
im2,contours,hierarchy = cv2.findContours(thresh, 1, 2)

print(len(contours))
a = np.empty([len(contours), 1])
cx = np.empty([len(contours), 1])
cy = np.empty([len(contours), 1])
for i in range(0, len(contours)):
     Mi = cv2.moments(contours[i])
#if any m00 moment is equal to zero the code can not be completed...
     if Mi['m00'] != 0:
     cx[i]= Mi['m10']/Mi['m00']
     cy[i]= Mi['m01']/Mi['m00']
     a[i] = cv2.contourArea(contours[i])

x = np.hstack((cx, cy, a))
#x is a len(contours), 3 matrix.  

I'm sure there is probably a more elegant way to do this, but this works.

D Palo
  • 21
  • 5