Ask Your Question
0

Convert the old code opencv 2.0 C++ visual studio to new api of opencv c++.

asked 2019-10-31 08:50:44 -0500

Gilal Nauman gravatar image

updated 2019-10-31 12:06:26 -0500

supra56 gravatar image

here is my code:

#include <stdio.h>
#include "cv.h"
#include <cxcore.h>
#include <highgui.h>
int main()
{
IplImage* frame = NULL;
IplImage* frame_img = NULL;
IplImage* background_img = NULL;
IplImage* temp_img1 = NULL;
CvMat* temp_mat1 = NULL;
CvMat* frame_mat = NULL;
CvMat* background_mat = NULL;
CvCapture* capture = NULL;
int frame_num;
cvNamedWindow("Actual_video", CV_WINDOW_AUTOSIZE);
cvNamedWindow("background_frames",CV_WINDOW_AUTOSIZE);
cvNamedWindow("foreground_frames", CV_WINDOW_AUTOSIZE);
cvMoveWindow("Actual_video", 30, 0);
cvMoveWindow("background_frames", 360, 0);
cvMoveWindow("foreground_frames", 360, 0);
capture = cvCreateFileCapture("sc123.avi");
for(frame_num=1; frame_num<=2 ;frame_num++)
{
frame = cvQueryFrame( capture );
if(frame_num == 1)
{
background_img = cvCreateImage(cvSize(frame->width, frame->height),
IPL_DEPTH_8U,1);
frame_img = cvCreateImage(cvSize(frame->width, frame->height),
IPL_DEPTH_8U,1);
background_mat = cvCreateMat(frame->height, frame->width, CV_32FC1);
frame_mat = cvCreateMat(frame->height, frame->width, CV_32FC1);
temp_mat1 = cvCreateMat(frame->height, frame->width, CV_32FC1);
cvCvtColor(frame, frame_img, CV_BGR2GRAY);
cvConvert(frame_img, frame_mat);
cvConvert(frame_img, background_mat);
cvSobel(frame_mat,background_mat,1,0,3);
cvThreshold(background_mat, background_mat, 10, 255.0,
CV_THRESH_BINARY);
}
else
{
temp_img1 = cvCreateImage(cvSize(frame->width, frame->height),
IPL_DEPTH_8U,1);
cvCvtColor(frame, frame_img, CV_BGR2GRAY);
cvConvert(frame_img, frame_mat);
cvSobel(frame_mat,temp_mat1,1,0,3);
cvThreshold(temp_mat1, temp_mat1, 24, 255.0, CV_THRESH_BINARY);
cvAdd(temp_mat1, background_mat, background_mat);
}
}
cvThreshold(background_mat, background_mat, 58, 255.0, CV_THRESH_BINARY);
while(frame = cvQueryFrame( capture ))
{
cvCvtColor(frame, frame_img, CV_BGR2GRAY);
cvConvert(frame_img, frame_mat);
cvSobel(frame_mat,frame_mat,1,0,3);
cvThreshold(frame_mat, frame_mat, 58, 255.0, CV_THRESH_BINARY);
cvSub(frame_mat,background_mat,temp_mat1);
cvErode(temp_img1, temp_img1, 0, 1);
cvDilate(temp_img1,temp_img1, 0, 1);
cvConvert(background_mat, background_img);
cvConvert(temp_mat1, temp_img1);
cvShowImage("Actual_video", frame_img);
cvShowImage("background_frames", background_img);
cvShowImage("foreground_frames", temp_img1);
if(cvWaitKey(30)>0)
break;
}
cvDestroyWindow("Actual_video");
cvDestroyWindow("background_frames");
cvDestroyWindow("foreground_frames");
cvReleaseImage(&frame_img);
cvReleaseImage(&background_img);
cvReleaseImage(&temp_img1);
cvReleaseMat(&temp_mat1);
cvReleaseMat(&frame_mat);
cvReleaseMat(&background_mat);
cvReleaseCapture(&capture);
return 0;
}
edit retag flag offensive close merge delete

Comments

The OpenCV 2.0 is obsoleted.

supra56 gravatar imagesupra56 ( 2019-10-31 12:07:54 -0500 )edit

that's even opencv1.0 code, that was deprecated, when 2.0 came along ;)

berak gravatar imageberak ( 2019-11-01 05:33:42 -0500 )edit

can you help to convert this code to new api i have tried but still no success

Gilal Nauman gravatar imageGilal Nauman ( 2019-11-02 01:35:43 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-11-01 01:38:42 -0500

berak gravatar image

though it might be possible to port that line by line, i'd suggest, that you ditch it all, and have a look at more sophisticated background subtraction methods

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-10-31 08:49:31 -0500

Seen: 103 times

Last updated: Oct 31 '19