图片的RGB表示

image.png
图片的格式是每一行是一个list,list中的每个元素存放了对应该像素点的(r,g,b)

包含头文件

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define STB_IMAGE_IMPLEMENTATION
  5. #include "stb_image/stb_image.h"
  6. #define STB_IMAGE_WRITE_IMPLEMENTATION
  7. #include "stb_image/stb_image_write.h"
  8. #include"error_check.h"
  9. #include"time_helper.h"

图像旧照片化

CPU处理版本

  1. void rgb_to_sepia_cpu(unsigned char *input_image, unsigned char *output_image, int width, int height, int channels){
  2. for(int row=0; row<height; row++){
  3. for(int col=0; col<width; col++){
  4. int offset = (row*width + col)*channels; // 拿到当前像素的点的三个RGB参数
  5. unsigned char c1 = input_image[offset];
  6. unsigned char c2 = input_image[offset+1];
  7. unsigned char c3 = input_image[offset+2];
  8. *(output_image + offset) = (unsigned char)fmin((c1 * 0.393 + c2 * 0.769 + c3 * 0.189), 255.0);
  9. *(output_image + offset + 1) = (unsigned char)fmin((c1 * 0.349 + c2 * 0.686 + c3 * 0.168), 255.0);
  10. *(output_image + offset + 2) = (unsigned char)fmin((c1 * 0.272 + c2 * 0.534 + c3 * 0.131), 255.0);
  11. if(channels==4){
  12. *(output_image + offset + 3) = input_image[offset + 3];
  13. }
  14. }
  15. }
  16. }

GPU处理版本

  1. int col = threadIdx.x+blockIdx.x*blockDim.x;
  2. int row =threadIdx.y+blockIdx.y*blockDim.y;
  3. if (col<width && row < height){
  4. int offset = row*width + col;
  5. int rgboffset = offset*channels;
  6. unsigned char c1 = g_input_image[rgboffset]; // r通道的值
  7. unsigned char c2 = g_input_image[rgboffset+1]; // g
  8. unsigned char c3 = g_input_image[rgboffset+2];//r
  9. *(g_output_image + rgboffset) = (unsigned char)fmin((c1 * 0.393 + c2 * 0.769 + c3 * 0.189), 255.0);
  10. *(g_output_image + rgboffset + 1) = (unsigned char)fmin((c1 * 0.349 + c2 * 0.686 + c3 * 0.168), 255.0);
  11. *(g_output_image + rgboffset + 2) = (unsigned char)fmin((c1 * 0.272 + c2 * 0.534 + c3 * 0.131), 255.0);
  12. if (channels ==4){
  13. *(g_output_image + rgboffset + 3) = g_input_image[rgboffset + 3];
  14. }
  15. }

灰度图答案

  1. __global__ void colorToGreyscaleConversion(unsigned char* g_output_image,unsigned char* g_input_image,int width,int channels){
  2. int col = threadIdx.x+blockIdx.x*blockDim.x;
  3. int row =threadIdx.y+blockIdx.y*blockDim.y;
  4. if (col<width && row < height){
  5. int offset = row*width + col;
  6. int rgboffset = offset*channels;
  7. unsigned char c1 = g_input_image[rgboffset]; // r通道的值
  8. unsigned char c2 = g_input_image[rgboffset+1]; // g
  9. unsigned char c3 = g_input_image[rgboffset+2];//b
  10. *(g_output_image + offset)=(unsigned char)fmin((c1 * 0.272 + c2 * 0.534 + c3 * 0.131), 255.0)
  11. if (channels ==4){
  12. *(g_output_image + rgboffset + 3) = g_input_image[rgboffset + 3];
  13. }
  14. }
  15. }