void Labeling::read(const cv::Mat3b &src) { std::map<cv::Vec3b, short, Vec3bCompare> palette; palette[cv::Vec3b(0, 0, 0)] = -1; // always have black background label // find all colors cv::Mat3b::const_iterator its; for (its = src.begin(); its != src.end(); ++its) palette[*its] = -1; // assign labelColors based on map order (somewhat canonical label indices) labelColors.clear(); std::map<cv::Vec3b, short, Vec3bCompare>::iterator itp; for (itp = palette.begin(); itp != palette.end(); ++itp) { itp->second = labelColors.size(); // index into labelColors labelColors.push_back(itp->first); } labelcount = labelColors.size(); // assign the color indices to the label matrix labels = cv::Mat1s(src.rows, src.cols); cv::Mat1s::iterator itl; for (its = src.begin(), itl = labels.begin(); its != src.end(); ++its, ++itl) { *itl = palette[*its]; } /* Special case: only one white label stored in RGB image (stupid). We don't want to use white color, it confuses the user. */ if ((labelcount == 2) && (labelColors[1][0] == labelColors[1][1]) && (labelColors[1][1] == labelColors[1][2])) labelColors[1] = cv::Vec3b(0, 255, 0); }
cv::Mat3b OpenCVUtil::pad_image(const cv::Mat3b& image, int paddingSize) { cv::Mat3b paddedImage = cv::Mat3b::zeros(image.rows + paddingSize * 2, image.cols + paddingSize * 2); cv::Rect roi(paddingSize, paddingSize, image.cols, image.rows); image.copyTo(paddedImage(roi)); return paddedImage; }
void RGBDImage :: fillRgbFromUserLabels(cv::Mat3b& img) const { if (!m_user_labels.data) return; const Vec3b colors[] = { Vec3b(255,0,0), Vec3b(255,255,0), Vec3b(255,0,255), Vec3b(255,255,255), Vec3b(0,255,0), Vec3b(0,255,255), Vec3b(0,0,255), }; int nb_colors = sizeof(colors) / sizeof(Vec3b); img.create(m_user_labels.size()); for_all_rc(m_user_labels) { int label = m_user_labels(r,c); if (label == 0) img(r,c) = Vec3b(0,0,0); else img(r,c) = colors[label%nb_colors]; } }
void HSColorModel :: show(cv::Mat3b& hist_img) const { ntk_assert(m_histogram.data, "Model was not build."); ntk_assert(m_histogram.dims == 2, "This is for 2d histograms"); int scale = 10; int x_bins = m_histogram.size[0]; int y_bins = m_histogram.size[1]; double min_value = 0, max_value = 0; minMaxLoc(m_histogram, &min_value, &max_value, 0, 0 ); hist_img.create(x_bins*scale,y_bins*scale); hist_img = Vec3b(0,0,0); for(int h = 0; h < x_bins; h++ ) { for(int s = 0; s < y_bins; s++ ) { float bin_val = m_histogram(h,s); int intensity = cvRound(bin_val*255.0/max_value); rectangle(hist_img, Point( h*scale, s*scale ), Point( (h+1)*scale - 1, (s+1)*scale - 1), CV_RGB(intensity,intensity,intensity), CV_FILLED ); } } }
void tgTextureModel::TextureFromImage(const tgCamera& cam, const cv::Mat3b& image) { ComputeFaceNormals(); m_face_tex_id.clear(); std::vector<bool> vertex_used(m_vertices.size(), false); for (size_t i = 0; i < m_faces.size(); i++) { TomGine::tgFace &f = m_faces[i]; for (size_t j = 0; j < f.v.size(); j++) { const unsigned &vidx = f.v[j]; if (vertex_used[vidx]) continue; vertex_used[vidx] = true; TomGine::tgVertex &v = m_vertices[vidx]; v.texCoord = cam.GetTexCoords(v.pos); v.texCoord.y = 1.0f - v.texCoord.y; // cv::Mat to OpenGL } m_face_tex_id.push_back(0); } m_tex_cv.resize(1); image.copyTo(m_tex_cv[0]); }
bool ImageWidget :: setImage(QImage& image, const cv::Mat3b& im) { bool geometryUpdated = false; if (image.isNull() || image.width() != im.cols || image.height() != im.rows) { image = QImage(im.cols, im.rows, QImage::Format_RGB32); geometryUpdated = true; } for (int r = 0; r < im.rows; ++r) { QRgb* ptr = (QRgb*) image.scanLine(r); const uchar* cv_ptr = im.ptr(r); for (int i = 0; i < im.cols; ++i) { int rgb = 0xff << 24; rgb |= (*cv_ptr++); rgb |= ((*cv_ptr++) << 8); rgb |= ((*cv_ptr++) << 16); *ptr++ = rgb; } } return geometryUpdated; }
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; } } } }
void ImageWidget :: setImage(const cv::Mat3b& im) { if (m_image.width() != im.cols || m_image.height() != im.rows) m_image = QImage(im.cols, im.rows, QImage::Format_RGB32); for (int r = 0; r < im.rows; ++r) { QRgb* ptr = (QRgb*) m_image.scanLine(r); const uchar* cv_ptr = im.ptr(r); for (int i = 0; i < im.cols; ++i) { int rgb = 0xff << 24; rgb |= (*cv_ptr++); rgb |= ((*cv_ptr++) << 8); rgb |= ((*cv_ptr++) << 16); *ptr++ = rgb; } } update(); }
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 {}; } }