Python learning-extract palm and some palm prints using opencv-python,
The last time we successfully trained the palm reader http://www.cnblogs.com/take-fetter/p/8438747.html, we can get the recognition result.
Next, we need to use opencv to obtain the palm of your hand and remove the background. here we need to use mask and region of interest, there are many websites.
Extract the palm of your hand based on the frame part of the previous program (of course you can save it yourself-.-) as follows:
Next we will explain how to extract the palm of your hand.
Palm extraction center:
Algorithm idea: Based on the black and white images, obtain the palm center based on distance transformation, and draw the incircle of the palm based on the maximum radius
The Code is as follows:
distance = cv2.distanceTransform(black_and_white, cv2.DIST_L2, 5, cv2.CV_32F) # Calculates the distance to the closest zero pixel for each pixel of the source image. maxdist = 0 # rows,cols = img.shape for i in range(distance.shape[0]): for j in range(distance.shape[1]): dist = distance[i][j] if maxdist < dist: x = j y = i maxdist = dist
cv2.circle(original, (x, y), maxdist, (255, 100, 255), 1, 8, 0)
Extract palm prints
Now we know the radius of the circle and the coordinates of the center of the circle, so we can extract the inner cut square based on the ROI (although the inner Cut Square will lose a lot of information, but currently I have not come up with any other better method ).
The code for square extraction is as follows:
final_img = original.copy()
#cv2.circle() this line half_slide = maxdist * math.cos(math.pi / 4) (left, right, top, bottom) = ((x - half_slide), (x + half_slide), (y - half_slide), (y + half_slide)) p1 = (int(left), int(top)) p2 = (int(right), int(bottom)) cv2.rectangle(original, p1, p2, (77, 255, 9), 1, 1) final_img = final_img[int(top):int(bottom),int(left):int(right)]
Run
We can see that the gray part does not exist. We can use cv2.imwrite to find that there is no problem,
It seems that cv2.imshow has a certain limit on the pixel size of the output image. It is automatically filled or the color is gray by default. It is larger than the image we extracted here.
Address: https://github.com/takefetter/Get_PalmPrint/blob/master/process_palm.py
Thanks:
1. https://github.com/dev-td7/Automatic-Hand-Detection-using-Wrist-localisation this old brother's repo, based on skin color extraction and the formation of an approximate elliptic inspired me a lot (although the second half is completely useless .....)
2. Although the http://answers.opencv.org/question/180668/how-to-find-the-center-of-one-palm-in-the-picture/ based on the distance change reference to the answer here, but also completed the Needs of the questioner.
Reprinted please indicate the source http://www.cnblogs.com/take-fetter/p/8453589.html