void mouse_events(int event, int x, int y, int flags, void* userdata) { if (event == cv::EVENT_LBUTTONDOWN) { cv::Point center = cv::Point(x, y); pthread_mutex_lock(&render_buffer_mutex); movement_buffer.setTo(cv::Scalar::all(0)); cv::circle(movement_buffer, center, 50, cv::Scalar(255, 255, 255), CV_FILLED); tracking_object = false; step_tracking(); pthread_mutex_unlock(&render_buffer_mutex); } else if (event == cv::EVENT_RBUTTONDOWN) { pthread_mutex_lock(&render_buffer_mutex); movement_buffer.setTo(cv::Scalar::all(0)); tracking_object = false; features_current.clear(); pthread_mutex_unlock(&render_buffer_mutex); } else if (event == cv::EVENT_MOUSEMOVE) { cursor_pos = cv::Point(x, y); } }
void normalize_residuals(cv::Mat& residual, cv::Mat& counts) { cv::Mat mask = counts == 0; counts.setTo(cv::Scalar(1.0f), mask); residual /= counts; cv::Mat mask_certainty = counts < 2; residual.setTo(cv::Scalar(0.0f), mask_certainty); }
void flood(const cv::Mat &edges, const cv::Point &anchor, const unsigned short label, cv::Mat &labels, const uchar edge, const unsigned int threshold) { assert(label > 0); assert(edges.type() == CV_8UC1); assert(labels.type() == CV_16UC1); assert(edges.rows == labels.rows); assert(edges.cols == labels.cols); cv::Mat mask(edges.rows, edges.cols, CV_8UC1, cv::Scalar::all(FLOOD_MASK_DEFAULT)); if(edges.at<uchar>(anchor.y, anchor.x) == edge) return; std::deque<cv::Point> Q; Q.push_back(anchor); const static int N_X[] = {-1, 0, 0, 1}; const static int N_Y[] = { 0,-1, 1, 0}; unsigned int size = 0; while(!Q.empty()) { cv::Point ca = Q.front(); Q.pop_front(); /// ALREADY VISITED if(labels.at<unsigned short>(ca.y, ca.x) != FLOOD_LABEL_DEFAULT || mask.at<uchar>(ca.y, ca.x) != FLOOD_MASK_DEFAULT) continue; /// NOT ALREADY VISITED mask.at<uchar>(ca.y, ca.x) = FLOOD_MASK_SET; ++size; for(int n = 0 ; n < 4 ; ++n) { cv::Point neighbour(ca.x + N_X[n], ca.y + N_Y[n]); if(neighbour.x < 0 || neighbour.x >= edges.cols || neighbour.y < 0 || neighbour.y >= edges.rows) continue; if(edges.at<uchar>(neighbour.y, neighbour.x) != edge) { Q.push_back(neighbour); } } } if(size > threshold) { labels.setTo(cv::Scalar::all(label), mask); } else { labels.setTo(cv::Scalar::all(FLOOD_LABEL_IMPLODE), mask); } }
/** * @brief create skeleton by morphology operations * @param input : input image * @param output : output image * @param kernel : structure element of the morphology operations */ void morphology_skeleton(cv::Mat &input, cv::Mat &output, cv::Mat const &kernel) { if(input.type() != CV_8U){ throw std::runtime_error(COMMON_DEBUG_MESSAGE + "input.type() != CV_8U"); } if(input.data == output.data){ output = cv::Mat::zeros(input.size(), CV_8U); }else{ output.create(input.size(), CV_8U); output.setTo(0); } cv::Mat temp; cv::Mat eroded; bool done; do { cv::erode(input, eroded, kernel); cv::dilate(eroded, temp, kernel); // temp = open(img) cv::subtract(input, temp, temp); cv::bitwise_or(output, temp, output); eroded.copyTo(input); done = (cv::countNonZero(input) == 0); } while (!done); input = output; }
DataProcessing() { //Initialize size of image rows = 800; cols = 800; //Initialize image image = cv::Mat::zeros(cols, rows, CV_8UC3); image.setTo(cv::Scalar(255, 255, 255)); //Initialize location of rover rover_rows = rows/2; rover_cols = cols/2; //Initialize compass compassValue = 0; compassStartPoint = -1; //Initialize max max_distance maxPointDifference = 20; //Pub and Sub turnPub = nh.advertise<geometry_msgs::PoseArray>("/data_processing/clusters", 2); straightPub = nh.advertise<geometry_msgs::PoseArray>("/data_processing/clusters", 4); switchRowsPub = nh.advertise<geometry_msgs::PoseArray>("/data_processing/points", 2); sub = nh.subscribe("/lidar_data/points_data", 360, &DataProcessing::ProcessingCallback, this); sub = nh.subscribe("/arduino/compass_value", 1, &DataProcessing::CompassCallback, this); sub = nh.subscribe("/arduino/compass_start_point", 1, &DataProcessing::CompassStartPointCallback, this); cv::namedWindow(OPENCV_WINDOW); }
void SrTextureShadRem::extractCandidateShadowPixels(const cv::Mat& grayFrame, const ConnCompGroup& fg, const cv::Mat& grayBg, cv::Mat& candidateShadows) { candidateShadows.create(grayFrame.size(), CV_8U); candidateShadows.setTo(cv::Scalar(0)); for (int cc = 0; cc < (int) fg.comps.size(); ++cc) { const ConnComp& object = fg.comps[cc]; for (int p = 0; p < (int) object.pixels.size(); ++p) { int x = object.pixels[p].x; int y = object.pixels[p].y; double frVal = grayFrame.at<uchar>(y, x); double bgVal = grayBg.at<uchar>(y, x); double gain = 0; if (frVal < bgVal) { gain = 1 - (frVal / bgVal) / (bgVal - frVal); } if (gain > params.gainThreshold) { candidateShadows.at<uchar>(y, x) = 255; } } } }
// determine the edge detection function g void edgeDetectionFct(cv::Mat I_Vec3b, cv::Mat &g) { // convert image from 0 ... 255 to 0 ... 1 cv::Mat I = cv::Mat(I_Vec3b.size(), CV_32FC3); I_Vec3b.convertTo(I, CV_32FC3, 1/255.0f, 0); // ATTENTION: if 32F type images have negative values included, they have to be converted! // compute the grayscale image and the edge detection function cv::Mat grayI(I.rows, I.cols, CV_32FC1); // float 0 ... 1, 1 channel grayI.setTo(0); g.create(I.rows, I.cols, CV_32FC1); g.setTo(0); for(int r = 0; r < I.rows; r++){ for(int c = 0; c < I.cols; c++){ float channel_r = (float) I.at<cv::Vec3f>(r,c)[2]; float channel_g = (float) I.at<cv::Vec3f>(r,c)[1]; float channel_b = (float) I.at<cv::Vec3f>(r,c)[0]; grayI.at<float>(r,c) = sqrtf( channel_r * channel_r + channel_g * channel_g + channel_b * channel_b ); } } blur(grayI,grayI,Size(3,3)); float norm_gradI; for(int r = 0; r < I.rows - 1; r++){ for(int c = 0; c < I.cols - 1; c++){ norm_gradI = sqrtf( (grayI.at<float>(r ,c+1) - grayI.at<float>(r,c) ) * ( grayI.at<float>(r, c+1) - grayI.at<float>(r,c) ) + (grayI.at<float>(r+1,c ) - grayI.at<float>(r,c) ) * ( grayI.at<float>(r+1,c ) - grayI.at<float>(r,c) ) ); g.at<float>(r,c) = exp(- 5 * norm_gradI ); } } }
void drawLineImage(cv::Mat& img, int cols, int rows, int w_cnt, int h_cnt, int line_width){ img = cv::Mat(rows, cols, CV_8UC3); img.setTo(0); int dh = rows/(h_cnt); int pos_y = dh; cv::line(img, cv::Point(0,line_width/2), cv::Point(cols,line_width/2), cv::Scalar(255,0,0),line_width); cv::line(img, cv::Point(0,rows-line_width/2), cv::Point(cols,rows-line_width/2), cv::Scalar(255,0,0),line_width); for (int i=1; i< h_cnt; ++i, pos_y+=dh){ cv::line(img, cv::Point(0,pos_y), cv::Point(cols,pos_y), cv::Scalar(255,255,255),line_width); } int dw = cols/(w_cnt); int pos_x = dw; cv::line(img, cv::Point(line_width/2,0), cv::Point(line_width/2,cols), cv::Scalar(255,0,0),line_width); cv::line(img, cv::Point(cols-line_width/2-5,0), cv::Point(cols-line_width/2-5,rows), cv::Scalar(255,0,0),line_width); for (int i=1; i< w_cnt; ++i, pos_x+=dw){ cv::line(img, cv::Point(pos_x,0), cv::Point(pos_x, rows), cv::Scalar(255,255,255),line_width); } }
void PixelEnvironmentModel::getSigmas(cv::Mat& vars, bool normalize){ if (vars.rows != height_ || vars.cols != width_ || vars.type() != CV_32FC1){ ROS_INFO("new sigma imags"); vars = cv::Mat(height_,width_,CV_32FC1); } vars.setTo(0); for (int y=0; y<height_; ++y) for (int x=0; x<width_; ++x){ if (mask_set && mask_.at<uchar>(y,x) == 0) continue; if (gaussians[y][x].initialized) vars.at<float>(y,x) = gaussians[y][x].sigma(); } if (normalize){ double min_val, max_val; cv::minMaxLoc(vars,&min_val,&max_val, NULL,NULL, mask_); ROS_INFO("normalizing: %f %f", min_val, max_val); vars = (vars-min_val)/(max_val-min_val); } }
/// point is foreground if it is not within N sigma void PixelEnvironmentModel::getForeground_prob(const Cloud& cloud, float N, cv::Mat& foreground){ if (foreground.rows != height_ || foreground.cols != width_ || foreground.type() != CV_8UC1){ foreground = cv::Mat(height_,width_,CV_8UC1); } foreground.setTo(0); for (int y=0; y<height_; ++y) for (int x=0; x<width_; ++x){ if (mask_set && mask_.at<uchar>(y,x) == 0) continue; bool inited = gaussians[y][x].initialized; float current = norm(cloud.at(x,y)); if (current < 0) continue; // nan point if (!inited || (current < gaussians[y][x].mean && !gaussians[y][x].isWithinNSigma(current,N))){ foreground.at<uchar>(y,x) = 255; } } cv::medianBlur(foreground,foreground,3); // cv::erode(foreground,foreground,cv::Mat(),cv::Point(-1,-1),2); // cv::dilate(foreground,foreground,cv::Mat(),cv::Point(-1,-1),2); }
void Background_substraction::showMask(const std::vector<cv::Point2i>& mask, cv::Mat& img){ img.setTo(0); for (uint i=0; i<mask.size(); ++i){ img.at<uchar>(mask[i].y,mask[i].x) = 255; } }
void segmentHand(cv::Mat &mask, Rect ®ion, const cv::Mat &depth) { CV_Assert(mask.type() == CV_8UC1); CV_Assert(depth.type() == CV_16UC1); CV_Assert(mask.rows == depth.rows); CV_Assert(mask.cols == depth.cols); mask.setTo(EMPTY); pair<int, int> current = searchNearestPixel(depth, region); if (current.first < 0){ return; } int rowcount = region.height, colcount = region.width; Mat visited(depth.rows, depth.cols, CV_8U, Scalar(0)); double mean = depth.at<unsigned short>(current.first,current.second); int minx=depth.cols,miny=depth.rows,maxx=0,maxy=0; unsigned short dv = 0; int pixelcount = 1; _pixels.push(current); while((!_pixels.empty()) & (pixelcount < _maxObjectSize)) { current = _pixels.front(); _pixels.pop(); dv = depth.at<unsigned short>(current.first,current.second); if (current.first < minx) minx = current.first; else if (current.first > maxx) maxx = current.first; if (current.second < miny) miny = current.second; else if (current.second > maxy) maxy = current.second; if ( current.first + 1 < rowcount+region.y && visited.at<uchar>(current.first+1, current.second) == 0 ){ visited.at<uchar>(current.first+1, current.second) = 255; processNeighbor(pixelcount,mean,mask,current.first + 1,current.second,depth); } if ( current.first - 1 > -1 + region.y && visited.at<uchar>(current.first-1, current.second) == 0){ visited.at<uchar>(current.first-1, current.second) = 255; processNeighbor(pixelcount,mean,mask,current.first - 1,current.second,depth); } if ( current.second + 1 < colcount + region.x && visited.at<uchar>(current.first, current.second+1) == 0){ visited.at<uchar>(current.first, current.second+1) = 255; processNeighbor(pixelcount,mean,mask,current.first,current.second + 1,depth); } if( current.second - 1 > -1 + region.x && visited.at<uchar>(current.first, current.second-1) == 0){ visited.at<uchar>(current.first, current.second-1) = 255; processNeighbor(pixelcount,mean,mask,current.first,current.second - 1,depth); } } }
//computes dot-product of each column in src with each basis vector inline void dot(const cv::Mat &src, cv::Mat &dst) const { dst.create(basis.size(), src.cols, CV_64FC1); dst.setTo(0); for (size_t i = 0; i < basis.size(); i++) { basis[i].dot(src, dst.row(i)); } }
/** * @brief QueryImage::buildObtainedMat Build the mat representing the obtained labeling * @param result Mat in which the function will print the result */ void QueryImage::buildObtainedMat(cv::Mat &result, set<int> *usedLabels){ result.create(image.getImage()->size(), CV_8UC1); result.setTo(cv::Scalar::all(0)); if(usedLabels!=NULL) usedLabels->clear(); for(uint i=0; i<superPixelList.size(); ++i){ if(usedLabels!=NULL) usedLabels->insert(superPixelList[i]->getLabel()); superPixelList[i]->printToMat(result); } }
int _tmain(int argc, _TCHAR* argv[]) { int display_x,display_y; display_x=480; display_y=480; img.create(display_x, display_y, CV_8UC3); img.setTo(cv::Scalar(255,255,255)); point eye(eye_x,eye_y,eye_z); camera cam(eye,display_x,display_y);//display_x,display_y); scenes scene; ball light1(point(20,240,26),5,color(255,255,255)); light1.set(0.2,0.2,8,0.3,0.2,0.2,0.2,true); //ball light2(point(360,340,20),5,color(255,255,255)); //light2.set(0.2,0.2,8,0.3,0.2,0.2,0.2,true); scene.lights.push_back(&light1); // scene.lights.push_back(&light2); ball ball1(point(360,370,100),30,color(0,0,0));//͸Ã÷ ball1.set(0.05,0.05,8,0.05,1,1,0.1,false); poly4 po1(point(150,150,100),point(200,100,100),point(200,200,200),point(220,200,80),color(50,205,50)); po1.set(0.2,0.2,8,0.3,0.4,0.5,0.2,false); ball ball2(point(360,290,100),30,color(255,0,0)); ball2.set(0.2,0.2,8,0.3,0.2,0.1,0.2,false); cuboid cub1(point(400,100,30),point(420,140,50),point(400,160,30),point(380,120,10),point(340,100,90),point(360,140,110),point(340,160,90),point(320,120,70),color(0,155,255)); cub1.set(0.2,0.2,8,0.3,0.4,0.5,0.2,false); scene.objects.push_back(&po1); Floor floor1(430); floor1.set(0.2,0.2,8,0.3,0.2,0.2,0.2,false); scene.objects.push_back(&ball2); scene.objects.push_back(&ball1); // scene.objects.push_back(&light2); scene.objects.push_back(&light1); scene.objects.push_back(&cub1); scene.objects.push_back(&floor1); #pragma omp parallel for for(int i=0;i<display_x;i++) for(int j=0;j<display_y;j++) { *Triangle::fout2<<"i="<<i<<" j="<<j; //paint(i,j,scene.trace(ray(eye,vector3(i-eye_x,j-eye_y,-eye_z)),depth)); //paint(i,j,scene.trace(ray(point(i,j,0),vector3(0,0,1)),3)); paint(i,j,(scene.trace(ray(eye,vector3(i-0.5-eye_x,j-0.5-eye_y,-eye_z)),depth) +scene.trace(ray(eye,vector3(i-0.5-eye_x,j+0.5-eye_y,-eye_z)),depth) +scene.trace(ray(eye,vector3(i-0.5-eye_x,j-eye_y,-eye_z)),depth) +scene.trace(ray(eye,vector3(i-eye_x,j+0.5-eye_y,-eye_z)),depth) +scene.trace(ray(eye,vector3(i+0.5-eye_x,j-eye_y,-eye_z)),depth) +scene.trace(ray(eye,vector3(i-eye_x,j-0.5-eye_y,-eye_z)),depth) +scene.trace(ray(eye,vector3(i-eye_x,j-eye_y,-eye_z)),depth) +scene.trace(ray(eye,vector3(i+0.5-eye_x,j-0.5-eye_y,-eye_z)),depth) +scene.trace(ray(eye,vector3(i+0.5-eye_x,j+0.5-eye_y,-eye_z)),depth))*(1.0/9)); *Triangle::fout2<<endl; } cv::imshow("show", img); cv::waitKey(0); return 0; }
void TImgStack::stack(cv::Mat& dest) { if(N_images > 0) { dest = *(img[0]); for(size_t i=1; i<N_images; i++) { dest += *(img[i]); } } else { dest.setTo(0); } }
std::vector<PlanarHorizontalSegment> PlaneExtractor::ExtractHorizontalPlanes(cv::Mat& pointCloud, cv::Mat ouputBGRImg) { //Constanst for normal calculation int normalsBlurSize = 8; float normalsZThreshold = 0.8f; // Plane segmentation RANSAC int minPointsForPlane = 15000; double distThresholdForPlane = 0.01; int maxIterations = 1000; std::vector<PlanarHorizontalSegment> horizontalPlanes; // Removing floor and far away points //"validPoints" is a mask cv::Mat validPoints, notValidPoints; cv::inRange( pointCloud , cv::Scalar( -2, -2, 0.10) , cv::Scalar( 2, 2, 1.5 ), validPoints); //Set to zero all far and floor points //In to not taking them into accounto for ransac cv::bitwise_not( validPoints, notValidPoints ); pointCloud.setTo( 0.0, notValidPoints); cv::Mat normals; cv::Mat bluredPointCloud; cv::blur( pointCloud, bluredPointCloud, cv::Size(normalsBlurSize,normalsBlurSize)); // Calculating Normals GetNormalsCross( bluredPointCloud , normals); // Getting horizontal normals Mask only cv::Mat horNormalsMask; cv::inRange( normals, cv::Scalar( -1, -1, normalsZThreshold ), cv::Scalar( 1, 1, 1 ), horNormalsMask); std::vector< cv::Point2i > horNormalsIndexes; if( !horNormalsMask.isContinuous() ){ return horizontalPlanes; } cv::Mat planesMask; cv::findNonZero( horNormalsMask , horNormalsIndexes ); horizontalPlanes = GetPlanesRANSAC( pointCloud, horNormalsIndexes, minPointsForPlane, distThresholdForPlane, maxIterations, planesMask); for(int i=0; i< horizontalPlanes.size(); i++) { //cv::Scalar color = cv::Scalar( rand() % 256 , rand() % 256 , rand() % 256 ); cv::Scalar color = cv::Scalar(196 , 127 , 127); for(size_t j=0; j < horizontalPlanes[i].indices.size(); j++) { ouputBGRImg.at<cv::Vec3b>(horizontalPlanes[i].indices[j])[0] = color[0]; ouputBGRImg.at<cv::Vec3b>(horizontalPlanes[i].indices[j])[1] = color[1]; ouputBGRImg.at<cv::Vec3b>(horizontalPlanes[i].indices[j])[2] = color[2]; } } return horizontalPlanes; }
void TemplateMatchCandidates::findCandidates( const cv::Mat &templ, const cv::Mat &templMask, cv::Mat &candidates, int maxWeakErrors, float maxMeanDifference) { CV_Assert( templ.type() == CV_MAKETYPE(CV_8U, _integrals.size()) && templ.size() == _templateSize && (templMask.empty() || templMask.size() == _templateSize)); candidates.create( _image.size().height - templ.size().height + 1, _image.size().width - templ.size().width + 1, CV_8UC1); candidates.setTo(255); std::vector< cv::Rect > blocks = _blocks; removeInvalidBlocks(templMask, blocks); cv::Mat_<int> referenceClass; cv::Scalar templMean; weakClassifiersForTemplate(templ, templMask, blocks, referenceClass, templMean); // For each channel we loop over all possible template positions and compare with classifiers. for (size_t i = 0; i < _integrals.size(); ++i) { cv::Mat_<int> &integral = _integrals[i]; const int *referenceClassRow = referenceClass.ptr<int>(i); // For all template positions ty, tx (top-left template position) for (int ty = 0; ty < candidates.rows; ++ty) { uchar *outputRow = candidates.ptr<uchar>(ty); for (int tx = 0; tx < candidates.cols; ++tx) { if (!outputRow[tx]) continue; outputRow[tx] = compareWeakClassifiers( integral, tx, ty, templ.size(), blocks, referenceClassRow, (float)templMean[i], maxMeanDifference, maxWeakErrors); } } } }
void init_cv_buffers() { bgr_buffer = cv::Mat(HEIGHT, WIDTH, CV_8UC3); depth_buffer = cv::Mat(HEIGHT, WIDTH, CV_8UC1); foreground_mask = cv::Mat(HEIGHT, WIDTH, CV_8UC1); movement_buffer = cv::Mat(HEIGHT, WIDTH, CV_8UC1); image_prev = cv::Mat(HEIGHT, WIDTH, CV_8UC1); image_current = cv::Mat(HEIGHT, WIDTH, CV_8UC1); movement_buffer.setTo(cv::Scalar::all(0)); background_subtractor = cv::BackgroundSubtractorMOG2(5, 16, true); }
void PixelEnvironmentModel::getMeans(cv::Mat& means){ if (means.rows != height_ || means.cols != width_ || means.type() != CV_32FC1){ means = cv::Mat(height_,width_,CV_32FC1); } means.setTo(0); for (int y=0; y<height_; ++y) for (int x=0; x<width_; ++x){ means.at<float>(y,x) = gaussians[y][x].mean; } }
void PixelEnvironmentModel::getInitialized(cv::Mat& inited){ if (inited.rows != height_ || inited.cols != width_ || inited.type() != CV_8UC1){ inited = cv::Mat(height_,width_,CV_8UC1); } inited.setTo(0); for (int y=0; y<height_; ++y) for (int x=0; x<width_; ++x){ if (gaussians[y][x].initialized) inited.at<uchar>(y,x) = 255; } }
void DrawScreen(cv::Mat& screen) { screen.setTo(0); // Draw target robot pose draw_robot(screen, pose_target_x, pose_target_y, pose_target_theta, 0,255,0); // Draw estimated robot pose draw_robot(screen, pose_x, pose_y, pose_theta, 0,0,255); cv::imshow("Human Interface", screen); cv::waitKey(1); }
void UtilsFlow::GetFlowImage(cv::Mat& U,cv::Mat& V, cv::Mat& dst, float maxMotion) { const float pi=3.1415926535897932384626433832795f; dst.create(U.rows,U.cols, CV_8UC3); dst.setTo(cv::Scalar(0)); float maxLength=FindMaxLength(U,V); if (maxMotion>0.0f) maxLength=maxMotion; if (maxLength==0.0f) maxLength=1.0f; if (ncols==0) makecolorwheel(); for (int y = 0; y < U.rows; y++) { for (int x = 0; x < U.cols; x++) { cv::Vec3b out = dst.at<cv::Vec3b>(y,x); float u,v; u = U.at<float>(y,x); v = V.at<float>(y,x); if (fabs(u)<1e9 && fabs(v)<1e9) { u=u/maxLength; v=v/maxLength; float rad = sqrt(u * u + v * v); float a = atan2(-v, -u) / pi; float fk = (a + 1.0f) / 2.0f * (ncols-1); int k0 = (int)fk; int k1 = (k0 + 1) % ncols; float f = fk - k0; //f = 0; // uncomment to see original color wheel for (int b = 0; b < 3; b++) { float col0 = colorwheel[k0][b] / 255.0f; float col1 = colorwheel[k1][b] / 255.0f; float col = (1 - f) * col0 + f * col1; if (rad <= 1) col = 1 - rad * (1 - col); // increase saturation with radius else { col *= .75; // out of range out.val[b]=(int)(255.0 * col); } } } dst.at<cv::Vec3b>(y,x) = out; } } }
void SrTextureShadRem::getShadows(const cv::Mat& grayFrame, const cv::Mat& candidateShadows, const cv::Mat& grayBg, cv::Mat& shadows) { gaborFilter.filter(grayFrame, frProjections, candidateShadows, params.neighborhood); gaborFilter.filter(grayBg, bgProjections, candidateShadows, params.neighborhood); GaborFilter::getDistance(frProjections, bgProjections, distances, candidateShadows); cv::threshold(distances, threshDistances, params.distThreshold, 255, cv::THRESH_BINARY_INV); threshDistances.convertTo(distanceMask, CV_8U); shadows.create(grayFrame.size(), CV_8U); shadows.setTo(cv::Scalar(0)); distanceMask.copyTo(shadows, candidateShadows); }
void af::displayBlendshapeWeights (const Eigen::VectorXf &values, cv::Mat &img, const int bar_height, const int bar_width) { int num_bs = values.rows (); PCL_ERROR ("Num bs: %d\n", num_bs); img = cv::Mat::zeros (bar_height * 2, bar_width * num_bs, CV_8UC3); img.setTo (cv::Scalar (255, 255, 255)); cv::Point origin (0, bar_height); PCL_ERROR ("Origin: %d %d\n", origin.x, origin.y); // cv::rectangle (img, cv::Rect (origin, origin + cv::Point (num_bs * bar_width, -10 - 50)), cv::Scalar (50, 50, 50), CV_FILLED); // cv::rectangle (img, cv::Rect (origin, origin + scale * cv::Point (14 * 4, -10 - 50)), cv::Scalar (15, 230, 230), CV_FILLED); // cv::rectangle (img, cv::Rect (origin, origin + scale * cv::Point (num_bs * 4, -10 - 50)), cv::Scalar (180, 180, 180)); for (size_t i = 0; i < num_bs; ++i) { cv::Point location = origin + cv::Point (bar_width * i, 0); // char str[4]; // sprintf (str, "%zu", i); // cv::putText (img, str, location, cv::FONT_HERSHEY_PLAIN, 0.35, cv::Scalar (255, 255, 255)); if (fabs (values (i)) < 0.1) { #ifdef OPENCV_3_0 cv::rectangle(img, cv::Rect(location + cv::Point(0, -10), location + cv::Point(bar_width, -10 - 5)), cv::Scalar(0, 0, 255), -1); #else cv::rectangle(img, cv::Rect(location + cv::Point(0, -10), location + cv::Point(bar_width, -10 - 5)), cv::Scalar(0, 0, 255), CV_FILLED); #endif cv::rectangle (img, cv::Rect (location + cv::Point (0, -10), location + cv::Point (bar_width, -10 - 5)), cv::Scalar (150, 150, 150)); } else { #ifdef OPENCV_3_0 cv::rectangle(img, cv::Rect(location + cv::Point(0, -10), location + cv::Point(bar_width, -10 - values(i) * bar_height)), cv::Scalar(255, 0, 0), 2); #else cv::rectangle(img, cv::Rect(location + cv::Point(0, -10), location + cv::Point(bar_width, -10 - values(i) * bar_height)), cv::Scalar(255, 0, 0), CV_FILLED); #endif cv::rectangle (img, cv::Rect (location + cv::Point (0, -10), location + cv::Point (bar_width, -10 - values (i) * bar_height)), cv::Scalar (150, 150, 150)); } } }
//=========================================================================== // Computing the image gradient void Grad(const cv::Mat& im, cv::Mat& grad) { /*float filter[3] = {1, 0, -1}; float dfilter[1] = {1}; cv::Mat filterX = cv::Mat(1,3,CV_32F, filter).clone(); cv::Mat filterY = cv::Mat(1,1,CV_32F, dfilter).clone(); cv::Mat gradX; cv::Mat gradY; cv::sepFilter2D(im, gradX, CV_32F, filterY, filterX, cv::Point(-1,-1), 0); cv::sepFilter2D(im, gradY, CV_32F, filterX.t(), filterY, cv::Point(-1,-1), 0); cv::pow(gradX,2, gradX); cv::pow(gradY,2, gradY); grad = gradX + gradY; grad.row(0).setTo(0); grad.col(0).setTo(0); grad.col(grad.cols-1).setTo(0); grad.row(grad.rows-1).setTo(0); */ // A quicker alternative int x,y,h = im.rows,w = im.cols; float vx,vy; // Initialise the gradient grad.create(im.size(), CV_32F); grad.setTo(0.0f); cv::MatIterator_<float> gp = grad.begin<float>() + w+1; cv::MatConstIterator_<float> px1 = im.begin<float>() + w+2; cv::MatConstIterator_<float> px2 = im.begin<float>() + w; cv::MatConstIterator_<float> py1 = im.begin<float>() + 2*w+1; cv::MatConstIterator_<float> py2 = im.begin<float>() + 1; for(y = 1; y < h-1; y++) { for(x = 1; x < w-1; x++) { vx = *px1++ - *px2++; vy = *py1++ - *py2++; *gp++ = vx*vx + vy*vy; } px1 += 2; px2 += 2; py1 += 2; py2 += 2; gp += 2; } }
void drawLines(cv::Mat& dst, const std::vector<cv::Vec2f>& lines) { dst.setTo(cv::Scalar::all(0)); for (size_t i = 0; i < lines.size(); ++i) { float rho = lines[i][0], theta = lines[i][1]; cv::Point pt1, pt2; double a = std::cos(theta), b = std::sin(theta); double x0 = a*rho, y0 = b*rho; pt1.x = cvRound(x0 + 1000*(-b)); pt1.y = cvRound(y0 + 1000*(a)); pt2.x = cvRound(x0 - 1000*(-b)); pt2.y = cvRound(y0 - 1000*(a)); cv::line(dst, pt1, pt2, cv::Scalar::all(255)); } }
void calcHistGold(const cv::Mat& src, cv::Mat& hist) { hist.create(1, 256, CV_32SC1); hist.setTo(cv::Scalar::all(0)); int* hist_row = hist.ptr<int>(); for (int y = 0; y < src.rows; ++y) { const uchar* src_row = src.ptr(y); for (int x = 0; x < src.cols; ++x) ++hist_row[src_row[x]]; } }
bool CV_GpuMatOpSetToTest::testSetTo(cv::Mat& cpumat, gpu::GpuMat& gpumat, const cv::Mat& cpumask, const cv::gpu::GpuMat& gpumask) { cpumat.setTo(s, cpumask); gpumat.setTo(s, gpumask); double ret = norm(cpumat, gpumat, NORM_INF); if (ret < std::numeric_limits<double>::epsilon()) return true; else { ts->printf(CvTS::LOG, "\nNorm: %f\n", ret); return false; } }
void addSpikeNoise(cv::Mat& image, int frequency) { cv::Mat_<uchar> mask(image.size(), 0); for (int y = 0; y < mask.rows; ++y) { for (int x = 0; x < mask.cols; ++x) { if (cvtest::TS::ptr()->get_rng().uniform(0, frequency) < 1) mask(y, x) = 255; } } image.setTo(cv::Scalar::all(255), mask); }