Skip to content

Commit

Permalink
Improved performance when counting black pixels.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasz.wyszomirski committed Aug 7, 2015
1 parent 81663c1 commit d1d20e5
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions MotionDetector.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,12 @@ def processImage(self, frame):

def somethingHasMoved(self):
nb=0 #Will hold the number of black pixels

for x in range(self.height): #Iterate the hole image
for y in range(self.width):
if self.res[x,y] == 0.0: #If the pixel is black keep it
nb += 1
avg = (nb*100.0)/self.nb_pixels #Calculate the average of black pixel in the image

if avg > self.threshold:#If over the ceiling trigger the alarm
return True
min_threshold = (self.nb_pixels/100) * self.threshold #Number of pixels for current threshold
nb = self.nb_pixels - cv.CountNonZero(self.res)
if (nb) > min_threshold:
return True
else:
return False
return False

if __name__=="__main__":
detect = MotionDetectorInstantaneous(doRecord=True)
Expand Down

2 comments on commit d1d20e5

@guyknowsguy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted to run this and see for myself as I am relatively new to programming ,but i got this error
"No module named 'cv2.cv'
could you please help. I liked your code and wanted to use it,would be glad to receive a response.

@Yeffian
Copy link

@Yeffian Yeffian commented on d1d20e5 Jan 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted to run this and see for myself as I am relatively new to programming ,but i got this error
"No module named 'cv2.cv'
could you please help. I liked your code and wanted to use it,would be glad to receive a response.

You need to install OpenCV on your system
go to the opencv github, they show how to install it

Please sign in to comment.