std::vector<cv::Point2f> findLabelPositions(const cv::Mat3b& img, double threshold, bool visualize) { std::vector<LabelContour> labels = findLabelContours(img, threshold, visualize); printf("Num labels: %lu\n", labels.size()); fflush(stdout); if (visualize) { cv::Mat3b canvas = img * 0.25f; drawLabels(canvas, labels, cv::Scalar(255, 255, 255)); cv::imshow("detected labels", canvas); } // Connect labels with each other in order to associate them. std::vector<std::vector<LabelContour>> grouped_labels; std::vector<std::vector<cv::Point2f>> spatial_indices; std::tie(grouped_labels, spatial_indices) = connectLabelContours(labels, visualize); Camera cam; try { cam = solveCamera(grouped_labels, spatial_indices, img.size()); } catch (cv::Exception) { return {}; } return projectCube(cam); }
void apply_mask(cv::Mat3b& 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) = Vec3b(0,0,0); }
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; } } } }
std::vector<cv::Scalar> readLabelColors(const cv::Mat3b img, const std::vector<cv::Point2f>& points) { std::vector<cv::Scalar> label_colors; cv::Rect img_rect(cv::Point(0,0), img.size()); for (const auto& p :points) { if (img_rect.contains(p)) { label_colors.push_back(img(p)); } } if (label_colors.size() == 9*3) { return label_colors; } else { return {}; } }