當前位置:首頁 » 編程語言 » sober運算元c語言代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sober運算元c語言代碼

發布時間: 2022-02-25 05:18:23

① 急求 sobel運算元檢測邊緣 Matlab程序代碼

f=imread('peppers.png'); % 讀入圖像
f=rgb2gray(f); % 灰度轉換
f=im2double(f); % 數據類型轉換
% 使用垂直Sobel運算元,自動選擇閾值
[VSFAT Threshold]=edge(f,'sobel','vertical'); % 邊緣探測
figure, imshow(f),title('Original Image'), % 顯示原始圖像
figure,imshow(VSFAT),title('Sobel Filter - Automatic Threshold'); % 顯示邊緣探測圖像
%使用水平和垂直Sobel運算元,自動選擇閾值
SFST=edge(f,'sobel',Threshold);
figure,imshow(SFST),title('Sobel Filter (Horizontal and Vertical)'); % 顯示邊緣探測圖像
%使用指定45度角Sobel運算元濾波器,指定閾值
s45=[-2 -1 0;-1 0 1;0 1 2];
SFST45=imfilter(f,s45,'replicate');
SFST45=SFST45>=Threshold;
figure,imshow(SFST45),title('Sobel Filter (45 Degree)'); % 顯示邊緣探測圖像
%使用指定-45度角Sobel運算元濾波器,指定閾值
sm45=[0 1 2;-1 0 1;-2 -1 0];
SFSTM45=imfilter(f,sm45,'replicate');
SFSTM45=SFSTM45>=Threshold;
figure,imshow(SFSTM45),title('Sobel Filter (-45 Degree)'); % 顯示邊緣探測圖像

② Sobel運算元的C代碼

