当前位置:网站首页>Palm detection and finger counting based on OpenCV
Palm detection and finger counting based on OpenCV
2022-06-10 16:31:00 【AI technology base camp】

author | Nuby
source | Xiaobai studies vision
Use the cosine theorem to use OpenCV-Python Realize finger counting and palm detection .

Hand detection and finger counting
Next, let's explore how the following functions are implemented .
OpenCV
OpenCV( Open source computer vision library ) Is an open source computer vision and machine learning software library .OpenCV Is built to provide a common infrastructure for computer vision applications , And accelerate the use of machine awareness in commercial products .
Import library
• cv2: opencv [pip install opencv]
• numpy: Used to handle arrays and math [pip install numpy]
import cv2 as cv
import numpy as npImport images
img_path = "data/palm.jpg"
img = cv.imread(img_path)
cv.imshow('palm image',img)
Palm image
The skin Mask
• Used to highlight a specific color on an image .
• hsvim: take BGR( Blue , green , Red ) The image is changed to HSV( Hue , saturation , value ).
• The lower :HSV The range of skin color in the skin is small .
• upper:HSV The upper limit of skin color .
• skinRegionHSV: stay HSV The skin is detected within the upper and lower pixel values of the color space .
• Fuzzy : Blur the image to improve the mask .
• threshing : threshing .
hsvim = cv.cvtColor(img, cv.COLOR_BGR2HSV)
lower = np.array([0, 48, 80], dtype = "uint8")
upper = np.array([20, 255, 255], dtype = "uint8")
skinRegionHSV = cv.inRange(hsvim, lower, upper)
blurred = cv.blur(skinRegionHSV, (2,2))
ret,thresh = cv.threshold(blurred,0,255,cv.THRESH_BINARY)
cv.imshow("thresh", thresh)
Processing results
Contour drawing
Now let's find the outline on the image .
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
contours = max(contours, key=lambda x: cv.contourArea(x))
cv.drawContours(img, [contours], -1, (255,255,0), 2)
cv.imshow("contours", img)
Palm contour
Convex hull detection
hull = cv.convexHull(contours)
cv.drawContours(img, [hull], -1, (0, 255, 255), 2)
cv.imshow("hull", img)
detection result
Convex defect detection
Any deviation between the palm and the convex hull detection contour line can be regarded as a convex defect .
hull = cv.convexHull(contours, returnPoints=False)
defects = cv.convexityDefects(contours, hull)
Examples of convex defects
Count fingers
Convex defect returns an array , Each of these lines contains the following values :
• The starting point
• End
• Farthest point
• The approximate distance to the farthest point
Through this , We can easily conclude that Sides:a,b,c( Please see the CODE), And according to the cosine theorem , We can also get gamma or angle between two fingers . As mentioned earlier , If gamma is less than 90 degree , We'll think of it as a finger . After knowing gamma , We just need to draw a radius of 4 The circle of , The approximate distance to the farthest point . After simply putting the text into the image , We mean the number of fingers (cnt).
if defects is not None:
cnt = 0
for i in range(defects.shape[0]): # calculate the angle
s, e, f, d = defects[i][0]
start = tuple(contours[s][0])
end = tuple(contours[e][0])
far = tuple(contours[f][0])
a = np.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2)
b = np.sqrt((far[0] - start[0]) ** 2 + (far[1] - start[1]) ** 2)
c = np.sqrt((end[0] - far[0]) ** 2 + (end[1] - far[1]) ** 2)
angle = np.arccos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)) # cosine theorem
if angle <= np.pi / 2: # angle less than 90 degree, treat as fingers
cnt += 1
cv.circle(img, far, 4, [0, 0, 255], -1)
if cnt > 0:
cnt = cnt+1
cv.putText(img, str(cnt), (0, 50), cv.FONT_HERSHEY_SIMPLEX,1, (255, 0, 0) , 2, cv.LINE_AA)Let's look at the final result
cv.imshow('final_result',img)
We can also do it by calling “ cv.VideoCapture()” To do this on the video .
The code link is as follows :
https://github.com/madhav727/medium/blob/master/finger_counting_video.py

