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

roberts運算元c語言

發布時間: 2022-05-18 12:02:52

『壹』 求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;d<m_nPixels;d++)
{
m_B[d]=m_pPixels[d];
}
if((m_pInfo->bmiHeader.biBitCount)==24)
for(int i=1;i<m_Y-1;i++)
for(int j=3;j<(m_X-2);j+=3)
{
for(int n=0;n<9;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;d<m_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]=(temp1>temp2?temp1:temp2);
}
}
else
{
Hui();
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]=(temp1>temp2?temp1:temp2);
}
}
}
}

『貳』 用c語言求圓的面積

用c語言求圓的面積編程如下:

#include <stdio.h>

#include <math.h>

#define PI 3.14

void main()

{ float r;

double c;

double s;

printf("請輸入半徑的值:");

scanf("%f",&r);

s=PI*r*r;

printf("這個圓的面積是:%f ",s);

(2)roberts運算元c語言擴展閱讀:

使用c語言的優點

1、簡潔緊湊、靈活方便

C語言一共只有32個關鍵字,9種控制語句,程序書寫形式自由,區分大小寫。把高級語言的基本結構和語句與低級語言的實用性結合起來。C 語言可以像匯編語言一樣對位、位元組和地址進行操作,而這三者是計算機最基本的工作單元。

2、運算符豐富

C語言的運算符包含的范圍很廣泛,共有34種運算符。C語言把括弧、賦值、強制類型轉換等都作為運算符處理。從而使C語言的運算類型極其豐富,表達式類型多樣化。靈活使用各種運算符可以實現在其它高級語言中難以實現的運算。

3、數據類型豐富

C語言的數據類型有:整型、實型、字元型、數組類型、指針類型、結構體類型、共用體類型等。能用來實現各種復雜的數據結構的運算。並引入了指針概念,使程序效率更高。

4、表達方式靈活實用

C語言提供多種運算符和表達式值的方法,對問題的表達可通過多種途徑獲得,其程序設計更主動、靈活。它語法限制不太嚴格,程序設計自由度大,如對整型量與字元型數據及邏輯型數據可以通用等。

5、允許直接訪問物理地址,對硬體進行操作

由於C語言允許直接訪問物理地址,可以直接對硬體進行操作,因此它既具有高級語言的功能,又具有低級語言的許多功能,能夠像匯編語言一樣對位(bit)、位元組和地址進行操作,而這三者是計算機最基本的工作單元,可用來寫系統軟體。

6、生成目標代碼質量高,程序執行效率高

C語言描述問題比匯編語言迅速,工作量小、可讀性好,易於調試、修改和移植,而代碼質量與匯編語言相當。C語言一般只比匯編程序生成的目標代碼效率低10%~20%。

7、可移植性好

C語言在不同機器上的C編譯程序,86%的代碼是公共的,所以C語言的編譯程序便於移植。在一個環境上用C語言編寫的程序,不改動或稍加改動,就可移植到另一個完全不同的環境中運行。

8、表達力強

C語言有豐富的數據結構和運算符。包含了各種數據結構,如整型、數組類型、指針類型和聯合類型等,用來實現各種數據結構的運算。C語言的運算符有34種,范圍很寬,靈活使用各種運算符可以實現難度極大的運算。

C語言能直接訪問硬體的物理地址,能進行位(bit)操作。兼有高級語言和低級語言的許多優點。

它既可用來編寫系統軟體,又可用來開發應用軟體,已成為一種通用程序設計語言。

另外C語言具有強大的圖形功能,支持多種顯示器和驅動器。且計算功能、邏輯判斷功能強大。

『叄』 C語言入門知識

char a[20],b[20],c[20];上面的a,b,c都是數組變數,在定義時已經為其分配棧內存,而char (*str1)[20],(*str2)[20],(*str3)[20]; 只是分配了4個位元組的內存來保存指針變數(即變數本身),而指針的值(即指向)還沒有,所以這些指針還不能保存數據!--------------char (*str1)[20],(*str2)[20],(*str3)[20]; str1 str2 str3均為指針,都指向長度為20的char數組.你只定義了指針,但是沒有對其進

『肆』 能否編寫一個用matlab實現的基於Roberts運算元的圖像邊緣檢測程序

clear; fid=fopen('d:\image\Lena.img','r'); %打開無格式文件 data1=(fread(fid,[256,256],'uint8'))'; subplot(2,3,1),imagesc(data1); colormap(gray); title('LENA','Color','r'); subplot(2,3,2); data2=uint8(data1); R=edge(data2,'Roberts'); imagesc(R); title('Roberts檢測運算元'); subplot(2,3,3); data2=uint8(data1); Q=edge(data2,'Prewitt'); imagesc(Q); title('Prewitt檢測運算元'); subplot(2,3,4); data2=uint8(data1); S=edge(data2,'Sobel'); imagesc(S); title('Sobel檢測運算元'); subplot(2,3,5); data2=uint8(data1); T=edge(data2,'Log'); imagesc(T); title('Log檢測運算元'); Matlab中運行過的 正確的!

『伍』 Matlab關於圖像邊緣提取,用Sobel運算元、Roberts運算元、Prewitt運算元,加QQ詳談 急用!!

4.2.1 Roberts演算法原理

Roberts運算元是一種最簡單的運算元,是一種利用局部差分運算元尋找邊緣的運算元,他採用對角線方向相鄰兩象素之差近似梯度幅值檢測邊緣。檢測垂直邊緣的效果好於斜向邊緣,定位精度高,對雜訊敏感,無法抑制雜訊的影響。

4.2.2 演算法流程

Roberts運算元在2×2領域上計算對角導數

(4-1)

成為Roberts交叉運算元。在實際應用中為了簡化計算,用梯度函數的Roberts絕對值來近似

(4-2)另外還可以用Roberts 最大運算元來計算

(4-3)

上式能夠提供較好的不變性邊緣取向。對於同等長度但取向不同的邊緣,應用Roberts最大值運算元比應用Roberts交叉運算元所得到的合成幅度變化小。Roberts邊緣檢測運算元的卷積運算元為

Roberts 邊緣運算元方向模版

由上面兩個卷積運算元對圖像運算後,代入(3-7)式,可求得圖像的梯度幅度值,然後選取門限TH,做如下判斷>TH,為階躍狀邊緣點為一個二值圖像,也就是圖像的邊緣圖像。4.4.1 Prewitt 演算法原理

Prewitt邊緣運算元是一種邊緣樣板運算元。Prewitt 從加大邊緣檢測運算元的模板大小出發,由2×2 擴大到3×3 來計算差分運算元,採用Prewitt 運算元不僅能檢測邊緣點,而且能抑制雜訊的影響。
Prewitt 採用計算偏微分估計的方法,由式(4-9)所示的兩個卷積運算元形成了Prewitt邊緣運算元,樣板運算元由理想的邊緣子圖像構成,依次用邊緣樣板去檢測圖像,與被檢測區域最為相似的樣板給出最大值,用這個最大值作為運算元的輸出

(4-9)

另一種方法是,可以將Prewitt運算元擴展到八個方向,每個模版對特定的邊緣方向做出最大響應,所有8個方向中最大值作為邊緣幅度圖像的輸出,這些運算元樣板由離線的邊緣子圖像構成。依次用邊緣樣板去檢測圖像,與被檢測區域最為相似的的樣板給出最大值。定義Prewitt 邊緣檢測的運算元模版如下:

(1)1方向 (2)2方向 (3)3方向 (4)4方

『陸』 vc圖像處理Robert運算元源代碼有點看不懂,請教高手幫我解答一下

/*
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 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: */
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---------*/
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運算元、Roberts運算元、Prewitt運算元對圖像進行邊緣檢測的程序,在matlab環境下,程序要完整,最好有圖

closeall

clearall

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,[99]);%中值濾波

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運算元');

『捌』 求用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; /*且當前像素為物體,則其必為邊界*/
}
}

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

『玖』 在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語言程序

抄來給你:

/// 按 Roberts 運算元進行邊緣檢測
///
/// 點陣圖流
///
public Bitmap Roberts(Bitmap b)
{
int width = b.Width;
int height = b.Height;
Bitmap dstImage = new Bitmap(width, height);
BitmapData srcData = b.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData dstData = dstImage.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
int stride = srcData.Stride;
int offset = stride - width * BPP;
unsafe
{
byte* src = (byte*)srcData.Scan0;
byte* dst = (byte*)dstData.Scan0;
int A, B; // A(x-1, y-1) B(x, y-1)
int C, D; // C(x-1, y) D(x, y)
// 指向第一行
src += stride;
dst += stride;
// 不處理最上邊和最左邊
for (int y = 1; y < height; y++)
{
// 指向每行第一列
src += BPP;
dst += BPP;
for (int x = 1; x < width; x++)
{
for (int i = 0; i < 3; i++)
{
A = src[i - stride - BPP];
B = src[i - stride];
C = src[i - BPP];
D = src[i];
dst[i] = (byte)(Math.Sqrt((A - D) * (A - D) + (B - C) * (B - C)));
} // i
dst[3] = src[3];
src += BPP;
dst += BPP;
} // x
src += offset;
dst += offset;
} // y
}
b.UnlockBits(srcData);
dstImage.UnlockBits(dstData);
b.Dispose();
return dstImage;
} // end of Roberts

/// 邊緣增強
///
/// 點陣圖流
/// 閾值
///
public Bitmap EdgeEnhance(Bitmap b, int threshold)
{
int width = b.Width;
int height = b.Height;
Bitmap dstImage = (Bitmap)b.Clone();
BitmapData srcData = b.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData dstData = dstImage.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
// 圖像實際處理區域
// 不考慮最左 1 列和最右 1 列
// 不考慮最上 1 行和最下 1 行
int rectTop = 1;
int rectBottom = height - 1;
int rectLeft = 1;
int rectRight = width - 1;
unsafe
{
byte* src = (byte*)srcData.Scan0;
byte* dst = (byte*)dstData.Scan0;
int stride = srcData.Stride;
int offset = stride - width * BPP;
int pixel = 0;
int maxPixel = 0;

// 指向第 1 行
src += stride;
dst += stride;
for (int y = rectTop; y < rectBottom; y++)
{
// 指向每行第 1 列像素
src += BPP;
dst += BPP;
for (int x = rectLeft; x < rectRight; x++)
{
// Alpha
dst[3] = src[3];
// 處理 B, G, R 三分量
for (int i = 0; i < 3; i++)
{
// 右上-左下
maxPixel = src[i - stride + BPP] - src[i + stride - BPP];
if (maxPixel < 0) maxPixel = -maxPixel;
// 左上-右下
pixel = src[i - stride - BPP] - src[i + stride + BPP];
if (pixel < 0) pixel = -pixel;
if (pixel > maxPixel) maxPixel = pixel;
// 上-下
pixel = src[i - stride] - src[i + stride];
if (pixel < 0) pixel = -pixel;
if (pixel > maxPixel) maxPixel = pixel;
// 左-右
pixel = src[i - BPP] - src[i + BPP];
if (pixel < 0) pixel = -pixel;
if (pixel > maxPixel) maxPixel = pixel;
// 進行閾值判斷
if (maxPixel < threshold) maxPixel = 0;

dst[i] = (byte)maxPixel;
}
// 向後移一像素
src += BPP;
dst += BPP;
} // x
// 移向下一行
// 這里得注意要多移 1 列,因最右邊還有 1 列不必處理
src += offset + BPP;
dst += offset + BPP;
} // y
}
// b.UnlockBits(srcData);
dstImage.UnlockBits(dstData);
b.Dispose();
return dstImage;
} // end of EdgeEnhance

}