diff --git a/OpenCV/Conveyor belt monitoring/src/optical.cpp b/OpenCV/Conveyor belt monitoring/src/optical.cpp index da2e038..25f39e2 100644 --- a/OpenCV/Conveyor belt monitoring/src/optical.cpp +++ b/OpenCV/Conveyor belt monitoring/src/optical.cpp @@ -13,13 +13,19 @@ int main(int argc, const char** argv ) cap.open(argv[1]); if( !cap.isOpened() ) return -1; + VideoWriter writer; makecolorwheel(colorwheel); Mat prevgray, gray, frame; cvNamedWindow("Image",1); - setMouseCallback( "Image", onMouse, 0 ); - cap >> frame; - resize(frame, frame, Size(640,480)); +/* setMouseCallback( "Image", onMouse, 0 ); +*/ cap >> frame; + resize(frame, frame, Size(640, 640*frame.rows/frame.cols), INTER_LINEAR); + selection.x=200; + selection.y=frame.rows*0.1; + selection.width=640-selection.x-640*0.1; + selection.height=frame.rows*0.9; + ROI_flag=true; imshow("Image", frame); while(1) { @@ -49,7 +55,7 @@ int main(int argc, const char** argv ) if( prevgray.data ) { Mat flow,flowx,flowy,motion2color; - calcOpticalFlowFarneback(prevgray, gray, flow, 0.5, 3, 15, 3, 5, 1.2, 0); + calcOpticalFlowFarneback(prevgray, gray, flow, 0.5, 3, 20, 3, 7, 1.5, 1); Mat tmpmat[2]; split(flow, tmpmat); flowx=tmpmat[0]; @@ -59,26 +65,28 @@ int main(int argc, const char** argv ) Mat mask; cvtColor(motion2color, mask, CV_BGR2GRAY); motion2color.copyTo(imageROI,mask); + + t = ((double)cvGetTickCount() - t)/((double)cvGetTickFrequency()*1000.) ; + cout << "cost time: " << t <<" ms/fps"<< endl; std::stringstream ss; - ss<>sss; - putText(frame, "Max speed angle : "+sss, Point(20,415), 4,0.5 ,Scalar(0,255,0),1,8); - std::stringstream ss1; - ss1<>sss1; - putText(frame, "MS angle rate : "+sss1+"%", Point(20,440), 4,0.5 ,Scalar(0,255,0),1,8); + putText(frame, sss+" fps", Point(20,50), 4,0.5 ,Scalar(0,255,0),1,8); + if (saveFlag) + { + imwrite("outlier.png",frame); + saveFlag=false; + } imshow("Image", frame); writer<=0) break; } writer.release(); return 0; -} \ No newline at end of file +} diff --git a/OpenCV/Conveyor belt monitoring/src/optical.h b/OpenCV/Conveyor belt monitoring/src/optical.h index ba0e37f..87d75e1 100644 --- a/OpenCV/Conveyor belt monitoring/src/optical.h +++ b/OpenCV/Conveyor belt monitoring/src/optical.h @@ -4,6 +4,7 @@ using namespace cv; using namespace std; bool ROI_flag=false; +bool saveFlag=false; int _time= 0 ; //selectObject的初始值为false, bool _initdone = false; float Rool_angle[2]={-1,-1.0}; @@ -15,216 +16,252 @@ static vector colorwheel; //Scalar r,g,b void onMouse( int event, int x, int y, int, void* ) { - //左键按下,且不移动 - if (event == CV_EVENT_LBUTTONDOWN && !CV_EVENT_MOUSEMOVE) - { - if(event==CV_EVENT_LBUTTONDOWN) - { - if(_time==0) - { - origin = Point(x,y); - selection = Rect(x,y,0,0); - cout<<"position1 "< &colorwheel) { //共55个方向 - int RY = 15; - int YG = 6; - int GC = 4; - int CB = 11; - int BM = 13; - int MR = 6; - - int i; - - for (i = 0; i < RY; i++) colorwheel.push_back(Scalar(255, 255*i/RY, 0)); - for (i = 0; i < YG; i++) colorwheel.push_back(Scalar(255-255*i/YG, 255, 0)); - for (i = 0; i < GC; i++) colorwheel.push_back(Scalar(0, 255, 255*i/GC)); - for (i = 0; i < CB; i++) colorwheel.push_back(Scalar(0, 255-255*i/CB, 255)); - for (i = 0; i < BM; i++) colorwheel.push_back(Scalar(255*i/BM, 0, 255)); - for (i = 0; i < MR; i++) colorwheel.push_back(Scalar(255, 0, 255-255*i/MR)); + int RY = 15; + int YG = 6; + int GC = 4; + int CB = 11; + int BM = 13; + int MR = 6; + + int i; + + for (i = 0; i < RY; i++) colorwheel.push_back(Scalar(255, 255*i/RY, 0)); + for (i = 0; i < YG; i++) colorwheel.push_back(Scalar(255-255*i/YG, 255, 0)); + for (i = 0; i < GC; i++) colorwheel.push_back(Scalar(0, 255, 255*i/GC)); + for (i = 0; i < CB; i++) colorwheel.push_back(Scalar(0, 255-255*i/CB, 255)); + for (i = 0; i < BM; i++) colorwheel.push_back(Scalar(255*i/BM, 0, 255)); + for (i = 0; i < MR; i++) colorwheel.push_back(Scalar(255, 0, 255-255*i/MR)); } void motionToColor(const Mat &flowx, const Mat &flowy,Mat &color) { - color.create(flowx.rows, flowx.cols, CV_8UC3); - - double xMin, xMax,yMin,yMax; // determine motion range: - cv::minMaxLoc(flowx, &xMin, &xMax, 0, 0); - cv::minMaxLoc(flowy, &yMin, &yMax, 0, 0); - float xVol = ::max(abs(xMin), abs(xMax) ) ; // Find max flow to normalize fx and fy - float yVol = ::max(abs(yMin), abs(yMax) ) ; - float rad = sqrt(xVol * xVol + yVol * yVol); - float maxrad = 0 > rad ? 0 : rad; - Mat Angle; - Angle=Mat::zeros (55, 1, CV_32FC1); - - for (int i= 0; i < flowx.rows; ++i) - { - for (int j = 0; j < flowx.cols; ++j) - { - uchar *data = color.data + color.step[0] * i + color.step[1] * j; - float fx = flowx.at(i,j )/maxrad ; - float fy = flowy.at(i,j )/maxrad ; - if ((fabs(fx) > UNKNOWN_FLOW_THRESH) || (fabs(fy) > UNKNOWN_FLOW_THRESH)) - { - data[0] = data[1] = data[2] = 0; - continue; - } - float rad = sqrt(fx * fx + fy * fy); - float angle = atan2(-fy, -fx) / CV_PI; - float anglee_1=atan2((double)selection.width, (double)selection.height)/CV_PI; - float anglee_2=atan2((double)selection.width, (double)selection.height)/CV_PI; - if(angle<=::max(anglee_1,anglee_2) && angle>=::min(anglee_1,anglee_2)); - { - float fk = (angle + 1.0) / 2.0 * (colorwheel.size()-1); - int k0 = (int)fk; - Angle.at(k0,1)=Angle.at(k0,1)+1; - for (int b = 0; b < 3; b++) - { - float col0 = colorwheel[k0][b] / 255.0; - col0 = rad<=1?1 - rad / maxrad* (1 - col0):0.75; // increase saturation with radius - data[2 - b] = (int)(255.0 * col0); - if(rad<0.6) - data[2 - b] =0; - } - } - } - //Angle=Angle/(double)(flowx.rows*flowx.cols); - // cout<<"Angle "<1) maxv=0; - if(maxv>20000) - { - cout<<" maxv "<(x, y) == Vec3b(255,255,255) ) { - color.at(x, y)[0] = 0; - color.at(x, y)[1] = 0; - color.at(x, y)[2] = 0; - } - } - } - - - Mat kernel = (Mat_(3,3) << + Mat color_tmp; + //color.copyTo(color_tmp); + color_tmp.create(flowx.rows, flowx.cols, CV_8UC3); + color.create(flowx.rows, flowx.cols, CV_8UC3); + double xMin, xMax,yMin,yMax; // determine motion range: + cv::minMaxLoc(flowx, &xMin, &xMax, 0, 0); + cv::minMaxLoc(flowy, &yMin, &yMax, 0, 0); + float xVol = ::max(abs(xMin), abs(xMax) ) ; // Find max flow to normalize fx and fy + float yVol = ::max(abs(yMin), abs(yMax) ) ; + float rad = xVol * xVol + yVol * yVol; + float maxrad = 0 > rad ? 0 : rad; + + Mat Rad; + Rad.create(flowx.rows,flowx.cols,CV_32FC1); + Rad=(flowx.mul(flowx)+flowy.mul(flowy))/maxrad; + cv::sqrt(Rad, Rad); + + Mat Wheel; + Wheel=Mat::zeros (55, 1, CV_32FC1); + color_tmp.setTo(Scalar(0,0,0)); + color.setTo(Scalar(0,0,0)); + + int num=0; + for (int i = 0; i < flowx.rows; ++i) + { + for (int j = 0; j < flowx.cols; ++j) + { + uchar *data = color_tmp.data + color_tmp.step[0] * i + color_tmp.step[1] * j; + float fx = flowx.at(i,j ) ; + float fy = flowy.at(i,j ) ; + if ((fabs(fx) > UNKNOWN_FLOW_THRESH) || (fabs(fy) > UNKNOWN_FLOW_THRESH)) + { + data[0] = data[1] = data[2] = 0; + continue; + } +/* float angle_tmp = atan2(-fy, -fx) / CV_PI; + float fk = (angle_tmp + 1.0) / 2.0 * (colorwheel.size()-1); + int k0 = (int)fk; + Wheel.at(k0,1)=Wheel.at(k0,1)+1; + for (int b = 0; b < 3; b++) + { + float col0 = colorwheel[k0][b] / 255.0; + col0 = Rad.at(i, j)<=1?1 - Rad.at(i, j) / maxrad* (1 - col0):0.75; // increase saturation with radius + data[2 - b] = (int)(255.0 * col0); + } */ + + float diff_XY=abs(fx)+abs(fy); + if (diff_XY > 5) + { + for (int b = 0; b < 3; b++) + { + data[2 - b] = (int)255.0 ; + } + num++; + } + + //arrowedLine(cflow, Point(x, y), Point(cvRound(x+fxy.x), cvRound(y+fxy.y)), CV_RGB(0,255,0)); + + //circle(cflow, Point(x,y), 2, CV_RGB(255, 0, 0), -1); + } + //Wheel=Wheel/(double)(flowx.rows*flowx.cols); + } + double minv = 0.0, maxv = 0.0; + Point minl,maxl; + minMaxLoc(Wheel, &minv, &maxv,&minl,&maxl); + if(num<0.2*color_tmp.rows*color_tmp.cols && num>1500) + { + cout<<" num "< > contours; + Mat greay; + cvtColor(color_tmp, greay, CV_BGR2GRAY); + findContours(greay,contours , CV_RETR_TREE, CV_CHAIN_APPROX_NONE); + cout<tmp[0]?contours.size():tmp[0]; + tmp[1]=c; + } + RotatedRect box = minAreaRect(Mat(contours[tmp[1]])); + if(box.boundingRect().area()>100 && box.boundingRect().area()<10000) + { + Point2f vertex[4]; + box.points(vertex); + cout<(x, y) == Vec3b(255,255,255) ) { + color.at(x, y)[0] = 0; + color.at(x, y)[1] = 0; + color.at(x, y)[2] = 0; + } + } +} + + +Mat kernel = (Mat_(3,3) << 1, 1, 1, 1, -8, 1, 1, 1, 1); - Mat imgLaplacian; - Mat sharp = color; // copy source image to another temporary one - filter2D(sharp, imgLaplacian, CV_32F, kernel); - color.convertTo(sharp, CV_32F); - Mat imgResult = sharp - imgLaplacian; - // convert back to 8bits gray scale - imgResult.convertTo(imgResult, CV_8UC3); - imgLaplacian.convertTo(imgLaplacian, CV_8UC3); - - imshow( "New Sharped Image", imgResult ); - moveWindow("New Sharped Image", 710, 10);*/ +Mat imgLaplacian; +Mat sharp = color; // copy source image to another temporary one +filter2D(sharp, imgLaplacian, CV_32F, kernel); +color.convertTo(sharp, CV_32F); +Mat imgResult = sharp - imgLaplacian; +// convert back to 8bits gray scale +imgResult.convertTo(imgResult, CV_8UC3); +imgLaplacian.convertTo(imgLaplacian, CV_8UC3); + +imshow( "New Sharped Image", imgResult ); +moveWindow("New Sharped Image", 710, 10);*/ /* color = imgResult; // copy back - // Create binary image from source image +// Create binary image from source image - Mat bw; - cvtColor(color, bw, CV_BGR2GRAY); - threshold(bw, bw, 40, 255, CV_THRESH_BINARY | CV_THRESH_OTSU); - // Perform the distance transform algorithm - imshow("Binary Image", bw); - moveWindow("Binary Image", 1200, 10);*/ +Mat bw; +cvtColor(color, bw, CV_BGR2GRAY); +threshold(bw, bw, 40, 255, CV_THRESH_BINARY | CV_THRESH_OTSU); +// Perform the distance transform algorithm +imshow("Binary Image", bw); +moveWindow("Binary Image", 1200, 10);*/ /* Mat dist; - distanceTransform(bw, dist, CV_DIST_L2, 3); - // Normalize the distance image for range = {0.0, 1.0} - // so we can visualize and threshold it - normalize(dist, dist, 0, 1., NORM_MINMAX); - imshow("Distance Transform Image", dist); - moveWindow("Distance Transform Image", 10, 500); +distanceTransform(bw, dist, CV_DIST_L2, 3); +// Normalize the distance image for range = {0.0, 1.0} +// so we can visualize and threshold it +normalize(dist, dist, 0, 1., NORM_MINMAX); +imshow("Distance Transform Image", dist); +moveWindow("Distance Transform Image", 10, 500); - threshold(dist, dist, .4, 1., CV_THRESH_BINARY); - // Dilate a bit the dist image - Mat kernel1 = Mat::ones(3, 3, CV_8UC1); - dilate(dist, dist, kernel1); - imshow("Peaks", dist); - moveWindow("Peaks", 710, 500);*/ +threshold(dist, dist, .4, 1., CV_THRESH_BINARY); +// Dilate a bit the dist image +Mat kernel1 = Mat::ones(3, 3, CV_8UC1); +dilate(dist, dist, kernel1); +imshow("Peaks", dist); +moveWindow("Peaks", 710, 500);*/ /* Mat dist_8u; - dist.convertTo(dist_8u, CV_8U); - // Find total markers - vector > contours; - findContours(dist_8u, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); - // Create the marker image for the watershed algorithm - - - Mat markers = Mat::zeros(dist.size(), CV_32SC1); - // Draw the foreground markers - for (size_t i = 0; i < contours.size(); i++) - drawContours(markers, contours, static_cast(i), Scalar::all(static_cast(i)+1), -1); - // Draw the background marker - circle(markers, Point(5,5), 3, CV_RGB(255,255,255), -1); - imshow("Markers", markers*10000); - moveWindow("Markers", 1200, 500); +dist.convertTo(dist_8u, CV_8U); +// Find total markers +vector > contours; +findContours(dist_8u, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); +// Create the marker image for the watershed algorithm + + +Mat markers = Mat::zeros(dist.size(), CV_32SC1); +// Draw the foreground markers +for (size_t i = 0; i < contours.size(); i++) +drawContours(markers, contours, static_cast(i), Scalar::all(static_cast(i)+1), -1); +// Draw the background marker +circle(markers, Point(5,5), 3, CV_RGB(255,255,255), -1); +imshow("Markers", markers*10000); +moveWindow("Markers", 1200, 500); */ /* watershed(color, markers); - Mat mark = Mat::zeros(markers.size(), CV_8UC1); - markers.convertTo(mark, CV_8UC1); - bitwise_not(mark, mark); - vector colors; - - - for (size_t i = 0; i < contours.size(); i++) - { - int b = theRNG().uniform(0, 255); - int g = theRNG().uniform(0, 255); - int r = theRNG().uniform(0, 255); - colors.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r)); - } - // Create the result image - color = Mat::zeros(markers.size(), CV_8UC3); - // Fill labeled objects with random colors - - - for (int i = 0; i < markers.rows; i++) - { - for (int j = 0; j < markers.cols; j++) - { - int index = markers.at(i,j); - if (index > 0 && index <= static_cast(contours.size())) - color.at(i,j) = colors[index-1]; - else - color.at(i,j) = Vec3b(0,0,0); - } - - }*/ +Mat mark = Mat::zeros(markers.size(), CV_8UC1); +markers.convertTo(mark, CV_8UC1); +bitwise_not(mark, mark); +vector colors; + + +for (size_t i = 0; i < contours.size(); i++) +{ + int b = theRNG().uniform(0, 255); + int g = theRNG().uniform(0, 255); + int r = theRNG().uniform(0, 255); + colors.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r)); +} +// Create the result image +color = Mat::zeros(markers.size(), CV_8UC3); +// Fill labeled objects with random colors + + +for (int i = 0; i < markers.rows; i++) +{ + for (int j = 0; j < markers.cols; j++) + { + int index = markers.at(i,j); + if (index > 0 && index <= static_cast(contours.size())) + color.at(i,j) = colors[index-1]; + else + color.at(i,j) = Vec3b(0,0,0); + } +}*/ }