#include <stdio.h>
#include <string.h>
#include <math.h>
#define MAX
#include "lib.h"
#define OUTLINE 25
/* Function prototypes */
void main( int argc, char **argv);
void shen(IMAGE im, IMAGE res);
void compute_ISEF (float **x, float **y, int nrows, int ncols);
float ** f2d (int nr, int nc);
void apply_ISEF_vertical (float **x, float **y, float **A, float **B,
int nrows, int ncols);
void apply_ISEF_horizontal (float **x, float **y, float **A, float **B,
int nrows, int ncols);
IMAGE compute_bli (float **buff1, float **buff2, int nrows, int ncols);
void locate_zero_crossings (float **orig, float **smoothed, IMAGE bli,
int nrows, int ncols);
void threshold_edges (float **in, IMAGE out, int nrows, int ncols);
int mark_connected (int i, int j,int level);
int is_candidate_edge (IMAGE buff, float **orig, int row, int col);
float compute_adaptive_gradient (IMAGE BLI_buffer, float **orig_buffer,
int row, int col);
void estimate_thresh (double *low, double *hi, int nr, int nc);
void debed (IMAGE im, int width);
void embed (IMAGE im, int width);
/* globals for shen operator*/
double b = 0.9; /* smoothing factor 0 < b < 1 */
double low_thresh=20, high_thresh=22; /* threshold for hysteresis */
double ratio = 0.99;
int window_size = 7;
int do_hysteresis = 1;
float **lap; /* keep track of laplacian of image */
int nr, nc; /* nrows, ncols */
IMAGE edges; /* keep track of edge points (thresholded) */
int thinFactor;
void main(int argc, char **argv)
{
int i,j,n,m;
IMAGE im, res;
FILE *params;
/* Command line args - file name, maybe sigma */
if (argc < 2)
{
printf ("USAGE: shen <inmagefile>\n");
exit (1);
}
im = Input_PBM (argv[1]);
if (im == 0)
{
printf ("Can't read input image from '%s'.\n", argv[1]);
exit (2);
}
/* Look for parameter file */
params = fopen ("shen.par", "r");
if (params)
{
fscanf (params, "%lf", &ratio);
fscanf (params, "%lf", &b);
if (b<0) b = 0;
else if (b>1.0) b = 1.0;
fscanf (params, "%d", &window_size);
fscanf (params, "%d", &thinFactor);
fscanf (params, "%d", &do_hysteresis);
printf ("Parameters:\n");
printf (" %% of pixels to be above HIGH threshold: %7.3f\n", ratio);
printf (" Size of window for adaptive gradient : %3d\n",
window_size);
printf (" Thinning factor : %d\n", thinFactor);
printf ("Smoothing factor : %7.4f\n", b);
if (do_hysteresis) printf ("Hysteresis thresholding turned on.\n");
else printf ("Hysteresis thresholding turned off.\n");
fclose (params);
}
else printf ("Parameter file 'shen.par' does not exist.\n");
embed (im, OUTLINE);
res = newimage (im->info->nr, im->info->nc);
shen (im, res);
debed (res, OUTLINE);
Output_PBM (res, "shen.pgm");
printf ("Output file is 'shen.pgm'\n");
}
void shen (IMAGE im, IMAGE res)
{
register int i,j;
float **buffer;
float **smoothed_buffer;
IMAGE bli_buffer;
/* Convert the input image to floating point */
buffer = f2d (im->info->nr, im->info->nc);
for (i=0; i<im->info->nr; i++)
for (j=0; j<im->info->nc; j++)
buffer[i][j] = (float)(im->data[i][j]);
/* Smooth input image using recursively implemented ISEF filter */
smoothed_buffer = f2d( im->info->nr, im->info->nc);
compute_ISEF (buffer, smoothed_buffer, im->info->nr, im->info->nc);
/* Compute bli image band-limited laplacian image from smoothed image */
bli_buffer = compute_bli(smoothed_buffer,
buffer,im->info->nr,im->info->nc);
/* Perform edge detection using bli and gradient thresholding */
locate_zero_crossings (buffer, smoothed_buffer, bli_buffer,
im->info->nr, im->info->nc);
free(smoothed_buffer[0]); free(smoothed_buffer);
freeimage (bli_buffer);
threshold_edges (buffer, res, im->info->nr, im->info->nc);
for (i=0; i<im->info->nr; i++)
for (j=0; j<im->info->nc; j++)
if (res->data[i][j] > 0) res->data[i][j] = 0;
else res->data[i][j] = 255;
free(buffer[0]); free(buffer);
}
/* Recursive filter realization of the ISEF
(Shen and Castan CVIGP March 1992) */
void compute_ISEF (float **x, float **y, int nrows, int ncols)
{
float **A, **B;
A = f2d(nrows, ncols); /* store causal component */
B = f2d(nrows, ncols); /* store anti-causal component */
/* first apply the filter in the vertical direcion (to the rows) */
apply_ISEF_vertical (x, y, A, B, nrows, ncols);
/* now apply the filter in the horizontal direction (to the columns) and */
/* apply this filter to the results of the previous one */
apply_ISEF_horizontal (y, y, A, B, nrows, ncols);
/* free up the memory */
free (B[0]); free(B);
free (A[0]); free(A);
}
void apply_ISEF_vertical (float **x, float **y, float **A, float **B,
int nrows, int ncols)
{
register int row, col;
float b1, b2;
b1 = (1.0 - b)/(1.0 + b);
b2 = b*b1;
/* compute boundary conditions */
for (col=0; col<ncols; col++)
{
/* boundary exists for 1st and last column */
A[0][col] = b1 * x[0][col];
B[nrows-1][col] = b2 * x[nrows-1][col];
}
/* compute causal component */
for (row=1; row<nrows; row++)
for (col=0; col<ncols; col++)
A[row][col] = b1 * x[row][col] + b * A[row-1][col];
/* compute anti-causal component */
for (row=nrows-2; row>=0; row--)
for (col=0; col<ncols; col++)
B[row][col] = b2 * x[row][col] + b * B[row+1][col];
/* boundary case for computing output of first filter */
for (col=0; col<ncols-1; col++)
y[nrows-1][col] = A[nrows-1][col];
/* now compute the output of the first filter and store in y */
/* this is the sum of the causal and anti-causal components */
for (row=0; row<nrows-2; row++)
for (col=0; col<ncols-1; col++)
y[row][col] = A[row][col] + B[row+1][col];
}
void apply_ISEF_horizontal (float **x, float **y, float **A, float **B,
int nrows, int ncols)
{
register int row, col;
float b1, b2;
b1 = (1.0 - b)/(1.0 + b);
b2 = b*b1;
/* compute boundary conditions */
for (row=0; row<nrows; row++)
{
A[row][0] = b1 * x[row][0];
B[row][ncols-1] = b2 * x[row][ncols-1];
}
/* compute causal component */
for (col=1; col<ncols; col++)
for (row=0; row<nrows; row++)
A[row][col] = b1 * x[row][col] + b * A[row][col-1];
/* compute anti-causal component */
for (col=ncols-2; col>=0; col--)
for (row=0; row<nrows;row++)
B[row][col] = b2 * x[row][col] + b * B[row][col+1];
/* boundary case for computing output of first filter */
for (row=0; row<nrows; row++)
y[row][ncols-1] = A[row][ncols-1];
/* now compute the output of the second filter and store in y */
/* this is the sum of the causal and anti-causal components */
for (row=0; row<nrows; row++)
for (col=0; col<ncols-1; col++)
y[row][col] = A[row][col] + B[row][col+1];
}
/* compute the band-limited laplacian of the input image */
IMAGE compute_bli (float **buff1, float **buff2, int nrows, int ncols)
{
register int row, col;
IMAGE bli_buffer;
bli_buffer = newimage(nrows, ncols);
for (row=0; row<nrows; row++)
for (col=0; col<ncols; col++)
bli_buffer->data[row][col] = 0;
/* The bli is computed by taking the difference between the smoothed image */
/* and the original image. In Shen and Castan's paper this is shown to */
/* approximate the band-limited laplacian of the image. The bli is then */
/* made by setting all values in the bli to 1 where the laplacian is */
/* positive and 0 otherwise. */
for (row=0; row<nrows; row++)
for (col=0; col<ncols; col++)
{
if (row<OUTLINE || row >= nrows-OUTLINE ||
col<OUTLINE || col >= ncols-OUTLINE) continue;
bli_buffer->data[row][col] =
((buff1[row][col] - buff2[row][col]) > 0.0);
}
return bli_buffer;
}
void locate_zero_crossings (float **orig, float **smoothed, IMAGE bli,
int nrows, int ncols)
{
register int row, col;
for (row=0; row<nrows; row++)
{
for (col=0; col<ncols; col++)
{
/* ignore pixels around the boundary
没有合适的资源?快使用搜索试试~ 我知道了~
fv.rar_球 检测_缺陷_钢球_钢缺陷
共63个文件
pgm:27个
exe:13个
c:13个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 112 浏览量
2022-09-21
07:47:49
上传
评论
收藏 1.82MB RAR 举报
温馨提示
带有缺陷钢球的边缘检测,从而可从数万产品中找出缺陷来。
资源推荐
资源详情
资源评论
收起资源包目录
fv.rar (63个子文件)
fv
ET1_9.PGM 259KB
GNOISE.EXE 121KB
GRAD2.EXE 121KB
dsd.EXE 121KB
ET3_9.PGM 259KB
ET1_3.PGM 259KB
fd.EXE 121KB
MARR.C 5KB
ET4_18.PGM 255KB
MEASURE.C 2KB
ET3_3.PGM 259KB
ET1_18.PGM 255KB
ET2.PGM 260KB
ET5.PGM 260KB
GRAD2.C 2KB
ET5_3.PGM 259KB
N20B.PGM 5KB
ET3_18.PGM 255KB
GRAD1.C 1KB
LIB.C 13KB
LIB.H 1KB
ET4.PGM 260KB
CANNY.C 11KB
ET2_18.PGM 255KB
ET5_18.PGM 255KB
EVAL1.C 3KB
DISP.EXE 57KB
ET3.PGM 260KB
MEASURE.EXE 121KB
CHESS_9.PGM 90KB
ET2.EDG 3KB
ET2_3.PGM 259KB
COMPILE.BAT 877B
EVAL2.EXE 133KB
ET5_9.PGM 259KB
df.EXE 130KB
EVAL2.C 11KB
MAKETMPL.EXE 105KB
CANNY.PAR 11B
CHESS_18.PGM 88KB
MAKETMPL.C 2KB
ET4_9.PGM 259KB
MARR.EXE 125KB
asda.EXE 129KB
WOOD.PGM 219KB
ET4_3.PGM 259KB
SOBEL.C 2KB
GRAD1.EXE 121KB
ET3.EDG 3KB
CHESS.PGM 105KB
ET5.EDG 3KB
ET1.PGM 260KB
SHEN.C 14KB
GNOISE.C 2KB
der.EXE 121KB
CHESS_3.PGM 90KB
KIRSCH.C 3KB
N20W.PGM 5KB
s_9.PGM 259KB
ET1.EDG 2KB
ET4.EDG 2KB
MARR.PAR 8B
SHEN.PAR 17B
共 63 条
- 1
资源评论
寒泊
- 粉丝: 76
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功