[問題] 加入中央加權中值濾波
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
VC6.0
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
已經完成開圖 開檔 灰階等動作 但是想要寫入中央加權中值法 卻無法使用
argc與argv 所以想要使用陣列方式讀取圖片 並以中央加權中值法寫入變化
餵入的資料(Input):
預期的正確結果(Expected Output):
錯誤結果(Wrong Output):
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <stdio.h>
#include <stdlib.h>
int bmp_read(unsigned char *image, int xsize, int ysize, const char
*filename) {
char fname_bmp[128];
FILE *fp;
unsigned char header[54];
sprintf(fname_bmp, "%s.bmp", filename);
if (!(fp = fopen(fname_bmp, "rb")))
return -1;
fread(header, sizeof(unsigned char), 54, fp);
fread(image, sizeof(unsigned char), (size_t)(long)xsize * ysize * 3, fp);
fclose(fp);
return 0;
}
int bmp_write(unsigned char *image, int xsize, int ysize, char *filename) {
unsigned char header[54] = {
0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,
54, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
};
long file_size = (long)xsize * (long)ysize * 3 + 54;
long width, height;
char fname_bmp[128];
FILE *fp;
header[2] = (unsigned char)(file_size &0x000000ff);
header[3] = (file_size >> 8) & 0x000000ff;
header[4] = (file_size >> 16) & 0x000000ff;
header[5] = (file_size >> 24) & 0x000000ff;
width = xsize;
header[18] = width & 0x000000ff;
header[19] = (width >> 8) &0x000000ff;
header[20] = (width >> 16) &0x000000ff;
header[21] = (width >> 24) &0x000000ff;
height = ysize;
header[22] = height &0x000000ff;
header[23] = (height >> 8) &0x000000ff;
header[24] = (height >> 16) &0x000000ff;
header[25] = (height >> 24) &0x000000ff;
sprintf(fname_bmp, "%s.bmp", filename);
if (!(fp = fopen(fname_bmp, "wb")))
return -1;
fwrite(header, sizeof(unsigned char), 54, fp);
fwrite(image, sizeof(unsigned char), (size_t)(long)xsize * ysize * 3, fp);
fclose(fp);
return 0;
}
int main(void)
{
unsigned char *image, gray;
int xsize = 256;
int ysize = 256;
int row, col;
image = (unsigned char *)malloc((size_t)xsize * ysize * 3);
if (image == NULL)
return -1;
bmp_read(image, xsize, ysize, "trees"); //real image file
//-------------color --> gray level-----------------------
for (row=0; row < xsize; row++)
{
//ptr=(Byte *) TempBitmap->ScanLine[row]; //real 1 row pixels
//index=0;
for (col=0; col< ysize; col++) // RGB -> GrayLevel for a pixel
{
gray=(unsigned char) ( 0.2126* (*(image + 3 * (row*xsize+col) )) +
0.7152* (*(image + 3 * (row*xsize+col)+1 )) + 0.0722* (*(image + 3 *
(row*xsize+col)+2 ) ) );
*(image + 3 * (row*xsize+col) +2)=gray;
*(image + 3 * (row*xsize+col) +1)=gray;
*(image + 3 * (row*xsize+col) ) =gray;
//index +=3;
//img[row][col]=gray;
}
}
//---------------------image processing algorithm ----------------
//--------------------------------------------------------------------
bmp_write(image, xsize, ysize, "clena_clone_C"); //write image file
free(image);
}
補充說明(Supplement):
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.121.137.228
→
08/30 13:19, , 1F
08/30 13:19, 1F
→
08/30 13:26, , 2F
08/30 13:26, 2F
→
08/30 13:27, , 3F
08/30 13:27, 3F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章