/**/unsignedchara00,a01,a02;unsignedchara10,a11,a12;unsignedchara20,a21,a22;voidMySobel(IplImage*gray,IplImage*gradient){CvScalarcolor;for(inti=1;i<gray->height-1;++i){for(intj=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方向上的近似導數doubleux=a20*(1)+a21*(2)+a22*(1)+(a00*(-1)+a01*(-2)+a02*(-1));//y方向上的近似導數doubleuy=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邊緣檢測得到的圖像');

③ 在C或C++環境下,分別利用sobel運算元,robert運算元,編程輸出邊緣圖像。選錯課了..完全不懂 求高手代碼..

俺就給你寫個sobel的,你把sobel模板換成robert模板就OK了。

本來sobel找閾值還有個小演算法,不過一般不要求的,俺就用黃金分割點乘以255替代了。

sobel卷積代碼如下:

voidCSobelDlg::CreateSobolImage(void)

{

staticconstintsizeOfSobelMask=9;

staticintsobelMaskHor[sizeOfSobelMask]=

{

-1,-2,-1,

0,0,0,

1,2,1

};

staticintSobelMaskVer[sizeOfSobelMask]=

{

1,0,-1,

2,0,-2,

1,0,1

};

intnumOfBytes=m_bmpInfo.bmWidthBytes*m_bmpInfo.bmHeight;

unsignedchar*pbuf1=newunsignedchar[numOfBytes];

unsignedchar*pbuf2=newunsignedchar[numOfBytes];

m_bmpOrg.GetBitmapBits(numOfBytes,pbuf1);

unsignedcharaverageColor=0;

for(introw=0;row<m_bmpInfo.bmHeight;++row)

{

for(intcol=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;

}

}

unsignedcharts=0,tv=0,tmp=0,dst=0,idx=0;

for(introw=1;row<m_bmpInfo.bmHeight-1;++row)

{

for(intcol=1;col<m_bmpInfo.bmWidth-1;++col)

{

idx=ts=tv=0;

for(intr=row-1;r<=row+1;++r)

{

for(intc=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=(unsignedchar)sqrt((float)(ts*ts+tv*tv));

if(dst>(unsignedchar)(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;

}

再給你一張本程序運行的效果圖。

④ 下面是關於C++ Sobel運算元的程序代碼 相關知識了解 語句看不懂 希望大神能幫我注釋下 方便我理解 謝謝

//Sobel運算元
BOOL SySobel(HDIB hDib)
{
if(hDib==NULL)return FALSE;

LPBYTE lpBits=FindDIBBits((LPBYTE)GlobalLock(hDib));
BITMAPINFO *bmi = (BITMAPINFO *)GlobalLock(hDib);
LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)bmi;
int nx,ny;
int height,width;
// get color number
WORD wNumColors = DIBNumColors((LPBYTE)bmi);
height=bmi->bmiHeader.biHeight;
width=bmi->bmiHeader.biWidth;

double gray;
double grayl;
double grayr;

BYTE **logImg;//二維緩沖區
logImg=new BYTE *[height];
for(ny=0;ny<height;ny++)
logImg[ny]=new BYTE [width];

BYTE **logImg1;//二維緩沖區
logImg1=new BYTE *[height];
for(ny=0;ny<height;ny++)
logImg1[ny]=new BYTE [width];

BYTE **logImg2;//二維緩沖區
logImg2=new BYTE *[height];
for(ny=0;ny<height;ny++)
logImg2[ny]=new BYTE [width];

lpBits=(LPBYTE)lpbi+lpbi->biSize+(int)wNumColors*sizeof(RGBQUAD);//獲取數據矩陣指針
int nDelta = WIDTHBYTES(lpbi->biBitCount*lpbi->biWidth) - lpbi->biWidth*lpbi->biBitCount/8;

for ( ny=0; ny<height; ny++)
{
for ( nx=0; nx<width; nx++)
{
gray=*(lpBits);
logImg[ny][nx]=(BYTE)gray;
logImg1[ny][nx]=(BYTE)gray;
logImg2[ny][nx]=(BYTE)gray;
lpBits++;
}
lpBits += nDelta;
}

for( ny=1; ny<(height-1); ny++)
{
for( nx=1; nx<(width-1); nx++)
{

//gray=(BYTE)sqrt(grayl*grayl+grayr*grayr);
grayr=fabs(grayr);
grayl=fabs(grayl);
if (grayr>255)
logImg1[ny][nx]=255;
else
logImg1[ny][nx]=(BYTE)grayr;
if (grayl>255)
logImg2[ny][nx]=255;
else
logImg2[ny][nx]=(BYTE)grayr;
}
}lpBits=(LPBYTE)lpbi+lpbi->biSize+(int)wNumColors*sizeof(RGBQUAD);//獲取數據矩陣指針

for( ny=1; ny<(height-1); ny++)
{
for( nx=1; nx<(width-1); nx++)
{
if(logImg2[ny][nx]>logImg1[ny][nx])
{
logImg1[ny][nx]=logImg2[ny][nx];
}
}
}

for ( ny=0; ny<lpbi->biHeight; ny++)
{
for ( nx=0; nx<lpbi->biWidth; nx++)
{
gray=logImg1[ny][nx];
*(lpBits)=(BYTE)gray;
lpBits++;
}
lpBits += nDelta;
}

for(ny=0;ny<height;ny++)
delete [] logImg[ny];
delete [] logImg;
for(ny=0;ny<height;ny++)
delete [] logImg1[ny];
delete [] logImg1;
for(ny=0;ny<height;ny++)
delete [] logImg2[ny];
delete [] logImg2;
GlobalUnlock(hDib);
//WaitCursorEnd();
return TRUE;
}

⑤ Sobel運算元的核心公式

該運算元包含兩組3x3的矩陣,分別為橫向及縱向,將之與圖像作平面卷積,即可分別得出橫向及縱向的亮度差分近似值。如果以A代表原始圖像,Gx及Gy分別代表經縱向及橫向邊緣檢測的圖像,其公式如下:
圖像的每一個像素的橫向及縱向梯度近似值可用以下的公式結合,來計算梯度的大小。

然後可用以下公式計算梯度方向。
在以上例子中,如果以上的角度Θ等於零,即代表圖像該處擁有縱向邊緣,左方較右方暗。

⑥ 求用C語言用Sobels運算元方法編寫圖像邊緣提取的程序演算法!(急)

定義:每個像素的取值均為0或1,稱這樣的圖像為二值圖像。

演算法:檢查所有像素,若該像素為物體上與背景接觸的像素(四連通像素中既有背景像素又有物體像素),則為邊界。

程序:

#define M 30
#define N 20

void edge(int image[M][N],int bianyuan[M][N])
{
int i,j;
int inner=1,outer=1;
for (i=0;i<M;i++)/*清除數據*/
for(j=0;j<N;j++)
bianyuan[i][j]=0;
for(i=1;i<M-1;i++)
for(j=1;j<N-1;j++)
{
inner=1;/*假設該像素或為物體,或為背景*/
outer=1;
if(image[i-1][j]==0||image[i+1][j]==0||image[i][j-1]==0||image[i][j+1]==0)
inner=0;
if(image[i-1][j]==1||image[i+1][j]==1||image[i][j-1]==1||image[i][j+1]==1)
outer=0;
if(inner==0&&outer==0&&image[i][j]==1)/*像素周圍既有物體又有背景*/ bianyuan[i][j]=1;/*,且該像素為物體上的像素(image[i][j]==1),則定義為邊界*/
}
}

void output(int array[M][N],int n)
{
int i,j;
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<N;j++)
if(array[i][j]==1)
printf("1");
else
printf(" ");
}
}

void main()
{
int image[M][N]={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,0,0,0,1,1,1,1,0,0,0,1,1,1,0,0},
{0,1,1,1,1,0,0,1,1,1,1,1,1,0,0,1,1,1,0},
{0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,1,1,1,0},
{0,0,1,1,1,1,0,0,0,1,1,1,1,1,0,1,1,1,0},
{0,1,1,1,1,1,1,0,0,1,1,1,0,0,1,1,1,1,0},
{0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,0},
{0,0,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,0},
{0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0}};
int bianyuan[M][N]={0};
int i,j;
printf("\nThe origianl image is:\n");
output(image,10);
edge(image,bianyuan);
printf("\nIts edge is:\n");
output(bianyuan,10);
}

寫完了,又看一下,感覺edge函數太羅嗦了,不夠簡練,想了一下,改成了下面的樣子,函數介面不變:

void edge(int image[M][N],int bianyuan[M][N])
{
int i,j;
for (i=0;i<M;i++)
for(j=0;j<N;j++)
bianyuan[i][j]=0;
for(i=1;i<M-1;i++)
for(j=1;j<N-1;j++)
{
int t=image[i-1][j]+image[i+1][j]+image[i][j-1]+image[i][j+1];
if(t>0&&t<4&&image[i][j]==1)/*周圍4個像素值介於1~3之間,*/
bianyuan[i][j]=1; /*且當前像素為物體,則其必為邊界*/
}
}

希望這段代碼對你有所幫助

⑦ 求高手 幫幫忙,用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 ImageInfo(FILE* inputFile, FILE* outputFile);
void ColorTable(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] = -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: www.cee.hw.ac.uk/hipr/html/sobel.html */
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---------*/
ImageInfo(bmpInput, bmpOutput);
ColorTable(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(sumX>255) sumX=255;
if(sumX<0) 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(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;
long value = 0L;
unsigned char mmy;
int i;

mmy = '0';
ptrC = &mmy;

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 ImageInfo(FILE* inputFile, FILE* outputFile)
{
unsigned char *ptrC;
unsigned char mmy;
int i;

mmy = '0';
ptrC = &mmy;

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 ColorTable(FILE* inputFile, FILE* outputFile, int nColors)
{
unsigned char *ptrC;
unsigned char mmy;
int i;

mmy = '0';
ptrC = &mmy;

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運算元是怎麼編程和實現的

function __construct($info=array()){
$this->host = $info['h'];
$this->name = $info['u'];
$this->pass = $info['pwd'];
$this->table = $info['dbname'];
$this->connect();
}

⑨ 急!Sobel運算元邊緣檢測演算法程序代碼誰有啊,能不能發到我的郵箱[email protected]

close all
clear all
I=imread('tig.jpg'); %讀取圖像
I1=im2double(I); %將彩圖序列變成雙精度
I2=rgb2gray(I1); %將彩色圖變成灰色圖
[thr, sorh, keepapp]=ddencmp('den','wv',I2);
I3=wdencmp('gbl',I2,'sym4',2,thr,sorh,keepapp); %小波除噪
I4=medfilt2(I3,[9 9]); %中值濾波
I5=imresize(I4,0.2,'bicubic'); %圖像大小
BW1=edge(I5,'sobel'); %sobel圖像邊緣提取
BW2=edge(I5,'roberts'); %roberts圖像邊緣提取
BW3=edge(I5,'prewitt'); %prewitt圖像邊緣提取
BW4=edge(I5,'log'); %log圖像邊緣提取
BW5=edge(I5,'canny'); %canny圖像邊緣提取
h=fspecial('gaussian',5); %高斯濾波
BW6=edge(I5,'zerocross',[ ],h); %zerocross圖像邊緣提取
figure;
subplot(1,3,1); %圖劃分為一行三幅圖,第一幅圖
imshow(I2); %繪圖
figure;
subplot(1,3,1);
imshow(BW1);
title('Sobel運算元');
subplot(1,3,2);
imshow(BW2);
title('Roberts運算元');
subplot(1,3,3);
imshow(BW3);
title('Prewitt運算元');