um wrote a code using template matching method for detect stamp in the envelops. but this program run in the slowly. what is the reason? the video refresh every 1 sec. i wanna reduce that time, is this possible? this is my code. my laptop have core i3 and 4GB ram under win10 technical preview. use opencv 2.4.10 and visual studio 10
#include <iostream>
#include "opencv2/opencv.hpp"
#include
#include
#include
#include <windows.h>
using namespace cv;
using namespace std;
Point point1, point2;
int drag = 0;
Rect rect;
Mat img, roiImg;
int select_flag = 0;
bool go_fast = false;
Mat mytemplate;
HANDLE hSerial = CreateFile(L"\\\\.\\COM10", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
char outputChars[5] = {};
DWORD btsIO;
Mat TplMatch( Mat &img, Mat &mytemplate )
{
Mat result;
matchTemplate( img, mytemplate, result, CV_TM_SQDIFF_NORMED );
normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );
return result;
}
Point minmax( Mat &result )
{
double minVal, maxVal;
Point minLoc, maxLoc, matchLoc;
minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
matchLoc = minLoc;
return matchLoc;
}
void track()
{
roiImg=imread("stamp1.jpg");
mytemplate=imread("stamp1.jpg");
if (select_flag)
{
go_fast = true;
}
Mat result = TplMatch( img, mytemplate );
Point match = minmax( result );
rectangle( img, match, Point( match.x + mytemplate.cols , match.y + mytemplate.rows ), CV_RGB(255, 0, 255), 1 );
char test[32];
sprintf(test,"%dx\n%dy\n", (int)match.x, (int)match.y);
WriteFile(hSerial, test, strlen(test), &btsIO, NULL);
std::cout << "match: " << match << endl;
Rect ROI = cv::Rect( match.x, match.y, mytemplate.cols, mytemplate.rows );
}
void mouseHandler(int event, int x, int y, int flags, void *param)
{
if (event == CV_EVENT_LBUTTONDOWN && !drag)
{
point1 = Point(x, y);
drag = 1;
}
if (event == CV_EVENT_MOUSEMOVE && drag)
{
Mat img1 = img.clone();
point2 = Point(x, y);
rectangle(img1, point1, point2, CV_RGB(255, 0, 0), 3, 8, 0);
imshow("image", img1);
}
if (event == CV_EVENT_LBUTTONUP && drag)
{
point2 = Point(x, y);
rect = Rect(point1.x, point1.y, x - point1.x, y - point1.y);
drag = 0;
roiImg = img(rect);
roiImg.copyTo(mytemplate);
roiImg=imread("stamp1.jpg");
imshow("MOUSE roiImg", roiImg); waitKey(0);
}
if (event == CV_EVENT_LBUTTONUP)
{
select_flag = 1;
drag = 0;
}
}
int main()
{
if (hSerial !=INVALID_HANDLE_VALUE)
{
printf("Port opened! \n");
DCB dcbSerialParams;
GetCommState(hSerial,&dcbSerialParams);
dcbSerialParams.BaudRate = CBR_9600;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.Parity = NOPARITY;
dcbSerialParams.StopBits = ONESTOPBIT;
SetCommState(hSerial, &dcbSerialParams);
}
else
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
printf("Serial port doesn't exist! \n");
}
printf("Error while setting up serial port! \n");
}
int k;
VideoCapture cap(0);
if (!cap.isOpened())
return 1;
if ( !cap.isOpened() )
{ cout << "Unable to open video file" << endl; return -1; }
cap >> img;
GaussianBlur( img, img, Size(7,7), 3.0 );
imshow( "image", img );
while (1)
{
cap >> img;
if ( img.empty() )
break;
cv::flip( img, img, 1 );
GaussianBlur( img, img, Size(7,7), 3.0 );
if ( rect.width == 0 && rect.height == 0 )
cvSetMouseCallback( "image", mouseHandler, NULL );
else
track();
imshow("image", img);
k = waitKey(go_fast ? 30 : 10000);
if (k == 27)
break;
}
CloseHandle(hSerial);
return 0;
}