0

I want to be able to count the number of pixels in a detected object. I'm using the cv2.threshold function. Here is some sudo code.

import cv2
import numpy as np
import time

while True:
    cam= cv2.VideoCapture(0)
    while(cam.isOpened())
        ret, image = cam.read()
        image = cv2.GaussianBlur(image, (5,5), 0)
        Image1 = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        lower= np.array([30,40,40], dtype='uint8')
        upper= np.array([95,240,240], dtype='uint8')
        Thresh= cv2.inRange(Image1, lower, upper)

From here on out, I have no idea how to count the pixels of my objects. How do you find the contours of a binary image? I suppose it could be possible to cv2.bitwise_and a full black image over the Thresh/ mask, but that seems like it could be slow and also I don't know how to create a fully black and white image like that.

So TD:LR, how do you count the number of pixels in an object from a binary image?

Note: I'm actually just after the largest object and only need the number of pixels, not the image.

Edit: not trying to count the total number of pixels detected, I've already done that. Want the number of pixels detected from the object with the largest number.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Davey Boy
  • 79
  • 1
  • 10
  • 1
    Possible duplicate of [count number of black pixels in an image in Python with OpenCV](http://stackoverflow.com/questions/32590932/count-number-of-black-pixels-in-an-image-in-python-with-opencv) – Miki Sep 25 '16 at 20:17
  • do the labeling, http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.ndimage.measurements.label.html. Get the label of the biggest object and count the pixels np.sum( label_image == label). – GpG Sep 25 '16 at 23:09
  • Never used scipy before but that could work in a speed efficient manner. Thank you! – Davey Boy Sep 26 '16 at 02:17
  • @GpG How do I get the label of the biggest object? – Davey Boy Sep 26 '16 at 03:00
  • Such a discussioin is easier if you provide an image together with your code. – tfv Sep 26 '16 at 03:52
  • check this out: http://stackoverflow.com/questions/15283849/isolate-greatest-smallest-labeled-patches-from-numpy-array – GpG Sep 26 '16 at 14:07

1 Answers1

0

This is how I did it

import cv2
import numpy as np
import time
from scipy.ndimage import (labeled_comprehension, label, measurements, generate_binary_structure) # new import

while True:
    cam= cv2.VideoCapture(0)
    while(cam.isOpened())
        ret, image = cam.read() # record image
        image = cv2.GaussianBlur(image, (5,5), 0) # blur to remove noise
        Image1 = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # convert to better color scheme
        lower= np.array([30,40,40], dtype='uint8') # low green
        upper= np.array([95,240,240], dtype='uint8') # high green
        Thresh= cv2.inRange(Image1, lower, upper) # returns array with 255 as pixel if in threshold
        struct = generate_binary_structure(2,2) # seems necessary for some reason
        Label, features = label(Thresh, struct) # label is object, features is number of objects
        Arange = np.arange(1, features+1) # seems necessary for some reason
        Biggest = sorted(labeled_comprehension(Thresh, Label, Arange, np.sum, float, -1))[features-1]//255 # counts and organises the objects based on size. [features-1] means last object, ie: biggest. //255 because that's each pixel work (from thresh)
Davey Boy
  • 79
  • 1
  • 10