Detect overlapping players from a blob - python

is there a way to detect the number of overlapping players in a blob?
I do contour detection and each detected contour is a player but how to know if there are players that a very close to each other that they are considered one.
for example to know that
this blob
has two players like in
this image.

If you didn't want to use machine learning you could try using the area of the contours by assigning a threshold. Generally speaking 1 person should only take up so much space, beyond that point you could assume that there MUST be someone else there by the area that is taken up.
Also depending on the teams clothing colors it would be pretty easy to extract contours by color. Red+blue for 1 team and white for the other in this example. Hope this helps!

Related

OpenCV - detecting missing coins from a tray using a live camera feed

I am building a system which detects coins that are picked up from a tray. This tray will be kept in a public place. People will pick up one or more coins, but would be expected to keep them back after some time.
I would have a live stream through a webcam placed at the top. I will have a calibration step, say at the beginning of the day, that captures the initial state of the tray to be used for comparing with the live feed. A few slots might be empty to begin with, as you can see in the sample image.
I need to detect slots that had a coin initially, but are missing the same at any given point of time during the day.
I am trying out a few approaches using OpenCV:
SSIM difference: I can use SSIM to find diff between my live image frame and initial state. However, a number of slots are larger than the corresponding coin sizes (e.g. top two rows). This could mean that if the coin was originally placed at the center, but was later put back to touch one of the edges, we may get a false positive.
Blob detection: Alternatively, I can pre-feed (or detect) slot co-ordinates. Then do a blob detection within every slot. If a blob was present in the original state, but is missing in a camera frame, this would mean a coin has been picked up. However, accurate blob detection could be a challenge if the contrast between the coin and the tray is low.
I might also need to watch out for slight variations in lighting due to shadows of people moving around.
Any thoughts on these or any pointers on alternate approaches that can be tried out? Is there any analogous implementation that I can learn from?
Many thanks in advance.
Edit: Thanks to #I.Newton's suggestion. For those who stumble upon this question and would benefit from a sample implementation, look here: https://github.com/kewats/computer-vision-samples/tree/master/image-processing/missing-coins-detection
If you complete control over the lighting conditions, you can use simple color thresholding to solve the problem.
First make a mask for the boxes. You can do it in multiple ways by color threshold or by using adaptive threshold or canny edge etc. I did by color threshold
Then make a mask for the coins by the same method.
Now flood fill your box mask from from the center of each of this coins. It'll retain only those which do not have the coins.
Now you can compare this with your initial mask to figure out if all the coins are present
This does not include frame subtraction. So you need not worry about different position of coin in the box. Only thing you need to make sure is the lighting conditions for making the masks. If you want to make sure the coins are returned to the same box, you should go for template matching etc which again needs effort.

Automatic ROI/rectangle recognition

I want to be able to pass on multiple images to python and detect the contour of the object in the picture. The pictures all represent money bills, hence the ROI is always going to be rectangular. Whatever ive tried, im not able to exactly detect the money bill.
I tried canny edge detection but the transparent regions on the money bill (canadian money)example of a canadian bill make it hard to detect the whole bill. Does anyone have any suggestions on how to automatically crop out just the money bill? Thanks in advance!
It is an easy matter to binarize the image, as the background is perfectly white. Use a threshold level as close as possible to white, to reduce the effect of the shadow, bottom right.
Then after connected component labelling, the convex hull gives you a nice crop polygon.
If you prefer a quadrilateral, you can pick the extreme vertices in the four cardinal directions.

detect card symbol using opencv python

