// Check if a specified image segment contains any motion features. bool contains_motion(CvRect* motion_segment_rect, CvSeq* motion_feature_sequence) { bool intersects = false; int area = 0; CvRect* motion_feature_rect; for(int i = 0; i < motion_feature_sequence->total; i++) { motion_feature_rect = &((CvConnectedComp*)cvGetSeqElem(motion_feature_sequence, i))->rect; //area = motion_feature_rect->width * motion_feature_rect->height; //printf("%d\n", area); CvRect* intersection_area = &CvRect(); //if (area > MIN_MOTION_FEATURE_AREA) intersects = intersect_rect_ex(motion_segment_rect, motion_feature_rect, intersection_area); if (intersects) area += intersection_area->width * intersection_area->height; //result = intersect_rect(motion_segment_rect, motion_feature_rect); //if (result == true) break; } return area > 0; }
long calculate_motion(CvRect* motion_segment_rect, CvSeq* motion_feature_sequence) { bool intersects = false; long area = 0; CvRect* motion_feature_rect; for(int i = 0; i < motion_feature_sequence->total; i++) { motion_feature_rect = &((CvConnectedComp*)cvGetSeqElem(motion_feature_sequence, i))->rect; CvRect* intersection_area = &CvRect(); intersects = intersect_rect_ex(motion_segment_rect, motion_feature_rect, intersection_area); if (intersects) area += intersection_area->width * intersection_area->height; } //printf("area: %i, ", area); //fflush(stdout); return area; }
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: prevFrame.searchRect = CvRect(); prevFrame.image = NULL; break; case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; }
void my_callback(int event,int x,int y,int flags,void* param) { IplImage* image=(IplImage*)param; switch(event) { case CV_EVENT_LBUTTONDOWN:{ g_isdown=true; g_rect=CvRect(x,y,0,0); } break; case CV_EVENT_MOUSEMOVE:{ if(true==g_isdown) { g_rect.width=x-g_rect.x; g_rect.height=y-g_rect.y; } } break; case CV_EVENT_LBUTTONUP:{ g_isdown=false; if(g_rect.width<0) { g_rect.x=g_rect.x+g_rect.width; g_rect.width*=-1; } if(g_rect.height<0) { g_rect.y=g_rect.y+g_rect.height; g_rect.height*=-1; } high_light(image,g_rect); Draw_hist(image,g_rect); } break; } }
/* * General image processing. Returns the original image with objects drawn on top of it. */ Mat RoadDetection::processImage() { Mat rawFrame, tmpFrame, grayFrame, blurredFrame, contoursFrame, houghFrame, sectionFrame; Point vanishingPoint; vector<Line> houghLines, houghMainLines; vector<Point> roadShape; int sectionOffset; // save a copy of the original frame original.copyTo(rawFrame); // smooth and remove noise GaussianBlur(rawFrame, blurredFrame, Size(BLUR_KERNEL, BLUR_KERNEL), 0, 0); // edge detection (canny, inverted) Canny(blurredFrame, contoursFrame, CANNY_MIN_THRESH, CANNY_MAX_THRESH); threshold(contoursFrame, contoursFrame, 128, 255, THRESH_BINARY); // hough transform lines houghLines = getHoughLines(contoursFrame, true); // vanishing point vanishingPoint = getVanishingPoint(houghLines, rawFrame.size()); sectionOffset = vanishingPoint.y; // section frame (below vanishing point) contoursFrame.copyTo(tmpFrame); sectionFrame = tmpFrame(CvRect(0, vanishingPoint.y, contoursFrame.cols, contoursFrame.rows - vanishingPoint.y)); // re-apply hough transform to section frame houghLines = getHoughLines(sectionFrame, true); // shift lines downwards houghLines = shiftLines(houghLines, sectionOffset); houghLines = getFilteredLines(houghLines); // best line matches houghMainLines = getMainLines(houghLines); if (houghMainLines.size() >= 2) { Point intersection = getLineIntersection(houghMainLines[0], houghMainLines[1]); if (intersection.x > 0 && intersection.y >= 0) { vanishingPoint = intersection; sectionOffset = intersection.y; } // get road shape roadShape = getRoadShape(rawFrame, houghMainLines[0], houghMainLines[1], vanishingPoint); } // limit lines houghLines = getLimitedLines(houghLines, sectionOffset); houghMainLines = getLimitedLines(houghMainLines, sectionOffset); // drawing process drawLines(rawFrame, houghLines, Scalar(0, 0, 255), 2, 0); drawLines(rawFrame, houghMainLines, Scalar(20, 125, 255), 2, 0); drawRoadShape(rawFrame, roadShape, Scalar(20, 125, 255), 0.4); drawCircle(rawFrame, vanishingPoint, Scalar(20, 125, 255), 15, -1, 0); return rawFrame; }
/* * General videos processing. @TODO Make use of previous frames? */ Mat RoadDetection::processVideo(Mat rawFrame) { Mat originalFrame, tmpFrame, grayFrame, blurredFrame, contoursFrame, houghFrame, sectionFrame, drawingFrame; Point vanishingPoint; vector<Line> houghLines, houghMainLines; vector<Point> roadShape; vector<Rect> vehicles; int sectionOffset; // convert to grayscale cvtColor(rawFrame, grayFrame, CV_BGR2GRAY); equalizeHist(grayFrame, grayFrame); // smooth and remove noise GaussianBlur(grayFrame, blurredFrame, Size(BLUR_KERNEL, BLUR_KERNEL), 0); // edge detection (canny, inverted) Canny(blurredFrame, contoursFrame, CANNY_MIN_THRESH, CANNY_MAX_THRESH); threshold(contoursFrame, contoursFrame, 128, 255, THRESH_BINARY); // hough transform houghLines = getHoughLines(contoursFrame, true); // vanishing point vanishingPoint = getVanishingPoint(houghLines, rawFrame.size()); sectionOffset = vanishingPoint.y; // if we can't find a point, use the one from a previous frame if (vanishingPoint.x == 0 && vanishingPoint.y == 0) { vanishingPoint = previousVanishingPoint; } // if we can, save it for future use else { previousVanishingPoint = vanishingPoint; } // section frame (below vanishing point) sectionFrame = contoursFrame(CvRect(0, sectionOffset, contoursFrame.cols, contoursFrame.rows - sectionOffset)); // re-apply hough transform to section frame houghLines = getHoughLines(sectionFrame, true); // shift lines downwards houghLines = shiftLines(houghLines, sectionOffset); // best line matches houghMainLines = getMainLines(houghLines); // update vanishing point according to the new section if (houghMainLines.size() >= 2) { previousLines = houghMainLines; } else { houghMainLines = previousLines; } if (houghMainLines.size() >= 2) { Point intersection = getLineIntersection(houghMainLines[0], houghMainLines[1]); if (intersection.x > 0 && intersection.x < rawFrame.cols && intersection.y > 0 && intersection.y < rawFrame.rows) { vanishingPoint = intersection; sectionOffset = intersection.y; } // get road shape roadShape = getRoadShape(rawFrame, houghMainLines[0], houghMainLines[1], vanishingPoint); } // limit lines houghLines = getLimitedLines(houghLines, sectionOffset); houghMainLines = getLimitedLines(houghMainLines, sectionOffset); // drawing frame and vehicles drawingFrame = rawFrame(CvRect(0, sectionOffset, contoursFrame.cols, contoursFrame.rows - sectionOffset)); // vehicles = getVehicles(drawingFrame); // drawing process drawLines(rawFrame, houghLines, Scalar(0, 0, 255), 2, 0); drawLines(rawFrame, houghMainLines, Scalar(20, 125, 255), 2, 0); drawRoadShape(rawFrame, roadShape, Scalar(20, 125, 255), 0.3); drawCircle(rawFrame, vanishingPoint, Scalar(20, 125, 255), 15, -1, 0); // drawRects(rawFrame, vehicles, Scalar(255, 0, 0), 1, sectionOffset); return rawFrame; }