OpenGL de プログラミング - OpenCV::モザイク処理
現在地 >> メニュー >> サンプルコード::OpenCV >> OpenCV::モザイク処理

問題


以下の画像をグレイスケールで読み込んで、
モザイク画像処理を行え。

画像

その他条件:
 領域は、
  縦=10
  横=20
 で分割する


 ⇒OpenCV::モザイク処理2

答え



#include <cv.h>
#include <highgui.h>
#include <cstdio>

#define H_DIV 10
#define W_DIV 20

int sum[H_DIV][W_DIV];

int main( int argc, char **argv)
{

IplImage *imgA = cvLoadImage( "test2.jpg", CV_LOAD_IMAGE_GRAYSCALE);//
if(imgA ==NULL)
{
printf("File not found\n");
exit(0);
}



for(int j = 0; j < H_DIV; j++)
{
for(int i = 0; i < W_DIV; i++)
{

for( int y = 0;( j == (H_DIV - 1) ? y < ( (imgA->height / H_DIV) + (imgA->height % H_DIV) ): y < (imgA->height / H_DIV) ); y++)
{

for(int x = 0; i == (W_DIV - 1) ? x < *1 : x < (imgA->widthStep / W_DIV) ; x++)
{

sum[j][i] += int(imgA->imageData[ ( ( j*( imgA->height / H_DIV) ) + y ) * imgA->widthStep + i*(imgA->widthStep / W_DIV) + x]);

}
}

sum[j][i] = sum[j][i]/( (imgA->widthStep / W_DIV)*(imgA->height / H_DIV) );


for( int y = 0; (j == (H_DIV - 1) ? y < *2 : y < (imgA->height / H_DIV) ); y++)
{

for(int x = 0; i == (W_DIV - 1) ? x < *3 : x < (imgA->widthStep / W_DIV) ; x++)
{
imgA->imageData[ *4 + y)*imgA->widthStep + i*(imgA->widthStep / W_DIV) + x] = char(sum[j][i]);
}
}

}
}



cvNamedWindow("window",CV_WINDOW_AUTOSIZE);
cvShowImage("window",imgA);

cvWaitKey(0); // 0秒待つ => ずっと入力待ち


cvReleaseImage( & imgA);

cvDestroyWindow("window");

return 0;
}

メモ


4重ループのなかに2重ループが2つある。

部分的に「三項演算子」を使用した。