Go to
period
return
Gu
information
Meta Development AI Voice assistant , Help the meta universe
technology
Pandas Duplicate data processing Encyclopedia
technology
5 A short and tough Python Interesting script
information
M2 The chip is finally coming ? Replace the whole line

Share

Point collection

A little bit of praise

Click to see
边栏推荐
- Devops-1- introduction
- Research Report on the application field of Chinese antirust oil market and the prospect planning of the 14th five year plan (2022-2028)
- Jerry's ble transmission rate [chapter]
- idea新建项目报错org.codehaus.plexus.component.repository.exception.ComponentLookupException:
- Aperçu en direct | déconstruire OLAP! Le nouveau paradigme de l'architecture d'analyse multidimensionnelle est entièrement ouvert! Apache Doris va apporter cinq gros problèmes!
- Save a window with a specific size, resolution, or background color
- Common sense: the number of neurons in the brain of mice is about 70million and that of humans is about 86billion
- Middle office: Data middle office, business middle office, technology middle office, application middle office, AI middle office
- Digital image processing: graying
- STOP在屏幕程序的应用_SAP刘梦_
猜你喜欢

Li Ling: in six years, how did I go from open source Xiaobai to Apache top project PMC

直播预告 | 解构OLAP!新型多维分析架构范式全公开!Apache Doris 将带来五个重磅议题!

PV operation daily question - single log bridge problem

MapReduce之Word Count案例代码实现
![Jerry's ble timer clock source cannot choose OSC crystal oscillator [chapter]](/img/87/9e78938faa327487e8d7045ab94bf2.png)
Jerry's ble timer clock source cannot choose OSC crystal oscillator [chapter]
![Jerry's interface for obtaining ble broadcast package and profile data [chapter]](/img/ae/70cde8684fa536c37387ce819a36eb.png)
Jerry's interface for obtaining ble broadcast package and profile data [chapter]

Fortex Fangda releases the electronic trading ecosystem to share and win-win with customers

安霸CV2FS/CV22FS获得ASIL C芯片功能安全认证,超越市场同类芯片水平

Research Report on the development scale of Chinese sanatorium industry and the 14th five year plan (2022-2028)

Sm59 remote connection. If you are prompted that there is no host, add host to the server and restart SAP_ SAP LIUMENG
随机推荐
Report on development status and investment planning recommendations of global and Chinese fuel filler pipe markets (2022-2028)
Detailed explanation of RGB color space, hue, saturation, brightness and HSV color space
RGB颜色空间、色调、饱和度、亮度、HSV颜色空间详解
Driver development and abnormal analysis of "technical dry goods" industrial touch screen (serial)
2D human pose estimation for pose estimation - simdr: is 2D Heatmap representation even necessity for human pose estimation?
Why do I need a thread pool? What is pooling technology?
Join operation cases in the map phase of MapReduce
MapReduce之Map阶段的join操作案例
2D pose estimation for pose estimation - (openpose) realtime multi person 2D pose estimation using part affinity fields
Software test architects, 16 pieces of advice for future generations, take a look and don't miss it
New York financial regulators issue official guidelines for stable currency
torch. utils. data. Dataloader() details [pytoch getting started manual]
MapReduce之Word Count案例代码实现
RK3308 按键Key与LED灯
Test question bank and simulation test for operation certificate of ordinary scaffolder (special type of construction work) in 2022
Implementation of word count case code in MapReduce
torch.nn.utils.rnn.pad_sequence()详解【Pytorch入门手册】
Devops-1- introduction
Duyuan outdoor sprint to Shenzhen Stock Exchange: the annual revenue is 350million, and the color of Lin Xizhen family is obvious
[section 7 function]