画像にモザイクをかける
画像にモザイクをかけてみます。
基本的にモザイクの大きさは10×10で、その中のRGB値の平均をモザイクの範囲の各画素値に代入します。
#include <cv.h> #include <stdio.h> #include <highgui.h> int main (int argc, char **argv) { IplImage *img; IplImage *output_img; char *file_name = "lena.jpg"; int blue, green , red; int size = 10; int tmp1, tmp2; img = cvLoadImage(file_name, CV_LOAD_IMAGE_COLOR); output_img = cvCloneImage(img); for(int y=0;y<img->height;y+=size){ for(int x=0;x<img->width;x+=size){ blue = 0; green = 0; red = 0; //基本的にモザイクの大きさは10 //しかし、画像の縦横の大きさが10で割り切れない場合もあるのでそこも考慮する for(int i=0;i<size;i++){ if( img->height < (y + i) ){ break; } tmp1 = i; for(int j=0;j<size;j++){ if( img->width < (x + j) ){ break; } blue += (unsigned char)img->imageData[img->widthStep * (y + i) + (x + j) * 3]; green += (unsigned char)img->imageData[img->widthStep * (y + i) + (x + j)* 3 + 1]; red += (unsigned char)img->imageData[img->widthStep * (y + i) + (x + j) * 3 + 2]; tmp2 = j; } } tmp1++; tmp2++; for(int i=0;i<tmp1;i++){ for(int j=0;j<tmp2;j++){ output_img->imageData[img->widthStep * (y + i) + (x + j) * 3] = cvRound(blue / (tmp1 * tmp2)); output_img->imageData[img->widthStep * (y + i) + (x + j) * 3 + 1] = cvRound(green / (tmp1 * tmp2)); output_img->imageData[img->widthStep * (y + i) + (x + j) * 3 + 2] = cvRound(red / (tmp1 * tmp2)); } } } } cvNamedWindow ("Mosaic", CV_WINDOW_AUTOSIZE); cvShowImage ("Mosaic", output_img); cvWaitKey (0); cvDestroyWindow("Mosaic"); cvReleaseImage(&img); cvReleaseImage(&output_img); return 0; }