static double _averageAngle( const QPointF& prevPt, const QPointF& pt, const QPointF& nextPt ) { // calc average angle between the previous and next point double a1 = MyLine( prevPt, pt ).angle(); double a2 = MyLine( pt, nextPt ).angle(); double unitX = cos( a1 ) + cos( a2 ), unitY = sin( a1 ) + sin( a2 ); return atan2( unitY, unitX ); }
double QgsMarkerLineSymbolLayerV2::markerAngle( const QPolygonF& points, bool isRing, int vertex ) { double angle = 0; const QPointF& pt = points[vertex]; if ( isRing || ( vertex > 0 && vertex < points.count() - 1 ) ) { int prevIndex = vertex - 1; int nextIndex = vertex + 1; if ( isRing && ( vertex == 0 || vertex == points.count() - 1 ) ) { prevIndex = points.count() - 2; nextIndex = 1; } QPointF prevPoint, nextPoint; while ( prevIndex >= 0 ) { prevPoint = points[ prevIndex ]; if ( prevPoint != pt ) { break; } --prevIndex; } while ( nextIndex < points.count() ) { nextPoint = points[ nextIndex ]; if ( nextPoint != pt ) { break; } ++nextIndex; } if ( prevIndex >= 0 && nextIndex < points.count() ) { angle = _averageAngle( prevPoint, pt, nextPoint ); } } else //no ring and vertex is at start / at end { if ( vertex == 0 ) { while ( vertex < points.size() - 1 ) { const QPointF& nextPt = points[vertex+1]; if ( pt != nextPt ) { angle = MyLine( pt, nextPt ).angle(); return angle; } ++vertex; } } else { // use last segment's angle while ( vertex >= 1 ) //in case of duplicated vertices, take the next suitable one { const QPointF& prevPt = points[vertex-1]; if ( pt != prevPt ) { angle = MyLine( prevPt, pt ).angle(); return angle; } --vertex; } } } return angle; }
/** @function thresh_callback */ void thresh_callback( Mat final_image , double yawO , vector<Point2i> cent_i , vector<double> area1 , int thresh, int max_thresh , RNG rng , int g) { Mat threshold_output; vector<vector<Point> > contours; vector<Vec4i> hierarchy; double largest_area = 0; int largest_contour_index = 0; double ang = 0; /// Detect edges using Threshold threshold( final_image, threshold_output, thresh, 255, THRESH_BINARY ); cvtColor(threshold_output, threshold_output, CV_BGR2GRAY); /// Find contours findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) ); if( contours.size() > 0 ) { /// checking if any countour is detected /// Approximate contours to polygons + get bounding rects and circles vector<vector<Point> > contours_poly( contours.size() ); // vector<Rect> boundRect( contours.size() ); vector<Point2f>center( contours.size() ); vector<float>radius( contours.size() ); for( int i = 0; i < contours.size(); i++ ) { approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true ); // boundRect[i] = boundingRect( Mat(contours_poly[i]) ); minEnclosingCircle( (Mat)contours_poly[i], center[i], radius[i] ); } // double b = contourArea( contours[0],false); for( int i = 0; i< contours.size(); i++ ) // iterate through each contour. { double a = contourArea( contours[i],false); // Find the area of contour if(a>largest_area){ largest_area=a; largest_contour_index = i; //Store the index of largest contour } } // minEnclosingCircle( (Mat)contours_poly[largest_contour_index], center[largest_contour_index], radius[largest_contour_index] ); /// Draw polygonal contour + bonding rects + circles Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 ); for( int i = 0; i< contours.size(); i++ ) { Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) ); // drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() ); //rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 ); circle( drawing, center[i], (int)radius[i], color, 2, 8, 0 ); setLabel(drawing, "Target", contours[0]); // Triangles } ////////////////////////////////////////////////////////////////////////////////// /////////// FInding the angle //////////////////////////////////////////////////// int dist_x = center[0].x - cent_i[0].x; int dist_y = center[0].y - cent_i[0].y; cout<< " dist_x = " << dist_x << " dist_y =" << dist_y <<endl; //int ang = atan2 (dist_y,dist_x) * 180 / PI; ang = atan2 ( (dist_x) , (dist_y)) * 180 / PI; //cout<< "The target is "<< ( 90 - (ang - 90 ) ) << " degrees from x-axis"<<endl; // if( dist_x > 0 && ) // yawO = yawO - (-ang); yawO = -ang - 180 ; cout<< "The output yaw is "<<yawO<<endl; int area = (int)largest_area; cout<<"area = "<<area<<endl; cout<<endl; cout<< "frame no. = " << g << endl; area1.push_back(area); if( g > 3) { if( (area1[g-2] + area1[g-1] + area1[g])/3 > area ) cout<<" wrong direction "<<endl; else cout<<" right direction "<<endl; } ///////////////////////////////////////////////////////////////////////////////////////// /////////////////// putting the label ////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////// /////// drawing the lines n the image ////////////////////////////////////////////// vector<Point2i> cent_k(1); vector<Point2i> cent_j(1); cent_k[0] = cent_i[0]; cent_k[0].x = cent_i[0].x - 20; cent_j[0] = cent_i[0]; cent_j[0].x = cent_i[0].x + 20; // Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) ); circle( drawing , cent_i[0] , 5 , Scalar( 72, 209, 51 ) , 2, 8 , 0); //circle( drawing , cent_i[0] , 2*5 , Scalar( 72, 209, 51 ) , 2, 8 , 0); //circle( drawing , cent_i[0] , 4*5 , Scalar( 72, 209, 51 ) , 2, 8 , 0); MyLine(drawing , (cent_k[0] ), (cent_j[0])); cent_k[0] = cent_i[0]; cent_k[0].y = cent_i[0].y - 20; cent_j[0] = cent_i[0]; cent_j[0].y = cent_i[0].y + 20; MyLine(drawing , (cent_k[0] ), (cent_j[0])); //////////////////////////////////////////////////////////////////////////////// // string x = -ang; std::vector<cv::Point> c_print; Point temp; temp.x = 10; temp.y = 10; c_print.push_back(temp); temp.x = 100; temp.y = 10; c_print.push_back(temp); temp.x = 10; temp.y = 20; c_print.push_back(temp); temp.x = 100; temp.y = 20; c_print.push_back(temp); setLabel(drawing, "Property of Somi" , c_print); stringstream ang_s; ang_s<< -ang; string ang_s1 = ang_s.str(); string s = "The angle of the target from x-axis is " + ang_s1; temp.x = 10; temp.y = 30; c_print.push_back(temp); temp.x = 100; temp.y = 30; c_print.push_back(temp); temp.x = 10; temp.y = 40; c_print.push_back(temp); temp.x = 100; temp.y = 40; c_print.push_back(temp); setLabel(drawing, s , c_print); /// Show in a window namedWindow( "Contours", CV_WINDOW_AUTOSIZE ); imshow( "Contours", drawing ); } }
void QgsMarkerLineSymbolLayerV2::renderPolylineVertex( const QPolygonF& points, QgsSymbolV2RenderContext& context, Placement placement ) { if ( points.isEmpty() ) return; QgsRenderContext& rc = context.renderContext(); double origAngle = mMarker->angle(); double angle; int i, maxCount; bool isRing = false; if ( placement == FirstVertex ) { i = 0; maxCount = 1; } else if ( placement == LastVertex ) { i = points.count() - 1; maxCount = points.count(); } else { i = 0; maxCount = points.count(); if ( points.first() == points.last() ) isRing = true; } for ( ; i < maxCount; ++i ) { const QPointF& pt = points[i]; // rotate marker (if desired) if ( mRotateMarker ) { if ( i == 0 ) { if ( !isRing ) { // use first segment's angle const QPointF& nextPt = points[i+1]; if ( pt == nextPt ) continue; angle = MyLine( pt, nextPt ).angle(); } else { // closed ring: use average angle between first and last segment const QPointF& prevPt = points[points.count() - 2]; const QPointF& nextPt = points[1]; if ( prevPt == pt || nextPt == pt ) continue; angle = _averageAngle( prevPt, pt, nextPt ); } } else if ( i == points.count() - 1 ) { if ( !isRing ) { // use last segment's angle const QPointF& prevPt = points[i-1]; if ( pt == prevPt ) continue; angle = MyLine( prevPt, pt ).angle(); } else { // don't draw the last marker - it has been drawn already continue; } } else { // use average angle const QPointF& prevPt = points[i-1]; const QPointF& nextPt = points[i+1]; if ( prevPt == pt || nextPt == pt ) continue; angle = _averageAngle( prevPt, pt, nextPt ); } mMarker->setAngle( origAngle + angle * 180 / M_PI ); } mMarker->renderPoint( points.at( i ), context.feature(), rc, -1, context.selected() ); } // restore original rotation mMarker->setAngle( origAngle ); }
int main(int argc, char ** argv) { string gauss = "Gaussino"; string canny = "Canny"; string hough = "Hough"; string binarizar = "Binarizar"; string Otsu = "Otsu"; string image_name = ""; int number; Point min, max, start; ofstream myfile; myfile.open("data.txt"); myfile << "ESCREVE QUALQUER COISA\n"; clock_t t1, t2, t3, t4; double threshold1, threshold2, thres, minLength, maxGap; bool f1, f2, f3, f4, f5, f6, f7, f8, f9; string Result; ostringstream convert; //int i; float temp; //for (i = 1; i <= 6; i++){ //number = i; //convert << number; //Result = convert.str(); //image_name = "a" + Result + ".JPG"; image_name = "a2.JPG"; //number++; //cout << number << endl; cout << image_name; myfile << image_name; myfile << "\n"; t1 = clock(); f1 = false; f2 = true; f3 = false; f4 = false; f5 = false; f6 = true; f7 = true; if (f7 == true){ threshold1 = 10; threshold2 = 19; } f8 = false; f9 = true; if (f9 == true){ thres = 10;// 40 minLength = 20; //50 maxGap = 30; //80 /* CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY ); if ( !capture ) { fprintf( stderr, "ERROR: capture is NULL \n" ); getchar(); return -1; } string original = "original.jpg"; string foto ="img"; IplImage* frame = cvQueryFrame( capture ); Mat img(frame); Mat I, I1, imge; cvtColor(img,imge,CV_RGB2GRAY); imge.convertTo(I, CV_8U); equalizeHist(I,I1); Mat aux = I1; savePictures(I1, original, foto); */ //realiza a leitura e carrega a imagem para a matriz I1 // a imagem tem apenas 1 canal de cor e por isso foi usado o parametro CV_LOAD_IMAGE_GRAYSCALE Mat lara = imread("lara.JPG", CV_LOAD_IMAGE_GRAYSCALE); Mat I = imread(image_name, CV_LOAD_IMAGE_GRAYSCALE); if (I.empty()) return -1; resize(I, I, lara.size(), 1.0, 1.0, INTER_LINEAR); Mat I1; //Mat aux = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE); equalizeHist(I, I1); Mat aux, original; aux = I1; //ShowImage(I, I1); // verifica se carregou e alocou a imagem com sucesso if (I1.empty()) return -1; // tipo Size contem largura e altura da imagem, recebe o retorno do metodo .size() //imSize = I1.size(); // Cria uma matriz do tamanho imSize, de 8 bits e 1 canal Mat I2 = Mat::zeros(I1.size(), CV_8UC1); if (f2 == true) { t2 = clock(); for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) GaussianBlur(I1, I1, Size(i, i), 0, 0, BORDER_DEFAULT); //ShowImage(aux, I1); cout << "Guassiano tempo : "; temp = tempo(t2); savePictures(I1, image_name, gauss); myfile << "Gauss: "; myfile << temp; myfile << "\n"; } if (f1 == true){ t2 = clock(); binarizacao(I1, 125); //ShowImage(aux, I1); cout << "binarizacao : "; temp = tempo(t2); savePictures(I1, image_name, binarizar); myfile << "Binarizacao: "; myfile << temp; myfile << "\n"; } if (f3 == true){ t2 = clock(); inversao(I1); cout << "inversao : "; tempo(t2); } if (f4 == true){ adaptiveThreshold(I1, I1, 255, ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY, 7, 0); } if (f5 == true) Laplacian(I1, I1, 125, 1, 1, 0, BORDER_DEFAULT); if (f7 == true){ t2 = clock(); Canny(I1, I2, threshold1, threshold2, 3, false); cout << "canny : "; temp = tempo(t2); savePictures(I2, image_name, canny); myfile << "Canny: " + (int)(temp * 1000); myfile << "\n"; } if (f9 == true){ t2 = clock(); Hough(I2, aux, thres, minLength, maxGap); cout << "hough : "; temp = tempo(t2); savePictures(aux, image_name, hough); myfile << "Hough: "; myfile << temp; myfile << "\n"; } if (f6 == true){ t2 = clock(); threshold_type = THRESH_BINARY; threshold(aux, I1, 9, max_BINARY_value, threshold_type); cout << "Threshold : "; //savePictures(aux, image_name, Otsu); temp = tempo(t2); myfile << "Threshold/OTSU: "; myfile << temp; myfile << "\n"; } string name = Otsu + image_name; imwrite(name, aux); ShowImage(I1, aux); t2 = clock(); max = maxPoint(aux); min = minPoint(aux); /*start.y = (max.y + min.y) / 2; start.x = (max.x + min.x) /2;*/ start.x = max.x; start.y = max.y; Point end; end.x = start.x; end.y = aux.size().height; MyLine(I, start, end, image_name, 0.3); temp = tempo(t2); ShowImage(I, aux); myfile << "Rota: "; myfile << temp; myfile << "\n"; temp = tempo(t1); cout << "Final time : "; myfile << "Final Time: "; myfile << temp; myfile << "\n"; //float angle = Angle(aux, min, 5); //cout << angle; } //} myfile.close(); //ShowImage(aux, I1); //imwrite(argv[2], I2); // salva imagem I2 no arquivo definido pelo usuario em argv[2] //} return 0; }