Ask Your Question

vector Mat

asked 2015-10-09 01:35:42 -0600

OpenCV_Learner gravatar image

updated 2015-10-09 03:15:50 -0600

Is it possible to insert images of different format in vector<Mat>?

I created a struct as:

struct MatImage
vector<Mat> images;
Inside main I create an object MatImage *obj; I try to insert 3 images into obj as:

where res_32f1 is CV_32FC3, roi_cropped and resizedTemplate1 is UC3. But on running im getting segmentation fault, as only the first image is getting inserted.

edit retag flag offensive close merge delete


please add relevant code snippet.

berak gravatar imageberak ( 2015-10-09 02:08:09 -0600 )edit

this looks murky:

MatImage *obj; // no new ?

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;

berak gravatar imageberak ( 2015-10-09 02:18:19 -0600 )edit

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)?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-10-09 04:14:00 -0600 )edit

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.

OpenCV_Learner gravatar imageOpenCV_Learner ( 2015-10-09 05:48:22 -0600 )edit

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.

OpenCV_Learner gravatar imageOpenCV_Learner ( 2015-10-10 09:57:10 -0600 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2015-10-09 02:15:06 -0600

updated 2015-10-09 02:39:24 -0600

"Is it possible to insert images of different format in vector<Mat>?"

you can simply test the code below and get the answer YES

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;
using namespace std;

int showImages( vector< Mat > images )
    for( size_t i = 0; i < images.size(); i++ )
        imshow(format("image - %d", i), images[i] );


int main( int argc, char** argv )
    vector< Mat > images;

    Mat img0 = Mat::zeros( 400, 400, CV_8UC1 );
    circle( img0, Point(250,250), 100, Scalar(255) );

    Mat img1 = Mat::zeros( 500, 500, CV_8UC3 );
    circle( img1, Point(250,250), 200, Scalar(0,0,255) );

    Mat img2 = Mat::ones( 300, 300, CV_32FC3 );



    return 0;
edit flag offensive delete link more


Hey, thanks for the code.

OpenCV_Learner gravatar imageOpenCV_Learner ( 2015-10-10 11:06:24 -0600 )edit

answered 2015-10-11 22:57:49 -0600

OpenCV_Learner gravatar image

updated 2015-10-12 00:12:49 -0600

 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;

cv::Mat rawMat;
    IplImage *frame;

    cvNamedWindow("Capture Frame",1);

    int counter=0;


        Mat img_scene=rawMat.clone();
        cv::Scalar   min(220/2, 0, 0);
        cv::Scalar   max(260/2,255, 255);
                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 );

            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);
                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));
                Mat roi_cropped=roi & mask;

                int W=0,H=0;
                int w=0,h=0;
                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;
                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 ...
edit flag offensive delete link more


I suppose your problems are linked to IPL and other c-api dunctions...

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-10-12 03:27:42 -0600 )edit

But this is just a normal c++ program written in QT which i try to run in Linux using raspi camera. Can anyone help me or suggest me how to make the above code run in 50 ms.

OpenCV_Learner gravatar imageOpenCV_Learner ( 2015-10-12 23:22:52 -0600 )edit

C++ and IplImage? I do not think so.... Maybe there are some more tasks to be done for OpenCV to completely pass to C++ api, then (and I suppose this is false)...

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-10-13 02:36:51 -0600 )edit

Question Tools

1 follower


Asked: 2015-10-09 01:35:42 -0600

Seen: 1,084 times

Last updated: Oct 12 '15