Here is my code:
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <raspicam/raspicam_cv.h>
#include <raspicam/raspicam.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include <cstdlib>
#include <fstream>
#include <string>
#include <sstream>
#include <opencv2/stitching/stitcher.hpp>
#include "camera.h"
#include <pthread.h>
using namespace std;
using namespace cv;
Mat dst;
int main(int argc,char **argv)
{
time_t start,end;
Mat template1 = imread( "100_1.jpg");//, CV_LOAD_IMAGE_GRAYSCALE ); //loading object to be detectedimg_object
Mat template2 = imread( "NoEntry.jpg");
Mat template3 = imread( "Speed80.png");
raspicam::RaspiCam_Cv Camera;
OpenPiCamera(Camera);
cv::Mat rawMat;
IplImage *frame;
cvNamedWindow("Capture Frame",1);
time(&start);
int counter=0;
while(1)
{
//if(counter%5==0)
{
Camera.grab();
Camera.retrieve(rawMat);
Mat img_scene=rawMat.clone();
cvtColor(rawMat,rawMat,CV_RGB2HSV);
cv::Scalar min(220/2, 0, 0);
cv::Scalar max(260/2,255, 255);
inRange(rawMat,min,max,dst);
vector<cv::Vec3f> circles;
HoughCircles( blur, circles, CV_HOUGH_GRADIENT,1,dst.rows/4,70, 20, 1, 40);//3, 5,400, 10, 0, 80 1, img_scene.rows/6, 100,70,0, 50 dst.rows/8
cv::Rect borders(Point(0,0), dst.size());
for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
cv::circle( dst, center, 3, Scalar(255,255,255), -1);
cv::circle( dst, center, radius, Scalar(255,255,255), 1 );
imshow("hough",dst);
int x=cvRound(circles[i][0])-cvRound(circles[i][2]);
int y=cvRound(circles[i][1])-cvRound(circles[i][2]);
Rect r(abs(x),abs(y),radius*2,radius*2);
if(r.area()>100)
{
Mat roi( radius*2, radius*2, CV_8UC1);
roi=img_scene( Rect(abs(x),abs(y),radius*2,radius*2)& borders);//save ROI
Mat mask( roi.size(),roi.type(),Scalar::all(0));
circle(mask,Point(radius,radius),radius,Scalar::all(255),-1);//circle(mask,Point(radius,radius),radius,Scalar::all(255),-1);
Mat roi_cropped=roi & mask;
int W=0,H=0;
W=dst.cols;
H=dst.rows;
int w=0,h=0;
w=roi_cropped.cols;
h=roi_cropped.rows;
Mat res_32f1(W - w + 1, H - h + 1, CV_32FC3);
Mat res_32f2(W - w + 1, H - h + 1, CV_32FC3);
Mat res_32f3(W - w + 1, H - h + 1, CV_32FC3);
Mat resizedTemplate1,resizedTemplate2,resizedTemplate3;
//resizedTemplate1=template1.clone();
resize(template1, resizedTemplate1, roi_cropped.size());//resizing template into ROI
resize(template2, resizedTemplate2, roi_cropped.size());
resize(template3, resizedTemplate3, roi_cropped.size());
matchTemplate(resizedTemplate1, roi_cropped, res_32f1, CV_TM_CCORR_NORMED);
matchTemplate(resizedTemplate2, roi_cropped, res_32f2, CV_TM_CCORR_NORMED);
matchTemplate(resizedTemplate3, roi_cropped, res_32f3, CV_TM_CCORR_NORMED);
double minval1, maxval1, minval2, maxval2,minval3, maxval3, minval4, maxval4,minval5, maxval5,threshold1 = 0.74;//.78
Point minloc1, maxloc1, minloc2, maxloc2,minloc3, maxloc3,minloc4, maxloc4,minloc5, maxloc5;
minMaxLoc(res_32f1, &minval1, &maxval1, &minloc1, &maxloc1);
minMaxLoc(res_32f2, &minval2, &maxval2, &minloc2, &maxloc2);
minMaxLoc(res_32f3, &minval3, &maxval3, &minloc3, &maxloc3);
double maxInddex;
double init[]={maxval1, maxval2,maxval3};//,maxval4,maxval5
valarray<double> myvalarray (init,3);
double max=(double)myvalarray.max();
if (max >= threshold1)
{
if(max==maxval1)rectangle(img_scene, Point(abs(x),abs(y ...
(more)
please add relevant code snippet.
this looks murky:
why do you even need this struct ? why the pointer (raw pointers should be forbidden to noobs) ?
again, please show exactly, how you construct your
MatImage *obj;
My question is why copying the cropped ROI? Isn't it better to make an object that has the image, the Rect of the ROI, and the template (if it is just one, then use a static Mat)?
I want to send the pointer to thread function call match1. pthread_t tid0, tid1,tid2; pthread_create(&tid0, NULL, match1, images ); hence to pass the images i push them into obj->images. Purpose is: I am trying to match template for 3 sign boards, hence i want to do this using threads. At each thread function I want to call matchTemplate of opencv to speed up processing.
Actually I am trying to do traffic sign detection. In which I try to extract ROI using Hough circles, so I get a lot of ROI from a single image(frame: in case of camera). Hence for each roi i do template matching with 3 signs. As Hough circle is time consuming running matchTemplate 3 times for each ROI makes it more slow. I thought of using threads to run 3 matchTemplate in parallel, for which i want to send the images. So I created the struct.
Purpose is to make it more fast and real time.