Advertisement
Guest User

Untitled

a guest
May 27th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include <opencv2\opencv.hpp>
  2.  
  3.  
  4.  
  5. int main(){
  6.     cv::Mat kernel = cv::getStructuringElement(cv::MORPH_ELLIPSE,cv::Size(35,35));
  7.     //Wczytujemy obraz w kolorze i konwertujemy go na odcien szarosci
  8.     cv::Mat imgColor = cv::imread("rezystor.jpg"), img, tmp1,tmp2;
  9.     cv::cvtColor(imgColor,img,cv::COLOR_RGB2GRAY);
  10.    
  11.     //zapisujemy obraz przed
  12.     cv::imwrite( "beforeOperations.jpg", img );
  13.     cv::namedWindow("mainWindow");
  14.     //cv::namedWindow("originalWindow");
  15.  
  16.     //cv::Rect cropArea(2000,1200,1500,1000);
  17.     //cv::Mat imgCrop = img(cropArea);
  18.     //progujemy
  19.     cv::threshold(img,img,100,255,CV_THRESH_BINARY);
  20.     //cv::adaptiveThreshold(img,img,255,cv::ADAPTIVE_THRESH_GAUSSIAN_C,CV_THRESH_BINARY,11,2);
  21.    
  22.  
  23.    
  24.  
  25.    
  26.     //usuwamy nozki rezystora
  27.     cv::dilate(img,img,kernel);
  28.     cv::erode(img,img,kernel);
  29.    
  30.    
  31.  
  32.    
  33.  
  34.     cv:: Mat imgGray;
  35.     img.copyTo(imgGray);
  36.  
  37.     cv::threshold(img,img,100,255,CV_THRESH_BINARY_INV);
  38.  
  39.     std::vector<std::vector<cv::Point>> contour;
  40.     cv::findContours(img,contour,cv::RETR_EXTERNAL,cv::CHAIN_APPROX_SIMPLE);
  41.     cv::RotatedRect rr=cv::minAreaRect(contour[0]);
  42.     cv::Point2f points[4];
  43.    
  44.     rr.points(points);
  45.     cv::Size s;
  46.     cv::Mat transform;
  47.     cv::Scalar c[4]={cv::Scalar(255,0,0), cv::Scalar(0,255,0), cv::Scalar(0,0,255), cv::Scalar(255,255,0)};
  48.     //for(int i=0;i<4;++i) cv::circle(imgColor,points[i],10,c[i]);
  49.     //cv::namedWindow("Test1",0);  
  50.     //cv::imshow("Test1",imgColor);
  51.     //cv::waitKey();
  52.     if(rr.size.width>rr.size.height)
  53.     {
  54.         cv::Point2f oPoints[]={cv::Point2f(0,0),
  55.             cv::Point2f(rr.size.height,0),
  56.             cv::Point2f(rr.size.height, rr.size.width)};
  57.         s=cv::Size(rr.size.height,rr.size.width);
  58.         transform=cv::getAffineTransform(points, oPoints);
  59.        
  60.     }
  61.     else{
  62.         cv::Point2f oPoints[]={cv::Point2f(0,rr.size.height),
  63.         cv::Point2f(0,0),
  64.         cv::Point2f(rr.size.width,0)};
  65.         s=rr.size;
  66.         transform=cv::getAffineTransform(points, oPoints);
  67.     }
  68.  
  69.     cv::Mat imgOut;
  70.     cv::warpAffine(imgColor,imgOut,transform,s);
  71.    
  72.     cv::imwrite("rezystorCut.jpg", imgOut);
  73.    
  74.     cv::imshow("mainWindow",imgOut);
  75.     cv::moveWindow("mainWindow",500,200);
  76.     cv::waitKey();
  77.    
  78.     cv::Mat colImg=imgOut.col(0).clone();
  79.     colImg=0;
  80.     for(int y=0;y<imgOut.rows;++y)
  81.     {
  82.         for(int x=0;x<img.cols;++x)
  83.         {
  84.             //jesli nie tlo
  85.             //wyliczyc srednia
  86.         }
  87.        
  88.     }
  89.  
  90.     //imgbw
  91.     /*
  92.     cv::Mat rowbw=img.row(18);
  93.     for(int x=0;x<rowbw.cols-1;++x)
  94.     {
  95.         if(rowbw.data[x]<128 && rowbw.data[x+1]>128)
  96.         {
  97.  
  98.         }
  99.     }
  100.     */
  101.  
  102.     //cv::imwrite("imgGray.jpg",imgColor);
  103.     //cv::imshow("originalWindow",imgColor);
  104.    
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement