⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 hough transformation
💻 CPP
📖 第 1 页 / 共 2 页
字号:
					edgeDirection = 135;
				}
				else{
					edgeDirection = 0;
				}

				/***************************************************
				* Obtain values of 2 adjacent pixels in edge
				* direction.
				****************************************************/
			 
				if(edgeDirection == 0){
					leftPixel = (int)(*(gaussImage.data + r * originalImage.cols + c - 1));
					rightPixel = (int)(*(gaussImage.data + r * originalImage.cols + c + 1));
				}
				else if(edgeDirection == 45){
					leftPixel = (int)(*(gaussImage.data + r * originalImage.cols + c + originalImage.cols - 1));
					rightPixel = (int)(*(gaussImage.data + r * originalImage.cols + c - originalImage.cols + 1));
				}
				else if(edgeDirection == 90){
					leftPixel = (int)(*(gaussImage.data + r * originalImage.cols + c - originalImage.cols));
					rightPixel = (int)(*(gaussImage.data + r * originalImage.cols + c + originalImage.cols));
				}
				else{
					leftPixel = (int)(*(gaussImage.data + r * originalImage.cols + c - originalImage.cols - 1));
					rightPixel = (int)(*(gaussImage.data + r * originalImage.cols + c + originalImage.cols + 1));
				}
								
				/*********************************************
				* Compare current magnitude to both adjacent
				* pixel values.  And if it is less than either
				* of the 2 adjacent values - suppress it and make
				* a nonedge.
				*********************************************/
				
				if(SUM < leftPixel || SUM < rightPixel){
					SUM = 0;
				}
				else{
					/**********************
					* Hysteresis
					**********************/
					highThreshold = 120;
					lowThreshold = 40;

					if(SUM >= highThreshold){
						SUM = 255; //...Edge...
					}
					else if(SUM < lowThreshold){
						SUM = 0; //...Nonedge...
					}
					//...SUM is between T1 & T2...
					else{
						//...Determine values of neighboring pixels...
						P1 = (int)(*(gaussImage.data + r * originalImage.cols + c - originalImage.cols - 1));
						P2 = (int)(*(gaussImage.data + r * originalImage.cols + c - originalImage.cols));
						P3 = (int)(*(gaussImage.data + r * originalImage.cols + c - originalImage.cols + 1));
						P4 = (int)(*(gaussImage.data + r * originalImage.cols + c - 1));
						P5 = (int)(*(gaussImage.data + r * originalImage.cols + c + 1));
						P6 = (int)(*(gaussImage.data + r * originalImage.cols + c + originalImage.cols - 1));
						P7 = (int)(*(gaussImage.data + r * originalImage.cols + c + originalImage.cols));
						P8 = (int)(*(gaussImage.data + r * originalImage.cols + c + originalImage.cols + 1));
						
						//...Check to see if neighboring pixel values are edges...
						if(P1 > highThreshold || P2 > highThreshold || P3 > highThreshold || P4 > highThreshold || 
							P5 > highThreshold || P6 > highThreshold || P7 > highThreshold || P8 > highThreshold){
							SUM = 255; //...Make edge...
						}
						else{
							SUM = 0; //...Make nonedge...
						}
					}
				}
			} 
			//...Else loop ends here (starts after b.c.)...
			//fprintf(dataOutput, "New MAG: \t\t%d\n\n\n\n", SUM);
			*(edgeImage.data + c + r * originalImage.cols) = 255 - (unsigned char)(SUM);
			fwrite((edgeImage.data + c + r * originalImage.cols), sizeof(char), 1, bmpOutput);
			fwrite((gaussImage.data + c + r * originalImage.cols), sizeof(char), 1, gaussOutput);
		}
	}

	fclose(bmpInput);
	fclose(bmpOutput);
	fclose(gaussOutput);
	return;
}

