1

The Live video resolution is around 4000x3000 pixels whose image has been displayed in the attachment. Could it be possible to detect the object, crop the image?

With some Earlier code, I am able to detect them object only but I am facing the issue of camera shit process in OpenCV

Note: The Image detection process was done with laptop webcam using mentioned below code:

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()

Suggestion Regarding this will be a great help

enter image description here

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
varul jain
  • 334
  • 8
  • 23

2 Answers2

0

You can use python's Slice Notation:

crop_img = img[y : y + h, x : x + w, :]
Eran W
  • 1,696
  • 15
  • 20
  • More about slice notation: https://stackoverflow.com/questions/509211/understanding-slice-notation – Eran W May 06 '19 at 11:02
  • 1
    i think to crop an image you need to give ``` [y1:y2, x1:x2] ``` not ``` [x1:x2, y1:y2] ``` – sanyam May 06 '19 at 11:26
0

correct way to crop image using opencv

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

where (x,y) are the tuple corresponding to xmin and ymin respectively h,w are the height and width of the detection box.

sanyam
  • 96
  • 5
  • FYI: This answer was flagged as low-quality because of its length and content. You may want to improve it by explaining how this code answers OP's question. – oguz ismail May 06 '19 at 13:47
  • I am making this thing work with some video, To detect the object get this thing done, do I have to first take the frame from the video? – varul jain May 07 '19 at 06:34
  • @varuljain Yes, you need to take the frame from the video then detect the object from the frame and then at last crop out your object. – sanyam May 07 '19 at 09:28