void Coral::on_pushButton_2_clicked() { Mat imgMean,Hsv,dst,Resultado,ResultadoRgb;//imagenes a utilizar source.copyTo(Resultado); vector<cv::Mat> canalesHsv; std::vector<std::vector<cv::Point> > contours; pyrMeanShiftFiltering(source, imgMean, 20, 40, 2);//algoritmo de Mean shift filtering cvtColor(imgMean,Hsv,CV_BGR2HSV);//Cambio de espacio de color a HSV //cv::resize(Hsv,Hsv,Size(1000,700)); cv::split(Hsv,canalesHsv); threshold(canalesHsv[1],canalesHsv[1],95,255,CV_THRESH_BINARY | CV_THRESH_OTSU); Scontour::GetContourMask(canalesHsv[1],dst,contours,50,1); for(int i=0;i<contours.size();i++) drawContours(Resultado,contours,i,Scalar(255,255,255),1,8); cv::cvtColor(Resultado,ResultadoRgb,CV_BGR2RGB); // threshold(canalesHsv[2],canalesHsv[2],100,255,0); // imshow("Canal Hue",canalesHsv[0]); // imshow("Canal Saturation",canalesHsv[1]); // imshow("Canal Value",canalesHsv[2]); //MUESTRA IMAGEN EN INTERFAZ QImage img= QImage((const unsigned char*)(ResultadoRgb.data),ResultadoRgb.cols,ResultadoRgb.rows,ResultadoRgb.step,QImage::Format_RGB888);//extrae imagen ui->label_2->setPixmap(QPixmap::fromImage(img));//muestra en label el video cv::waitKey(0); }
void ImageSefmentByMeanshift(cv::Mat& src,cv::Mat& dst){ //#ifndef SPATIAL_RAD // #define SPATIAL_RAD 10 //#endif //#ifndef COLOR_RAD // #define COLOR_RAD 10 //#endif //#ifndef MAX_PRY_LEVEL // #define MAX_PRY_LEVEL 3 //#endif int spatialRad = 10; int colorRad = 10; int maxPryLevel = 1; //调用meanshift图像金字塔进行分割 pyrMeanShiftFiltering(src,dst,spatialRad,colorRad,maxPryLevel); RNG rng=theRNG(); Mat mask(dst.rows+2,dst.cols+2,CV_8UC1,Scalar::all(0)); for(int i=0;i<dst.rows;i++) //opencv图像等矩阵也是基于0索引的 for(int j=0;j<dst.cols;j++) if(mask.at<uchar>(i+1,j+1)==0) { Scalar newcolor(rng(256),rng(256),rng(256)); floodFill(dst,mask,Point(j,i),newcolor,0,Scalar::all(1),Scalar::all(1)); // floodFill(dst,mask,Point(i,j),newcolor,0,colorDiff,colorDiff); } imshow("src",src); imshow("dst",dst);waitKey(0); /*createTrackbar("spatialRad","dst",&spatialRad,80,meanshift_seg); createTrackbar("colorRad","dst",&colorRad,60,meanshift_seg); createTrackbar("maxPryLevel","dst",&maxPryLevel,5,meanshift_seg); imshow("src",src_S); imshow("dst",dst_S); waitKey();*/ }
percepunit::percepunit(Mat *src) { Mat orig, meanshift, mask, flood, reconstruction, Matrice; vector<percepunit> percepunits; // dynamic vector to store instances of percepunit. image = *src; /// Copy the original image for in-place processing. image.copyTo(orig); image.copyTo(Matrice); // morphology (supports in place operation) Mat element = getStructuringElement(MORPH_ELLIPSE, Size(5,5), Point(2, 2) ); morphologyEx(image, image, MORPH_CLOSE, element); morphologyEx(image, image, MORPH_OPEN, element); // Mean shift filtering pyrMeanShiftFiltering(image, meanshift, 10, 35, 3); RNG rng = theRNG(); // place to store ffill masks mask = Mat( meanshift.rows+2, meanshift.cols+2, CV_8UC1, Scalar::all(0) ); // Make black single-channel image. meanshift.copyTo(flood); // copy image int area; Rect *boundingRect = new Rect(); // Stored bounding box for each flooded area. // Loop through all the pixels and flood fill. for( int y = 0; y < meanshift.rows; y++ ) { for( int x = 0; x < meanshift.cols; x++ ) { if( mask.at<uchar>(y+1, x+1) == 0 ) // mask is offset from original image. { Scalar newVal( rng(256), rng(256), rng(256) ); area = floodFill( flood, mask, Point(x,y), newVal, boundingRect, Scalar::all(1), Scalar::all(1), 8|255<<8); //Extract a subimage for each flood, if the flood is large enough. if (boundingRect->width >35 && boundingRect->height >35) { Mat ROI = orig(*boundingRect); // Make a cropped reference (not copy) of the image // crop translated mask to register with original image. boundingRect->y++; boundingRect->height++; boundingRect->x++; boundingRect->width++; Mat alpha = mask(*boundingRect); // Append an instance to the vector. percepunits.push_back(percepunit(ROI, alpha, boundingRect->x-1, boundingRect->y-1, boundingRect->width-1, boundingRect->height-1)); } } } } // New Image for reconstruction reconstruction = Mat(mask.rows,mask.cols,CV_8UC3, Scalar(0,0,0)); // red background /// Loop through instances and print for(int i = 0; i <percepunits.size(); i++) { // Copy percept into reconstruction. copyPercept(percepunits[i], reconstruction); } for (int row = 0 ; row < Matrice.rows ; row++ ) { for (int col = 0 ; col < Matrice.cols ; col++ ) { Matrice.at<uchar>(row,col) = reconstruction.at<uchar>(row,col); } } *src = Matrice; /// Destructor free memory orig.release(); meanshift.release(); mask.release(); flood.release(); reconstruction.release(); Matrice.release(); }
bool MeanShiftChopper::getPicChopped(Mat* src, Mat* dst, map<Scalar, int>& m){ pyrMeanShiftFiltering( *src, *dst, spatialRad, colorRad, maxPyrLevel); floodFillPostprocess(*dst, m, Scalar::all(2)); return true; }