C语言怎么处理图片

Python022

C语言怎么处理图片,第1张

首先现在图片取模软件找到软件快捷方式,点击打开软件

现在进入到了取模软件,点击“载入图片”,现在就可以进行添加图片了

选择需要添加的图片,点击选择图片,然后单击“打开”

现在点击“设置”进入图片参数设置

现在有输出格式,取模方式,图片截取范围一些参数设置进行设置

如果确认无误,直接单击“参数确认就可以了”

然后进行数据保存,点击数据保存

选择文件保存路径,点击“保存”就可以了

现在就可以看到刚才生成的图片C文件了,直接将代码添加进C语言就可以了

#include

using namespace std

#define Twoto1(i,j,w) i*w+j

void createimage(unsigned char *&img, int w, int h)

{img = new unsigned char[w*h]}

void delateimage(unsigned char*img)

{delete []img}

void readimage(unsigned char*img, int w, int h, char *fname)

{

FILE *fp

fopen_s(&fp,fname, "rb")

if (fp == NULL){ cout <<"error" <<endlreturn}

size_t result

result=fread(img , sizeof(unsigned char), w*h, fp)

if (result != w*h)

{

cout <<"Reading error" <<endl

return

}

else

cout <<"Reading Ok!" <<endl

fclose(fp)

}

void mobanjuanji(unsigned char image, unsigned char*image1, int w, int h, float moban[5][5])

{

for (int i = 0i for (int j = 0j if (iw - 3 || j>h - 3)

image1[Twoto1(i,j,w)] = 0

else

{

float temp = 0

for (int m = 0m<5m++)

for (int n = 0n<5n++)

{

temp += (image[Twoto1(i-2+m,j-2+n,w)] moban[m][n])

}

if (temp>255) image1[Twoto1(i, j, w)] = 255

else if (temp<0) image1[Twoto1(i, j, w)] = 0

else image1[Twoto1(i, j, w)] = temp

}

}

void saveimage(unsigned char *img, int w, int h, char *fname)

{

FILE *fp

fopen_s(&fp, fname, "wb")

if (fp == NULL) { cout <<"error" <<endlreturn}

size_t result

result = fwrite(img, sizeof(unsigned char), w*h, fp)

if (result != w*h)

{

cout <<"Write error" <<endl

return

}

else

cout <<"Write Ok!" <<endl

fclose(fp)

}

void main()

{

unsigned char *img

unsigned char *img1

float moban[5][5] = { {0,0,0,0,0},{0, -1, 0, 1, 0 }, { 0, -2, 0, 2, 0 }, { 0, -1, 0, 1, 0 }, { 0,0,0,0,0 } }

//float moban[5][5] = { 0 }

int w = 512, h = 512

createimage(img, w, h)

createimage(img1, w, h)

readimage(img, w, h, "E:\ss.raw")

mobanjuanji(img, img1,w, h, moban)

saveimage(img, w, h, "E:\ss_1.raw")

saveimage(img1, w, h, "E:\ss_2.raw")

delateimage(img)

delateimage(img1)

}

扩展资料

C语言实现一个图片的读出和写入

#include <stdlib.h>

#include <windows.h>

int file_size(char* filename)//获取文件名为filename的文件大小。

{

FILE *fp = fopen(filename, "rb")//打开文件。

int size

if(fp == NULL) // 打开文件失败

return -1

fseek(fp, 0, SEEK_END)//定位文件指针到文件尾。

size=ftell(fp)//获取文件指针偏移量,即文件大小。

fclose(fp)//关闭文件。

return size

}

int main ()

{

int size=0

size=file_size("qw")

printf("%d\n",size)

FILE * pFile,*qw

char *buffer=(char*)malloc(sizeof(char)*size)

qw   =fopen("qw","r")

pFile = fopen ( "qwe" , "wb" )

printf("%d==\n",pFile)

printf("%d\n",size)

fread(buffer,1,size,qw)

fwrite (buffer , sizeof(byte), size , pFile )

fclose (pFile)

rename("qwe","Groot.jpg")

return 0

}

图片格式有很多种

bmp,

jpg,

gif,

png

等等

每种都有自己的格式。

处理图片

一般流程

都是先读取图片文件,根据格式解析成位图(bitmap)

然后对位图进行处理。

所以

重点是解析。

这个是有很多的开源C库的。