图片的RGB表示

图片的格式是每一行是一个list,list中的每个元素存放了对应该像素点的(r,g,b)
包含头文件
#include<stdio.h>#include<stdlib.h>#include<string.h>#define STB_IMAGE_IMPLEMENTATION#include "stb_image/stb_image.h"#define STB_IMAGE_WRITE_IMPLEMENTATION#include "stb_image/stb_image_write.h"#include"error_check.h"#include"time_helper.h"
图像旧照片化
CPU处理版本
void rgb_to_sepia_cpu(unsigned char *input_image, unsigned char *output_image, int width, int height, int channels){for(int row=0; row<height; row++){for(int col=0; col<width; col++){int offset = (row*width + col)*channels; // 拿到当前像素的点的三个RGB参数unsigned char c1 = input_image[offset];unsigned char c2 = input_image[offset+1];unsigned char c3 = input_image[offset+2];*(output_image + offset) = (unsigned char)fmin((c1 * 0.393 + c2 * 0.769 + c3 * 0.189), 255.0);*(output_image + offset + 1) = (unsigned char)fmin((c1 * 0.349 + c2 * 0.686 + c3 * 0.168), 255.0);*(output_image + offset + 2) = (unsigned char)fmin((c1 * 0.272 + c2 * 0.534 + c3 * 0.131), 255.0);if(channels==4){*(output_image + offset + 3) = input_image[offset + 3];}}}}
GPU处理版本
int col = threadIdx.x+blockIdx.x*blockDim.x;int row =threadIdx.y+blockIdx.y*blockDim.y;if (col<width && row < height){int offset = row*width + col;int rgboffset = offset*channels;unsigned char c1 = g_input_image[rgboffset]; // r通道的值unsigned char c2 = g_input_image[rgboffset+1]; // gunsigned char c3 = g_input_image[rgboffset+2];//r*(g_output_image + rgboffset) = (unsigned char)fmin((c1 * 0.393 + c2 * 0.769 + c3 * 0.189), 255.0);*(g_output_image + rgboffset + 1) = (unsigned char)fmin((c1 * 0.349 + c2 * 0.686 + c3 * 0.168), 255.0);*(g_output_image + rgboffset + 2) = (unsigned char)fmin((c1 * 0.272 + c2 * 0.534 + c3 * 0.131), 255.0);if (channels ==4){*(g_output_image + rgboffset + 3) = g_input_image[rgboffset + 3];}}
灰度图答案
__global__ void colorToGreyscaleConversion(unsigned char* g_output_image,unsigned char* g_input_image,int width,int channels){int col = threadIdx.x+blockIdx.x*blockDim.x;int row =threadIdx.y+blockIdx.y*blockDim.y;if (col<width && row < height){int offset = row*width + col;int rgboffset = offset*channels;unsigned char c1 = g_input_image[rgboffset]; // r通道的值unsigned char c2 = g_input_image[rgboffset+1]; // gunsigned char c3 = g_input_image[rgboffset+2];//b*(g_output_image + offset)=(unsigned char)fmin((c1 * 0.272 + c2 * 0.534 + c3 * 0.131), 255.0)if (channels ==4){*(g_output_image + rgboffset + 3) = g_input_image[rgboffset + 3];}}}
