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

羅伯特運算元c語言程序

發布時間: 2022-06-20 19:19:38

A. c語言編程題

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char expr[256] = {0}; //表達式字元串
char *tmp = expr;
char operator; //運算符
int a,b;

fgets(expr,255,stdin);

//取第一個數字
a = b = 0;
while(*tmp >= 0 && *tmp <= 9 && *tmp != '\0')
{
a = a * 10 + *tmp++ - '0';
}
//取運算符
if(*tmp == '\0')
{
printf("表達式不完整\n");
return 0;
}
operator = *tmp++;
//取第二個數字
if(*tmp == '\0')
{
printf("表達式不完整\n");
return 0;
}
b = atoi(tmp);

switch(operator)
{
case '+':
printf("%d\n",a + b);
break;
case '-':
printf("%d\n",a - b);
break;
case '*':
printf("%d\n",a * b);
break;
case '/':
printf("%d\n",a / b);
break;
default:
printf("運算符錯誤");
}

return 0;
}

B. 用c/c++實現Prewitt運算元

matlab。。。

C. C語言問題

關系表達式:顧名思義,就是由關系運算符結合操作數組成的表達式。
邏輯表達式:自然就是以邏輯運算符結合操作數組成的表達式。

在c語言中:
1.關系運算符:>(大於),<(小於),==(等於),!=(不等於),>=(大於等於),<=(小於等於)。
2.邏輯運算符:!(邏輯非),&&(邏輯與),||(邏輯或)。注意與下面的位運算符區別
3.位運算符:~(按位取返),&(按位與),|(按位或),^(按位異或)。注意比較一下上面的邏輯運算符。

所以,你上面的1&0是位運算中的按位與運算。1與0 結果是0

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

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

E. 在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;

}

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

F. 求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);
}
}
}
}

G. 求,用C語言編的遺傳演算法程序(生產批量問題)

我一個朋友曾經編過類似的,不知道你能不能用的上,給我email,我給你發過去參考一下吧,赫赫,希望對你有幫助。

H. 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);
}

}

I. C語言:已有定義int x=3,y=4,z=5;則表達式 !(x+y)+z-1&&y+z/2的值是

答案是1。

涉及到的運算符: ! () + - && /

運算符優先順序為:() ! / + - &&

運算過程:

x=3,y=4,z=5

!(x+y)+z-1&&y+z/2

!(3+4)+5-1&&4+5/2

所以表達式計算步驟為

!(3+4)+5-1&&4+5/2 -> !7+5-1&&4+5/2

!7為 0

-> 0+5-1&&4+5/2

5/2 整數除整數得整數 2

-> 0+5-1&&4+2

-> 4&&6

邏輯運算 &&

如果同一優先順序的運算符,結合次序由結合方向所決定。