📄 fig2.cpp
字号:
#include "stdafx.h"
#include "comapi.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define GL 256
#define GL1 255
void fatal_error(char *msg);
/************* fspace_2d ***************************************/
/* To allocation a 2_dimension dynamic array */
/*
void **fspace_2d(int row,int col,int lenth)
{
int i,j;
void **b;
b = (void **)calloc(sizeof(void *),row);
if (b==NULL)
{
free(b);
return NULL;
}
for(i=0;i<row;i++)
{
b[i] = (void *)calloc(lenth,col);
if (b[i]==NULL)
{ for(j=i-1;j>=0;j--)
free(b[j]);
free(b);
return NULL;
}
}
return(b);
}
*/
/******************************* ffree_2d ****************************/
/* To free a 2_dimension dynamic array */
/*
void ffree_2d(void **a,int row)
{
int i;
for(i=0;i<row;i++) free(a[i]);
free(a);
}
*/
/********************************************************/
void Sobel(unsigned char **ImgSrc, unsigned char **ImgDes,
unsigned ImgSizeX, unsigned ImgSizeY)
{
int i, j;
int tmp1, tmp2;
for (i= 1; i< ImgSizeY-1; i++)
{
for (j= 1; j< ImgSizeX-1; j++)
{
tmp1= (ImgSrc[i-1][j-1]+ 2* ImgSrc[i-1][j]+ ImgSrc[i-1][j+1])-
(ImgSrc[i+1][j-1]+ 2* ImgSrc[i+1][j]+ ImgSrc[i+1][j+1]);
tmp2= (ImgSrc[i-1][j-1]+ 2* ImgSrc[i][j-1]+ ImgSrc[i+1][j-1])-
(ImgSrc[i-1][j+1]+ 2* ImgSrc[i][j+1]+ ImgSrc[i+1][j+1]);
if (tmp1 < 0) tmp1=-tmp1;
if (tmp2 < 0) tmp2= -tmp2;
if (tmp1 < tmp2) tmp1= tmp2;
if (tmp1 > GL1) tmp1= GL1;
ImgDes[i][j]= tmp1;
}
}
for(i=0;i<ImgSizeY;i++){
ImgDes[i][0]=0;
ImgDes[i][ImgSizeX-1]=0;
}
for(j=0;j<ImgSizeX;j++){
ImgDes[0][j]=0;
ImgDes[ImgSizeY-1][j]=0;
}
}
void Sobel0(unsigned char **ImgSrc, unsigned char **ImgDes,
unsigned ImgSizeX, unsigned ImgSizeY)
{
int i, j;
int tmp1;
for (i= 1; i< ImgSizeY-1; i++)
{
for (j= 1; j< ImgSizeX-1; j++)
{
tmp1=(+1)*ImgSrc[i-1][j-1]+(+2)*ImgSrc[i-1][j]+(+1)*ImgSrc[i-1][j+1]
+( 0)*ImgSrc[i][j-1] +( 0)*ImgSrc[1][j+1]
+(-1)*ImgSrc[i+1][j-1]+(-2)*ImgSrc[i+1][j]+(+1)*ImgSrc[i+1][j+1];
if (tmp1 < 0) tmp1=-tmp1;
if (tmp1 > GL1) tmp1= GL1;
ImgDes[i][j]= tmp1;
}
}
for(i=0;i<ImgSizeY;i++){
ImgDes[i][0]=0;
ImgDes[i][ImgSizeX-1]=0;
}
for(j=0;j<ImgSizeX;j++){
ImgDes[0][j]=0;
ImgDes[ImgSizeY-1][j]=0;
}
}
void Sobel45(unsigned char **ImgSrc, unsigned char **ImgDes,
unsigned ImgSizeX, unsigned ImgSizeY)
{
int i, j;
int tmp1;
for (i= 1; i< ImgSizeY-1; i++)
{
for (j= 1; j< ImgSizeX-1; j++)
{
tmp1=(+2)*ImgSrc[i-1][j-1]+(+1)*ImgSrc[i-1][j]+( 0)*ImgSrc[i-1][j+1]
+( 1)*ImgSrc[i][j-1] +(-1)*ImgSrc[1][j+1]
+( 0)*ImgSrc[i+1][j-1]+(-1)*ImgSrc[i+1][j]+(-2)*ImgSrc[i+1][j+1];
if (tmp1 < 0) tmp1=-tmp1;
if (tmp1 > GL1) tmp1= GL1;
ImgDes[i][j]= tmp1;
}
}
for(i=0;i<ImgSizeY;i++){
ImgDes[i][0]=0;
ImgDes[i][ImgSizeX-1]=0;
}
for(j=0;j<ImgSizeX;j++){
ImgDes[0][j]=0;
ImgDes[ImgSizeY-1][j]=0;
}
}
void Sobel90(unsigned char **ImgSrc, unsigned char **ImgDes,
unsigned ImgSizeX, unsigned ImgSizeY)
{
int i, j;
int tmp1;
for (i= 1; i< ImgSizeY-1; i++)
{
for (j= 1; j< ImgSizeX-1; j++)
{
tmp1=(+1)*ImgSrc[i-1][j-1]+( 0)*ImgSrc[i-1][j]+(-1)*ImgSrc[i-1][j+1]
+(+2)*ImgSrc[i][j-1] +(-2)*ImgSrc[1][j+1]
+(+1)*ImgSrc[i+1][j-1]+( 0)*ImgSrc[i+1][j]+(-1)*ImgSrc[i+1][j+1];
if (tmp1 < 0) tmp1=-tmp1;
if (tmp1 > GL1) tmp1= GL1;
ImgDes[i][j]= tmp1;
}
}
for(i=0;i<ImgSizeY;i++){
ImgDes[i][0]=0;
ImgDes[i][ImgSizeX-1]=0;
}
for(j=0;j<ImgSizeX;j++){
ImgDes[0][j]=0;
ImgDes[ImgSizeY-1][j]=0;
}
}
void Sobel135(unsigned char **ImgSrc, unsigned char **ImgDes,
unsigned ImgSizeX, unsigned ImgSizeY)
{
int i, j;
int tmp1;
for (i= 1; i< ImgSizeY-1; i++)
{
for (j= 1; j< ImgSizeX-1; j++)
{
tmp1=( 0)*ImgSrc[i-1][j-1]+(+2)*ImgSrc[i-1][j]+(+2)*ImgSrc[i-1][j+1]
+(-1)*ImgSrc[i][j-1] +(+1)*ImgSrc[1][j+1]
+(-2)*ImgSrc[i+1][j-1]+(-2)*ImgSrc[i+1][j]+( 0)*ImgSrc[i+1][j+1];
if (tmp1 < 0) tmp1=-tmp1;
if (tmp1 > GL1) tmp1= GL1;
ImgDes[i][j]= tmp1;
}
}
for(i=0;i<ImgSizeY;i++){
ImgDes[i][0]=0;
ImgDes[i][ImgSizeX-1]=0;
}
for(j=0;j<ImgSizeX;j++){
ImgDes[0][j]=0;
ImgDes[ImgSizeY-1][j]=0;
}
}
void Laplacian(unsigned char **ImgSrc, unsigned char **ImgDes,
unsigned ImgSizeX, unsigned ImgSizeY)
{
int i,j;
int tmp;
for (i= 1; i< ImgSizeY-1; i++)
{
for (j= 1; j< ImgSizeX-1; j++)
{
tmp = ImgSrc[i-1][j] + ImgSrc[i][j-1] + ImgSrc[i+1][j] +
ImgSrc[i][j+1] - 4*ImgSrc[i][j];
if (tmp < 0) tmp=-tmp;
if (tmp > GL1) tmp= GL1;
ImgDes[i][j] = tmp;
}
}
for(i=0;i<ImgSizeY;i++){
ImgDes[i][0]=0;
ImgDes[i][ImgSizeX-1]=0;
}
for(j=0;j<ImgSizeX;j++){
ImgDes[0][j]=0;
ImgDes[ImgSizeY-1][j]=0;
}
}
void Roberts(unsigned char **ImgSrc, unsigned char **ImgDes,
unsigned ImgSizeX, unsigned ImgSizeY)
{
int i,j;
int tmp1, tmp2;
for (i= 1; i< ImgSizeY-1; i++)
{
for (j= 1; j< ImgSizeX-1; j++)
{
tmp1= ImgSrc[i-1][j-1] - ImgSrc[i+1][j+1];
tmp2= ImgSrc[i-1][j+1] - ImgSrc[i+1][j-1];
if (tmp1 < 0) tmp1=-tmp1;
if (tmp2 < 0) tmp2= -tmp2;
if (tmp1 < tmp2) tmp1= tmp2;
if (tmp1 > GL1) tmp1= GL1;
ImgDes[i][j]= tmp1;
}
}
for(i=0;i<ImgSizeY;i++){
ImgDes[i][0]=0;
ImgDes[i][ImgSizeX-1]=0;
}
for(j=0;j<ImgSizeX;j++){
ImgDes[0][j]=0;
ImgDes[ImgSizeY-1][j]=0;
}
}
void Kirsch(unsigned char **ImgSrc, unsigned char **ImgDes,
unsigned ImgSizeX, unsigned ImgSizeY)
{
int i,j,k;
int tmp[8],max;
for (i= 1; i< ImgSizeY-1; i++)
{
for (j= 1; j< ImgSizeX-1; j++)
{
tmp[0]=( 5)*ImgSrc[i-1][j-1]+( 5)*ImgSrc[i-1][j]+( 5)*ImgSrc[i-1][j+1]+
(-3)*ImgSrc[i ][j-1]+( 0)*ImgSrc[i ][j]+(-3)*ImgSrc[i ][j+1]+
(-3)*ImgSrc[i+1][j-1]+(-3)*ImgSrc[i+1][j]+(-3)*ImgSrc[i+1][j+1];
tmp[1]=(-3)*ImgSrc[i-1][j-1]+( 5)*ImgSrc[i-1][j]+( 5)*ImgSrc[i-1][j+1]+
(-3)*ImgSrc[i ][j-1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]+(-3)*ImgSrc[i+1][j+1];
tmp[7]=( 5)*ImgSrc[i-1][j-1]+( 5)*ImgSrc[i-1][j]+(-3)*ImgSrc[i-1][j+1]+
( 5)*ImgSrc[i ][j-1]+( 0)*ImgSrc[i ][j]+(-3)*ImgSrc[i ][j+1]+
(-3)*ImgSrc[i+1][j-1]+(-3)*ImgSrc[i+1][j]+(-3)*ImgSrc[i+1][j+1];
max=0;
for(k=0;k<8;k++){
if(tmp[k] < 0) tmp[k]=-tmp[k];
if(tmp[k] > max) max=tmp[k];
}
if(max < 1) max=1;
if(max > GL1) max= GL1;
ImgDes[i][j] = max;
}
}
for(i=0;i<ImgSizeY;i++){
ImgDes[i][0]=0;
ImgDes[i][ImgSizeX-1]=0;
}
for(j=0;j<ImgSizeX;j++){
ImgDes[0][j]=0;
ImgDes[ImgSizeY-1][j]=0;
}
}
/********************************************************
Purpose : Rotate an image by an input angle
Argument: I_image---origin image array
row,col---the size of origin image
angle---rotation angle
*********************************************************/
void rotate( unsigned char **I_image,unsigned char **O_image,
int /*row*/col,int /* col*/row,int angle)
{
int m,n;
float center_x,center_y;
float dx,dx1,dy,dy1; /*linear interpolation argument*/
int x1,x2,y1,y2;
float oldx,oldy; /*origin image coodinate */
float newx,newy,gray;
double radians;
float PI=3.141259;
radians=angle*PI/180;
center_x=(float)(col)/2.0;
center_y=(float)(row)/2.0;
for (n=0;n<row;n++)
{
newy=n-center_y;
for (m=0;m<col;m++)
{
newx=m-center_x;
/*convert newx,newy of the rotated image to oldx,oldy of the origin image*/
oldy=newy*cosl(radians)-newx*sinl(radians);
oldx=newy*sinl(radians)+newx*cosl(radians);
/*if (oldx<(1-center_x)||oldx>(col-center_x)) continue;*/
/*if (oldy<(1-center_y)||oldy>(row-center_y)) continue;*/
oldx+=center_x;
oldy+=center_y;
if (oldx<2||oldx>col-2) { O_image [n][m]=1;continue;}
if (oldy<2||oldy>row-2) { O_image [n][m]=1;continue;}
/*get the pre&next point of (oldx oldy)*/
y1=(int)oldy;y2=y1+1;
x1=(int)oldx;x2=x1+1;
if(y2>=row||x2>=col)
continue;
/*linear interpolation*/
dx=oldx-x1;dx1=1.0-dx;
dy=oldy-y1;dy1=1.0-dy;
gray=(float) I_image [y1][x1]*dx1*dy1
+(float) I_image [y2][x1]*dx*dy1
+(float) I_image [y1][x2]*dx1*dy
+(float) I_image [y2][x2]*dx*dy;
O_image [n][m]=gray+0.5;
/*should be changed here !!!*/
}
}
return;
}
/********************************************************
Purpose : Rotate an image by an input angle
Argument: I_image---origin image array
row,col---the size of origin image
angle---rotation angle
*********************************************************/
void rotate1(unsigned char **I_image,unsigned char **O_image,
int row,int col,int angle)
{
int m,n;
float center_x,center_y;
float dx,dy,dx1,dy1; /*linear interpolation argument*/
int x1,x2,y1,y2;
float oldx,oldy; /*origin image coodinate */
float newx,newy,gray;
double radians;
float PI=3.141259;
radians=angle*PI/180;
center_x=(col)/2;
center_y=(row)/2;
for (n=0;n<row;n++)
{
newy=n-center_y;
for (m=0;m<col;m++)
{
newx=m-center_x;
/*convert newx,newy of the rotated image to oldx,oldy of the origin image*/
oldy=newy*cosl(radians)-newx*sinl(radians);
oldx=newy*sinl(radians)+newx*cosl(radians);
/*if (oldx<(1-center_x)||oldx>(col-center_x)) continue;*/
/*if (oldy<(1-center_y)||oldy>(row-center_y)) continue;*/
oldx+=center_x;
oldy+=center_y;
if (oldx<0||oldx>col-2) { O_image [n][m]=1; continue;}
if (oldy<0||oldy>row-2) { O_image [n][m]=1; continue;}
/*get the pre&next point of (oldx oldy)*/
y1=(int)oldy;y2=y1+1;
x1=(int)oldx;x2=x1+1;
/*linear interpolation*/
dx=oldx-x1;dx1=1.0-dx;
dy=oldy-y1;dy1=1.0-dy;
if(dx<dx1){dx=0;dx1=1;}
else {dx=1;dx1=0;};
if(dy<dy1){dy=0;dy1=1;}
else {dy=1;dy1=0;};
gray=(float) I_image [y1][x1]*dx1*dy1
+(float) I_image [y2][x1]*dx*dy1
+(float) I_image [y1][x2]*dx1*dy
+(float) I_image [y2][x2]*dx*dy;
O_image [n][m]=gray;
}
}
return;
}
/*********************************************************************
Purpose : Image size scaling ( integer factor )
Argument: I_image---origin image array
row,col---the size of origin image
k---scaled argument
(result image resolution devide original image resolution)
**********************************************************************/
void lower(unsigned char **I_image, unsigned char **O_image,
short /* row*/col,short /* col*/row,short k)
{
short i,j;
short oldx,oldy; /*origin image array argument */
short newx,newy; /*scaled image array argument */
short ix,iy;
short S_row,S_col;
float avg,sum; /*the avg&sum value of k*k points */
/*calculate the size of scaled image*/
S_row=(int)(row/k);
S_col=(int)(col/k);
/*scaling*/
for (iy=1;iy<=S_row;iy++)
{
oldy=(iy-1)*k;
newy=iy-1;
for (ix=1;ix<=S_col;ix++)
{
oldx=(ix-1)*k;
newx=ix-1;
sum=0;
for (i=0;i<k;i++)
{
if( (oldx+i)>col ) continue;
for (j=0;j<k;j++)
{
if ( (oldy+j)>row ) continue;
sum=sum+I_image[oldy+i][oldx+j];
}
}
avg=sum/(float)(k*k)+0.5;
O_image[newy][newx]=(int)avg;
}
}
return;
}
/***********************************************************************
* Function : Gauss, create gaussian noise
* Parameter: mean, deviation : mean and deviate of the noise
* Return : noise
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -