Ejemplo n.º 1
0
// draws the cluster
void Tcluster::draw()
{
  if (!(options&clVISITED)) {
    // if the cluster is visited for the first time this frame
    options|=clVISITED;
    // allocate memory for the contours
    cont=(Tcont *)malloc(MAX_CONT*sizeof(Tcont));
    // calculate the contours
    Tsector *s=sectors;
    int i;
    int cn=0;
    for (i=0;i<sectorsnum;i++,NEXTSECTOR(s))
      cn += s->build_contours(cont+cn);

    // sort the contours
    sort_contours(cn);
    contn=cn;
    conti=-1;
  }

  int oldconti = conti;
  // draw the lines from every contour
  for (conti++;conti<contn;conti++) {
    Tsector *s=cont[conti].s;
    int n = cont[conti].n+1;
    for (Tline *l=cont[conti].l1;--n;l=l->next_in_contour())
      l->draw( s );
  }
  conti=oldconti;
}
Ejemplo n.º 2
0
int main(int argc, char **argv)
{
    cv::Mat ms,md;
    cv::Mat cleared, cannyed;
    cv::Mat flt(1, 3, 4);

    // out names
    char clear_noise_tpl[100] = SRCDIR "/%s.clear_noise.bmp";
    char canny_tpl[100] = SRCDIR "/%s.canny.bmp";
    char split_tpls[][100] = {
        SPODIR "/%s.split1.bmp",
        SPODIR "/%s.split2.bmp",
        SPODIR "/%s.split3.bmp",
        SPODIR "/%s.split4.bmp",
        SPODIR "/%s.split5.bmp"
    };
    char buff[100] = {0};

    const char *src_path = argc >= 2 ? argv[1] : DEFALT_SRC;
    const char *fname_start = strrchr(src_path, '/');
    if (fname_start == NULL) fname_start = src_path;
    else fname_start = fname_start + 1;

    // read src
    ms = cv::imread(src_path);

    // 转灰
    cv::cvtColor(ms, md, CV_BGR2GRAY, 1);
    // cv::blur(md, ms, cv::Size(5, 5));
    // cv::bilateralFilter(md, md2, 9, 260, 260);
    // cv::GaussianBlur(md, md2, cv::Size(5, 5), 60);
    // cv::sepFilter2D(md, md2, -1, flt, flt);
    // cv::erode(md, md2, cv::Mat(), cv::Point(-1, -1), 2);
    // cv::dilate(md2, md, cv::Mat(), cv::Point(-1, -1), 2);
    // cv::fastNlMeansDenoising(md, md2, 20);

    cv::imwrite("gray.bmp", md);
    md = detect_background(md);
    cleared = md.clone();
    memset(buff, 0, sizeof(buff));
    sprintf(buff, clear_noise_tpl, fname_start);
    cv::imwrite(buff, cleared);

    // 反转
    for (int i = 0; i < md.rows; i ++) {
        for (int j = 0; j < md.cols; j ++) {
            if ((short)md.at<uchar>(i, j) == 0) {
                md.at<uchar>(i, j) = 255;
            } else {
                md.at<uchar>(i, j) = 0;
            }
        }
    }


    cv::Canny(md, cannyed, 50, 200, 3);
    memset(buff, 0, sizeof(buff));
    sprintf(buff, canny_tpl, fname_start);
    cv::imwrite(buff, cannyed);

    md = cannyed.clone();
    std::vector<std::vector<cv::Point> > contours, sorted_contours;
    std::vector<cv::Vec4i> hia;
    cv::findContours(md, contours, hia, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
    // 好像这个连接区域检测效果很差啊

    std::cout<< "\n" << contours.size() << "\n";
    for (int i = 0; i < contours.size(); i ++) {
        std::vector<cv::Point> c1 = contours.at(i);
        std::cout<< "" << c1.size() << "," << hia[i] << "\n";        

        for (int j = 0; j < contours.at(i).size(); j++) {
            // std::cout << contours.at(i).at(j) << std::endl;
            md.at<uchar>(contours.at(i).at(j)) = 255;
        }
    }

    /*
    int nc = 7;
    std::vector<std::vector<cv::Point> > ct;
    ct.push_back(contours.at(nc));

    cv::RotatedRect area = cv::minAreaRect(contours.at(nc));
    std::cout<<area.angle<<std::endl;
    area.size = cv::Size(area.size.width + 1, area.size.height + 2);

    cv::Mat mask = cv::Mat::zeros(ms.rows, ms.cols, CV_8UC1);
    cv::drawContours(mask, ct, -1, cv::Scalar(255), CV_FILLED);

    cv::Rect drect = area.boundingRect();
    cv::rectangle(mask, drect, cv::Scalar(255, 0, 0), 2);

    cv::Mat chimg = getRotatedImage(nobg, area);
    */

    // sort
    sorted_contours = sort_contours(contours, SPICNT);

    std::cout<< "\n" << sorted_contours.size() << "\n";
    for (int i = 0; i < sorted_contours.size(); i ++) {
        std::vector<cv::Point> c1 = sorted_contours.at(i);
        std::cout<< "sorted: " << c1.size() << ", " << hia[i] << "\n";        
    }

    for (int i = 0; i < sorted_contours.size(); i ++) {
        cv::RotatedRect area = cv::minAreaRect(sorted_contours.at(i));
        area.size = cv::Size(area.size.width + 1, area.size.height + 2);
        cv::Mat chimg = getRotatedImage(cleared, area);

        memset(buff, 0, sizeof(buff));
        sprintf(buff, split_tpls[i], fname_start);

        std::cout<<buff<<std::endl;
        cv::imwrite(buff, chimg);
    }

    cv::imwrite("graymy.bmp", md);

    return 0;
}