我正致力于图像卷积。我想给它一个边缘效果。 当它复杂时,图像应该接近黑色,但我的图像更接近灰色。如果您看到我的代码并使用image进行操作,您就会看到我在说什么。
RE.pattern = "([\w\W]+?)\n\n+([\w\W]+)"
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<stdbool.h>
#include<assert.h>
#include "BMPImage.h"
RGB getPixelColor(
const RGB* rgb_array,
const int res_x,
const int res_y,
int i,
int j)
{
if (i < 0)
i += res_x;
if (j < 0)
j += res_y;
i = i%res_x;
j = j%res_y;
const int ix = i + res_x*j;
assert(ix >= 0);
assert(ix < res_x * res_y);
return rgb_array[ix];
}
void main()
{
int res_x, res_y;
RGB* rgb_array = NULL;
readBMP24("my_pic.bmp", &res_x, &res_y, &rgb_array);
RGB* rgb_temp = (RGB*)malloc(sizeof(RGB)*res_x*res_y);
float conv_mat[3][3] = { -1,-1,-1,
-1,8,-1,
-1,-1,-1 };
for (int j = 0; j < res_y; j++)
{
for (int i = 0; i < res_x; i++)
{
RGB color_temp = { 0.0f, 0.0f, 0.0f };
for(int sub_j = 0; sub_j < 3; sub_j++)
{
for (int sub_i = 0; sub_i <3; sub_i++)
{
RGB color_neighbor = getPixelColor(
rgb_array,
res_x,
res_y,
i + sub_i - 1,
j + sub_j - 1);
color_neighbor.red_ *= conv_mat[sub_i][sub_j];
color_neighbor.green_ *= conv_mat[sub_i][sub_j];
color_neighbor.blue_ *= conv_mat[sub_i][sub_j];
color_temp.red_ += color_neighbor.red_;
color_temp.green_ += color_neighbor.green_;
color_temp.blue_ += color_neighbor.blue_;
}
rgb_temp[i + res_x * j] = color_temp;
}
//rgb_temp[i + res_x * j] = getPixelColor(
// rgb_array, res_x, res_y, i, j);
}
}
writeBMP24("changed_output.bmp", res_x, res_y, rgb_temp);
free(rgb_array);
free(rgb_temp);
}