I am trying to figure out how to change a line drawn in cv2 in some code from horizontal to vertical. It comes out horizontal with this:
cv2.line(frame, (0, H // 2), (W, H // 2), (0, 255, 255), 2)
How to change to vertical?
I understand the line starts with parameter (0, H // 2) and ends with (W, H // 2) but its puzzling to me how to change it around from horizontal coordinate definition to vertical. Some experimentations with this have been unsuccessful and tips GREATLY appreciated.
H & W are defined to have a maximum of 500 pixels defined here:
# loop over frames from the video stream
while True:
# grab the next frame and handle if we are reading from either
# VideoCapture or VideoStream
frame = vs.read()
frame = frame[1] if args.get("input", False) else frame
# if we are viewing a video and we did not grab a frame then we
# have reached the end of the video
if args["input"] is not None and frame is None:
break
# resize the frame to have a maximum width of 500 pixels (the
# less data we have, the faster we can process it), then convert
# the frame from BGR to RGB for dlib
frame = imutils.resize(frame, width=500)
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# if the frame dimensions are empty, set them
if W is None or H is None:
(H, W) = frame.shape[:2]
CodePudding user response:
In your particular case the answer will be:
cv2.line(frame, (W//2,0), (W//2, H) , (0,255,255), 2)
As general discussion:
If you work with OpenCV in python there is some thing you must have present. When you create an array of any type its dimensions are NxM N will be de number of rows and M the number of columns. If you take this as an image N will be the HEIGHT and M the WIDTH.
usually you will access to the i-th row and j-th collumn as :
import numpy as np
import cv2
a = np.random.randint(255,size=(500,300),dtype=np.uint8)
i=20
j=30
#access to ith jth
print( a[i,j] )
OpenCV functions usually takes as coordinates first the column then the row So if you want yo use the same point to access and create an horizontal line of lenght lets say 50. you can call
line_length = 50
a = cv2.line(a,(j,i),(j line_length,i),(0,0,0),2)
and then show it.
cv2.line takes at least 4 arguments but you can put some others:
1_ img: the image to work 2_ p0: initial point of line in image (coord_x,coord_y) 3_ p1: final point of line in image (coord_x,coord_y) 4_ color: as a tuple (B,G,R) of intensities (ussually 0-255)
the fisrt optional is line width.
You can check de docs:
https://docs.opencv.org/4.x/d6/d6e/group__imgproc__draw.html#ga7078a9fae8c7e7d13d24dac2520ae4a2
