使用Opencv和python

时间:2019-05-06 10:41:03

标签: python python-3.x opencv opencv3.0 opencv-contour

实时视频分辨率约为4000x3000像素,其图像已显示在附件中。可以检测物体,裁剪图像吗?

使用一些较早的代码,我只能检测到它们的对象,但是我面临着OpenCV中相机拉屎过程的问题

注意:图像检测过程是通过笔记本电脑摄像头使用下面提到的代码完成的:

import cv2
import numpy as np
video = cv2.VideoCapture("output.avi")
_, first_frame = video.read()
x = 210
y = 310
width = 230
height = 115
roi = first_frame[y: y + height, x: x + width]
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
roi_hist = cv2.calcHist([hsv_roi], [0], None, [180], [0, 180])
roi_hist = cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
term_criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1)

print(term_criteria)
while True:
    _, frame = video.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    mask = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)
    _, track_window = cv2.meanShift(mask, (x, y, width, height), term_criteria)
    x, y, w, h = track_window
    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
    cv2.imshow("Mask", mask)
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(60)
    if key == 27:
        break
video.release()
cv2.destroyAllWindows()

有关此建议将有很大帮助

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以使用python的切片符号:

crop_img = img[y : y + h, x : x + w, :]

答案 1 :(得分:0)

使用opencv裁剪图像的正确方法

crop_image = img[y : y + h, x : x + w]

其中(x,y)是分别对应于xmin和ymin的元组 h,w是检测盒的高度和宽度。