void apply_mask(cv::Mat1b& im, const cv::Mat1b& mask) { if (!mask.data) return; ntk_assert(im.size() == mask.size(), "Wrong mask size"); for_all_rc(im) if (mask(r,c) == 0) im(r,c) = 0; }
void Binalize(const cv::Mat& depth_image, const cv::Mat& uv_map, cv::Mat1b& binary_image, short depth_threshold_mm) { assert(depth_image.size() == uv_map.size()); assert(depth_image.type() == CV_16SC1); assert(uv_map.type() == CV_32FC2); cv::Size binary_size = binary_image.size(); //Clear all pixels of binary_image binary_image = 0; cv::Size loop_size = depth_image.size(); if(depth_image.isContinuous() && uv_map.isContinuous()) { loop_size.width *= loop_size.height; loop_size.height = 1; } for(int i = 0; i < loop_size.height; ++i) { const short* depth = depth_image.ptr<short>(i); const cv::Vec2f* uv = uv_map.ptr<cv::Vec2f>(i); for(int j = 0; j < loop_size.width; ++j) { if(depth[j] < depth_threshold_mm) { int x = cvRound(uv[j][0] * binary_size.width); int y = cvRound(uv[j][1] * binary_size.height); if(0 <= x && x < binary_size.width && 0 <= y && y < binary_size.height) { binary_image.at<unsigned char>(cv::Point(x, y)) = 255; } } } } }
void ObjectDetector :: detect(const cv::Mat1f& distance_image, cv::Mat1b& mask_image, std::list<cv::Rect>& rects) { if (mask_image.size() != distance_image.size()) mask_image = cv::Mat1b(distance_image.size()); for (int r = 0; r < distance_image.rows; ++r) for (int c = 0; c < distance_image.cols; ++c) { if (distance_image(r,c) >= m_min_threshold && distance_image(r,c) <= m_max_threshold) mask_image(r,c) = 255; else mask_image(r,c) = 0; } cv::morphologyEx(mask_image, mask_image, cv::MORPH_OPEN, getStructuringElement(cv::MORPH_RECT, cv::Size(3,3))); cv::morphologyEx(mask_image, mask_image, cv::MORPH_CLOSE, getStructuringElement(cv::MORPH_RECT, cv::Size(3,3))); std::vector< std::vector<cv::Point> > contours; cv::Mat1b contour_image = mask_image.clone(); cv::findContours(contour_image, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); for (int i = 0; i < contours.size(); ++i) { cv::Rect rect = cv::boundingRect(cv::Mat(contours[i])); if (rect.area() > 300) rects.push_back(rect); } }
void BlendScreen(cv::Mat3b& target_image, const cv::Mat1b& binary_image, cv::Vec3b color) { assert(target_image.size() == binary_image.size()); cv::Size loop_size = target_image.size(); if(target_image.isContinuous() && binary_image.isContinuous()) { loop_size.width *= loop_size.height; loop_size.height = 1; } for(int i = 0; i < loop_size.height; ++i) { cv::Vec3b* target = target_image.ptr<cv::Vec3b>(i); const unsigned char* binary = binary_image.ptr<unsigned char>(i); for(int j = 0; j < loop_size.width; ++j) { if(!binary[j]) { target[j][0] = target[j][0] + color[0] - (target[j][0] * color[0]) / 255; target[j][1] = target[j][1] + color[1] - (target[j][1] * color[1]) / 255; target[j][2] = target[j][2] + color[2] - (target[j][2] * color[2]) / 255; } } } }