Related
I have written a simple code to perform canny edge detection on a live stream. The code is as shown below,
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int lowThreshold=0;
int const max_lowThreshold = 100;
int kernel_size = 3;
int ratio = 3;
Mat img;
Mat display;
void CannyThreshold()
{
cvtColor(img, display, COLOR_RGB2GRAY);
// GaussianBlur(display,display,Size(7,7),3,3);
GaussianBlur(display, display, Size(1, 1), 1,1);
printf("%d\n",lowThreshold);
Canny(display,display,lowThreshold,3);
imshow("Canny",display);
}
int main()
{
VideoCapture cap(0);
namedWindow("Canny");
createTrackbar("Min Threshold: ","Canny",&lowThreshold,max_lowThreshold);
while(1)
{
cap.read(img);
int ret = waitKey(1);
CannyThreshold();
if(ret == 'q')
break;
}
cap.release();
return 0;
}
I get the following run-time error when I run the code. (I'm using OpenCV 4)
error: (-215:Assertion failed) ksize.width > 0 && ksize.width % 2 == 1 && ksize.height > 0 && ksize.height % 2 == 1 in function 'createGaussianKernels'
Any suggestions on how I can solve this error?
The issue is GaussianBlur cant accept kernel size of 1. Correct it to 3x3 or 5x5 in your code as follows
#include <opencv2/core/utility.hpp>
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
#include <ctype.h>
using namespace cv;
using namespace std;
int main(int argc, const char** argv)
{
VideoCapture cap;
Mat frame;
Mat image; // from cap to image
Mat src_gray;
Mat dst;
Mat detected_edges;
const String window_name = "Canny Edge Detector - VideoCapture";
int lowThreshold = 10;
const int max_lowThreshold = 100;
const int ratio = 3;
const int kernel_size = 3;
int FPS;
int frame_width, frame_height;
int camNum = 0;
cap.open(camNum);
if (!cap.isOpened())
{
cout << "***Could not initialize capturing...***\n";
cout << "Current parameter's value: \n";
return -1;
}
FPS = cap.get(CAP_PROP_FPS);
frame_width = cap.get(CAP_PROP_FRAME_WIDTH);
frame_height = cap.get(CAP_PROP_FRAME_HEIGHT);
dst.create(frame_width, frame_height, CV_8UC3);
//cout << CV_8UC3;
while (true)
{
cap >> frame;
if (frame.empty())
break;
frame.copyTo(image);
// Convert the image to grayscale
cvtColor(image, src_gray, COLOR_BGR2GRAY);
//![reduce_noise]
/// Reduce noise with a kernel 3x3
blur(src_gray, detected_edges, Size(3, 3));
//![reduce_noise]
//![canny]
/// Canny detector
Canny(detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size);
//![canny]
/// Using Canny's output as a mask, we display our result
//![fill]
dst = Scalar::all(0);
//![fill]
//![copyto]
image.copyTo(dst, detected_edges);
//![copyto]
//![display]
imshow(window_name, dst);
if (waitKey(1000 / FPS) >= 0)
break;
}
return 0;
}
I am using the OpenCV LK Optical Flow example, for some robot vision application with ROS.
It seems to be working reasonably well, however, I am having the error below:
OpenCV Error: Assertion failed ((npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0) in calc, file /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/video/src/lkpyramid.cpp, line 1231
terminate called after throwing an instance of 'cv::Exception'
what(): /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/video/src/lkpyramid.cpp:1231: error: (-215) (npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0 in function calc
Aborted (core dumped)
As far I could understand by reading the error message, and by the system behavior, it is happening when all of the initial tracking points are gone in the current frame.
There would be a way to restart this process (defining new points) instead of aborting?
My code by the way:
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/video.hpp>
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
using namespace cv;
using namespace std;
static const std::string OPENCV_WINDOW = "Image_window";
class ImageConverter
{
ros::NodeHandle nh_;
image_transport::ImageTransport it_;
image_transport::Subscriber image_sub_;
image_transport::Publisher image_pub_;
public:
cv::Mat current_frame;
public:
ImageConverter()
: it_(nh_)
{
image_sub_ = it_.subscribe("/realsense/color/image_raw", 1,
&ImageConverter::imageCallback, this);
cv::namedWindow(OPENCV_WINDOW);
}
~ImageConverter()
{
cv::destroyWindow(OPENCV_WINDOW);
}
void imageCallback(const sensor_msgs::ImageConstPtr& msg)
{
cv_bridge::CvImagePtr cv_image;
try
{
cv_image = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
current_frame = cv_image->image;
}
catch (cv_bridge::Exception& e)
{
ROS_ERROR("cv_bridge exception: %s", e.what());
return;
}
}
};
int main(int argc, char** argv)
{
ros::init(argc, argv, "OpenCV_Node");
ImageConverter MyNode;
ros::Rate r(30);
r.sleep();
ros::spinOnce();
r.sleep();
// Tentative of Using Open CV Tools with a ROS Image
// Lucas-Kanade Optical Flow from: https://docs.opencv.org/master/d4/dee/tutorial_optical_flow.html
// Create some random colors
vector<Scalar> colors;
RNG rng;
for(int i = 0; i < 100; i++)
{
int r = rng.uniform(0, 256);
int g = rng.uniform(0, 256);
int b = rng.uniform(0, 256);
colors.push_back(Scalar(r,g,b));
}
Mat old_frame, old_gray;
vector<Point2f> p0, p1;
// Take first frame and find corners in it
old_frame = MyNode.current_frame;
cvtColor(old_frame, old_gray, COLOR_BGR2GRAY);
goodFeaturesToTrack(old_gray, p0, 100, 0.3, 7, Mat(), 7, false, 0.04);
// Create a mask image for drawing purposes
Mat mask = Mat::zeros(old_frame.size(), old_frame.type());
//while(true){
while(ros::ok()){
r.sleep();
Mat frame, frame_gray;
frame = MyNode.current_frame;
if (frame.empty())
break;
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
// calculate optical flow
vector<uchar> status;
vector<float> err;
TermCriteria criteria = TermCriteria((TermCriteria::COUNT) + (TermCriteria::EPS), 10, 0.03);
calcOpticalFlowPyrLK(old_gray, frame_gray, p0, p1, status, err, Size(15,15), 2, criteria);
vector<Point2f> good_new;
for(uint i = 0; i < p0.size(); i++)
{
// Select good points
if(status[i] == 1) {
good_new.push_back(p1[i]);
// draw the tracks
line(mask,p1[i], p0[i], colors[i], 2);
circle(frame, p1[i], 5, colors[i], -1);
}
}
Mat img;
add(frame, mask, img);
imshow(OPENCV_WINDOW, img);
int keyboard = waitKey(30);
if (keyboard == 'q' || keyboard == 27)
break;
// Now update the previous frame and previous points
old_gray = frame_gray.clone();
p0 = good_new;
ros::spinOnce();
}
return 0;
}
I want to draw a circle on an image given 3 mouse clicks.
What I mean is that I have an image and when I click 3 times on this image the circle is drawn. In addition I already have the code to find the parameters of the circle given 3 points.
Mat cember_denklemi(Point2f A,Point2f B,Point2f C) {
double W[3][3]={{A.x,A.y,1},
{B.x,B.y,1},
{C.x,C.y,1}};
double T[3][1]={-(A.x*A.x+A.y*A.y),
-(B.x*B.x+B.y*B.y),
-(C.x*C.x+C.y*C.y)};
Mat M=Mat(3,3,CV_64F,W);
Mat N=Mat(3,1,CV_64F,T);
Mat L=M.inv()*N;
return L;
}
And this my main:
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
#include <iostream>
using namespace std;
using namespace cv;
void mouseKordinat(int evt, int x, int y, int flags, void* ptr);
void getPixelValue(Mat img, int x, int y);
Mat image;
int main() {
image = imread("c:/opencv2.4.6./atlas15.jpg");
imshow("MyWindow", image);
setMouseCallback("MyWindow", mouseKordinat, 0 );
waitKey(0);
return 0;
}
void mouseKordinat(int evt, int c, int r, int flags, void* ptr) {
if(evt==CV_EVENT_LBUTTONDOWN) {
getPixelValue(image,r,c);
}
}
void getPixelValue(Mat img, int r, int c) {
Vec3b pix=img.at<Vec3b>(r,c);
int B = pix.val[0];
int G = pix.val[1];
int R = pix.val[2];
cout<<"Row:"<<r<<" "<<"Column:"<<c<<" - "; // mouse kordinatlari
cout<<"B:"<<B<<" "<<"G:"<<G<<" "<<"R:"<<R<<"\n"; // kordinatın pixel değerleri
}
I can't combine these cods; but I hve done it
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
#include <iostream>
using namespace std;
using namespace cv;
static int mouse_x = -1;
static int mouse_y = -1;
Mat circle_eq(Point2f A,Point2f B,Point2f C)
{
double W[3][3]={{A.x,A.y,1},
{B.x,B.y,1},
{C.x,C.y,1}};
double T[3][1]={-(A.x*A.x+A.y*A.y),
-(B.x*B.x+B.y*B.y),
-(C.x*C.x+C.y*C.y)};
Mat M=Mat(3,3,CV_64F,W);
Mat N=Mat(3,1,CV_64F,T);
Mat L=M.inv()*N;
return L;
}
void mouseKordinat(int evt, int x, int y, int flags, void* ptr)
{
if(evt == CV_EVENT_MOUSEMOVE)
{
mouse_x = x;
mouse_y = y;
}
int tik;
tik = tik + 1;
}
void getPixelValue(Mat img, int x, int y);
Mat image;
int main()
{
image = imread("c:/qwz/1q.jpg");
imshow("MyWindow", image);
setMouseCallback("MyWindow", mouseKordinat, 0 );
tik = tik%=3;
if(tik==0)
{
int merkez=circle_eq(nokta(0), nokta(1), nokta(2));
circle(image,merkez,3, Scalar(255,255,255),-1,8, nokta(0));
}
if(tik>3)
{
int nokta[0][0]=0;
}
waitKey(0);
return 0;
}
void mouseKordinat(int evt, int c, int r, int flags, void* ptr)
{
if(evt==CV_EVENT_LBUTTONDOWN)
{
getPixelValue(image,r,c);
}
}
void getPixelValue(Mat img, int r, int c)
{
Vec3b pix=img.at<Vec3b>(r,c);
int B = pix.val[0];
int G = pix.val[1];
int R = pix.val[2];
cout<<"Row:"<<r<<" "<<"Column:"<<c<<" - ";
cout<<"B:"<<B<<" "<<"G:"<<G<<" "<<"R:"<<R<<"\n";
}
I am trying to take difference of center pixel with 4 neighbor and add them and then replace the original with that difference value. but it always replace pixel with zero. I don't what i am doing wrong. thanks for any help
// newproject.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "highgui.h"
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <conio.h>
#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <conio.h>
using namespace cv;
using namespace std;
class frameprocessing{
Mat hsv_base;
MatND hist_base;
public:
void whatever(Mat Frame)
{
for(int i=0;i<Frame.cols;i++)
for(int j=0;j<Frame.rows;j++)
{
if(i==0&&j==0)
{
// cout<<"Check 1"<<endl;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i)-(Frame.at<Vec3b>(j+1,i))+(Frame.at<Vec3b>(j,i)-Frame.at<Vec3b>(j,i+1))+(Frame.at<Vec3b>(j,i)-Frame.at<Vec3b>(j+1,i))+(Frame.at<Vec3b>(j,i)-Frame.at<Vec3b>(j,i+1)));
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[0]/4;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[1]/4;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[2]/4;
}
else if(i==Frame.cols-1&&j==Frame.rows-1)
{
// cout<<"Check 2"<<endl;
Frame.at<Vec3b>(j,i)=((Frame.at<Vec3b>(j-1,i)-Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j,i-1)+Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j-1,i)-Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j,i-1)+Frame.at<Vec3b>(j,i)));
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[0]/4;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[1]/4;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[2]/4;
}
else if(i==Frame.cols-1&&j==0)
{
//cout<<"Check 3"<<endl;
Frame.at<Vec3b>(j,i)=((Frame.at<Vec3b>(j+1,i)-Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j,i-1)+Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j+1,i)-Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j,i-1)+Frame.at<Vec3b>(j,i)));
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[0]/4;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[1]/4;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[2]/4;
}
else if(i==0&&j==Frame.rows-1)
{
// cout<<"Check 4"<<endl;
Frame.at<Vec3b>(j,i)=((Frame.at<Vec3b>(j-1,i)-Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j,i+1)+Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j-1,i)-Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j,i+1)+Frame.at<Vec3b>(j,i)));
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[0]/4;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[1]/4;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[2]/4;
}
else if(i==0)
{
// cout<<"Check 5"<<endl;
Frame.at<Vec3b>(j,i)=((Frame.at<Vec3b>(j+1,i)-Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j,i+1)-Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j,i+1)-Frame.at<Vec3b>(j,i))+((Frame.at<Vec3b>(j-1,i)-Frame.at<Vec3b>(j,i))));
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[0]/4;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[1]/4;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[2]/4;
}
else if(j==0)
{
// cout<<"Check 6"<<endl;
Frame.at<Vec3b>(j,i)=((Frame.at<Vec3b>(j+1,i)-Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j+1,i)-Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j,i+1)-Frame.at<Vec3b>(j,i)+(Frame.at<Vec3b>(j,i-1)-Frame.at<Vec3b>(j,i))));
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[0]/4;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[1]/4;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[2]/4;
}
else if(i==Frame.cols-1)
{
// cout<<"Check 7"<<endl;
Frame.at<Vec3b>(j,i)=((Frame.at<Vec3b>(j+1,i)-Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j-1,i)-Frame.at<Vec3b>(j,i))+((Frame.at<Vec3b>(j,i-1)-Frame.at<Vec3b>(j,i)))+((Frame.at<Vec3b>(j,i-1)-Frame.at<Vec3b>(j,i))));
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[0]/4;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[1]/4;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[2]/4;
}
else if(j==Frame.rows-1)
{
// cout<<"Check 8"<<endl;
Frame.at<Vec3b>(j,i)=((Frame.at<Vec3b(j,i+1)-Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j-1,i)-Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j-1,i)-Frame.at<Vec3b>(j,i))+((Frame.at<Vec3b>(j,i-1)-Frame.at<Vec3b>(j,i))));
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[0]/4;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[1]/4;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[2]/4;
}
else
{
Frame.at<Vec3b>(j,i)=((Frame.at<Vec3b>(j-1,i)-Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j+1,i)-Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j,i+1)-Frame.at<Vec3b>(j,i))+(Frame.at<Vec3b>(j,i-1)+Frame.at<Vec3b>(j,i)));
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[0]/4;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[1]/4;
Frame.at<Vec3b>(j,i)=(Frame.at<Vec3b>(j,i))[2]/4;
Vec3d value = Frame.at<Vec3b>(j,i);
cout<<value[0]<<endl;
cout<<value[1]<<endl;
cout<<value[2]<<endl;
}
}
//hell(Frame);
}
};
class video{
Mat frame;
string filename;
double dWidth;
double dHeight;
public:
video()
{
}
video(string videoname)
{
vector<Mat> videoframes;
filename = videoname;
VideoCapture capture(filename);
if( !capture.isOpened() )
{
exit(0);
}
dWidth = capture.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
dHeight = capture.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
frameprocessing obj;
for( ; ; )
{
capture >> frame;
if(frame.empty())
break;
// Mat tmp=frame.clone();
obj.whatever(frame);
// obj.hsv_histogram(frame);
// videoframes.push_back(tmp);
}
//displayvideo(videoframes);
//writer(videoframes);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
video obj("video.avi");
It seems you need Lapacian with kernel size parameter = 1, see here: http://docs.opencv.org/modules/imgproc/doc/filtering.html?highlight=laplacian#laplacian
You just need divide result by 4 after convolution for correct Laplacian.
As for your code, I think you should use different images as source and destination, you cant do this inplace.
class frameprocessing{
Mat hsv_base;
MatND hist_base;
public:
void whatever(Mat Frame)
{
Frame.convertTo(Frame,CV_32FC3);
Mat newimage=Mat::zeros(Frame.size(),CV_32FC3);
for (int j=1;j<Frame.rows-1;++j)
{
for (int i=1;i<Frame.cols-1;++i)
{
newimage.at<Vec3f>(j,i)=Frame.at<Vec3f>(j,i)-
0.25*Frame.at<Vec3f>(j+1,i)-
0.25*Frame.at<Vec3f>(j,i+1)-
0.25*Frame.at<Vec3f>(j-1,i)-
0.25*Frame.at<Vec3f>(j,i-1);
cout << newimage.at<Vec3f>(j,i) << endl;
}
}
//imshow("result",newimage);
//cv::waitKey(30);
}
};
class video{
Mat frame;
string filename;
double dWidth;
double dHeight;
public:
video()
{
}
video(string videoname)
{
vector<Mat> videoframes;
filename = videoname;
VideoCapture capture(filename);
if( !capture.isOpened() )
{
exit(0);
}
dWidth = capture.get(cv::CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
dHeight = capture.get(cv::CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
frameprocessing obj;
for( ; ; )
{
capture >> frame;
if(frame.empty())
break;
// Mat tmp=frame.clone();
obj.whatever(frame);
// obj.hsv_histogram(frame);
// videoframes.push_back(tmp);
}
//displayvideo(videoframes);
//writer(videoframes);
}
};
int main(int argc, char* argv[])
{
namedWindow("result");
video obj("D:\\ImagesForTest\\atrium.avi");
}
I am trying to display video for in a separate function for this i am using vector i push_back each frame in and then pass this vector to function but my function displays a single frame repetitively. My code is below. Please tell me what i am doing wrong.
// newproject.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "highgui.h"
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <conio.h>
#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <conio.h>
using namespace cv;
using namespace std;
class frameprocessing{
Mat hsv_base;
MatND hist_base;
public:
void hsv_histogram(Mat Frame)
{
cvtColor( Frame, hsv_base, CV_BGR2HSV );
int h_bins = 50;
int s_bins = 32;
int histSize[] = { h_bins, s_bins };
float h_ranges[] = { 0, 256 };
float s_ranges[] = { 0, 180 };
const float* ranges[] = { h_ranges, s_ranges };
int channels[] = { 0, 1 };
calcHist( &hsv_base, 1, channels, Mat(), hist_base, 2, histSize, ranges, true, false );
}
};
class video{
Mat frame;
string filename;
double dWidth;
double dHeight;
public:
video()
{
}
video(string videoname)
{
vector<Mat> videoframes;
std::vector<Mat>::iterator it;
it = videoframes.begin();
filename = videoname;
VideoCapture capture(filename);
if( !capture.isOpened() )
{
exit(0);
}
dWidth = capture.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
dHeight = capture.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
frameprocessing obj;
for( ; ; )
{
capture >> frame;
if(frame.empty())
break;
obj.hsv_histogram(frame);
videoframes.push_back(frame);
}
displayvideo(videoframes);
// waitKey(0); // key press to close window
}
void writer()
{
Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
VideoWriter oVideoWriter ("D:/MyVideo.avi", CV_FOURCC('P','I','M','1'), 20, frameSize, true); //initialize the VideoWriter object
if ( !oVideoWriter.isOpened() ) //if not initialize the VideoWriter successfully, exit the program
{
cout << "ERROR: Failed to write the video" << endl;
exit(0);
}
}
void displayvideo(vector<Mat> videoframe)
{
Mat tempframe;
while(!videoframe.empty()) //Show the image captured in the window and repeat
{
tempframe = videoframe.back();
imshow("video", tempframe);
videoframe.pop_back();
waitKey(20); // waits to display frame
}
// waitKey(0);
}
void displayframe(Mat frame)
{
imshow("video", frame);
waitKey(20); // waits to display frame
}
};
int _tmain(int argc, _TCHAR* argv[])
{
video obj("video.avi");
//obj.readvideo();
}
You need to copy or clone your frame to another Mat then push to your vector, change your code like
for( ; ; )
{
capture >> frame;
if(frame.empty())
break;
Mat tmp=frame.clone();
obj.hsv_histogram(tmp);
videoframes.push_back(tmp);
}
In your code your passing the same pointer allocated for frame to your vector every time, so you will get array of Mat pointing single(same) memory location in your vector. To know more about OpenCV Mat and memory allocation See documantation