scipy - python count number of ones in a binary image -
What is the Python module for calculating the number of people in a binary image?
To rephrase,
I have a matrix that has only one and zero, it's a narrow type and I want to know how many people are there.
You can just use sum : < Pre>
& gt; & Gt; & Gt; Import samples & gt; & Gt; & Gt; N = numpy.random.randint (0, 2, size = (3,3))> gt; & Gt; & Gt; N array ([[1, 0, 1], [0, 1, 1], [1, 1, 1]]) gt; & Gt; & Gt; Since n.sum () 7, the integer value of 0/1 in bools is false / true, even if there were elements in the array that were not 0 or 1, then you can use a different type. This trick:
& gt; & Gt; & Gt; N = numpy.random.randint (0, 3, size = (3,3))> gt; & Gt; & Gt; N array ([[2, 2, 0], [0, 2, 0], [1, 1, 0]]) gt; & Gt; & Gt; N == 1 array ([[false, false, false], [false, false, false], [true, false]], dtype = bool gt; & Gt; & Gt; (N == 1) .sum () 2
Comments
Post a Comment