sobel算子c代码(sobel算子是什么)
admin 发布:2022-12-19 23:37 170
本篇文章给大家谈谈sobel算子c代码,以及sobel算子是什么对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、Sobel算子是什么?
- 2、帮我解释个C程序(Sobel算子)
- 3、在C或C++环境下,分别利用sobel算子,robert算子,编程输出边缘图像。选错课了..完全不懂 求高手代码..
- 4、求soble算子和prewitt算子源代码,用C语言编的!用于数字图像处理!
- 5、Sobel算子的C代码
- 6、求高手 帮帮忙,用vc编程利用sobel算子对图像进行边缘检测
Sobel算子是什么?
C++Builder下的sobel算子的程序如下:
/// summary
/// 按 Sobel 算子进行边缘检测
/// /summary
/// param name= "b " 位图流 /param
/// returns /returns
public Bitmap Sobel(Bitmap b)
{
Matrix3x3 m = new Matrix3x3();
// -1 -2 -1
// 0 0 0
// 1 2 1
m.Init(0);
m.TopLeft = m.TopRight = -1;
m.BottomLeft = m.BottomRight = 1;
m.TopMid = -2;
m.BottomMid = 2;
Bitmap b1 = m.Convolute((Bitmap)b.Clone());
// -1 0 1
// -2 0 2
// -1 0 1
m.Init(0);
m.TopLeft = m.BottomLeft = -1;
m.TopRight = m.BottomRight = 1;
m.MidLeft = -2;
m.MidRight = 2;
Bitmap b2 = m.Convolute((Bitmap)b.Clone());
// 0 1 2
// -1 0 1
// -2 -1 0
m.Init(0);
m.TopMid = m.MidRight = 1;
m.MidLeft = m.BottomMid = -1;
m.TopRight = 2;
m.BottomLeft = -2;
Bitmap b3 = m.Convolute((Bitmap)b.Clone());
// -2 -1 0
// -1 0 1
// 0 1 2
m.Init(0);
m.TopMid = m.MidLeft = -1;
m.MidRight = m.BottomMid = 1;
m.TopLeft = -2;
m.BottomRight = 2;
Bitmap b4 = m.Convolute((Bitmap)b.Clone());
// 梯度运算
b = Gradient(Gradient(b1, b2), Gradient(b3, b4));
b1.Dispose(); b2.Dispose(); b3.Dispose(); b4.Dispose();
return b;
} // end of Sobel
帮我解释个C程序(Sobel算子)
RSA算法非常简单,概述如下:
找两素数p和q
取n=p*q
取t=(p-1)*(q-1)
取任何一个数e,要求满足et并且e与t互素(就是最大公因数为1)
取d*e%t==1
在C或C++环境下,分别利用sobel算子,robert算子,编程输出边缘图像。选错课了..完全不懂 求高手代码..
俺就给你写个sobel的,你把sobel模板换成robert模板就OK了。
本来sobel找阈值还有个小算法,不过一般不要求的,俺就用黄金分割点乘以255替代了。
sobel卷积代码如下:
void CSobelDlg::CreateSobolImage(void)
{
static const int sizeOfSobelMask = 9;
static int sobelMaskHor[sizeOfSobelMask] =
{
-1, -2, -1,
0, 0, 0,
1, 2, 1
};
static int SobelMaskVer[sizeOfSobelMask] =
{
1, 0, -1,
2, 0, -2,
1, 0, 1
};
int numOfBytes = m_bmpInfo.bmWidthBytes * m_bmpInfo.bmHeight;
unsigned char* pbuf1 = new unsigned char[numOfBytes];
unsigned char* pbuf2 = new unsigned char[numOfBytes];
m_bmpOrg.GetBitmapBits(numOfBytes, pbuf1);
unsigned char averageColor = 0;
for(int row = 0; row m_bmpInfo.bmHeight; ++ row)
{
for(int col = 0; col m_bmpInfo.bmWidth; ++col)
{
averageColor = ( pbuf1[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 0] +
pbuf1[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 1] +
pbuf1[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 2] ) / 3;
pbuf1[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 0] = averageColor;
}
}
unsigned char ts = 0, tv = 0, tmp = 0, dst = 0, idx = 0;
for(int row = 1; row m_bmpInfo.bmHeight - 1; ++ row)
{
for(int col = 1; col m_bmpInfo.bmWidth - 1; ++col)
{
idx = ts = tv = 0;
for(int r = row - 1; r = row + 1; ++r)
{
for(int c = col - 1; c = col + 1; ++c)
{
tmp = pbuf1[r * m_bmpInfo.bmWidthBytes + c * m_bmpInfo.bmBitsPixel / 8];
ts += (sobelMaskHor[idx] * tmp );
tv += (SobelMaskVer[idx] * tmp );
++idx;
}
}
dst = (unsigned char)sqrt( (float)(ts * ts + tv * tv) );
if(dst (unsigned char)(0.6180339887 * 255.0) ) {
pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 0] = 0;
pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 1] = 255;
pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 2] = 0;
pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 3] = 255;
}
else {
pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 0] = 0;
pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 1] = 0;
pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 2] = 255;
pbuf2[row * m_bmpInfo.bmWidthBytes + col * m_bmpInfo.bmBitsPixel / 8 + 3] = 255;
}
}
}
m_bmpSobol.CreateBitmap(m_bmpInfo.bmWidth, m_bmpInfo.bmHeight, 1, 32, pbuf2);
delete []pbuf1;
delete []pbuf2;
}
再给你一张本程序运行的效果图。
求soble算子和prewitt算子源代码,用C语言编的!用于数字图像处理!
自己以前图像处理的时候写的,用的是C++, 不过处理流程一样的,可以参考一下
//Soble
void CBmp::RhSobel()
{
double temp[9];
DWORD m_Y=m_pInfo-bmiHeader.biHeight;
DWORD m_X=WIDTH((m_pInfo-bmiHeader.biWidth)*(m_pInfo-bmiHeader.biBitCount));
BYTE *m_B=(BYTE *) new char[m_Y*m_X];
for(int d=0;dm_nPixels;d++)
{
m_B[d]=m_pPixels[d];
}
if((m_pInfo-bmiHeader.biBitCount)==24)
for(int i=1;im_Y-1;i++)
for(int j=3;j(m_X-2);j+=3)
{
for(int n=0;n9;n+=3)
{
temp[n]=(m_B[(i-1+n/3)*m_X+j-3]+m_B[(i-1+n/3)*m_X+j-2]+m_B[(i-1+n/3)*m_X+j-1])/3;
temp[n+1]=(m_B[(i-1+n/3)*m_X+j]+m_B[(i-1+n/3)*m_X+j+1]+m_B[(i-1+n/3)*m_X+j+2])/3;
temp[n+2]=(m_B[(i-1+n/3)*m_X+j+3]+m_B[(i-1+n/3)*m_X+j+4]+m_B[(i-1+n/3)*m_X+j+5])/3;
}
m_pPixels[i*m_X+j]=m_pPixels[i*m_X+j+1]=m_pPixels[i*m_X+j+2]=//
(BYTE((abs(temp[2]+2*temp[5]+temp[8]-//
temp[0]-2*temp[3]-temp[6])+
abs(temp[0]+2*temp[1]+temp[2]-//
temp[6]-2*temp[7]-temp[8]))));
}
else
for(int i=1;i(m_Y-1);i++)
{
for(int j=1;j(m_X-1);j++)
{
m_pPixels[i*m_X+j]=(abs(m_B[(i-1)*m_X+j+1]+(2*m_B[(i)*m_X+j+1])+m_B[(i+1)*m_X+j+1]-//
m_B[(i-1)*m_X+j-1]-(2*m_B[(i)*m_X+j-1])-m_B[(i+1)*m_X+j-1])+
abs(m_B[(i-1)*m_X+j-1]+(2*m_B[(i-1)*m_X+j])+m_B[(i-1)*m_X+j+1]-//
m_B[(i+1)*m_X+j-1]-(2*m_B[(i+1)*m_X+j])-m_B[(i+1)*m_X+j+1]));
}
}
}
//Prewitt
void CBmp::ByPrewitt()
{
double temp1,temp2;
DWORD m_Y=m_pInfo-bmiHeader.biHeight;
DWORD m_X=WIDTH((m_pInfo-bmiHeader.biWidth)*(m_pInfo-bmiHeader.biBitCount));
BYTE *m_B=(BYTE *) new char[m_Y*m_X];
for(int d=0;dm_nPixels;d++)
{
m_B[d]=m_pPixels[d];
}
if(m_pInfo-bmiHeader.biBitCount==8)
for(int i=1;i(m_Y-1);i++)
{
for(int j=1;j(m_X-1);j++)
{
temp1=abs(m_B[(i-1)*m_X+j+1]-m_B[(i-1)*m_X+j-1]+m_B[i*m_X+j+1]-//
m_B[i*m_X+j-1]+m_B[(i+1)*m_X+j+1]-m_B[(i+1)*m_X+j-1]);
temp2=abs(m_B[(i-1)*m_X+j-1]+m_B[(i-1)*m_X+j]+m_B[(i-1)*m_X+j+1]-//
m_B[(i+1)*m_X+j-1]-m_B[(i+1)*m_X+j]-m_B[(i+1)*m_X+j+1]);
m_pPixels[i*m_X+j]=(temp1temp2?temp1:temp2);
}
}
else
{
Huidu();
for(int i=1;i(m_Y-1);i++)
{
for(int j=3;j(m_X-5);j+=3)
{
temp1=abs(m_B[(i-1)*m_X+j+3]-m_B[(i-1)*m_X+j-3]+m_B[i*m_X+j+3]-//
m_B[i*m_X+j-3]+m_B[(i+1)*m_X+j+3]-m_B[(i+1)*m_X+j-3]);
temp2=abs(m_B[(i-1)*m_X+j-3]+m_B[(i-1)*m_X+j]+m_B[(i-1)*m_X+j+3]-//
m_B[(i+1)*m_X+j-3]-m_B[(i+1)*m_X+j]-m_B[(i+1)*m_X+j+3]);
m_pPixels[i*m_X+j]=m_pPixels[i*m_X+j+1]=m_pPixels[i*m_X+j+2]=(temp1temp2?temp1:temp2);
}
}
}
}
Sobel算子的C代码
/* Sobel templatea00 a01 a02a10 a11 a12a20 a21 a22*/unsigned char a00, a01, a02;unsigned char a10, a11, a12;unsigned char a20, a21, a22;void MySobel(IplImage* gray, IplImage* gradient){CvScalar color ;for (int i=1; igray-height-1; ++i){for (int j=1; jgray-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边缘检测得到的图像');
求高手 帮帮忙,用vc编程利用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 rows; int cols; unsigned 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: */
GX[0][0] = -1; GX[0][1] = 0; GX[0][2] = 1;
GX[1][0] = -2; GX[1][1] = 0; GX[1][2] = 2;
GX[2][0] = -1; GX[2][1] = 0; GX[2][2] = 1;
/* 3x3 GY Sobel mask. Ref: */
GY[0][0] = 1; GY[0][1] = 2; GY[0][2] = 1;
GY[1][0] = 0; GY[1][1] = 0; GY[1][2] = 0;
GY[2][0] = -1; GY[2][1] = -2; GY[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=0; row=originalImage.rows-1; row++) {
for(col=0; col=originalImage.cols-1; col++) {
fread(pChar, sizeof(char), 1, bmpInput);
*(originalImage.data + row*originalImage.cols + col) = *pChar;
}
}
/*---------------------------------------------------
SOBEL ALGORITHM STARTS HERE
---------------------------------------------------*/
for(Y=0; Y=(originalImage.rows-1); Y++) {
for(X=0; X=(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=-1; I=1; I++) {
for(J=-1; J=1; J++) {
sumX = sumX + (int)( (*(originalImage.data + X + I + (Y + J)*originalImage.cols)) * GX[I+1][J+1]);
}
}
if(sumX255) sumX=255;
if(sumX0) sumX=0;
/*-------Y GRADIENT APPROXIMATION-------*/
for(I=-1; I=1; I++) {
for(J=-1; J=1; J++) {
sumY = sumY + (int)( (*(originalImage.data + X + I + (Y + J)*originalImage.cols)) * GY[I+1][J+1]);
}
}
if(sumY255) sumY=255;
if(sumY0) 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;
long value = 0L;
unsigned char dummy;
int i;
dummy = '0';
ptrC = dummy;
fseek(inputFile, offset, SEEK_SET);
for(i=1; i=numberOfChars; i++)
{
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;
int i;
dummy = '0';
ptrC = dummy;
fseek(inputFile, 0L, SEEK_SET);
fseek(outputFile, 0L, SEEK_SET);
for(i=0; i=50; i++)
{
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;
int i;
dummy = '0';
ptrC = dummy;
fseek(inputFile, 54L, SEEK_SET);
fseek(outputFile, 54L, SEEK_SET);
for(i=0; i=(4*nColors); i++) /* there are (4*nColors) bytesin color table */
{
fread(ptrC, sizeof(char), 1, inputFile);
fwrite(ptrC, sizeof(char), 1, outputFile);
}
}
sobel算子c代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于sobel算子是什么、sobel算子c代码的信息别忘了在本站进行查找喔。
版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;
相关推荐
- 05-04html上下箭头代码(html下拉箭头符号)[20240504更新]
- 05-04点击切换图片代码滚动(图片左右滚动代码)[20240504更新]
- 05-04易语言代码格式(易语言编码转换)[20240504更新]
- 05-04c语言连连看开源代码(python连连看源代码)[20240504更新]
- 05-04七夕html代码(七夕网页源码)[20240504更新]
- 05-04html合同模板代码(开发合同模版)[20240504更新]
- 05-04盖章代码(盖章代码是什么东西)[20240504更新]
- 05-04sql超市商品信息视图代码的简单介绍[20240504更新]
- 05-04淘宝静态全屏海报代码(淘宝页头全屏代码)[20240504更新]
- 05-04图片放大代码(图片放大代码怎么做)[20240504更新]
取消回复欢迎 你 发表评论:
- 标签列表
- 最近发表
-
- 网络营销策划公司,网络营销策划公司的发展目标与方向有哪些内容
- 易语言代码格式(易语言编码转换)[20240504更新]
- c语言连连看开源代码(python连连看源代码)[20240504更新]
- 七夕html代码(七夕网页源码)[20240504更新]
- html合同模板代码(开发合同模版)[20240504更新]
- 盖章代码(盖章代码是什么东西)[20240504更新]
- java简易计算器源代码(简易计算器编程java)[20240504更新]
- 网页代码下载html(网页代码下载音乐)[20240504更新]
- 飞秋文件传输源代码(飞秋怎么传输文件)[20240504更新]
- sql超市商品信息视图代码的简单介绍[20240504更新]
- 友情链接