I want to read a sequence of frames from any folder using openCV. All frames are in sequence i.e. (1).jpg,(2).jpg,....
I tried
VideoCapture cap;
cap.open("Directory/");
for(;;)
{
Mat frame;
cap >> frame;
}
but it doesn't work.
This question has been asked before but i don't know why this answer doesn't work for me.
OpenCV: Reading image series from a folder
do i need to rename the images?.
cap open should be cap.open("Directory/(%02d).jpg"); and you have to rename your images so that they look like (01).jpg,(02).jpg etc so that they have fixed length. if the images are like (001).jpg then you should use `cap.open("Directory/(%03d).jpg");
edit
#include "opencv2/opencv.hpp"
using namespace cv;
int main()
{
VideoCapture cap;
cap.open("imgs/(%02d).jpg");
int i=0;
for(;;)
{
if(i++%37==0)cap=VideoCapture("imgs/(%02d).jpg");//there are 37 frames in the dir
Mat frame;
cap >> frame;
imshow("frame",frame);
if(waitKey(1)==27)
exit(0);
}
return 0;
}
Try to prepare an xml/yaml file with list of names with path to the images, in the desired order. Then load the list as a vector or some resembling structure, and then open them one by one in a loop.
Here the full code to do read a sequence of frames with five zeros in name "frame00000.jpg, frame00001.jpg,.....,frame00010.jpg...) using string concatination idea just like matlab.
#include "stdafx.h"
#include <stdlib.h>
#include <math.h>
#include <opencv/cv.h> // include it to used Main OpenCV functions.
#include <opencv/highgui.h> //include it to use GUI functions.
using namespace std;
using namespace cv;
string intToStr(int i,string path){
string bla = "00000";
stringstream ss;
ss<<i;
string ret ="";
ss>>ret;
string name = bla.substr(0,bla.size()-ret.size());
name = path+name+ret+".jpg";
return name;
}
int main(int, char**)
{
string previous_window = "Previous frame";
string current_window = "Current frame ";
int i=0;
for(int i = 1 ; i< 10 ; i++)
{
Mat Current, Previous;
string Curr_name = intToStr(i,"D:/NU/Junior Scientist/Datasets/egtest04/frame");
string Prev_name = intToStr(i-1,"D:/NU/Junior Scientist/Datasets/egtest04/frame");
Current = imread(Curr_name,1);
Previous = imread(Prev_name,1);
namedWindow(current_window,WINDOW_AUTOSIZE);
namedWindow(current_window,WINDOW_AUTOSIZE);
imshow(current_window,Current);
imshow(previous_window,Previous);
waitKey(0);
}
}
Where "D:/NU/Junior Scientist/Datasets/egtest04/frame" is the path sting.
Related
I am using darknet to detect objects from live video stream and want to pass each frame to dlib for tracking that object but i'm confused that how i pass frames from darknet's demo.c to dlib and do the tracking.
Do i need to use c c++ connector ? if yes, how? any explanation or clues would be helpful.
thanks...
Dlib file where i want to pass ipl image and want to track the object.
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/dir_nav.h>
#include "dlib/opencv.h"
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace dlib;
using namespace std;
extern "C"{
int enx = 203, int eny = 190, int enw = 98, int enh = 86;
void track(IplImage * ipl, int enx, int eny, int enw, int enh)
{
Mat frame = cvarrToMat(ipl);
image_window win;
correlation_tracker tracker;
array2d<rgb_pixel> img;
std::cout << "Starting" << std::endl;
assign_image(img, cv_image<bgr_pixel>(frame));
tracker.start_track(img, centered_rect(point(enx, eny), enw, enh));
win.set_image(img);
win.clear_overlay();
win.add_overlay(tracker.get_position());
while(ipl) {
Mat frame = cvarrToMat(ipl);
assign_image(img, cv_image<bgr_pixel>(frame));
tracker.update(img);
win.set_image(img);
win.clear_overlay();
win.add_overlay(tracker.get_position());
}
}
}
I would recommend you to look at https://github.com/AlexeyAB/darknet/blob/master/src/yolo_console_dll.cpp
where you will find many ideas about darknet data manipulation. Including tracker
I am training out the tutorial in opencv.
it had no error when compile.
I know the code for the tutorial is for opencv2.4 and I had change the coding for cvquery and videoframe.
My output is like this
.
My webcam is working fine but it not showing anything in my result.
If you wish to perform face detection using HaarCascades, you can use this code:
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
CascadeClassifier facecascade;
int main()
{
Mat frame;
facecascade.load("haarcascade_frontalface_alt.xml");
if(facecascade.empty())
{
cout<<"Error";
}
VideoCapture cap(0);
int i=0,j=0,k=0;
while(1)
{
cap>>frame;
Mat frame_gray;
cvtColor(frame,frame_gray,CV_BGR2GRAY);
vector<Rect>faces;
facecascade.detectMultiScale(frame_gray,faces,1.1,2,0|CV_HAAR_SCALE_IMAGE,Size(70,70));
if(faces.size()>0)
{
for(i=0;i<faces.size();i++)
{
rectangle(frame_gray,faces[i],Scalar(200,200,250),2,8,0);
}
char no[5];
sprintf(no,"No. of faces detected = %d",int(faces.size()));
putText(frame_gray,no,Point(10,30),FONT_HERSHEY_TRIPLEX,1,Scalar(255,255,255),1);
imshow("out",frame_gray);
char c= waitKey(5);
if(c=='b')
break;
}
return 0;
}
i'am tray to print a strings from user to screen the problem i faced that i nedd when user insert (enter)key the program go to new line and user still have the ability to print on screen , but when i insert (enter)the program go to the first line and overwrite the old words here is my code
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
int len;
char searsh_word[10];
char ch;
printf("enter your strings\n");
while(ch!='EOF'){
ch=getch();
printf("%c",ch);
}
puts("\nEnter the word you need to search for : ");
scanf("%s",searsh_word);
len=strlen(searsh_word);
printf("your word length is : %d",len);
return 0;
}
the problem was by useing getch() you can use grtchar() for better result
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
FILE *file;
int main()
{
int len;
char searsh_word[10];
char ch;
file=fopen("test.txt","w");
if(!file){
printf("Error opening file\n");
exit(1);
}else{
printf("enter your strings\n");
}
do{
ch=getchar();
fprintf(file,"%c",ch);
}while(ch!='.');
fclose(file);
puts("\nEnter the word you need to search for : ");
scanf("%s",searsh_word);
len=strlen(searsh_word);
printf("your word length is : %d",len);
return 0;
}
All my OpenCV functions are working perfectly fine. But cvResize() is not found by the compiler. I guess this function is not installed during installation.
The following program tells me the error that cvResize identifier is undefined
Is it possible to download this function separately and use it? How?
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <ctype.h>
#include <iostream>
using namespace std;
int main( int argc, char** argv )
{
// Create an IplImage object *image
IplImage *source = cvLoadImage( argv[1]);
// Here we retrieve a percentage value to a integer
int percent = atoi(argv[3]);
// declare a destination IplImage object with correct size, depth and channels
IplImage *destination = cvCreateImage
( cvSize((int)((source->width*percent)/100) , (int)((source->height*percent)/100) ), source->depth, source->nChannels );
//use cvResize to resize source to a destination image
cvResize(source, destination);
// save image with a name supplied with a second argument
cvSaveImage( argv[2], destination );
return 0;
}
You are missing an include:
#include "opencv2/imgproc/imgproc_c.h"
I fixed the error by
#import <opencv2/imgproc/imgproc_c.h>
I am using Background Subtraction and want to display the contents. Somehow the code seems to break all the time due to a memory exception. The error seems to be in cvCopy function. Can't figure it out.
#include "cv.h"
#include "highgui.h"
#include "opencv2\core\operations.hpp"
#include "opencv2\core\core.hpp"
#include "opencv2\core\types_c.h"
#include "opencv\cxcore.h"
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int, char**)
{
bool flag=0;
VideoCapture cap(0); // open the default camera
VideoCapture cap1(0);
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat gray,bg,result,frame,result1,final,frame1;
//CvMemStorage* contours = NULL;
cap>>frame;
cvtColor(frame,bg,CV_BGR2GRAY);
namedWindow("GRAY",1);
for(;;)
{
//final = Mat::zeros(mGreenScale.rows, mGreenScale.cols, CV_8UC3);
cap >> frame; // get a new frame from camera
cap1 >> frame1;
cvtColor(frame, gray, CV_BGR2GRAY);
absdiff(gray,bg,result);
threshold(result,result1,50,255,THRESH_BINARY);
//cvCopy(const CvArr* src, CvArr* dst, const CvArr* mask=NULL)ΒΆ
//cvCopy(&frame1, &final, &result1);
//findContours(result1,contours, ;CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
//drawContours(final,contours,-1,CV_RGB(0,255,0));
//imshow("GRAY",result1);
//imshow("GRAY", result);
imshow("GRAY1",final);
if(flag)
{
imshow("BG",bg);
}
//if(waitKey(0)==27) break;
if(waitKey(1)==32)
{
cvtColor(frame,bg,CV_BGR2GRAY);
flag=!flag;
}
if(waitKey(1)==27)
{
break;
}
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
Instead of mixing the C and C++ APIs I would recommend you stick to the C++ API where possible. If you merely want to copy a matrix, just use either Mat::clone() or Mat::copyTo(). Since you want to use a mask, use the copyTo member function like this:
Mat final;
frame1.copyTo(final, result1);
Hope that helps!