示例#1
0
// calculate encoded size
int BitMask::RLEsize() const {
    // Next input byte
    Byte *src = m_pBits;
    // left to process
    int sz = Size();
    // current non-repeated byte count
    int oddrun = 0;
    // Simulate an odd run flush
#define SIMFLUSH if (oddrun) { osz += oddrun + 2; oddrun = 0; }
    // output size, start with size of end marker
    int osz = 2;
    while (sz) {
        int run = run_length(src, sz);
        if (run < MIN_RUN) {
            src++;
            sz--;
            if (MAX_RUN == ++oddrun)
                SIMFLUSH;
        } else {
            SIMFLUSH;
            src += run;
            sz -= run;
            osz += 3; // Any run is 3 bytes
        }
    }
    SIMFLUSH;
    return osz;
}
示例#2
0
文件: hw2.cpp 项目: j0x7c4/cvhw
void CVHW::connected_components( cv::Mat& t_image, int threshold ,int flag )
{
	binary( t_image);
	run_length(t_image,flag);
	if ( flag == 4 )
		printf("4-connected\n");
	else
		printf("8-connected\n");
	printf("Each connected-component contains at least %d pixels\n",threshold);
	std::map<int,BOUNDING_BOX> pixel_in_component;
	std::vector<BOUNDING_BOX> draw_bounding_boxes;
	
	//count how many connected components
	for ( int i = 1 ; i<=runs ; i++ )
	{
		pixel_in_component[perm_label[i]].sum_pixel+=end_col[i]-start_col[i]+1;
		if ( pixel_in_component[perm_label[i]].top_left_x < 0 )
			pixel_in_component[perm_label[i]].top_left_x = row[i];
		if ( pixel_in_component[perm_label[i]].top_left_y < 0 || pixel_in_component[perm_label[i]].top_left_y > start_col[i] )
			pixel_in_component[perm_label[i]].top_left_y = start_col[i];
		if ( pixel_in_component[perm_label[i]].bottom_right_x < row[i] )
			pixel_in_component[perm_label[i]].bottom_right_x = row[i];
		if ( pixel_in_component[perm_label[i]].bottom_right_y < end_col[i] )
			pixel_in_component[perm_label[i]].bottom_right_y = end_col[i];
	}

	//fliter small connected componets
	for ( std::map<int,BOUNDING_BOX>::iterator iter= pixel_in_component.begin(); iter != pixel_in_component.end() ; iter++ )
	{
		if ( iter->second.sum_pixel >= threshold )
		{
			iter->second.centroid_x =( iter->second.top_left_x + iter->second.bottom_right_x ) / 2;
			iter->second.centroid_y =( iter->second.top_left_y + iter->second.bottom_right_y ) / 2;
			draw_bounding_boxes.push_back(iter->second);
			printf("Label:%3d\t",iter->first);
			printf("Pixels:%3d\t",iter->second.sum_pixel);
			printf("Top-left Corner: (%3d,%3d)\t",iter->second.top_left_x,iter->second.top_left_x);
			printf("Bottom-right Corner: (%3d,%3d)\t",iter->second.bottom_right_x,iter->second.bottom_right_y);
			printf("\n");
		}
	}

	for ( std::vector<BOUNDING_BOX>::iterator iter = draw_bounding_boxes.begin(); iter!=draw_bounding_boxes.end() ; iter++ )
	{
		draw_connected_components(t_image,*iter);
		//break;
	}
}
示例#3
0
//
// RLE compressed size is bound by n + 4 + 2 * (n - 1) / 32767
//
int BitMask::RLEcompress(Byte *dst) const {
    assert(dst);
    // Next input byte
    Byte *src = m_pBits;
    Byte *start = dst;
    // left to process
    int sz = Size();

    // Pointer to current sequence count
    Byte *pCnt = dst;
    // non-repeated byte count
    int oddrun = 0;

// Store a two byte count in low endian
#define WRITE_COUNT(val) if (true) { *pCnt++ = Byte(val & 0xff); *pCnt++ = Byte(val >> 8); }
// Flush an existing odd run
#define FLUSH if (oddrun) { WRITE_COUNT(oddrun); pCnt += oddrun; dst = pCnt + 2; oddrun = 0; }

    dst += 2; // Skip the space for the first count
    while (sz) {
        int run = run_length(src, sz);
        if (run < MIN_RUN) { // Use one byte
            *dst++ = *src++;
            sz--;
            if (MAX_RUN == ++oddrun)
                FLUSH;
        } else { // Found a run
            FLUSH;
            WRITE_COUNT(-run);
            *pCnt++ = *src;
            src += run;
            sz -= run;
            dst = pCnt + 2; // after the next marker
        }
    }
    // cppcheck-suppress uselessAssignmentPtrArg
    FLUSH;
    WRITE_COUNT(EOT); // End marker
    // return compressed output size
    return int(pCnt - start);
}
int main() {
    rpn_item add([](const int x, const int y) { return x + y; });
    rpn_item sub([](const int x, const int y) { return x - y; });
    rpn_item mult([](const int x, const int y) { return x * y; });
    rpn_item div([](const int x, const int y) {
            if (y == 0 || x % y != 0)
                return BAD_DIVIDE;
            return x / y;
        }
    );
    std::vector<rpn_item> ops {add, sub, mult, div};
    int max = 0;
    std::set<int> best;

    for (int i = 1; i <= 9; ++i) {
        for (int j = i + 1; j <= 9; ++j) {
            for (int k = j + 1; k <= 9; ++k) {
                for (int l = k + 1; l <= 9; ++l) {
                    std::set<int> all;
                    std::set<int> poss{i, j, k, l};
                    std::stack<rpn_item> equ;
                    perms(poss, all, equ, ops);
                    auto test = run_length(all);
                    if (test >= max) {
                        max = test;
                        best = std::move(poss);
                    }
                }
            }
        }
    }

    std::cout << max << std::endl;
    for (auto i : best)
        std::cout << i;
    std::cout << std::endl;
    return 0;
}
int main()
{
  printf("# Random Number Generation & Probability Distributions\n\n");

  printf("## Random Number Testing\n\n");
  autoseed_old_c_rand();
  autoseed_aes();
  autoseed_von_neumann();
  autoseed_mesrene_twister();
  for(int i = 1; i <= MAX_TRIES; i ++) {
    word32 generated_rand_upper[ARRAY_MAX_SIZE],
          generated_rand_lower[ARRAY_MAX_SIZE],
          generated_VN[ARRAY_MAX_SIZE],
          generated_MT[ARRAY_MAX_SIZE],
          generated_AES[ARRAY_MAX_SIZE];

    printf("### Try %d\n\n", i);

    printf("#### %d generated numbers\n\n", ARRAY_MAX_SIZE);
    printf("```\n");
    printf(CSV_HEADER);
    for(int j = 0; j < ARRAY_MAX_SIZE; j ++) {
      int output_rand = autonext_old_c_rand();
      generated_rand_upper[j] = (word32) msb(output_rand);
      generated_rand_lower[j] = (word32) lsb(output_rand);
      generated_VN[j] = (word32) autonext_von_neumann(); // Von Neumann
      generated_MT[j] = (word32) autonext_mesrene_twister(); // Mersenne-Twister
      generated_AES[j] = (word32) autonext_aes(); // AES

      printf("%u,", generated_rand_upper[j]);
      printf("%u,", generated_rand_lower[j]);
      printf("%u,", generated_VN[j]);
      printf("%u,", generated_MT[j]);
      printf("%u\n",generated_AES[j]);
    }
    printf("```\n\n");

    printf("#### Monobit Frequency Test\n\n");

    printf(" - Old C Rand 4 MSBs: %f\n", frequency(ARRAY_MAX_SIZE, 4, generated_rand_upper));
    printf(" - Old C Rand 4 LSBs: %f\n", frequency(ARRAY_MAX_SIZE, 4, generated_rand_lower));
    printf(" - Von Neumann: %f\n", frequency(ARRAY_MAX_SIZE, 4, generated_VN));
    printf(" - Mesrene Twister: %f\n", frequency(ARRAY_MAX_SIZE, 4, generated_MT));
    printf(" - AES: %f\n\n", frequency(ARRAY_MAX_SIZE, 4, generated_AES));

    printf("#### Runs Length Test\n\n");

    printf(" - Old C Rand 4 MSBs: %f\n", run_length(ARRAY_MAX_SIZE, 4, generated_rand_upper));
    printf(" - Old C Rand 4 LSBs: %f\n", run_length(ARRAY_MAX_SIZE, 4, generated_rand_lower));
    printf(" - Von Neumann: %f\n", run_length(ARRAY_MAX_SIZE, 4, generated_VN));
    printf(" - Mesrene Twister: %f\n", run_length(ARRAY_MAX_SIZE, 4, generated_MT));
    printf(" - AES: %f\n\n", run_length(ARRAY_MAX_SIZE, 4, generated_AES));
  }

  printf("## M/M/1 queue system\n\n");

  run_simulation(12, 20, 180);

  run_simulation(24, 20, 180);

  return 0;
}