
/* Sobel templatea00 a01 a02a10 a11 a12a20 a21 a22*/unsigned char a00, a01, a02unsigned char a10, a11, a12unsigned char a20, a21, a22void MySobel(IplImage* gray, IplImage* gradient){CvScalar color for (int i=1 i<gray->height-1 ++i){for (int j=1 j<gray->width-1 ++j){a00 = cvGet2D(gray, i-1, j-1).val[0]a01 = cvGet2D(gray, i-1, j).val[0]a02 = cvGet2D(gray, i-1, j+1).val[0]a10 = cvGet2D(gray, 或孙毁i, j-1).val[0]a11 = cvGet2D(gray, i, j).val[0]a12 = cvGet2D(gray, i, j+1).val[0]a20 = cvGet2D(gray, i+1, j-1).val[0]a21 = cvGet2D(gray, 衫备i+1, j).val[0]a22 = cvGet2D(gray, i+1, j+1).val[0]// x方向上的近似导数double ux = a20 * (1) + a21 * (2) + a22 * (1)+ (a00 * (-1) + a01 * (-2) + a02 * (-1))// y方向上的近似导数double uy = a02 * (1) + a12 * (2) + a22 * (1)+ a00 * (-1) + a10 * (-2) + a20 * (-1)color.val[0] = sqrt(ux*ux + uy*uy)cvSet2D(gradient, i, j, color)}}}//注释:该程序需要在安装Opencv软件下运行。Matlabps=imread('D:\14.jpg') %读取图像subplot(1,3,1)imshow(ps)title('原图像')ps=rgb2gray(ps)[m,n]=size(ps) %用Sobel微分算子进行边缘检测pa = edge(ps,'sobel')subplot(1,3,2)imshow(pa)title('Sobel边缘检测得到凯返的图像')
/*FILE: edgeSob.c - WORKS!!
AUTH: Bill Green
DESC: 2 3x3 Sobel masks for edge detection
DATE: 07/23/02
REFS: edgeLap.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <alloc.h>
/*-------STRUCTURES---------*/
typedef struct {int rowsint colsunsigned char* data} sImage
/*-------PROTOTYPES---------*/
long getImageInfo(FILE*, long, int)
void copyImageInfo(FILE* inputFile, FILE* outputFile)
void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors)
int main(int argc, char* argv[])
{
FILE *bmpInput, *bmpOutput
sImage originalImage
sImage edgeImage
unsigned int X, Y
int I, J
long sumX, sumY
int nColors, SUM
unsigned long vectorSize
unsigned long fileSize
int GX[3][3]
int GY[3][3]
unsigned char *pChar, someChar
unsigned int row, col
someChar = '0'pChar = &someChar
/* 3x3 GX Sobel mask. Ref: www.cee.hw.ac.uk/hipr/html/sobel.html */
GX[0][0] = -1GX[0][1] = 0GX[0][2] = 1
GX[1][0] = -2GX[1][1] = 0GX[1][2] = 2
GX[2][0] = -1GX[2][1] = 0GX[2][2] = 1
/* 3x3 GY Sobel mask. Ref: www.cee.hw.ac.uk/hipr/html/sobel.html */
GY[0][0] = 1GY[0][1] = 2GY[0][2] = 1
GY[1][0] = 0GY[1][1] = 0GY[1][2] = 0
GY[2][0] = -1GY[2][1] = -2GY[2][2] = -1
if(argc <2) {
printf("Usage: %s bmpInput.bmp\n", argv[0])
exit(0)
}
printf("Reading filename %s\n", argv[1])
/*-------DECLARE INPUT &OUTPUT FILES-------*/
bmpInput = fopen(argv[1], "rb")
bmpOutput = fopen("edgeSob.bmp", "wb")
/*---SET POINTER TO BEGINNING OF FILE----*/
fseek(bmpInput, 0L, SEEK_END)
/*-------GET INPUT BMP DATA--------*/
fileSize = getImageInfo(bmpInput, 2, 4)
originalImage.cols = (int)getImageInfo(bmpInput, 18, 4)
originalImage.rows = (int)getImageInfo(bmpInput, 22, 4)
edgeImage.rows = originalImage.rows
edgeImage.cols = originalImage.cols
/*--------PRINT DATA TO SCREEN----------*/
printf("Width: %d\n", originalImage.cols)
printf("Height: %d\n", originalImage.rows)
printf("File size: %lu\n", fileSize)
nColors = (int)getImageInfo(bmpInput, 46, 4)
printf("nColors: %d\n", nColors)
/*------ALLOCATE MEMORY FOR FILES--------*/
vectorSize = fileSize - (14+40+4*nColors)
printf("vectorSize: %lu\n", vectorSize)
edgeImage.data = farmalloc(vectorSize*sizeof(unsigned char))
if(edgeImage.data == NULL) {
printf("Failed to malloc edgeImage.data\n")
exit(0)
}
printf("%lu bytes malloc'ed for edgeImage.data\n", vectorSize)
originalImage.data = farmalloc(vectorSize*sizeof(unsigned char))
if(originalImage.data == NULL) {
printf("Failed to malloc originalImage.data\n")
exit(0)
}
printf("%lu bytes malloc'ed for originalImage.datt\n", vectorSize)
/*------COPY HEADER AND COLOR TABLE---------*/
copyImageInfo(bmpInput, bmpOutput)
copyColorTable(bmpInput, bmpOutput, nColors)
fseek(bmpInput, (14+40+4*nColors), SEEK_SET)
fseek(bmpOutput, (14+40+4*nColors), SEEK_SET)
/* Read input.bmp and store it's raster data into originalImage.data */
for(row=0row<=originalImage.rows-1row++) {
for(col=0col<=originalImage.cols-1col++) {
fread(pChar, sizeof(char), 1, bmpInput)
*(originalImage.data + row*originalImage.cols + col) = *pChar
}
}
/*---------------------------------------------------
SOBEL ALGORITHM STARTS HERE
---------------------------------------------------*/
for(Y=0Y<=(originalImage.rows-1)Y++) {
for(X=0X<=(originalImage.cols-1)X++) {
sumX = 0
sumY = 0
/* image boundaries */
if(Y==0 || Y==originalImage.rows-1)
SUM = 0
else if(X==0 || X==originalImage.cols-1)
SUM = 0
/* Convolution starts here */
else {
/*-------X GRADIENT APPROXIMATION------*/
for(I=-1I<=1I++) {
for(J=-1J<=1J++) {
sumX = sumX + (int)( (*(originalImage.data + X + I + (Y + J)*originalImage.cols)) * GX[I+1][J+1])
}
}
if(sumX>255) sumX=255
if(sumX<0)sumX=0
/*-------Y GRADIENT APPROXIMATION-------*/
for(I=-1I<=1I++) {
for(J=-1J<=1J++) {
sumY = sumY + (int)( (*(originalImage.data + X + I + (Y + J)*originalImage.cols)) * GY[I+1][J+1])
}
}
if(sumY>255) sumY=255
if(sumY<0) sumY=0
SUM = abs(sumX) + abs(sumY)/*---GRADIENT MAGNITUDE APPROXIMATION (Myler p.218)----*/
}
*(edgeImage.data + X + Y*originalImage.cols) = 255 - (unsigned char)(SUM) /* make edges black and background white */
fwrite( (edgeImage.data + X + Y*originalImage.cols), sizeof(char), 1, bmpOutput)
}
}
printf("See edgeSob.bmp for results\n")
fclose(bmpInput)
fclose(bmpOutput)
farfree(edgeImage.data) /* Finished with edgeImage.data */
farfree(originalImage.data) /* Finished with originalImage.data */
return 0
}
/*----------GET IMAGE INFO SUBPROGRAM--------------*/
long getImageInfo(FILE* inputFile, long offset, int numberOfChars)
{
unsigned char *ptrC
longvalue = 0L
unsigned char dummy
inti
dummy = '0'
ptrC = &dummy
fseek(inputFile, offset, SEEK_SET)
for(i=1i<=numberOfCharsi++)
{
fread(ptrC, sizeof(char), 1, inputFile)
/* calculate value based on adding bytes */
value = (long)(value + (*ptrC)*(pow(256, (i-1))))
}
return(value)
} /* end of getImageInfo */
/*-------------COPIES HEADER AND INFO HEADER----------------*/
void copyImageInfo(FILE* inputFile, FILE* outputFile)
{
unsigned char *ptrC
unsigned char dummy
inti
dummy = '0'
ptrC = &dummy
fseek(inputFile, 0L, SEEK_SET)
fseek(outputFile, 0L, SEEK_SET)
for(i=0i<=50i++)
{
fread(ptrC, sizeof(char), 1, inputFile)
fwrite(ptrC, sizeof(char), 1, outputFile)
}
}
/*----------------COPIES COLOR TABLE-----------------------------*/
void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors)
{
unsigned char *ptrC
unsigned char dummy
inti
dummy = '0'
ptrC = &dummy
fseek(inputFile, 54L, SEEK_SET)
fseek(outputFile, 54L, SEEK_SET)
for(i=0i<=(4*nColors)i++) /* there are (4*nColors) bytesin color table */
{
fread(ptrC, sizeof(char), 1, inputFile)
fwrite(ptrC, sizeof(char), 1, outputFile)
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)