I am using OpenCV 3 and Visual Studio.
The issue here is that I am unable to save video for any particular resolution other than default full camera resolution.
There is no error, but video file doesn't grow. It stays at 5.54kb.
Here is my code:
#include"opencv2\opencv.hpp"
using namespace cv;
using namespace std;
VideoCapture cap_cam1(0);
double Width = cap_cam1.get(CV_CAP_PROP_FRAME_WIDTH);
double Height = cap_cam1.get(CV_CAP_PROP_FRAME_HEIGHT);
cv::Size frameSize(static_cast<int>(Width), static_cast<int>(Height));
string videoFileName = "D://current.avi";
VideoWriter cam1_write(videoFileName, CV_FOURCC('D', 'I', 'V', '3'), 10.0, frameSize, true);
Mat image;
int main()
{
while (true)
{
cap_cam1 >> image;
resize(image, image, Size(320, 240));
imshow("image", image);
cam1_write.write(image);
if (waitKey(33) == 27)break;
}
}
If I remove the resize function, then file size grows and frames are added.
I also tried adding below lines after VideoWriter definition
cap_cam1.set(CV_CAP_PROP_FRAME_WIDTH, 240);
cap_cam1.set(CAP_PROP_FRAME_HEIGHT,320);
I also tried changing resolution at VideoWriter definition, after everything file size remains at 5.54kb.
How to record video at custom resolution?
you are using
double Width = cap_cam1.get(CV_CAP_PROP_FRAME_WIDTH);
double Height = cap_cam1.get(CV_CAP_PROP_FRAME_HEIGHT);
cv::Size frameSize(static_cast<int>(Width), static_cast<int>(Height));
VideoWriter cam1_write(videoFileName, CV_FOURCC('D', 'I', 'V', '3'), 10.0, frameSize, true);
which means you want to write a video with the VideoCapture image size.
Here you have to change to
cv::Size targetSize = cv::Size(320,240);
VideoWriter cam1_write(videoFileName, CV_FOURCC('D', 'I', 'V', '3'), 10.0, targetSize, true);
now in your loop use
resize(image, image, targetSize);
That means you have to decide what kind of output image size you want to have when creating the VideoWriter. That's because typical codecs assume constant (known) image resolution.
For setting resolution you have some typo:
cap_cam1.set(CV_CAP_PROP_FRAME_WIDTH, 240);
cap_cam1.set(CAP_PROP_FRAME_HEIGHT,320);
this would mean a resolution of cv::Size(240,320), so your code might work if you changed the order there to cv::Size(320,240). See comment of #MBo
Related
I'm using OpenCV with ffmpeg support to read a RTSP stream coming from an IP camera and then to write the frames to a video. The problem is that the frame size is 2816x2816 at 20 fps i.e. there's a lot of data coming in.
I noticed that there was a significant delay in the stream, so I set the buffer size of the cv::VideoCapture object to 1, because I thought that the frames might just get stuck in the buffer instead of being grabbed and processed. This however just caused for frames to be dropped instead.
My next move was to experiment a bit with the frame size/fps and the encoding of the video that I'm writing. All of those things helped to improve the situation, but in the long run I still have to use a frame size of 2816x2816 and support up to 20 fps, so I can't set it lower sadly.
That's where my question comes in: given the fact that the camera stream is going to be either h264 or h265, which one would be read faster by the cv::VideoCapture object? And how should I encode the video I'm writing in order to minimize the time spent decoding/encoding frames?
That's the code I'm using for reference:
using namespace cv;
int main(int argc, char** argv)
{
VideoCapture cap;
cap.set(CAP_PROP_BUFFERSIZE, 1); // internal buffer will now store only 1 frames
if (!cap.open("rtsp://admin:admin#1.1.1.1:554/stream")) {
return -1;
}
VideoWriter videoWr;
Mat frame;
cap >> frame;
//int x264 = cv::VideoWriter::fourcc('x', '2', '6', '4'); //I was trying different options
int x264 = cv::VideoWriter::fourcc('M', 'J', 'P', 'G');
videoWr = cv::VideoWriter("test_video.avi", 0, 0, 20, frame.size(), true);
namedWindow("test", WINDOW_NORMAL);
cv::resizeWindow("test", 1024, 768);
for (;;)
{
cap >> frame;
if (frame.empty()) break; // end of video stream
imshow("test", frame);
if (waitKey(10) == 27) break;
videoWr << frame;
}
return 0;
}
I am trying to write the frame into my local machine in mp4 format. The frame is read from an existing
mp4 file. After running the following code, I was able to see the VideoOutput.mp4 file, but it is corrupted for some reason. Anyone knows why?
VideoCapture capture("videoSample.mp4");
if (capture.isOpened())
{
while (true)
{
capture >> frame;
}
VideoWriter video("somepath\\videoOutput.mp4", VideoWriter::fourcc('m', 'p', '4', 'v'), 10, Size(win_width, win_width * 2));
video.write(frame);
}
I'm attempting to utilize OpenCV, C++, on my windows 10 system to record the screen as part of a larger program I am writing. I need the ability to record the display and save the recording for later review.
I was able to find this link on stackoverflow
How to capture the desktop in OpenCV (ie. turn a bitmap into a Mat)?
User john ktejik created a function that in essence completed exactly what I am looking to accomplish, short of saving the stream to file.
Now what I have always done in the past was once I've opened a connection to my webcam or a video file, I could simply create a VideoWriter Object and write the individual frames to file. I have attempted to do just that utilizing John's function to act as a video source.
int main (int argc, char **argv)
{
HWND hwndDesktop = GetDesktopWindow ();
int key = 0;
int frame_width = 1920;
int frame_height = 1080;
VideoWriter video ("screenCap.avi", CV_FOURCC ('M', 'J', 'P', 'G'), 15, Size (frame_width, frame_height));
while (key != 27)
{
Mat src = hwnd2mat (hwndDesktop);
video.write (src);
imshow ("Screen Capture", src);
key = waitKey (27);
}
video.release ();
destroyAllWindows ();
return 0;
}
What I'm seeing as the output, is the file labeled "screenCap.avi", however the file is empty of video. The file saves as 16KB storage space.
John's function is on point, as it displays the frames just fine via imshow(), but doesn't seem to allow me to save them.
So over the weekend I played with the software some more. And as I really don't have a firm grasp on it, I figured that there had to be a problem with settings between the screen capture and the file writer.
So I started looking at each of the lines in John's function. I came across
src.create(height, width, CV_8UC4);
It seems that the Mat object is being created as with 4 color channels. Did a bit more digging and I found a couple references that point to Videowriter expecting 3 channels.
So a simple change was to convert the output of Johns function from 4 channels to 3 channels. This fixed the problem and I can now write the frames to file.
int main (int argc, char **argv)
{
HWND hwndDesktop = GetDesktopWindow ();
int key = 0;
int frame_width = 1920;
int frame_height = 1080;
VideoWriter video ("screenCap.avi", CV_FOURCC ('M', 'J', 'P', 'G'), 15, Size
(frame_width, frame_height));
while (key != 27)
{
Mat src = hwnd2mat (hwndDesktop);
Mat dst;
cvtColor (src, dst, COLOR_BGRA2RGB);
video.write (dst);
imshow ("Screen Capture", dst);
key = waitKey (27);
}
video.release ();
destroyAllWindows ();
return 0;
}
I am working on a OCR project and I am in the training phase, so the goal is for my program to detect the characters as objects in an image and allow them to be classified.
The problem is that I cannot get it with the Ñ because it is formed by two objects (the N and the stick).
Do you know if there is any way to get it classified as a single object?
Here is the program:
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/ml/ml.hpp>
#include<iostream>
#include<vector>
// global variables
const int MIN_CONTOUR_AREA = 30;
const int RESIZED_IMAGE_WIDTH = 20;
const int RESIZED_IMAGE_HEIGHT = 30;
//////////////////////////////////////
int main() {
cv::Mat imgTrainingNumbers; // input image
cv::Mat imgGrayscale; //
cv::Mat imgBlurred; // declare various images
cv::Mat imgThresh; //
cv::Mat imgThreshCopy; //
std::vector<std::vector<cv::Point> > ptContours; // declare contours vector
std::vector<cv::Vec4i> v4iHierarchy; // declare contours hierarchy
cv::Mat matClassificationInts; // these are our training classifications, note we will have to perform some conversions before writing to file later
// these are our training images, due to the data types that the KNN object KNearest requires, we have to declare a single Mat,
// then append to it as though it's a vector, also we will have to perform some conversions before writing to file later
cv::Mat matTrainingImagesAsFlattenedFloats;
// possible chars we are interested in are digits 0 through 9 and capital letters A through Z, put these in vector intValidChars
std::vector<int> intValidChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z'};
imgTrainingNumbers = cv::imread("caracteres_prueba.png"); // read in training numbers image
if (imgTrainingNumbers.empty()) { // if unable to open image
std::cout << "error: image not read from file\n\n"; // show error message on command line
return(0); // and exit program
}
cv::cvtColor(imgTrainingNumbers, imgGrayscale, CV_BGR2GRAY); // convert to grayscale
cv::GaussianBlur(imgGrayscale, // input image
imgBlurred, // output image
cv::Size(5, 5), // smoothing window width and height in pixels
0); // sigma value, determines how much the image will be blurred, zero makes function choose the sigma value
// filter image from grayscale to black and white
cv::adaptiveThreshold(imgBlurred, // input image
imgThresh, // output image
255, // make pixels that pass the threshold full white
cv::ADAPTIVE_THRESH_GAUSSIAN_C, // use gaussian rather than mean, seems to give better results
cv::THRESH_BINARY_INV, // invert so foreground will be white, background will be black
11, // size of a pixel neighborhood used to calculate threshold value
2); // constant subtracted from the mean or weighted mean
cv::imshow("imgThresh", imgThresh); // show threshold image for reference
imgThreshCopy = imgThresh.clone(); // make a copy of the thresh image, this in necessary b/c findContours modifies the image
cv::findContours(imgThreshCopy, // input image, make sure to use a copy since the function will modify this image in the course of finding contours
ptContours, // output contours
v4iHierarchy, // output hierarchy
cv::RETR_EXTERNAL, // retrieve the outermost contours only
cv::CHAIN_APPROX_SIMPLE); // compress horizontal, vertical, and diagonal segments and leave only their end points
for (int i = 0; i < ptContours.size(); i++) { // for each contour
if (cv::contourArea(ptContours[i]) > MIN_CONTOUR_AREA) { // if contour is big enough to consider
cv::Rect boundingRect = cv::boundingRect(ptContours[i]); // get the bounding rect
cv::rectangle(imgTrainingNumbers, boundingRect, cv::Scalar(0, 0, 255), 2); // draw red rectangle around each contour as we ask user for input
cv::Mat matROI = imgThresh(boundingRect); // get ROI image of bounding rect
cv::Mat matROIResized;
cv::resize(matROI, matROIResized, cv::Size(RESIZED_IMAGE_WIDTH, RESIZED_IMAGE_HEIGHT)); // resize image, this will be more consistent for recognition and storage
cv::imshow("matROI", matROI); // show ROI image for reference
cv::imshow("matROIResized", matROIResized); // show resized ROI image for reference
cv::imshow("imgTrainingNumbers", imgTrainingNumbers); // show training numbers image, this will now have red rectangles drawn on it
int intChar = cv::waitKey(0); // get key press
if (intChar == 27) { // if esc key was pressed
return(0); // exit program
}
else if (std::find(intValidChars.begin(), intValidChars.end(), intChar) != intValidChars.end()) { // else if the char is in the list of chars we are looking for . . .
matClassificationInts.push_back(intChar); // append classification char to integer list of chars
cv::Mat matImageFloat; // now add the training image (some conversion is necessary first) . . .
matROIResized.convertTo(matImageFloat, CV_32FC1); // convert Mat to float
cv::Mat matImageFlattenedFloat = matImageFloat.reshape(1, 1); // flatten
matTrainingImagesAsFlattenedFloats.push_back(matImageFlattenedFloat); // add to Mat as though it was a vector, this is necessary due to the
// data types that KNearest.train accepts
} // end if
} // end if
} // end for
std::cout << "training complete\n\n";
// save classifications to file ///////////////////////////////////////////////////////
cv::FileStorage fsClassifications("classifications.xml", cv::FileStorage::WRITE); // open the classifications file
if (fsClassifications.isOpened() == false) { // if the file was not opened successfully
std::cout << "error, unable to open training classifications file, exiting program\n\n"; // show error message
return(0); // and exit program
}
fsClassifications << "classifications" << matClassificationInts; // write classifications into classifications section of classifications file
fsClassifications.release(); // close the classifications file
// save training images to file ///////////////////////////////////////////////////////
cv::FileStorage fsTrainingImages("images.xml", cv::FileStorage::WRITE); // open the training images file
if (fsTrainingImages.isOpened() == false) { // if the file was not opened successfully
std::cout << "error, unable to open training images file, exiting program\n\n"; // show error message
return(0); // and exit program
}
fsTrainingImages << "images" << matTrainingImagesAsFlattenedFloats; // write training images into images section of images file
fsTrainingImages.release(); // close the training images file
return(0);
}
first you have an error in
int intChar = cv::waitKey(0);
it must be
char intChar = cv::waitKey(0);
try it and if ther are error tell me
I'm trying to modify and write some video using openCV 2.4.6.1 using the following code:
cv::VideoCapture capture( video_filename );
// Check if the capture object successfully initialized
if ( !capture.isOpened() )
{
printf( "Failed to load video, exiting.\n" );
return -1;
}
cv::Mat frame, cropped_img;
cv::Rect ROI( OFFSET_X, OFFSET_Y, WIDTH, HEIGHT );
int fourcc = static_cast<int>(capture.get(CV_CAP_PROP_FOURCC));
double fps = 30;
cv::Size frame_size( RADIUS, (int) 2*PI*RADIUS );
video_filename = "test.avi";
cv::VideoWriter writer( video_filename, fourcc, fps, frame_size );
if ( !writer.isOpened() && save )
{
printf("Failed to initialize video writer, unable to save video!\n");
}
while(true)
{
if ( !capture.read(frame) )
{
printf("Failed to read next frame, exiting.\n");
break;
}
// select the region of interest in the frame
cropped_img = frame( ROI );
// display the image and wait
imshow("cropped", cropped_img);
// if we are saving video, write the unwrapped image
if (save)
{
writer.write( cropped_img );
}
char key = cv::waitKey(30);
When I try to run the output video 'test.avi' with VLC I get the following error: avidemux error: no key frame set for track 0. I'm using Ubuntu 13.04, and I've tried using videos encoded with MPEG-4 and libx264. I think the fix should be straightforward but can't find any guidance. The actual code is available at https://github.com/benselby/robot_nav/tree/master/video_unwrap. Thanks in advance!
[PYTHON] Apart from the resolution mismatch, there can also be a frames-per-second mismatch. In my case, the resolution was correctly set, but the problem was with fps. Checking the frames per second at which VideoCapture object was reading, it showed to be 30.0, but if I set the fps of VideoWriter object to 30.0, the same error was being thrown in VLC. Instead of setting it to 30.0, you can get by with the error by setting it to 30.
P.S. You can check the resolution and the fps at which you are recording by using the cap.get(3) for width, cap.get(4) for height and cap.get(5) for fps inside the capturing while/for loop.
The full code is as follows:
import numpy as np
import cv2 as cv2
cap = cv2.VideoCapture(0)
#Define Codec and create a VideoWriter Object
fourcc = cv2.VideoWriter_fourcc('X','V','I','D')
#30.0 in the below line doesn't work while 30 does work.
out = cv2.VideoWriter('output.mp4', fourcc, 30, (640, 480))
while(True):
ret, frame = cap.read()
colored_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
print('Width = ', cap.get(3),' Height = ', cap.get(4),' fps = ', cap.get(5))
out.write(colored_frame)
cv2.imshow('frame', colored_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
The full documentation (C++) for what all properties can be checked is available here : propId OpenCV Documentation
This appears to be an issue of size mismatch between the frames written and the VideoWriter object opened. I was running into this issue when trying to write a series of resized images from my webcam into a video output. When I removed the resizing step and just grabbed the size from an initial test frame, everything worked perfectly.
To fix my resizing code, I essentially ran a single test frame through my processing and then pulled its size when creating the VideoWriter object:
#include <cassert>
#include <iostream>
#include <time.h>
#include "opencv2/opencv.hpp"
using namespace cv;
int main()
{
VideoCapture cap(0);
assert(cap.isOpened());
Mat testFrame;
cap >> testFrame;
Mat testDown;
resize(testFrame, testDown, Size(), 0.5, 0.5, INTER_NEAREST);
bool ret = imwrite("test.png", testDown);
assert(ret);
Size outSize = Size(testDown.cols, testDown.rows);
VideoWriter outVid("test.avi", CV_FOURCC('M','P','4','2'),1,outSize,true);
assert(outVid.isOpened());
for (int i = 0; i < 10; ++i) {
Mat frame;
cap >> frame;
std::cout << "Grabbed frame" << std::endl;
Mat down;
resize(frame, down, Size(), 0.5, 0.5, INTER_NEAREST);
//bool ret = imwrite("test.png", down);
//assert(ret);
outVid << down;
std::cout << "Wrote frame" << std::endl;
struct timespec tim, tim2;
tim.tv_sec = 1;
tim.tv_nsec = 0;
nanosleep(&tim, &tim2);
}
}
My guess is that your problem is in the size calculation:
cv::Size frame_size( RADIUS, (int) 2*PI*RADIUS );
I'm not sure where your frames are coming from (i.e. how the capture is set up), but likely in rounding or somewhere else your size gets messed up. I would suggest doing something similar to my solution above.