I'm trying to detect the difference between a spade, club, diamond and hart. the number on the card is irrelevant, just the suit matters.
i've tried color detection by looking at just the red or black colors, but that still leaves me with two results per color. how could i make sure i can detect each symbol individually?
for instance: i have a picture of a red hart, a red diamond, a black spade and a black club. i want to draw the contours of each symbol in a different color.
I'm using my webcam as a camera.
If the suit of your cards is always identical (only one single kind of card) you could just store images of spade, club, diamond and hart and then check the cross-correlation of the given image to your references and pick the highest value (see this question). If you have a fairly large sample set of different kinds of cards that have slight differences between the suit-representations you can pretty much follow this tutorial almost exactly to do a four-way classification of your training set and use a svm, clustering or other approaches, which are available in open-cv.
hope this helps and all the best
I don't see any code, so can't really tweak what you have, but more generally you ought to be able to train it just the same except for the symbol instead of color:
Here is a sample code for doing a specific card. You can either train them to be card specific, thus capturing all of the symbols by specific card, or back it up and more generally train for the symbol (heart / diamond). You should also look at template matching on opencv.
There are a couple guides and code already completed for this on github.
Here is a tutorial worth reading
As the card symbol is at fixed positions, you may try below (e.g. in OpenCV 3.2 Python):
Crop the symbol at top left corner, image = image[h1:h2,w1:w2]
Threshold the symbol colors to black, the rest to white, thresh = mask = cv2.inRange(image,(0,0,0),(100,100,100))
Perform a find contour detection, _, contours, hierarchy = cv2.findContours()
Get the area size of the contour. area = cv2.contourArea(contour)
Compare the area to determine which one of the 4 symbols it belongs to.
In which you have to build the area thresholds of each symbol for step 4 to compare. All the cv2 functions above are just for reference.
Hope this help.
Unless you have major perspective distortion (when it may work anyway), template matching is very effective. You can run this in red and black channels separately (obviously don't look for red spades etc.).
Take a look at e.g. (not mine)
http://www.pyimagesearch.com/2015/01/26/multi-scale-template-matching-using-python-opencv
For playing cards, see also https://github.com/arnabdotorg/Playing-Card-Recognition

Detect region from edges

I've got a micrograph showing a number of grains that have a rather clear boundary. I've used OpenCV-Python to detect these boundaries (with a Canny filter), and I think it was rather successful in its attempt, see figure. I would like to identify and mark the individual regions bounded by the detected edges, and then get the area (number of pixels) contained those regions. My apologies if the question was asked (and answered) before, but I could not find any satisfying answers yet.
Thanks in advance
Original image
Original image overlain by the detected edges
If the grain makes no difference in the color (maybe on the raw data rather than a compressed format), you may wanna use the Becke line to distinguish inside and outside. The borders of your grain appear dark on the inside and white on the outside. But this depends also on the focus of the microscope. See here.
In the case that your grains do not enclose totally a background spot you can use a point in polygon approach.

Determine height of Coffee in the pot using Python imaging

We have a web-cam in our office kitchenette focused at our coffee maker. The coffee pot is clearly visible. Both the location of the coffee pot and the camera are static. Is it possible to calculate the height of coffee in the pot using image recognition? I've seen image recognition used for quite complex stuff like face-recognition. As compared to those projects, this seems to be a trivial task of measuring the height.
(That's my best guess and I have no idea of the underlying complexities.)
How would I go about this? Would this be considered a very complex job to partake? FYI, I've never done any kind of imaging-related work.
Since the coffee pot position is stationary, get a sample frame and locate a single column of pixels where the minimum and maximum coffee quantities can easily be seen, in a spot where there are no reflections. Check the green vertical line segment in the following picture:
(source: nullnetwork.net)
The easiest way is to have two frames, one with the pot empty, one with the pot full (obviously under the same lighting conditions, which typically would be the case), convert to grayscale (colorsys.rgb_to_hsv each RGB pixel and keep only the v (3rd) component) and sum the luminosity of all pixels in the chosen line segment. Let's say the pot-empty case reaches a sum of 550 and the pot-full case a sum of 220 (coffee is dark). By comparing an input frame sum to these two sums, you can have a rough estimate of the percentage of coffee in the pot.
I wouldn't bet my life on the accuracy of this method, though, and the fluctuations even from second to second might be wild :)
N.B: in my example, the green column of pixels should extend to the bottom of the pot; I just provided an example of what I meant.
Steps that I'd try:
Convert the image in grayscale.
Binarize the image, and leave only the coffee. You can discover a good threshold manually through experimentation.
Blob extraction. Blob's area (number of pixels) is one way to calculate the height, ie area / width.
First do thresholding, then segmentation. Then you can more easily detect edges.
You're looking for edge detection. But you only need to do it between the brown/black of the coffee and the color of the background behind the pot.
make pictures of the pot with different levels of coffe in it.
downsample the image to maybe 4*10 pixels.
make the same in a loop for each new live picture.
calculate the difference of each pixels value compared to the reference images.
take the reference image with the least difference sum and you get the state of your coffe machine.
you might experiment if a grayscale version or only red or green might give better results.
if it gives problems with different light settings this aproach is useless. just buy a spotlight for the coffe machine, or lighten up, or darken each picture till the sum of all pixels reaches a reference value.

Categories