使用 OpenCV 在图像上绘制十字
在本文中,我们将讨论如何使用 OpenCV-Python 在图像上绘制十字。我们可以绘制两条线的叠加层,以在图像上形成十字。
为了在 OpenCV 上画一条线,使用下面的函数。
Syntax: cv2.line(image, starting Point, ending Point, color, thickness)
Parameters:
- Image – The source image on which you need to draw the shape.
- startingPoint – The coordinates of the image where the line should start. Which is represented by a tuple of co-ordinates (x-axis, y-axis), basically the pixel’s location from where to start.
- endingPoint – The coordinates of the image where the line should end. Which is represented by a tuple of co-ordinates (x-axis, y-axis).
- Color – Color of the line drawn on the image. Represented by BGR color values on a tuple in which each value is between 0 – 255
- (255,0,0) for BLUE
- (0,255,0) for GREEN
- (0,0,255) for RED]
- Thickness – Thickness of the drawn line in pixels.
要在图像上画十字,我们首先需要导入必要的库,
- 开放式CV
- 数字货币
然后读取要在其上绘制形状的图像,然后调用 OpenCV 上的 line函数在给定的 x 轴和 y 轴坐标上画一条线,这基本上是起点和终点像素的位置图片。在 OpenCV 中,出于某种原因,x 和 y 平面与我们使用的普通笛卡尔平面直接相反。其中 x 轴位于图像的顶部边缘,y 轴位于图像的左边缘但上下颠倒,这意味着 x 轴的增加意味着向图像的右侧移动并增加 y 轴意味着向下移动图像。
为了获得图像的宽度和高度,我们需要在 OpenCV 中调用一个简单的函数,该函数将返回图像的形状,从中我们可以获得图像的宽度和高度,然后我们可以在程序中使用它。只需在图像上调用 .shape 方法即可获取尺寸。
在图像上画十字的程序,
Python3
# Importing the necessary libraries
import cv2
import numpy
# Reading the image
image = cv2.imread('image.png')
# Getting the height and width of the image
height = image.shape[0]
width = image.shape[1]
# Drawing the lines
cv2.line(image, (0, 0), (width, height), (0, 0, 255), 5)
cv2.line(image, (width, 0), (0, height), (0, 0, 255), 5)
# Showing the image
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Python3
# Importing the libraries
import cv2
import numpy
# Initiating the webcam
vid = cv2.VideoCapture(0)
# Capturing frames and showing as a video
while(True):
ret, frame = vid.read()
# Getting the width and height of the feed
height = int(vid.get(4))
width = int(vid.get(3))
# Drawing cross on the webcam feed
cv2.line(frame, (0, 0), (width, height), (0, 0, 255), 5)
cv2.line(frame, (width, 0), (0, height), (0, 0, 255), 5)
# Showing the video
cv2.imshow('LIVE', frame)
# Making sure that we have a key to break the while loop
# Which checks for the ascii value of the key pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# At last release the camera
vid.release()
cv2.destroyAllWindows()
Python3
# Importing the modules
import cv2
import numpy
# Reading the image
image = cv2.imread('image.png')
# Function to manually select the area
r = cv2.selectROI("select the area", image)
# Calculating the coordinates with the coordinates
# returned by the function
opposite_x = int(r[2]) - int(r[0])
bottom_y = int(r[1]) + (int(r[3]) - int(r[1]))
# Drawing the cross by the calculated and
# obtained coordinates
cv2.line(image, (int(r[0]), int(r[1])), (int(r[2]), int(r[3])), (0, 0, 255), 5)
cv2.line(image, (opposite_x, int(r[1])), (int(r[0]), bottom_y), (0, 0, 255), 5)
# Showing the image
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
注意:您可以提及您选择的起始和结束像素,以根据您的需要使十字变小和变大
交叉直播网络摄像头提要
现在让我们看看如何从我们机器的实时摄像头馈送的视频上绘制图像。为此,我们首先需要启动我们的实时网络摄像头,然后我们需要每毫秒捕获一张图像并相互显示以从我们的网络摄像头获取视频,然后在我们从网络摄像头捕获的每一帧上添加覆盖线网络摄像头每毫秒实现一次结果。
注意:要了解更多关于在 OpenCV 中使用网络摄像头的信息,请参阅 – 使用 Python-OpenCV 访问网络摄像头
Python3
# Importing the libraries
import cv2
import numpy
# Initiating the webcam
vid = cv2.VideoCapture(0)
# Capturing frames and showing as a video
while(True):
ret, frame = vid.read()
# Getting the width and height of the feed
height = int(vid.get(4))
width = int(vid.get(3))
# Drawing cross on the webcam feed
cv2.line(frame, (0, 0), (width, height), (0, 0, 255), 5)
cv2.line(frame, (width, 0), (0, height), (0, 0, 255), 5)
# Showing the video
cv2.imshow('LIVE', frame)
# Making sure that we have a key to break the while loop
# Which checks for the ascii value of the key pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# At last release the camera
vid.release()
cv2.destroyAllWindows()
输出:
通过手动选择区域绘制十字:
我们可以用我们刚刚学到的基础做很多事情。我们可以在 OpenCV 中创造奇迹。例如,现在我们可以通过手动选择图像中的区域来创建交叉。我们所要做的就是手动选择我们需要绘制十字的图像区域。为此,我们将使用 OpenCV 中的 selectROI()函数,该函数将返回图像中所选感兴趣范围的坐标。使用这些坐标,我们可以绘制十字并显示输出。
Syntax: cv2.selectROI(Window_name, source image)
Parameter:
- window_name: name of the window where selection process will be shown.
- source image: image to select a ROI.
- showCrosshair: if true crosshair of the selection rectangle will be displayed.
- fromCenter: if true origin of selection will match initial mouse position
例子:
Python3
# Importing the modules
import cv2
import numpy
# Reading the image
image = cv2.imread('image.png')
# Function to manually select the area
r = cv2.selectROI("select the area", image)
# Calculating the coordinates with the coordinates
# returned by the function
opposite_x = int(r[2]) - int(r[0])
bottom_y = int(r[1]) + (int(r[3]) - int(r[1]))
# Drawing the cross by the calculated and
# obtained coordinates
cv2.line(image, (int(r[0]), int(r[1])), (int(r[2]), int(r[3])), (0, 0, 255), 5)
cv2.line(image, (opposite_x, int(r[1])), (int(r[0]), bottom_y), (0, 0, 255), 5)
# Showing the image
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出: