i would like to print a real time 3D Plot of the mediapipe 3D Landmarks.
I am able to get the holistic as shown below in real time from a Video File:
Now i would like to plot the real time 3D Plot like this:
The code i used to plot the real time holistic is shown below. How i am able to plot the real time Plot only from the holistic part ?
import cv2
import mediapipe as mp
import urllib.request
import numpy as np
import pickle
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import animation
import PyQt5
from PIL import Image
from IPython.display import Video
import nb_helpers
mp_drawing = mp.solutions.drawing_utils
mp_holistic = mp.solutions.holistic
file = 'walking.mp4'
cap = cv2.VideoCapture(file)
# Initiate holistic model
with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
while cap.isOpened():
ret, frame = cap.read()
# Recolor Feed
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
# Make Detections
results = holistic.process(image)
# print(results.face_landmarks)
# face_landmarks, pose_landmarks, left_hand_landmarks, right_hand_landmarks
# Recolor image back to BGR for rendering
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# 1. Draw face landmarks
mp_drawing.draw_landmarks(image, results.face_landmarks, mp_holistic.FACEMESH_TESSELATION,
mp_drawing.DrawingSpec(color=(80,110,10), thickness=1, circle_radius=1),
mp_drawing.DrawingSpec(color=(80,256,121), thickness=1, circle_radius=1)
)
# 2. Right hand
mp_drawing.draw_landmarks(image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS,
mp_drawing.DrawingSpec(color=(80,22,10), thickness=2, circle_radius=4),
mp_drawing.DrawingSpec(color=(80,44,121), thickness=2, circle_radius=2)
)
# 3. Left Hand
mp_drawing.draw_landmarks(image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS,
mp_drawing.DrawingSpec(color=(121,22,76), thickness=2, circle_radius=4),
mp_drawing.DrawingSpec(color=(121,44,250), thickness=2, circle_radius=2)
)
# 4. Pose Detections
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_holistic.POSE_CONNECTIONS,
mp_drawing.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=4),
mp_drawing.DrawingSpec(color=(245,66,230), thickness=2, circle_radius=2)
)
cv2.imshow('Raw Webcam Feed', image)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
I would be grateful for any help !
Greets!
You can try the below code , which I got from https://github.com/Kazuhito00/mediapipe-python-sample/blob/main/sample_pose.py
Below is the function you can include to plot the realtime 3d landmarks
def plot_world_landmarks(
plt,
ax,
landmarks,
visibility_th=0.5,
):
landmark_point = []
for index, landmark in enumerate(landmarks.landmark):
landmark_point.append(
[landmark.visibility, (landmark.x, landmark.y, landmark.z)])
face_index_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
right_arm_index_list = [11, 13, 15, 17, 19, 21]
left_arm_index_list = [12, 14, 16, 18, 20, 22]
right_body_side_index_list = [11, 23, 25, 27, 29, 31]
left_body_side_index_list = [12, 24, 26, 28, 30, 32]
shoulder_index_list = [11, 12]
waist_index_list = [23, 24]
# 顔
face_x, face_y, face_z = [], [], []
for index in face_index_list:
point = landmark_point[index][1]
face_x.append(point[0])
face_y.append(point[2])
face_z.append(point[1] * (-1))
# 右腕
right_arm_x, right_arm_y, right_arm_z = [], [], []
for index in right_arm_index_list:
point = landmark_point[index][1]
right_arm_x.append(point[0])
right_arm_y.append(point[2])
right_arm_z.append(point[1] * (-1))
# 左腕
left_arm_x, left_arm_y, left_arm_z = [], [], []
for index in left_arm_index_list:
point = landmark_point[index][1]
left_arm_x.append(point[0])
left_arm_y.append(point[2])
left_arm_z.append(point[1] * (-1))
# 右半身
right_body_side_x, right_body_side_y, right_body_side_z = [], [], []
for index in right_body_side_index_list:
point = landmark_point[index][1]
right_body_side_x.append(point[0])
right_body_side_y.append(point[2])
right_body_side_z.append(point[1] * (-1))
# 左半身
left_body_side_x, left_body_side_y, left_body_side_z = [], [], []
for index in left_body_side_index_list:
point = landmark_point[index][1]
left_body_side_x.append(point[0])
left_body_side_y.append(point[2])
left_body_side_z.append(point[1] * (-1))
# 肩
shoulder_x, shoulder_y, shoulder_z = [], [], []
for index in shoulder_index_list:
point = landmark_point[index][1]
shoulder_x.append(point[0])
shoulder_y.append(point[2])
shoulder_z.append(point[1] * (-1))
# 腰
waist_x, waist_y, waist_z = [], [], []
for index in waist_index_list:
point = landmark_point[index][1]
waist_x.append(point[0])
waist_y.append(point[2])
waist_z.append(point[1] * (-1))
ax.cla()
ax.set_xlim3d(-1, 1)
ax.set_ylim3d(-1, 1)
ax.set_zlim3d(-1, 1)
ax.scatter(face_x, face_y, face_z)
ax.plot(right_arm_x, right_arm_y, right_arm_z)
ax.plot(left_arm_x, left_arm_y, left_arm_z)
ax.plot(right_body_side_x, right_body_side_y, right_body_side_z)
ax.plot(left_body_side_x, left_body_side_y, left_body_side_z)
ax.plot(shoulder_x, shoulder_y, shoulder_z)
ax.plot(waist_x, waist_y, waist_z)
plt.pause(.001)
return
Related
How can I convert an objects position in PyBullet to pixel coordinates & draw a line onto the frame using PyBullet & OpenCV?
We would like to do this because PyBullet native addUserDebugLine() function is not available in DIRECT mode.
import pybullet as p
import numpy as np
import time
import pybullet_data
import cv2
VIDEO_RESOLUTION = (1280, 720)
MY_COLORS = [(255,0,0), (0,255,0), (0,0,255)]
def capture_frame(base_pos=[0,0,0], _cam_dist=3, _cam_yaw=45, _cam_pitch=-45):
_render_width, _render_height = VIDEO_RESOLUTION
view_matrix = p.computeViewMatrixFromYawPitchRoll(
cameraTargetPosition=base_pos,
distance=_cam_dist,
yaw=_cam_yaw,
pitch=_cam_pitch,
roll=0,
upAxisIndex=2)
proj_matrix = p.computeProjectionMatrixFOV(
fov=90, aspect=float(_render_width) / _render_height,
nearVal=0.01, farVal=100.0)
(_, _, px, _, _) = p.getCameraImage(
width=_render_width, height=_render_height, viewMatrix=view_matrix,
projectionMatrix=proj_matrix, renderer=p.ER_TINY_RENDERER) # ER_BULLET_HARDWARE_OPENGL)
rgb_array = np.array(px, dtype=np.uint8)
rgb_array = np.reshape(rgb_array, (_render_height, _render_width, 4))
rgb_array = rgb_array[:, :, :3]
return rgb_array, view_matrix, proj_matrix
def render():
frame, vmat, pmat = capture_frame()
p1, cubeOrn = p.getBasePositionAndOrientation(1)
p2, cubeOrn = p.getBasePositionAndOrientation(2)
frame, view_matrix, proj_matrix = capture_frame()
frame = cv2.resize(frame, VIDEO_RESOLUTION)
points = {}
# reshape matrices
my_order = 'C'
pmat = np.array(proj_matrix).reshape((4,4), order=my_order)
vmat = np.array(view_matrix).reshape((4,4), order=my_order)
fmat = vmat.T # pmat.T
# compute origin from origin point in simulation
origin = np.array([0,0,0,1])
frame_origin = (fmat # origin)[:3]*np.array([1280, 640, 0]) + np.array([640, 360, 0])
# define unit vectors
unit_vectors = [ np.array([1,0,0,1]),
np.array([0,1,0,1]),
np.array([0,0,1,1]) ]
for col_id, unit_vector in enumerate(unit_vectors):
cur_point = (fmat # unit_vector)[:3]*np.array([1280, 640, 0]) + np.array([640, 360, 0])
cv2.line(frame, (640,360), (int(cur_point[0]),int(cur_point[1])), color=MY_COLORS[col_id], thickness=2)
cv2.imwrite("my_rendering.jpg", frame)
print(p1,p2)
if __name__ == '__main__':
physicsClient = p.connect(p.DIRECT)#or p.DIRECT for non-graphical version
p.setAdditionalSearchPath(pybullet_data.getDataPath()) #optionally
p.setGravity(0,0,-10)
planeId = p.loadURDF("plane.urdf")
startPos = [1,0,0.2]
startOrientation = p.getQuaternionFromEuler([0,0,0])
boxId = p.loadURDF("r2d2.urdf",startPos, startOrientation)
startPos = [0,2,0.2]
boxId = p.loadURDF("r2d2.urdf",startPos, startOrientation)
#set the center of mass frame (loadURDF sets base link frame) startPos/Ornp.resetBasePositionAndOrientation(boxId, startPos, startOrientation)
for i in range (2400):
if i == 2399:
render()
p.stepSimulation()
p.disconnect()
The expected output would be the following frame but with the origin-coordinate frame drawn correctly. E.g. X, Y, and Z axis are colored Red, Blue, and Green respectively.
Since the two R2D2 robots are positioned at [1,0,0] and [0,1,0] respectively, we can see that the coordinate frame is off. (See image below)
We tried the following:
transposing the matrices
not transposing the matrices
changing the order of how we compute fmat e.g. pmat # vmat instead of vmat # pmat etc.
Any help is appreciated.
After a lot of fiddling, I came to a solution.
Playing with it for a while, I came to a point where it looked almost OK except for a rotation of the axes given by the yaw angle. So, I did a second call to computeViewMatrixFromYawPitchRoll but with the opposite yaw in order to compute the transformation for the axes.
Unfortunately, I'm not sure about why this works... But it works!
Note: base_pos, _cam_dist, _cam_yaw and _cam_pitch have been displaced into render() Note also: the up direction has been reversed too (don't ask why... :-) ) A pretty messy explanation, I must admit...
import pybullet as p
import numpy as np
import time
import pybullet_data
import cv2
import os
VIDEO_RESOLUTION = (1280, 720)
MY_COLORS = [(255,0,0), (0,255,0), (0,0,255)]
K=np.array([[1280,0,0],[0,720,0],[0,0,1]])
def capture_frame(base_pos, _cam_dist, _cam_yaw, _cam_pitch):
_render_width, _render_height = VIDEO_RESOLUTION
view_matrix = p.computeViewMatrixFromYawPitchRoll(
cameraTargetPosition=base_pos,
distance=_cam_dist,
yaw=_cam_yaw,
pitch=_cam_pitch,
roll=0,
upAxisIndex=2)
proj_matrix = p.computeProjectionMatrixFOV(
fov=90, aspect=float(_render_width) / _render_height,
nearVal=0.01, farVal=100.0)
(_, _, px, _, _) = p.getCameraImage(
width=_render_width, height=_render_height, viewMatrix=view_matrix,
projectionMatrix=proj_matrix, renderer=p.ER_TINY_RENDERER) # ER_BULLET_HARDWARE_OPENGL)
rgb_array = np.array(px, dtype=np.uint8)
rgb_array = np.reshape(rgb_array, (_render_height, _render_width, 4))
rgb_array = rgb_array[:, :, :3]
return rgb_array, view_matrix, proj_matrix
def render():
p1, cubeOrn = p.getBasePositionAndOrientation(1)
p2, cubeOrn = p.getBasePositionAndOrientation(2)
base_pos=[0,0,0]
_cam_dist=3
_cam_yaw=45
_cam_pitch=-30
frame, view_matrix, proj_matrix = capture_frame(base_pos, _cam_dist, _cam_yaw, _cam_pitch)
frame = cv2.resize(frame, VIDEO_RESOLUTION)
points = {}
# inverse transform
view_matrix = p.computeViewMatrixFromYawPitchRoll(
cameraTargetPosition=base_pos,
distance=_cam_dist,
yaw=-_cam_yaw,
pitch=_cam_pitch,
roll=0,
upAxisIndex=2)
my_order = 'C'
pmat = np.array(proj_matrix).reshape((4,4), order=my_order)
vmat = np.array(view_matrix).reshape((4,4), order=my_order)
fmat = pmat # vmat.T
# compute origin from origin point in simulation
origin = np.array([0,0,0,1])
frame_origin = (fmat # origin)[:3]*np.array([1280, 720, 0]) + np.array([640, 360, 0])
# define unit vectors
unit_vectors = [ np.array([1,0,0,1]),
np.array([0,1,0,1]),
np.array([0,0,-1,1]) ]
for col_id, unit_vector in enumerate(unit_vectors):
cur_point = (fmat # unit_vector)[:3]*np.array([1280, 720, 0]) + np.array([640, 360, 0])
cv2.line(frame, (640,360), (int(cur_point[0]),int(cur_point[1])), color=MY_COLORS[col_id], thickness=2)
cv2.imwrite("my_rendering.jpg", frame)
print(p1,p2)
if __name__ == '__main__':
physicsClient = p.connect(p.DIRECT)#or p.DIRECT for non-graphical version
#physicsClient = p.connect(p.GUI)#or p.DIRECT for non-graphical version
p.setAdditionalSearchPath(pybullet_data.getDataPath()) #optionally
p.setGravity(0,0,-10)
planeId = p.loadURDF("plane.urdf")
#arrows = p.loadURDF("arrows.urdf")
startPos = [1,0,0.2]
startOrientation = p.getQuaternionFromEuler([0,0,0])
boxId = p.loadURDF("r2d2.urdf",startPos, startOrientation)
startPos = [0,2,0.2]
boxId = p.loadURDF("r2d2.urdf",startPos, startOrientation)
#set the center of mass frame (loadURDF sets base link frame) startPos/Ornp.resetBasePositionAndOrientation(boxId, startPos, startOrientation)
for i in range (2400):
if i == 2399:
render()
p.stepSimulation()
p.disconnect()
Here is the result:
Best regards.
I am attempting to find a way to visualize the separate regions/phases of the MJO. I believe one way to do so would be by plotting the longitude lines that separate each phase region (at roughly 60E, 80E, 100E, 120E, 140E, 160E, 180), but I am unsure if it is possible to add to my existing plots.
I am using GRID-Sat B1 data from NCEI. Here is what my current code looks like:
import matplotlib.pyplot as plt
from metpy.plots import declarative, colortables
import cartopy.crs as ccrs
import xarray as xr
file = "GRIDSAT-B1.2003.11.23.00.v02r01.nc"
dataset = xr.open_dataset(file)
vtime = dataset.time.values.astype('datetime64[s]').astype('O')
date_long = vtime[0]
date = date_long.strftime("%d-%b-%Y-%HZ")
# Create water vapor image
img = declarative.ImagePlot()
img.data = dataset
img.field = 'irwvp'
img.colormap = 'WVCIMSS_r'
img.image_range = (180, 280)
panel = declarative.MapPanel()
panel.layers = ['coastline', 'borders']
panel.title = f'GridSat-B1 (Water Vapor Imagery): {date}'
panel.projection = (ccrs.Mollweide(central_longitude=-240))
panel.area = ([-370, -140, -30, 30])
panel.layout = (2, 1, 2)
panel.plots = [img]
# Create the IR image
img2 = declarative.ImagePlot()
img2.data = dataset
img2.field = 'irwin_cdr'
img2.colormap = 'turbo_r' #maybe use cubehelix instead?
img2.image_range = (180, 300)
panel2 = declarative.MapPanel()
panel2.layers = ['coastline', 'borders']
panel2.title = f'GridSat-B1 (Infrared Imagery): {date}'
panel2.projection = (ccrs.Mollweide(central_longitude=-240))
panel2.area = ([-370, -140, -30, 30])
panel2.layout = (2, 1, 1)
panel2.plots = [img2]
# Plot both panels in one figure
pc = declarative.PanelContainer()
pc.size = (20, 14)
pc.panels = [panel, panel2]
pc.show()
Here is the current output that is created when I run the script:
Nov03.png
Any help/suggestions are appreciated - thanks in advance!
There's nothing built into MetPy's declarative interface, but fortunately the MapPanel objects expose a .ax attribute that gives you a Matplotlib Axes object and all its plotting methods:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import metpy.plots as mpplots
import numpy as np
import xarray as xr
file = "/Users/rmay/Downloads/GRIDSAT-B1.2003.11.23.00.v02r01.nc"
dataset = xr.open_dataset(file)
vtime = dataset.time.values.astype('datetime64[s]').astype('O')
date_long = vtime[0]
date = date_long.strftime("%d-%b-%Y-%HZ")
# Create water vapor image
img = mpplots.ImagePlot()
img.data = dataset
img.field = 'irwvp'
img.colormap = 'WVCIMSS_r'
img.image_range = (180, 280)
panel = mpplots.MapPanel()
panel.layers = ['coastline', 'borders']
panel.title = f'GridSat-B1 (Water Vapor Imagery): {date}'
panel.projection = ccrs.Mollweide(central_longitude=-240)
panel.area = (-370, -140, -30, 30)
panel.layout = (2, 1, 2)
panel.plots = [img]
# Create the IR image
img2 = mpplots.ImagePlot()
img2.data = dataset
img2.field = 'irwin_cdr'
img2.colormap = 'turbo_r' #maybe use cubehelix instead?
img2.image_range = (180, 300)
panel2 = mpplots.MapPanel()
panel2.layers = ['coastline', 'borders']
panel2.title = f'GridSat-B1 (Infrared Imagery): {date}'
panel2.projection = ccrs.Mollweide(central_longitude=-240)
panel2.area = (-370, -140, -30, 30)
panel2.layout = (2, 1, 1)
panel2.plots = [img2]
# Plot both panels in one figure
pc = mpplots.PanelContainer()
pc.size = (20, 14)
pc.panels = [panel, panel2]
lons = np.array([60, 80, 100, 120, 140, 160, 180]).reshape(1, -1)
lats = np.linspace(-90, 90).reshape(-1, 1)
# Match up the arrays into 2xN arrays fit to plot in call
lons, lats = np.broadcast_arrays(lons, lats)
# Needs to be *after* the panels are assigned to a PanelContainer
# Using Geodetic gives lines interpolated on the curved globe
panel.ax.plot(lons, lats, transform=ccrs.Geodetic(), color='black', linewidth=3)
panel2.ax.plot(lons, lats, transform=ccrs.Geodetic(), color='black', linewidth=3)
pc.show()
(Note: it's not recommended to import from metpy's declarative module directly since that's an implementation detail subject to change--just get things from metpy.plots). So this is using Matplotlib's standard call to plot to draw the lines. Another option would be to use CartoPy's Gridliner.
Hi I'm a student and I try to make a 3d map reconstruction for a signature and just follow the example in the book "OpenCV with Python by Example" and I don't know why, or how to fix this mistake. I got this error, please help me.
This is the code:
import argparse
import cv2
import numpy as np
def build_arg_parser():
parser = argparse.ArgumentParser(description='Reconstruct the 3D map from \the two input stereo images. Output will be saved in\'output.ply\'')
parser.add_argument("--image-left", dest="image_left", required=True,
help="Input image captured from the left")
parser.add_argument("--image-right", dest="image_right", required=True,
help="Input image captured from the right")
parser.add_argument("--output-file", dest="output_file", required=True,
help="Output filename (without the extension) where the point cloud will be saved")
return parser
def create_output(vertices, colors, filename):
colors = colors.reshape(-1, 3)
vertices = np.hstack([vertices.reshape(-1,3), colors])
ply_header = '''ply
format ascii 1.0
element vertex %(vert_num)d
property float x
property float y
property float z
property uchar red
property uchar green
property uchar blue
end_header
'''
with open(filename, 'w') as f:
f.write(ply_header % dict(vert_num=len(vertices)))
np.savetxt(f, vertices, '%f %f %f %d %d %d')
if __name__ == '__main__':
args = build_arg_parser().parse_args()
image_left = cv2.imread(args.image_left)
image_right = cv2.imread(args.image_right)
output_file = args.output_file + '.ply'
if image_left.shape[0] != image_right.shape[0] or \
image_left.shape[1] != image_right.shape[1]:
raise TypeError("Input images must be of the same size")
# downscale images for faster processing
image_left = cv2.pyrDown(image_left)
image_right = cv2.pyrDown(image_right)
# disparity range is tuned for 'aloe' image pair
win_size = 1
min_disp = 16
max_disp = min_disp * 9
num_disp = max_disp - min_disp# Needs to be divisible by 16
stereo = cv2.StereoSGBM(minDisparity = min_disp,
numDisparities = num_disp,
SADWindowSize = win_size,
uniquenessRatio = 10,
speckleWindowSize = 100,
speckleRange = 32,
disp12MaxDiff = 1,
P1 = 8*3*win_size**2,
P2 = 32*3*win_size**2,
fullDP = True
)
print "\nComputing the disparity map..."
disparity_map = stereo.compute(image_left,image_right).astype(np.float32) /16.0
print "\nGenerating the 3D map..."
h, w = image_left.shape[:2]
focal_length = 0.8*w
# Perspective transformation matrix
Q = np.float32([[1, 0, 0, -w/2.0],
[0,-1, 0, h/2.0],
[0, 0, 0, -focal_length],
[0, 0, 1, 0]])
points_3D = cv2.reprojectImageTo3D(disparity_map, Q)
colors = cv2.cvtColor(image_left, cv2.COLOR_BGR2RGB)
mask_map = disparity_map > disparity_map.min()
output_points = points_3D[mask_map]
output_colors = colors[mask_map]
print "\nCreating the output file...\n"
create_output(output_points, output_colors, output_file)
This the error I'm getting in the console:
Computing the disparity map...
Traceback (most recent call last):
File "rec.py", line 58, in <module>
disparity_map = stereo.compute(image_left,image_right).astype(np.float32) / 16.0
TypeError: Incorrect type of self (must be 'StereoMatcher' or its derivative)
you should use:
stereo = cv2.StereoSGBM_create(
minDisparity = min_disp,
numDisparities = num_disp,
SADWindowSize = win_size,
uniquenessRatio = 10,
speckleWindowSize = 100,
speckleRange = 32,
disp12MaxDiff = 1,
P1 = 8*3*win_size**2,
P2 = 32*3*win_size**2,
fullDP = True
)
I am new at opencv and I am trying to use opencv to find the center of all the disk in this image,and i tried in many method,the result is not so accurate.Here is my code to find the center.I want to find a method to improve the result.
import cv2
import numpy as np
import cv2.cv as cv
imgorg = cv2.imread("keli.jpg")
img = cv2.resize(imgorg,(1024,800),interpolation=cv2.INTER_CUBIC)
x = cv2.Sobel(img,cv2.CV_16S,1,0)
y = cv2.Sobel(img,cv2.CV_16S,0,1)
cv2.imshow("img",img)
absX = cv2.convertScaleAbs(x)
absY = cv2.convertScaleAbs(y)
sobelresult = cv2.addWeighted(absX,0.5,absY,0.5,0)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3, 3))
opened = cv2.morphologyEx(sobelresult, cv2.MORPH_OPEN, kernel)
closed = cv2.morphologyEx(opened, cv2.MORPH_CLOSE, kernel)
cv2.imshow("Open", opened);
cv2.imshow("Close",closed);
element = cv2.getStructuringElement (cv2.MORPH_ELLIPSE ,(3,3))
dilate = cv2.dilate(closed, element)
erode = cv2.erode(closed, element)
result = cv2.absdiff(dilate,erode)
retval, result = cv2.threshold(result, 40, 255, cv2.THRESH_BINARY)
result = cv2.bitwise_not(result)
def nothing(x):
pass
cv2.namedWindow('image')
#hough transform
cv2.createTrackbar('param1','image',1,255,nothing)
cv2.createTrackbar('param2','image',1,255,nothing)
cv2.createTrackbar('minDistance','image',1,255,nothing)
param1 = cv2.setTrackbarPos('param1','image',55)
param2 = cv2.setTrackbarPos('param2','image',45)
minDistance = cv2.setTrackbarPos('minDistance','image',25)
while(1):
anotherimg = cv2.imread("keli.jpg")
anotherimgeresize = cv2.resize(anotherimg,(1024,800),interpolation=cv2.INTER_CUBIC)
gray = cv2.cvtColor(result,cv2.COLOR_BGR2GRAY)
k=cv2.waitKey(0)&0xFF
if k==27:
break
param1 = cv2.getTrackbarPos('param1','image')
param2 = cv2.getTrackbarPos('param2','image')
minDistance = cv2.getTrackbarPos('minDistance','image')
circles1 = cv2.HoughCircles(gray,cv.CV_HOUGH_GRADIENT,1,
minDistance,param1=param1,param2=param2,
minRadius=2,maxRadius=80)
circles = circles1[0,:,:]
circles = np.uint16(np.around(circles))
for i in circles[:]:
cv2.circle(anotherimgeresize,(i[0],i[1]),i[2],(255,0,0),2)
cv2.circle(anotherimgeresize,(i[0],i[1]),2,(0,0,0),2)
cv2.imshow('result',anotherimgeresize)
cv2.destroyAllWindows()
The param: param1 = 75,param2 =34,minDistance = 25
I'm running opencv 2.4.1 using python bindings and am having difficulty calculating the optical flow.
Specifically this section of code:
#calculate the opticalflow
if prev_saturation_thresh_img==None:
prev_saturation_thresh_img=saturation_img
if i >=0:
prev_img=prev_saturation_thresh_img
next_img=saturation_thresh_img
p1, st, err = cv2.calcOpticalFlowPyrLK(prev_img,next_img,tracks_np,**lk_params)
Returns the error:
<unknown> is not a numpy array
So then I try to convert the images to numpy arrays:
prev_img=prev_saturation_thresh_img
next_img=saturation_thresh_img
Now I have a new error:
<unknown> data type = 17 is not supported
In a last-ditch effort I convert the images to cvmat (from iplimage) before converting it to a numpy array, just to see what happens
error: ..\..\..\OpenCV-2.4.1\modules\video\src\lkpyramid.cpp:607: error: (-215) nextPtsMat.checkVector(2, CV_32F, true) == npoints
So now I'm stuck. Below is the code in it's entirety for reference
import cv
import cv2
import numpy as np
class Target:
def __init__(self):
self.capture = cv.CaptureFromFile("raw_gait_cropped.avi")
def run(self):
#initiate font
font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8)
#instantiate images
img_size=cv.GetSize(cv.QueryFrame(self.capture))
hsv_img=cv.CreateImage(img_size,8,3)
saturation_img=cv.CreateImage(img_size,8,1)
saturation_thresh_img=cv.CreateImage(img_size,8,1)
prev_saturation_thresh_img=None
#create params for GoodFeaturesToTrack and calcOpticalFlowPyrLK
gftt_params = dict( cornerCount=11,
qualityLevel=0.2,
minDistance=5,
mask=None,
useHarris=True
)
lk_params = dict( winSize = (15, 15),
maxLevel = 2,
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03),
flags = cv2.OPTFLOW_USE_INITIAL_FLOW,
minEigThreshold=1
)
tracks=[]
writer=cv.CreateVideoWriter("angle_tracking.avi",cv.CV_FOURCC('M','J','P','G'),30,cv.GetSize(hsv_img),1)
i=0
while True:
#grab a frame from the video capture
img=cv.QueryFrame(self.capture)
#break the loop when the video is over
if img == None:
break
#convert the image to HSV
cv.CvtColor(img,hsv_img,cv.CV_BGR2HSV)
#Get Saturation channel
cv.MixChannels([hsv_img],[saturation_img],[(1,0)])
#Apply threshold to saturation channel
cv.InRangeS(saturation_img,145,255,saturation_thresh_img)
#locate initial features to track
if i==0:
eig_image=temp_image = cv.CreateMat(img.height, img.width, cv.CV_32FC1)
for (x,y) in cv.GoodFeaturesToTrack(saturation_thresh_img, eig_image, temp_image, **gftt_params):
tracks.append([(x,y)])
cv.Circle(saturation_thresh_img,(int(x),int(y)),5,(255,255,255),-1,cv.CV_AA,0)
tracks_np=np.float32(tracks).reshape(-1,2)
print tracks
#calculate the opticalflow
if prev_saturation_thresh_img==None:
prev_saturation_thresh_img=saturation_img
if i >=0:
prev_img=prev_saturation_thresh_img
next_img=saturation_thresh_img
p1, st, err = cv2.calcOpticalFlowPyrLK(prev_img,next_img,tracks_np,**lk_params)
prev_saturation_thresh_img=saturation_img
i=i+1
print i
#display frames to users
cv.ShowImage("Raw Video",img)
cv.ShowImage("Saturation Channel",saturation_img)
cv.ShowImage("Saturation Thresholded",saturation_thresh_img)
# Listen for ESC or ENTER key
c = cv.WaitKey(7) % 0x100
if c == 27 or c == 10:
break
#close all windows once video is done
cv.DestroyAllWindows()
if __name__=="__main__":
t = Target()
t.run()
OpenCV can be very picky about the data formats it accepts. The following code extract works for me:
prev = cv.LoadImage('images/'+file_list[0])
prev = np.asarray(prev[:,:])
prev_gs = cv2.cvtColor(prev, cv2.COLOR_BGR2GRAY)
current = cv.LoadImage('images/'+file)
current = np.asarray(current[:,:])
current_gs = cv2.cvtColor(current, cv2.COLOR_BGR2GRAY)
features, status, track_error = cv2.calcOpticalFlowPyrLK(prev_gs, current_gs, good_features, None,
**lk_params)
Note the [:,:] when converting from images to numpy arrays, I have found that they are required.
I hope that this may solve your problem.