コード例 #1
0
// Write out the number of spoons likely to be in the image on the basis of the percentage of red points
// located.  Normally this would be done by using a large number of training images in order to allow
// optimal classification.
void write_number_of_spoons_on_image( IplImage* image, int percentage_of_red_points )
{
    int num_spoons = (percentage_of_red_points < 3) ? 0 : (percentage_of_red_points > 7) ? 2 : 1;
    char spoons_text[100];
    sprintf(spoons_text,"%d spoon%c in can (Redness %% = %d%%)",num_spoons,(num_spoons==1) ? ' ':'s',percentage_of_red_points);
    write_text_on_image(image,1,1,spoons_text);
}
コード例 #2
0
ファイル: glue.cpp プロジェクト: hahalaugh/ComputerVision
void check_glue_bottle( IplImage* original_image, IplImage* result_image )
{
	// TO-DO:  Inspect the image of the glue bottle passed.  This routine should check a number of rows as specified by 
	//        FIRST_LABEL_ROW_TO_CHECK, LAST_LABEL_ROW_TO_CHECK and ROW_STEP_FOR_LABEL_CHECK.  If any of these searches
	//        fail then "No Label" should be written on the result image.  Otherwise if all left and right column values
	//        are roughly the same "Label Present" should be written on the result image.  Otherwise "Label crooked" should
	//        be written on the result image.

	//         To implement this you may need to use smoothing (cv::GaussianBlur() perhaps) and edge detection (cvCanny() perhaps).
	//        You might also need cvConvertImage() which converts between different types of image.


	IplImage* grayscale_image = cvCreateImage( cvGetSize(original_image), 8, 1 );
	cvConvertImage( original_image, grayscale_image ); 

	IplImage* temp = cvCloneImage(grayscale_image);
	//GAUSSIAN SMOOTH.
	cvSmooth(temp, grayscale_image, CV_GAUSSIAN,7,7,0,0);
	//find the edge pixels.
	cvCanny(grayscale_image,grayscale_image,20,100);
	
	int temp_edge_left = 0;
	int temp_edge_right = 0;

	int label_flag = false;
	int i = 0;
	int row = 0;
	for(row = FIRST_LABEL_ROW_TO_CHECK,i = 0;row <= LAST_LABEL_ROW_TO_CHECK;i++, row += ROW_STEP_FOR_LABEL_CHECK )
	{
		if(!find_label_edges(grayscale_image,grayscale_image,row,temp_edge_left,temp_edge_right))
		{
			//no label found.
			label_flag = false;
			break;
		}
		else
		{
			//label in the image and store the pixel coordinates.
			label_edge_left[i] = temp_edge_left;
			label_edge_right[i] = temp_edge_right;
			label_flag = true;
		}
	}
	
	int width_step =grayscale_image->widthStep;
	int pixel_step =grayscale_image->widthStep/grayscale_image->width;

	int result_width_step =result_image->widthStep;
	int result_pixel_step =result_image->widthStep/result_image->width;
	cvZero(result_image);

	unsigned char white_pixel[4] = {255,255,255,0};
	unsigned char yellow_pixel[4] = {255,255,0,0};
	unsigned char red_pixel[4] = {255,0,0,0};
	unsigned char blue_pixel[4] = {0,0,255,0};

	//convert the gray scale image to a RGB image.
	for (row=0; row < grayscale_image->height; row++)
	{
		for (int col=0; col < grayscale_image->width; col++)
		{
			unsigned char* curr_point = GETPIXELPTRMACRO( grayscale_image, col, row, width_step, pixel_step );
			if(curr_point[0] == 255)
			{
				PUTPIXELMACRO( result_image, col, row, white_pixel, result_width_step, result_pixel_step, 4 );
			}
		}
	}
	


	int edge_flag = 0;
	int temp_col = 0;
	//detect the edge pixels of bottle and label respectively.
	for(row = FIRST_LABEL_ROW_TO_CHECK;row <= LAST_LABEL_ROW_TO_CHECK;row++)
	{
		for (int col=0; col < result_image->width; col++)
		{
			unsigned char* curr_point = GETPIXELPTRMACRO( result_image, col, row, result_width_step, result_pixel_step );
			if(curr_point[0] == 255)
			{
				//an edge pixel.
				if(edge_flag == 0)
				{
					//the firest edge pixel from left to right. it is the bottle edge pixel.
					PUTPIXELMACRO( result_image, col, row, red_pixel, result_width_step, result_pixel_step, 4 );
					temp_col = col;	//store the current col value.
					edge_flag++;
				}
				else if(edge_flag == 1)
				{
					//the seconde edge pixel from left to right. it is the lable edge pixel if the spacing between to edge pixel is less than 
					// a specific value(in case of taking other edge of the bottle as the label edge in an image without label)
					if(abs(temp_col - col) > 100)
					{
						temp_col = 0;
						edge_flag = 0;
						break;
					}
					else
					{	
						//is a label pixel.
						PUTPIXELMACRO( result_image, col, row, blue_pixel, result_width_step, result_pixel_step, 4 );
						edge_flag = 0;
						temp_col = 0;
						break;	
					}
				}
				
			}
		}
	}
	//reset the temp col value and do the same thing from the opposite direction.
	temp_col = 0;
	for(row = FIRST_LABEL_ROW_TO_CHECK;row <= LAST_LABEL_ROW_TO_CHECK;row++)
	{
		for (int col=result_image->width; col > 0 ; col--)
		{
			unsigned char* curr_point = GETPIXELPTRMACRO( result_image, col, row, result_width_step, result_pixel_step );
			if(curr_point[0] == 255)
			{
				if(edge_flag == 0)
				{
					PUTPIXELMACRO( result_image, col, row, red_pixel, result_width_step, result_pixel_step, 4 );
					temp_col = col;
					edge_flag++;
				}
				else if(edge_flag == 1)
				{
					if(abs(temp_col - col) > 100)
					{
						temp_col = 0;
						edge_flag = 0;
						break;
					}
					else
					{	
						PUTPIXELMACRO( result_image, col, row, blue_pixel, result_width_step, result_pixel_step, 4 );
						edge_flag = 0;
						temp_col = 0;
						break;	
					}
				}
				
			}
		}
	}

	//draw yellow pixels on the intersections.
	for(row = FIRST_LABEL_ROW_TO_CHECK,i = 0;row <= LAST_LABEL_ROW_TO_CHECK;i++, row += ROW_STEP_FOR_LABEL_CHECK )
	{
		PUTPIXELMACRO( result_image, label_edge_left[i], row, yellow_pixel, result_width_step, result_pixel_step, 4 );
		PUTPIXELMACRO( result_image, label_edge_right[i], row, yellow_pixel, result_width_step, result_pixel_step, 4 );
	}

	if(!label_flag)
	{
		write_text_on_image(result_image,0,0,"No Label");
	}
	else
	{
		//if the deviation of 6 edge pixels is greater than a specific value, the label is crooked.
		if(abs(label_edge_left[5] - label_edge_left[0] )> DEVIATION)
		{
			write_text_on_image(result_image,0,0,"Crooked Label");
		}
		else
		{	//label position is appropriate.
			write_text_on_image(result_image,0,0,"Label Presentation");
		}
	}
}
コード例 #3
0
ファイル: postboxes.cpp プロジェクト: ljw7630/ComputerVision
void indicate_post_in_box( IplImage* image, int postbox )
{
	write_text_on_image(image,(PostboxLocations[postbox][POSTBOX_TOP_ROW]+PostboxLocations[postbox][POSTBOX_BOTTOM_ROW])/2,PostboxLocations[postbox][POSTBOX_LEFT_COLUMN]+2, "Post in");
	write_text_on_image(image,(PostboxLocations[postbox][POSTBOX_TOP_ROW]+PostboxLocations[postbox][POSTBOX_BOTTOM_ROW])/2+19,PostboxLocations[postbox][POSTBOX_LEFT_COLUMN]+2, "this box");
}