My code opens image with road signs, detects them, rescale to specified size and then puts them into matrix.
vector<vector<Point> > contours;
vector<Vec4i> hierarchy; findContours(maski, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
Mat output1= Mat::zeros(cropImg.rows,cropImg.cols, CV_8UC3);
for(int i = 0; i < contours.size(); i++)
{
drawContours(output1 , contours, i, Scalar(0,0,255), 1, 8, hierarchy );
imshow("kontury z findContours", output1);
}
vector<Rect> boundRect( contours.size() );
Mat drawing1 = Mat::zeros(cropImg.size(), CV_8UC3 );
Mat image_roi = Mat::zeros(Size(1000,1000), CV_8UC3 );
Mat przeskalowane1;
for( int i = 0; i < contours.size(); i++ )
{
double obwod = arcLength(Mat(contours[i]), true);
if(obwod>150)
{
boundRect[i] = boundingRect(Mat(contours[i]));
cout<<"Obwod: "<<obwod<<" Wymiar: "<<boundRect[i].width<<"x"<<boundRect[i].height<<endl;
if(boundRect[i].height > 50 && boundRect[i].width > 50)
{
drawContours( drawing1, contours, i, Scalar(3, 200, 2), CV_FILLED, 8, hierarchy, 0, Point() );
imshow("kontury brane pod uwage przed skalowaniem", drawing1);
Rect mask(boundRect[i].x, boundRect[i].y, boundRect[i].width, boundRect[i].height);
//cout << "#" << i << " rectangle x:" << mask.x << " y:" << mask.y << " " << mask.width << "x" << mask.height << endl;
Mat image_roi = drawing1(mask);
double wys = boundRect[i].height;
double szer = boundRect[i].width;
double skala1 = wys/128;
double y = wys/skala1;
double x = szer/skala1;
resize(image_roi, image_roi, Size(x,y));
przeskalowane1.push_back(image_roi);
} // ERROR in this line
}
}
if(przeskalowane1.cols > 0)
{
cout<<"Przeskalowane: "<<przeskalowane1.cols<<"x"<<przeskalowane1.rows<<endl;
imshow("Przeskalowane", przeskalowane1);
cvMoveWindow("Przeskalowane", 1128, 0);
cvtColor(przeskalowane1, przeskalowane1, CV_BGR2GRAY);
}
It all works properly when there is only one road sign found or signs found in the image have very similar dimensions to specified. If sizes of found signs are different, then I get following error:
Error: Sizes of input arguments do not match <> in unknown function, file......\modules\core\src\matrix.cpp, line 598"
It is very important for me to have these signs in matrix.
The help page for the cv::Mat::push_back method says:
The methods add one or more elements to the bottom of the matrix. They emulate the corresponding method of the STL vector class. When elem is Mat , its type and the number of columns must be the same as in the container matrix.
So, in order to add multiple images to przeskalowane1
, you need to rescale them to the same width (not height).
Mat image_roi = drawing1(mask);
double wys = boundRect[i].height;
double szer = boundRect[i].width;
double x = 128;
double skala1 = szer/x;
double y = wys/skala1;
resize(image_roi, image_roi, Size(x, y));