Ejemplo n.º 1
0
void test_performance_parts(){
	tester->start_timer();
	printf("edge detection\n");
	IplImage* edges_image = min_max_edge_detection(image);
	tester->stop_timer();
	tester->start_timer();
	printf("otsu\n");
	IplImage * contrast_image = otsu_algorithm(edges_image);
	tester->stop_timer();
	tester->start_timer();
	printf("invert image\n");
	contrast_image = invert_image(contrast_image);
	tester->stop_timer();
	tester->start_timer();
	printf("get peak pixels\n");
	IplImage* peak_pixels_image = get_peak_pixels(contrast_image);
	tester->stop_timer();
	tester->start_timer();
	printf("text width\n");
	int text_width = 3;//text_width_approximation(peak_pixels_image);
	printf("text width: %d \n",text_width);
	tester->stop_timer();
	tester->start_timer();
	printf("local thresholding\n");
	IplImage* rimage = perform_local_thresholding(image,contrast_image,text_width);
	tester->stop_timer();
	tester->get_total_testing_time();
	display_image("rimage",rimage);

}
Ejemplo n.º 2
0
static Image hessian(Image const &img, unsigned gauss = 0) {
  Image src = img.clone();
  cv::cvtColor(src, src, CV_BGR2GRAY);
  Image dXX, dYY, dXY, hessian;

  cv::Sobel(src, dXX, CV_64F, 2, 0);
  cv::Sobel(src, dYY, CV_64F, 0, 2);
  cv::Sobel(src, dXY, CV_64F, 1, 1);

  for (int k = 0; k < gauss; ++k) {
    GaussianBlur(dXX, dXX, cv::Size(3, 3), 0);
    GaussianBlur(dXY, dXY, cv::Size(3, 3), 0);
    GaussianBlur(dYY, dYY, cv::Size(3, 3), 0);
  }

  hessian = dXX.mul(dYY) - dXY.mul(dXY);

  hessian = invert_image(hessian);
  for (int k = 0; k < gauss; ++k) {
    GaussianBlur(hessian, hessian, cv::Size(3, 3), 0);
  }
#ifdef PRINT_META
  cv::imwrite("/Users/ivan/.supp/code/snakes/hess.jpg", hessian);
#endif
  return hessian;
}
Ejemplo n.º 3
0
void read_tiff_image(TIFF* tif, IMAGE* image) {
  tdata_t buf;
  uint32 image_width, image_height;
  uint16 photometric;
  inT16 bpp;
  inT16 samples_per_pixel = 0;
  TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &image_width);
  TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &image_height);
  TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bpp);
  TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel);
  TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric);
  if (samples_per_pixel > 1)
    bpp *= samples_per_pixel;
  // Tesseract's internal representation is 0-is-black,
  // so if the photometric is 1 (min is black) then high-valued pixels
  // are 1 (white), otherwise they are 0 (black).
  uinT8 high_value = photometric == 1;
  image->create(image_width, image_height, bpp);
  IMAGELINE line;
  line.init(image_width);

  buf = _TIFFmalloc(TIFFScanlineSize(tif));
  int bytes_per_line = (image_width*bpp + 7)/8;
  uinT8* dest_buf = image->get_buffer();
  // This will go badly wrong with one of the more exotic tiff formats,
  // but the majority will work OK.
  for (int y = 0; y < image_height; ++y) {
    TIFFReadScanline(tif, buf, y);
    memcpy(dest_buf, buf, bytes_per_line);
    dest_buf += bytes_per_line;
  }
  if (high_value == 0)
    invert_image(image);
  _TIFFfree(buf);
}
Ejemplo n.º 4
0
void find_components(
                       BLOCK_LIST *blocks,
                       TO_BLOCK_LIST *land_blocks,
                       TO_BLOCK_LIST *port_blocks,
                       TBOX *page_box) {
  BLOCK *block;                  //current block
  PDBLK_CLIST pd_blocks;         //copy of list
  BLOCK_IT block_it = blocks;    //iterator
  PDBLK_C_IT pd_it = &pd_blocks; //iterator
  IMAGE thresh_image;            //thresholded

  int width = page_image.get_xsize();
  int height = page_image.get_ysize();
  if (width > MAX_INT16 || height > MAX_INT16) {
    tprintf("Input image too large! (%d, %d)\n", width, height);
    return;  // Can't handle it.
  }

  ICOORD page_tr(width, height);
  block_it.set_to_list (blocks);
  if (global_monitor != NULL)
    global_monitor->ocr_alive = TRUE;

  set_global_loc_code(LOC_EDGE_PROG);
  if (!page_image.white_high ())
    invert_image(&page_image);

#ifndef EMBEDDED
  previous_cpu = clock ();
#endif

  for (block_it.mark_cycle_pt(); !block_it.cycled_list();
       block_it.forward()) {
    block = block_it.data();
    if (block->poly_block() == NULL ||
        block->poly_block()->IsText()) {
#ifndef GRAPHICS_DISABLED
      extract_edges(NULL, &page_image, &page_image, page_tr, block);
#else
      extract_edges(&page_image, &page_image, page_tr, block);
#endif
      *page_box += block->bounding_box ();
    }
  }
  if (global_monitor != NULL) {
    global_monitor->ocr_alive = TRUE;
    global_monitor->progress = 10;
  }

  assign_blobs_to_blocks2(blocks, land_blocks, port_blocks);
  if (global_monitor != NULL)
    global_monitor->ocr_alive = TRUE;
  filter_blobs (page_box->topright (), land_blocks, textord_test_landscape);
#ifndef EMBEDDED
  previous_cpu = clock ();
#endif
  filter_blobs (page_box->topright (), port_blocks, !textord_test_landscape);
  if (global_monitor != NULL)
    global_monitor->ocr_alive = TRUE;
}
Ejemplo n.º 5
0
CHAR_SAMPLE *clip_sample(              //lines of the image
                         PIXROW *pixrow,
                         IMAGELINE *imlines,
                         TBOX pix_box,  //box of imlines extent
                         BOOL8 white_on_black,
                         char c) {
  TBOX b_box = pixrow->bounding_box ();
  float baseline_pos = 0;
  inT32 resolution = page_image.get_res ();

  if (!b_box.null_box ()) {
    ASSERT_HOST (b_box.width () < page_image.get_xsize () &&
      b_box.height () < page_image.get_ysize ());

    if (b_box.width () > resolution || b_box.height () > resolution) {
      tprintf ("clip sample: sample too big (%d x %d)\n",
        b_box.width (), b_box.height ());

      return NULL;
    }

    IMAGE *image = new (IMAGE);
    if (image->create (b_box.width (), b_box.height (), 1) == -1) {
      tprintf ("clip sample: create image failed (%d x %d)\n",
        b_box.width (), b_box.height ());

      delete image;
      return NULL;
    }

    if (!white_on_black)
      invert_image(image);  // Set background to white
    pixrow->char_clip_image (imlines, pix_box, NULL, *image, baseline_pos);
    if (white_on_black)
      invert_image(image);  //invert white on black for scaling &NN
    return new CHAR_SAMPLE (image, c);
  }
  else
    return NULL;
}
Ejemplo n.º 6
0
void test_su_algorithm_parts(){
	IplImage* edges_image = min_max_edge_detection(image);
	display_image("edges image",edges_image);
	IplImage * contrast_image = otsu_algorithm(edges_image);
	contrast_image = invert_image(contrast_image);
	display_image("otsu image",contrast_image);
	IplImage* peak_pixels_image = get_peak_pixels(contrast_image);
	display_image("peak_pixels_image",peak_pixels_image);
	int text_width = text_width_approximation(peak_pixels_image);
	printf("text width: %d",text_width);
	display_image("image before local thresholding",image);
	IplImage* rimage = perform_local_thresholding(image,contrast_image,text_width);
	display_image("resutl",rimage);

}
Ejemplo n.º 7
0
void test_otsu_step(){
	//IplImage * edge_image = min_max_edge_detection(image);
	//display_image("edge_image",edge_image);
	IplImage * edge_image = cvLoadImage("images/edges.png",0);
	display_image("edges image",edge_image);
	IplImage * otsu_image = otsu_algorithm(edge_image);
	otsu_image = invert_image(otsu_image);
	display_image("otsu image",otsu_image);
	IplImage * segmented_pixels = get_pixels_on_template(image,otsu_image);
	display_image("segmented pixs",segmented_pixels);
	int threshold = otsu_thresholding_no_white(segmented_pixels);
	IplImage* fimage = apply_threshold(segmented_pixels,threshold);
	display_image("fimage", fimage);
	cvSaveImage("images/otsu_edges.png",otsu_image);

	
}
Ejemplo n.º 8
0
void test_stable(){

	//image = cvLoadImage("C:\\Users\\ninao\\Documents\\images\\testimages\\bigimage.bmp",0);
	tester->start_timer();
	test_min_max_edge_detection();
	IplImage * edges_image = cvLoadImage("images/edges.png",0);
	display_image("edges",edges_image);
	IplImage * rimage = cvCreateImage(cvGetSize(image),image->depth,image->nChannels); 
	int values [] = {0,0,0,0,0,0,0,0,0};
	IplConvKernel * kernel = cvCreateStructuringElementEx(3,3,1,1,CV_SHAPE_RECT,values);
	//cvDilate(edges_image,rimage,kernel);
	//display_image("dilate",rimage);
	IplImage * otsu_image = otsu_algorithm(edges_image);
	otsu_image = invert_image(otsu_image);
	tester->stop_timer();
	cvSaveImage("images/fasterver.png",otsu_image);
	display_image("otsu image",otsu_image);

}
Ejemplo n.º 9
0
int TessDllAPI::ProcessPagePass1() {
  STRING pagefile;               //input file
  static const char *fake_name = "noname.tif";

  //invert just to make it a normal black on white image.
  invert_image(&page_image);

  block_list = new BLOCK_LIST;
  pagefile = fake_name;
  pgeditor_read_file(pagefile, block_list);


  if (tessedit_resegment_from_boxes)
    apply_boxes(block_list);
  page_res = new PAGE_RES(block_list);

  if (page_res)
    recog_all_words(page_res, global_monitor,0L,1);

  return (page_res!=0);
}
Ejemplo n.º 10
0
//process the templates into images with same color features of processed very original image with real objects.
void normalize_template(IplImage** templates)
{
	IplImage* selected_image = NULL;
	IplImage* temp_image = NULL;
	IplImage* red_point_image = NULL;
	IplImage* connected_reds_image = NULL;
	IplImage* connected_background_image = NULL;
	IplImage* result_image = NULL;
	CvSeq* red_components = NULL;
	CvSeq* background_components = NULL;

	for(int i = 0; i < TEMPLATES_NUM; i++)
	{
		if (red_point_image != NULL)
		{
			cvReleaseImage( &red_point_image );
			cvReleaseImage( &temp_image );
			cvReleaseImage( &connected_reds_image );
			cvReleaseImage( &connected_background_image );
			cvReleaseImage( &result_image );
		}
		selected_image = templates[i];
		red_point_image = cvCloneImage( selected_image );
		result_image = cvCloneImage( selected_image );
		temp_image = cvCloneImage( selected_image );
		connected_reds_image = cvCloneImage( selected_image );
		connected_background_image = cvCloneImage( selected_image );

		//the same algorithm as process the image with real objects is used. 
		find_red_points( selected_image, red_point_image, temp_image );
		red_components = connected_components( red_point_image, connected_reds_image );
		invert_image( red_point_image, temp_image );
		background_components = connected_components( temp_image, connected_background_image );
		determine_optimal_sign_classification( selected_image, red_point_image, red_components, background_components, result_image );

		cvCopy(result_image, templates[i]);  
	}
}
Ejemplo n.º 11
0
bool GLTexture::writeFile(const char *fname) const
{
    const int2 sz = ceil_int(m_texsize);
    const size_t size = sz.x * sz.y * 4;
    uint *pix = (uint*) malloc(size);

    OutlawImage img = {};
    img.width = sz.x;
    img.height = sz.y;
    img.format = GL_RGBA;
    img.type = GL_UNSIGNED_BYTE;
    img.data = (char*) pix;

    BindTexture(0);
    glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pix);
    glReportError();

    invert_image(pix, sz.x, sz.y);

    const int success = OL_SaveImage(&img, fname);
    free(pix);
    return success;
}
Ejemplo n.º 12
0
int main( int argc, char** argv )
{
	int selected_image_num = 1;
	char show_ch = 's';
	IplImage* images[NUM_IMAGES];
	IplImage* selected_image = NULL;
	IplImage* temp_image = NULL;
	IplImage* red_point_image = NULL;
	IplImage* connected_reds_image = NULL;
	IplImage* connected_background_image = NULL;
	IplImage* result_image = NULL;
	CvSeq* red_components = NULL;
	CvSeq* background_components = NULL;

	// Load all the images.
	for (int file_num=1; (file_num <= NUM_IMAGES); file_num++)
	{
		if( (images[0] = cvLoadImage("./RealRoadSigns.jpg",-1)) == 0 )
			return 0;
		if( (images[1] = cvLoadImage("./RealRoadSigns2.jpg",-1)) == 0 )
			return 0;
		if( (images[2] = cvLoadImage("./ExampleRoadSigns.jpg",-1)) == 0 )
			return 0;
		if( (images[3] = cvLoadImage("./Parking.jpg",-1)) == 0 )
			return 0;
		if( (images[4] = cvLoadImage("./NoParking.jpg",-1)) == 0 )
			return 0;
	}

	//load the template images and do normalization.
	IplImage* templates[TEMPLATES_NUM];
	load_templates(templates);
	normalize_template(templates);


	// Explain the User Interface
    printf( "Hot keys: \n"
            "\tESC - quit the program\n"
			"\t1 - Real Road Signs (image 1)\n"
			"\t2 - Real Road Signs (image 2)\n"
			"\t3 - Synthetic Road Signs\n"
			"\t4 - Synthetic Parking Road Sign\n"
			"\t5 - Synthetic No Parking Road Sign\n"
			"\tr - Show red points\n"
			"\tc - Show connected red points\n"
			"\th - Show connected holes (non-red points)\n"
			"\ts - Show optimal signs\n"
			);
    
	// Create display windows for images
    //cvNamedWindow( "Original", 1 );
    cvNamedWindow( "Processed Image", 1 );


	// Setup mouse callback on the original image so that the user can see image values as they move the
	// cursor over the image.
    cvSetMouseCallback( "Original", on_mouse_show_values, 0 );
	window_name_for_on_mouse_show_values="Original";
	image_for_on_mouse_show_values=selected_image;

	int user_clicked_key = 0;
	do {
		// Create images to do the processing in.
		if (red_point_image != NULL)
		{
			cvReleaseImage( &red_point_image );
			cvReleaseImage( &temp_image );
			cvReleaseImage( &connected_reds_image );
			cvReleaseImage( &connected_background_image );
			cvReleaseImage( &result_image );
		}
		selected_image = images[selected_image_num-1];
		red_point_image = cvCloneImage( selected_image );
		result_image = cvCloneImage( selected_image );
		temp_image = cvCloneImage( selected_image );
		connected_reds_image = cvCloneImage( selected_image );
		connected_background_image = cvCloneImage( selected_image );

		// Process image
		image_for_on_mouse_show_values = selected_image;
		find_red_points( selected_image, red_point_image, temp_image );
		red_components = connected_components( red_point_image, connected_reds_image );
		invert_image( red_point_image, temp_image );
		background_components = connected_components( temp_image, connected_background_image );
		determine_optimal_sign_classification( selected_image, red_point_image, red_components, background_components, result_image );

		//recognize the result_image(with white/black/red only) with the processed templates.
		recognize(selected_image,result_image,templates);

		// Show the original & result
        //cvShowImage( "Original", selected_image );
		do {
			if ((user_clicked_key == 'r') || (user_clicked_key == 'c') || (user_clicked_key == 'h') || (user_clicked_key == 's'))
				show_ch = user_clicked_key;
			switch (show_ch)
			{
			case 'c':
				cvShowImage( "Processed Image", connected_reds_image );
				break;
			case 'h':
				cvShowImage( "Processed Image", connected_background_image );
				break;
			case 'r':
				cvShowImage( "Processed Image", red_point_image );
				break;
			case 's':
			default:
				cvShowImage( "Processed Image", result_image );
				break;
			}
	        user_clicked_key = cvWaitKey(0);
		} while ((!((user_clicked_key >= '1') && (user_clicked_key <= '0'+NUM_IMAGES))) &&
			     ( user_clicked_key != ESC ));
		if ((user_clicked_key >= '1') && (user_clicked_key <= '0'+NUM_IMAGES))
		{
			selected_image_num = user_clicked_key-'0';
		}
	} while ( user_clicked_key != ESC );

    return 1;
}
Ejemplo n.º 13
0
int main(int argc, char* argv[]) {
	bool quit		= false;
	bool refresh	= true;
	FILE *f;
	char *filename;
	int  pdx, pdy;

	if (argc < 2) {
		puts("Usage: fbi16 [file]");
		exit(0);
	}
	else
		filename = argv[1];
	
	init();

	image = NULL;
	f = fopen(filename, "rb"); halt_on_error(filename);
	read_pgm(f);
	fclose(f);


	/* center horizontal */
	if (blocks < 640/8)
		sdx = (640/8 - blocks)/2;
	else
		sdx = 0;
	
	/* center verical */
	if (height < 480)
		sdy = (480 - height)/2;
	else
		sdy = 0;

	pdx = pdy = dx = dy = 0;
	while (!quit) {
		if (refresh || pdx != dx || pdy != dy) {
			show_image(dx, dy);
			pdx = dx;
			pdy = dy;
			refresh = false;
		}
		switch (getchar()) {
			case 'q':
			case 'Q':
				quit = true;
				break;

			/* scroll left */
			case 's':
				if (blocks > 640/8) {
					dx += 1;
					if (dx > (blocks - 640/8))
						dx = blocks - 640/8;
				}
				break;
			case 'S':
				if (blocks > 640/8) {
					dx += 2;
					if (dx > (blocks - 640/8))
						dx = blocks - 640/8;
				}
				break;

			/* scroll right */
			case 'a':
				if (blocks > 640/8) {
					dx -= 1;
					if (dx < 0) dx = 0;
				}
				break;
			case 'A':
				if (blocks > 640/8) {
					dx -= 2;
					if (dx < 0) dx = 0;
				}
				break;

			/* scroll down */
			case 'w':
				if (height > 480) {
					dy += 10;
					if (dy > (height - 480))
						dy = height - 480;
				}
				break;
			case 'W':
				if (height > 480) {
					dy += 20;
					if (dy > (height - 480))
						dy = height - 480;
				}
				break;
			
			/* scroll up */
			case 'z':
				if (height > 480) {
					dy -= 10;
					if (dy < 0) dy = 0;
				}
				break;
			case 'Z':
				if (height > 480) {
					dy -= 20;
					if (dy < 0) dy = 0;
				}
				break;

			/* refresh image */
			case '\n':
			case 'r':
			case 'R':
				refresh = true;
				break;

			/* invert image */ 
			case 'i':
			case 'I':
				invert_image();
				refresh = true;
				break;
		}
	}

	clean();
	return EXIT_SUCCESS;
}
Ejemplo n.º 14
0
void edges_and_textord(                       //read .pb file
                       const char *filename,  //.pb file
                       BLOCK_LIST *blocks) {
  BLOCK *block;                  //current block
  char *lastdot;                 //of name
  STRING name = filename;        //truncated name
  ICOORD page_tr;
  BOX page_box;                  //bounding_box
  PDBLK_CLIST pd_blocks;         //copy of list
  BLOCK_IT block_it = blocks;    //iterator
  PDBLK_C_IT pd_it = &pd_blocks; //iterator
                                 //different orientations
  TO_BLOCK_LIST land_blocks, port_blocks;
  IMAGE thresh_image;            //thresholded

  lastdot = strrchr (name.string (), '.');
  if (lastdot != NULL)
    *lastdot = '\0';
  if (page_image.get_bpp () == 0) {
    name += tessedit_image_ext;
    if (page_image.read_header (name.string ()))
      CANTOPENFILE.error ("edges_and_textord", EXIT, name.string ());
    if (page_image.read (0))
      READFAILED.error ("edges_and_textord", EXIT, name.string ());
    name = filename;
    lastdot = strrchr (name.string (), '.');
    if (lastdot != NULL)
      *lastdot = '\0';
  }
  page_tr = ICOORD (page_image.get_xsize (), page_image.get_ysize ());
  read_pd_file (name, page_image.get_xsize (), page_image.get_ysize (),
    blocks);
  block_it.set_to_list (blocks);
  if (global_monitor != NULL)
    global_monitor->ocr_alive = TRUE;

  if (page_image.get_bpp () > 1) {
    set_global_loc_code(LOC_ADAPTIVE);
    for (block_it.mark_cycle_pt (); !block_it.cycled_list ();
    block_it.forward ()) {
      block = block_it.data ();
      pd_it.add_after_then_move (block);
    }
    //              adaptive_threshold(&page_image,&pd_blocks,&thresh_image);
    set_global_loc_code(LOC_EDGE_PROG);
#ifndef EMBEDDED
    previous_cpu = clock ();
#endif
    for (block_it.mark_cycle_pt (); !block_it.cycled_list ();
    block_it.forward ()) {
      block = block_it.data ();
      if (!polygon_tess_approximation)
        invert_image(&page_image);
#ifndef GRAPHICS_DISABLED
      extract_edges(NO_WINDOW, &page_image, &thresh_image, page_tr, block);
#else
      extract_edges(&page_image, &thresh_image, page_tr, block);
#endif
      page_box += block->bounding_box ();
    }
    page_image = thresh_image;   //everyone else gets it
  }
  else {
    set_global_loc_code(LOC_EDGE_PROG);
    if (!page_image.white_high ())
      invert_image(&page_image);

#ifndef EMBEDDED
    previous_cpu = clock ();
#endif

    for (block_it.mark_cycle_pt (); !block_it.cycled_list ();
    block_it.forward ()) {
      block = block_it.data ();
#ifndef GRAPHICS_DISABLED
      extract_edges(NO_WINDOW, &page_image, &page_image, page_tr, block);
#else
      extract_edges(&page_image, &page_image, page_tr, block);
#endif
      page_box += block->bounding_box ();
    }
  }
  if (global_monitor != NULL) {
    global_monitor->ocr_alive = TRUE;
    global_monitor->progress = 10;
  }

  assign_blobs_to_blocks2(blocks, &land_blocks, &port_blocks);
  if (global_monitor != NULL)
    global_monitor->ocr_alive = TRUE;
  filter_blobs (page_box.topright (), &land_blocks, textord_test_landscape);
#ifndef EMBEDDED
  previous_cpu = clock ();
#endif
  filter_blobs (page_box.topright (), &port_blocks, !textord_test_landscape);
  if (global_monitor != NULL)
    global_monitor->ocr_alive = TRUE;
  textord_page (page_box.topright (), blocks, &land_blocks, &port_blocks);
}