void Extractor::extract(MatSet& srcSet, Region& result) { //いったん手続き型でアルゴリズムを作成する //TODO : メソッド分割すべし ChannelSet channelSet(srcSet); //エッジ画像を取得する vector<Mat> rawEdges; _edgeFactory.createEdges(srcSet, rawEdges, channelSet); Mat dstEdgeImg(srcSet.size().height,srcSet.size().width, CV_8UC1, 255); revMergeEdges(rawEdges, _previousRegion.expectedRoi(), dstEdgeImg); erode(dstEdgeImg, dstEdgeImg, cv::Mat(), Point(-1,-1), 1); imshow("revEdge", dstEdgeImg); Mat mat = Mat::zeros(srcSet.size(), CV_8UC1); if(_indexOfMaxArea >=0){ int yBegin = _previousRegion.expectedRoi().y; int yEnd = _previousRegion.expectedRoi().y+_previousRegion.expectedRoi().height; int xBegin = _previousRegion.expectedRoi().x; int xEnd = _previousRegion.expectedRoi().x+_previousRegion.expectedRoi().width; for(int y=yBegin; y<yEnd; y++) { for(int x=xBegin; x<xEnd; x++) { // if(_featureReference.isWithinThreshold(srcSet, Point(x,y)) ) { // L(mat,x,y) = 255; // } if(L(channelSet.crMat(),x,y) >= _binarizationThreshold[7] && L(channelSet.gMat(),x,y) <=_binarizationThreshold[1] && L(channelSet.rMat(),x,y) >=_binarizationThreshold[2]) { L(mat,x,y) = 255; } else if(L(channelSet.sMat(),x,y) >=_binarizationThreshold[4] && L(channelSet.crMat(),x,y) >= 154 && L(channelSet.gMat(),x,y) <= 40 && L(channelSet.bMat(),x,y) <= 41 ) { L(mat,x,y) = 255; } } } } else { for(int y=0; y<srcSet.size().height; y++) { for(int x=0; x<srcSet.size().width; x++) { // if(_featureReference.isWithinThreshold(srcSet, Point(x,y)) ) { // L(mat,x,y) = 255; // } if(L(channelSet.crMat(),x,y) >= _binarizationThreshold[7] && L(channelSet.gMat(),x,y) <=_binarizationThreshold[1] && L(channelSet.rMat(),x,y) >=_binarizationThreshold[2]) { L(mat,x,y) = 255; } else if(L(channelSet.sMat(),x,y) >=_binarizationThreshold[4] && L(channelSet.crMat(),x,y) >= 154 && L(channelSet.gMat(),x,y) <= 40 && L(channelSet.bMat(),x,y) <= 41 ) { L(mat,x,y) = 255; } } } } dilate(mat, mat, cv::Mat(), Point(-1,-1), _extractionManager.dilateCount()); erode(mat, mat, cv::Mat(), Point(-1,-1), _extractionManager.erodeCount()); // imshow("colorExtract", mat); //最大面積 vPs contours; findContours(mat, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE); vector<int> indexiesOfTop3Area = calcIndexiesOfTop3Area(contours); // _indexOfMaxArea = calcIndexOfMaxArea(contours); _indexOfMaxArea = indexiesOfTop3Area[0]; Mat mat2 = Mat::zeros(srcSet.size(), CV_8UC1); if(indexiesOfTop3Area[1] >= 0) { Point2f vertices[4]; _previousRegion.rotatedRect().points(vertices); vector<Point> allPoints = contours[indexiesOfTop3Area[0]]; if(indexiesOfTop3Area[2] >= 0) { drawContours(mat2, contours, indexiesOfTop3Area[1], Scalar(255, 255, 255), CV_FILLED, LINK_EIGHT); drawContours(mat2, contours, indexiesOfTop3Area[2], Scalar(255, 255, 255), CV_FILLED, LINK_EIGHT); drawContours(mat2, contours, _indexOfMaxArea, Scalar(255, 255, 255), CV_FILLED, LINK_EIGHT); for(int i=1; i<3; i++) { for(Point point :contours[indexiesOfTop3Area[i]]) { if(isInROI(point, vertices)) { allPoints.push_back(point); } } } } else { drawContours(mat2, contours, indexiesOfTop3Area[1], Scalar(255, 255, 255), CV_FILLED, LINK_EIGHT); drawContours(mat2, contours, _indexOfMaxArea, Scalar(255, 255, 255), CV_FILLED, LINK_EIGHT); for(int i=1; i<2; i++) { for(Point point :contours[indexiesOfTop3Area[i]]) { if(isInROI(point, vertices)) { allPoints.push_back(point); } } } } RotatedRect rect = minAreaRect(Mat(allPoints)); // Point2f vertices[4]; // rect.points(vertices); // for (int i = 0; i < 4; i++) line(mat2, vertices[i], vertices[(i+1)%4], Scalar(255,255,255), 4, 8, 0); result.setContour(contours[_indexOfMaxArea]);//TODO:本来は結合した輪郭を入れるか別にいれる必要がある result.setMaskImg(mat2); result.setRotatedRect(rect); result.calcRoiWithRotatedRect(); result.calcExpectedRoiConsideringMoveWithRotatedRect(_previousRegion); _previousRegion = result; // imshow("merge", mat2); } else { drawContours(mat2, contours, _indexOfMaxArea, Scalar(255, 255, 255), CV_FILLED, LINK_EIGHT); result.setMaskImg(mat2); if(_indexOfMaxArea>=0) { result.setContour(contours[_indexOfMaxArea]); result.calcRotatedRect(); result.calcRoi(); result.calcExpectedRoiConsideringMove(_previousRegion); } _previousRegion = result; // imshow("merge", mat2); } //エッジ画像を取得する // // _edgeService.extractEdge(rawEdges, areamaxRegion.rois()[0], dstEdgeImg); // // imshow("edge", dstEdgeImg); // //残ったエッジ画像と色による抽出画像を合成する // bitwise_or(areamaxRegion.maskImg(), dstEdgeImg, dstEdgeImg); // drawContours(dstEdgeImg, areamaxRegion.contours(), 0, Scalar(255, 255, 255), CV_FILLED, LINK_EIGHT); // int minSize = 200; // _contourService.fillContours(dstEdgeImg, minSize); }