noob here.
I have a (kind of) binary image that contains a known number of blobs that vary in shape and size. The pixel values in each blob equivalent to the blob index. I would like to process (using moments) only the largest 5 blobs.
At the moment I am iterating through every connected pixel incrementing a variable to get the area of each blob (see code below). I then process only the largest blobs as required, however this pixel iteration method is very slow in python.
for i in range(1, objectCount):
zm=0.0
for h in range(im.height):
for w in range(im.width):
pixVal = cv.Get2D(im, h, w)
if (pixVal[0] == i):
zm=zm+1
objectArea.append([int(zm)])
Is there a faster way to do this?
Here's the code to replace the above:
hist = cv.CreateHist([255], cv.CV_HIST_ARRAY, [[0,255]], 1)
cv.CalcHist([im] , hist)
for h in range(255):
zm = cv.QueryHistValue_1D(hist, h)
objectArea.append([int(zm)])