code test

download code test

If you can't read please download the document

Transcript of code test

//.h file code: //C# TO C++ CONVERTER TODO TASK: The .NET System native C++: //using namespace System; //C# TO C++ CONVERTER TODO TASK: The .NET System native C++: //using namespace System::Collections::Generic; //C# TO C++ CONVERTER TODO TASK: The .NET System native C++: //using namespace System::Diagnostics; //C# TO C++ CONVERTER TODO TASK: The .NET System native C++: //using namespace System::Drawing; //C# TO C++ CONVERTER TODO TASK: The .NET System native C++: //using namespace System::Text; using namespace Emgu::CV; using namespace Emgu::CV::Features2D; using namespace Emgu::CV::Structure; using namespace Emgu::Util; namespace is not available from namespace is not available from namespace is not available from namespace is not available from namespace is not available from

namespace TrafficSignRecognition { class StopSignDetector : public DisposableObject { private: Features2DTracker *_tracker; SURFDetector *_detector; MemStorage *_octagonStorage; Contour *_octagon; public: StopSignDetector(Image *stopSignModel); /// /// Compute the red pixel mask for the given image. /// A red pixel is a pixel where: 20 < hue < 160 AND satuation > 10 /// /// The color image to find red mask from /// The red pixel mask private: static Image *GetRedPixelMask(Image *image); //.cpp file code: //C# TO native //using //C# TO native //using //C# TO native //using //C# TO native //using C++ CONVERTER TODO TASK: The .NET System C++: namespace System; C++ CONVERTER TODO TASK: The .NET System C++: namespace System::Collections::Generic; C++ CONVERTER TODO TASK: The .NET System C++: namespace System::Diagnostics; C++ CONVERTER TODO TASK: The .NET System C++: namespace System::Drawing; namespace is not available from namespace is not available from namespace is not available from namespace is not available from

//C# TO C++ CONVERTER TODO TASK: The .NET System namespace is not available from native C++: //using namespace System::Text; using namespace Emgu::CV; using namespace Emgu::CV::Features2D; using namespace Emgu::CV::Structure; using namespace Emgu::Util; namespace TrafficSignRecognition { StopSignDetector::StopSignDetector(Image *stopSignM odel) { _detector = new SURFDetector(500, false); //C# TO C++ CONVERTER NOTE: The following 'using' block is replaced by its C++ e quivalent: // using (Image redMask = GetRedPixelMask(stopSignModel)) Image *redMask = GetRedPixelMask(stopSignModel) ; try { _tracker = new Features2DTracker(_detector->DetectFeatu res(redMask, 0)); } //C# TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to the except ion 'finally' clause: finally { if (redMask != 0) { redMask.Dispose(); } } _octagonStorage = new MemStorage(); _octagon = new Contour(_octagonStorage); _octagon->PushMulti(new Point[] {Point(1, 0), Point(2, 0), Point(3, 1 ), Point(3, 2), Point(2, 3), Point(1, 3), Point(0, 2), Point(0, 1)}, Emgu::CV::C vEnum::BACK_OR_FRONT::FRONT); } Image *StopSignDetector::GetRedPixelMask(Image *image) { //C# TO C++ CONVERTER NOTE: The following 'using' block is replaced by its C++ e quivalent: // using (Image hsv = image.Convert()) Image *hsv = image->Convert (); try { //C# TO C++ CONVERTER WARNING: Since the array size is not known in this declara tion, C# to C++ Converter has converted this array to a pointer. You will need to call 'delete[]' where appropriate: //ORIGINAL LINE: Image[] channels = hsv.Split(); //C# TO C++ CONVERTER TODO TASK: There is no direct native C++ equivalent to thi s .NET String method: Image *channels = hsv->Split(); try

{ //channels[0] is the mask for hue less than 20 or large r than 160 CvInvoke::cvInRangeS(channels[0], new MCvScalar(20), ne w MCvScalar(160), channels[0]); channels[0]->_Not(); //channels[1] is the mask for satuation of at least 10, this is mainly used to filter out white pixels channels[1]->_ThresholdBinary(new Gray(10), new Gray(25 5.0)); CvInvoke::cvAnd(channels[0], channels[1], channels[0], void*::Zero); } //C# TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to the except ion 'finally' clause: finally { channels[1]->Dispose(); channels[2]->Dispose(); } return channels[0]; } //C# TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to the except ion 'finally' clause: finally { if (hsv != 0) { hsv.Dispose(); } } } #include private: void FindStopSign(Image *img, std::vector &stopSignList, std::vector &boxList, Contour *c ontours); public: void DetectStopSign(Image *img, std::vector &stopSignList, std::vector &boxList); protected: virtual void DisposeObject(); } } //.cpp file code: void ::FindStopSign(Image *img, s td::vector &stopSignList, std::vector &boxList, Contour *contours) { for (; contours != 0; contours = contours->HNext) { contours->ApproxPoly(contours->Perimeter * 0.02, 0, cont ours->Storage);

if (contours->Area > 200) { double ratio = CvInvoke::cvMatchShapes(_octagon, cont ours, Emgu::CV::CvEnum::CONTOURS_MATCH_TYPE::CV_CONTOURS_MATCH_I3, 0); if (ratio > 0.1) //not a good match of contour shape { Contour *child = contours->VNext; if (child != 0) { FindStopSign(img, stopSignList, boxList , child); } continue; } Rectangle *box = contours->BoundingRectangle; Image *candidate; //C# TO C++ CONVERTER NOTE: The following 'using' block is replaced by its C++ e quivalent: // using (Image tmp = img.Copy(box)) Image *tmp = img->Copy(box); try { candidate = tmp->Convert (); } //C# TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to the except ion 'finally' clause: finally { if (tmp != 0) { tmp.Dispose(); } } //set the value of pixels not in the contour region t o zero //C# TO C++ CONVERTER NOTE: The following 'using' block is replaced by its C++ e quivalent: // using (Image mask = new Image (box.Size)) Image *mask = new Image(box->Size); try { mask->Draw(contours, new Gray(255), new Gray(2 55), 0, -1, new Point(-box->X, -box->Y)); double mean = CvInvoke::cvAvg(candidate, mask) ->v0; candidate->_ThresholdBinary(new Gray(mean), ne w Gray(255.0)); candidate->_Not(); mask->_Not(); candidate->SetValue(0, mask); } //C# TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to the except

ion 'finally' clause: finally { if (mask != 0) { mask.Dispose(); } } //C# TO C++ CONVERTER WARNING: Since the array size is not known in this declara tion, C# to C++ Converter has converted this array to a pointer. You will need to call 'delete[]' where appropriate: //ORIGINAL LINE: ImageFeature[] features = _detector.DetectFeatures(candi date, nullptr); ImageFeature *features = _detector::DetectFeat ures(candidate, 0); //C# TO C++ CONVERTER WARNING: Since the array size is not known in this declara tion, C# to C++ Converter has converted this array to a pointer. You will need to call 'delete[]' where appropriate: //ORIGINAL LINE: Features2DTracker.MatchedImageFeature[] matchedFeatures = _tracker.MatchFeature(features, 2); Features2DTracker::MatchedImageFeature *matche dFeatures = _tracker::MatchFeature(features, 2); int goodMatchCount = 0; for (Features2DTracker::MatchedImageFeature::c onst_iterator ms = matchedFeatures->begin(); ms != matchedFeatures->end(); ++ms) { if ((*ms)->SimilarFeatures[0]->Distance < 0.5) { goodMatchCount++; } } if (goodMatchCount >= 10) { boxList.push_back(box); stopSignList.push_back(candidate); } } } } void ::DetectStopSign(Image *img, std::vector &stopSignList, std::vector &boxList) { Image *smoothImg = img->SmoothGaussian(5, 5, 1.5, 1.5); Image *smoothedRedMask = GetRedPixelMask(smoothImg); //Use Dilate followed by Erode to eliminate small gaps in some countour. smoothedRedMask->_Dilate(1); smoothedRedMask->_Erode(1); //C# TO C++ CONVERTER NOTE: The following 'using' block is replaced by its C++ e quivalent: // using (Image canny = smoothedRedMask.Canny(new Gray(100), new G ray(50))) Image *canny = smoothedRedMask->Canny(new Gray(100), ne

w Gray(50)); try { //C# TO C++ CONVERTER NOTE: The following 'using' block is replaced by its C++ e quivalent: // using (MemStorage stor = new MemStorage()) MemStorage *stor = new MemStorage(); try { Contour *contours = canny->FindContours(Emgu::CV::CvEnum::CHAI N_APPROX_METHOD::CV_CHAIN_APPROX_SIMPLE, Emgu::CV::CvEnum::RETR_TYPE::CV_RETR_TR EE, stor); FindStopSign(img, stopSignList, boxList, contours); } //C# TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to the except ion 'finally' clause: finally { if (stor != 0) { stor.Dispose(); } } } //C# TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to the except ion 'finally' clause: finally { if (canny != 0) { canny.Dispose(); } } } void ::DisposeObject() { delete _tracker; delete _octagonStorage; }