void CalculateHoughTransform(char* fName){
	FILE *edgeInput, *lineOutput, *houghOutput;
	sImage edgeImage, lineImage, houghImage;
	int nColors;
	unsigned long vectorSize;
	unsigned long fileSize;
	unsigned char *pChar, someChar;
	unsigned int row, col;

	someChar = '0'; 
	pChar = &someChar;

	//...Open files for reading and writing to...
	edgeInput = fopen(fName, "rb");
	lineOutput = fopen("line.bmp", "wb");
	houghOutput = fopen("hough.bmp", "wb");
	
	//...Start pointer at beginning of file...
	fseek(edgeInput, 0L, SEEK_END);

	//..Retrieve and print file size and number of cols and rows...
	fileSize = getImageInfo(edgeInput, 2, 4);
	edgeImage.cols = (int)getImageInfo(edgeInput, 18, 4);
	edgeImage.rows = (int)getImageInfo(edgeInput, 22, 4);
	houghImage.rows = lineImage.rows = edgeImage.rows;
	houghImage.cols = lineImage.cols = edgeImage.cols;

	//...Retrieve and print Number of colors...
	nColors = (int)getImageInfo(edgeInput, 46, 4);

	//...Allocate memory for originalImage.data...
	vectorSize = fileSize - (14 + 40 + 4 * nColors);
	edgeImage.data = (unsigned char*)malloc(vectorSize * sizeof(unsigned char));
	if(edgeImage.data == NULL){
		printf("Failed to malloc originalImage.data\n");
		exit(0);
	}

	//...Allocate memory for gaussImage.data...
	//printf("vectorSize: %lu\n", vectorSize);
	houghImage.data = (unsigned char*)calloc(vectorSize, sizeof(unsigned char));
	if(houghImage.data == NULL){
		printf("Failed to malloc gaussImage.data\n");
		exit(0);
	}

	//...Allocate memory for edgeImage.data...
	lineImage.data = (unsigned char*)malloc(vectorSize * sizeof(unsigned char));
	if(lineImage.data == NULL){
		printf("Failed to malloc edgeImage.data\n");
		exit(0);
	}
	
	copyImageInfo(edgeInput, lineOutput);
	copyColorTable(edgeInput, lineOutput, nColors);
	copyImageInfo(edgeInput, houghOutput);
	copyColorTable(edgeInput, houghOutput, nColors);

	fseek(edgeInput, (14 + 40 + 4 * nColors), SEEK_SET);
	fseek(lineOutput, (14 + 40 + 4 * nColors), SEEK_SET);
	fseek(houghOutput, (14 + 40 + 4 * nColors), SEEK_SET);

	//...Read input.bmp and store it's raster data into originalImage.data... 
	for(row = 0; row <= edgeImage.rows - 1; row++){
		for(col = 0; col <= edgeImage.cols - 1; col++){
			fread(pChar, sizeof(char), 1, edgeInput);
			*(edgeImage.data + row * edgeImage.cols + col) = *pChar;
		}
	}

	//...Create the hough transform of the image...
	int max_radius = (int)sqrt(edgeImage.rows * edgeImage.rows + edgeImage.cols * edgeImage.cols);
	
	for(row = 0; row <= edgeImage.rows - 1; row++){
		for(col = 0; col <= edgeImage.cols - 1; col++){			
			if(*(edgeImage.data + row * edgeImage.cols + col) > 0){
				for(int t = 0; t < 360; t++){
					int r = (int)((row - edgeImage.rows / 2) * cos(M_PI / 180.0 * t) + (col - edgeImage.cols / 2) * sin(M_PI / 180.0 * t));
					if((r > -max_radius / 2) && (r < max_radius / 2) && (r != 0)){
						*(houghImage.data + t + (r + max_radius / 2) * houghImage.cols) += 1;
					}
				}
			}
		}
	}

	//...Detect lines and draw them on the line image...
	int h_width = 360;
	int h_height = (int)max_radius;

	int max_accum = 0;
	int dr, dt;
	int maxima, max_value = 0;

	//...The the maxima in the accumulator...
	for(row = 0; row < h_height; row++){
		for(col = 0; col < h_width; col++){
			if(*(houghImage.data + row * houghImage.cols + col) > max_value){
				max_value = *(houghImage.data + row * houghImage.cols + col);
			}
		}
	}

	//...Detect local maximas...
	for(row = 4; row < h_height - 5; row++){
		for (col = 4; col < h_width - 5; col++){
			if(*(houghImage.data + row * houghImage.cols + col) > 0.7 * max_value){
				maxima = 1;

				for(dr = row-4; dr < row+5; dr++){
					for(dt = col-5; dt < col+5; dt++){
						if(*(houghImage.data + dr * houghImage.cols + dt) > *(houghImage.data + row * houghImage.cols + col)){
							maxima = 0;
						}
					}
				}

				if(maxima == 1){
					//...Draw the line...
					float radius = (float)(row - h_height / 2);
					float theta = (float)((col-180)*M_PI/180.0);
					float M, B;
					int x, y;
					int start, end;

					if(((theta >= M_PI/4) && (theta <= 3*M_PI/4)) || ((-theta >= M_PI/4) && (-theta <= 3*M_PI/4))){
						M = (float)cos(theta) / (float)sin(theta);
						B = radius / (float)sin(theta) + (houghImage.cols/2) * M + houghImage.rows / 2;
						start = end = -1;
						for(x = 0; x < houghImage.cols; x++){
							y = (int)(0.5 + B - x * M);
							if((y >= 0) && (y < houghImage.rows ))
								if (start < 0)
								start = x;
							else
							{
								end = x;
								for (x = start; x < end; x++)
								{
									y = (int) (0.5 + B - x * M);
									*(edgeImage.data + y * edgeImage.cols + x) = 150;
								}
								start = end = -1;
							}		
						}
					}
					else{
						M = (float)sin(theta) / (float)cos(theta);
						B = radius / (float)cos(theta) + (houghImage.rows /2) * M + houghImage.cols /2;
						start = end = -1;
						for(y = 0; y < houghImage.rows; y++){
							x = (int)(0.5 + B - y * M);
							if((x >= 0) && (x < houghImage.cols)){
								if(start < 0){
									start = y;
								}
								else{
									end = y;
									for(y = start; y < end; y++){
										x = (int)(0.5 + B - y * M);
										*(edgeImage.data + y * edgeImage.cols + x) = 150;	
									}
									start = end = -1;
								}
							}
						}
					}
				}
			}
		}
	}

	//...Write the output to files...
	for(row = 0; row <= edgeImage.rows - 1; row++){
		for(col = 0; col <= edgeImage.cols - 1; col++){
			*(houghImage.data + col + row * houghImage.cols) *= 200;
			*(lineImage.data + col + row * lineImage.cols) = *(edgeImage.data + row * edgeImage.cols + col);
			fwrite((houghImage.data + col + row * houghImage.cols), sizeof(char), 1, houghOutput);
			fwrite((lineImage.data + col + row * lineImage.cols), sizeof(char), 1, lineOutput);
		}
	}

	fclose(edgeInput);
	fclose(lineOutput);
	fclose(houghOutput);
	
	return;
}

void main(int argc, char* argv[]){

	if(argc < 2){
		printf("Usage: %s bmpInput.bmp\n", argv[0]);
		exit(0);
	};
	
	CalculateCannyEdgeImage(argv[1]);
	printf("Canny edge detector was applied\n");
	CalculateHoughTransform("canny.bmp");
	printf("Hough transform was applied\n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -