Ask Your Question

harsha's profile - activity

2016-08-25 10:19:02 -0600 commented answer output.avi file created but its size is 0k

Problem could be in videoCapture or videoWriter. Could you please check if there are any issues with videoCapture, by just visualizing the frames?

2016-08-24 16:35:24 -0600 received badge  Organizer (source)
2016-08-24 12:35:44 -0600 answered a question output.avi file created but its size is 0k

From this answer, frame size used while initializing videoWriter and the size of the frame being written to the file should be the same.

Resize "frame" before writing it

import numpy
import cv2

cap = cv2.VideoCapture(0)
fourcc = cv2.cv.CV_FOURCC(*'MJPG')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frameTmp = cap.read()
    if ret==True:
        frame = cv2.resize(frameTmp, (640, 480))
        frame = cv2.flip(frame,1)

        # write the flipped frame
        out.write(frame)
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()

Get frame size from videoCapture to initialize videoWriter.

import numpy
import cv2

cap = cv2.VideoCapture(0)
width = cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)
fourcc = cv2.cv.CV_FOURCC(*'MJPG')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (int(width),int(height)))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,1)

        # write the flipped frame
        out.write(frame)
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()

Update 1: Added code snippets

2016-08-22 17:04:15 -0600 answered a question i want to change the black pixels in the image to Red

img_rgb is a numpy ndarray.

To find indices of the mask

indices = np.where(mask==255)

To fill pixels at these indices with red color

img_rgb[indices[0], indices[1], :] = [0, 0, 255]

img_rgb now has red pixels

2016-01-15 04:42:26 -0600 received badge  Nice Answer (source)
2016-01-12 09:01:51 -0600 received badge  Teacher (source)
2016-01-03 20:02:04 -0600 answered a question VideoCapture not obtaining newest frames when I run read()

If you're using ROS, it might be better to use usb_cam package to open a usb camera. You can subscribe to the image topic, use cv_bridge to convert sensor_msgs::image to cv::Mat, process your image and publish or send service calls from there. This way you'll always get the latest frame for your callback.

2016-01-03 19:56:06 -0600 commented question object tracking in video sequence

My answer here might be helpful.

2016-01-03 19:45:45 -0600 commented question How do you stop a capture frame getting split ?

I've seen this type of image corruption when a USB2 camera is connected to a USB3 port or a USB SS port.

2016-01-03 19:42:02 -0600 commented question Informative links related to OpenCV
2016-01-03 19:40:24 -0600 commented question Informative links related to OpenCV
2016-01-01 18:39:42 -0600 answered a question Skipping all but the latest frame in VideoCapture

You can keep reading frames and save them in to a buffer of size n, and throw out old frames making way for new ones. While all the capture and save to buffer runs in one thread, processing can be done in a separate thread which has access to the buffer. This way you will always have access to n latest frames and n can be chosen depending on the application.

2015-12-30 10:56:23 -0600 commented answer How to record the time of stay by detected people in a video?

I've updated the answer with links to some tutorials on kalman filter and object tracking. Hope it helps.

2015-12-30 10:53:59 -0600 received badge  Editor (source)
2015-12-26 10:16:43 -0600 received badge  Enthusiast
2015-12-25 18:22:13 -0600 answered a question How to record the time of stay by detected people in a video?

You are able to detect people in a frame using HOG features. In order to compute how long a person was present in a video, we need to know the first and last frames of their presence in the video and the video's frame rate.

timeOfStay = (lastFrame - firstFrame) / frameRate

If you would like to record time of stay for each person, detection is not enough. We need to able to track them. Since there is no intersection of trajectories, a simple Kalman filter would suffice. Here is a good example: http://www.morethantechnical.com/2011...

You get the first frame of detection from initializing a kalman filter and last frame when you lose track of the person of interest. OpenCV provides both frame number and frame rate and hopefully this helps with your problem.

Edit: Including information on kalman filter and object tracking

HOG provides detections in a given frame. A video has multiple frames and we now have all these different detections in each of these frames. If we expect to see only one person, then just the occurrence of that person (HOG detection) is enough to identify which of the frames that person was present. In this case, we have multiple entities. We need to associate detections in each frame with detections in the previous frame and account for addition or deletion of entities.

Here are some good tutorials on kalman filter and object tracking: http://www.mathworks.com/help/vision/... https://www.youtube.com/watch?v=FkCT_... https://www.youtube.com/watch?v=NT7nY... https://www.youtube.com/watch?v=rUgKn...

There is some C++ and OpenCV code here: https://github.com/Smorodov/Multitarg...

2015-06-04 19:11:00 -0600 received badge  Supporter (source)
2015-05-17 03:34:37 -0600 answered a question Download the Source Files for QT Framework

The source files can be found here on github. And here is qt5.

2015-05-17 03:21:40 -0600 commented question problems in building opencv 2.4 on ubuntu

Error message says it make cannot find zlib.h. Try to install zlib from apt-get. There are some instructions on building opencv on linux here

2015-05-17 03:15:18 -0600 answered a question Ubuntu 14.04 apt-get installation missing headers

Feature detection tutorial depends on the nonfree module, which is not included in 14.04 apt-get repos. It can be installed following instructions in this answer.

sudo add-apt-repository --yes ppa:xqms/opencv-nonfree
sudo apt-get update 
sudo apt-get install libopencv-nonfree-dev
2015-05-17 03:05:34 -0600 answered a question Extract a frame every second in Python
import cv2
import math

videoFile = "capture.avi"
imagesFolder = "/other/images"
cap = cv2.VideoCapture(videoFile)
frameRate = cap.get(5) #frame rate
while(cap.isOpened()):
    frameId = cap.get(1) #current frame number
    ret, frame = cap.read()
    if (ret != True):
        break
    if (frameId % math.floor(frameRate) == 0):
        filename = imagesFolder + "/image_" +  str(int(frameId)) + ".jpg"
        cv2.imwrite(filename, frame)
cap.release()
print "Done!"