#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define IMG_WIDTH 1440 // Example image width
#define IMG_HEIGHT 1440 // Example image height
#define CHANNELS 1.5 // RGB三个通道
void read_image(unsigned char* image, int width, int height) {
FILE* input_file = fopen("E:\\Projects\\matlab\\yuv420.yuv", "rb");
if (input_file == NULL) {
fprintf(stderr, "Error opening input file.\n");
return;
}
fread(image, sizeof(unsigned char), width * height * CHANNELS, input_file);
fclose(input_file);
}
// Function to write image (placeholder, need to replace with actual image saving code)
void write_image(unsigned char* image, int width, int height) {
FILE* output_file = fopen(“E:\Projects\vs2022\masaic\mosaic_image.yuv”, “wb”);
if (output_file == NULL) {
fprintf(stderr, “Error opening output file.\n”);
return;
}
fwrite(image, sizeof(unsigned char), width * height * CHANNELS, output_file);
fclose(output_file);
}
int main() {
// Read the image
int width = IMG_WIDTH, height = IMG_HEIGHT;
unsigned char* image = (unsigned char*)malloc(width * height * CHANNELS);
read_image(image, width, height);
// Define face region coordinates (example)
int face_x = 527;
int face_y = 551;
int face_width = 500;
int face_height = 500;
// Define mosaic strength
int mosaic_strength = 10;
// y
for (int i = face_x; i < face_x + face_width; ++i) {
for (int j = face_y; j < face_y + face_height; ++j) {
image[j * width + i] = image[(j - j % mosaic_strength) * width + (i - i % mosaic_strength)];
}
}
// u
for (int i = face_x / 2; i < (face_x + face_width) / 2; i++) {
for (int j = face_y / 2; j < (face_y + face_height)/2; j++) {
image[(j * width/2 + i) + width * height] = image[(j - j % mosaic_strength) * width/2 + (i - i % mosaic_strength) + width * height];
}
}
// v
for (int i = face_x / 2; i < (face_x + face_width) / 2; i++) {
for (int j = face_y / 2; j < (face_y + face_height) / 2; j++) {
image[(j * width / 2 + i) + width * height * 5 / 4] = image[(j - j % mosaic_strength) * width/2 + (i - i % mosaic_strength) + width * height * 5 / 4];
}
}
// Save the processed image
write_image(image, width, height);
// Free the image memory
free(image);
return 0;
}