最終更新: mikk_ni3_92 2007年11月06日(火) 11:57:03履歴
現在地 >> メニュー >> サンプルコード::OpenCV >> 誤差拡散法
#include <cv.h>
#include <highgui.h>
#include <cstdio>
void DITHERING(IplImage *imgA,IplImage *dst);
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);
}
IplImage *imgB = cvCreateImage(cvGetSize(imgA),IPL_DEPTH_8U,1);
DITHERING(imgA, imgB); //第2引数が出力先
cvNamedWindow("original",CV_WINDOW_AUTOSIZE);
cvShowImage("original",imgA);
cvNamedWindow("Dithering",CV_WINDOW_AUTOSIZE);
cvShowImage("Dithering",imgB);
cvWaitKey(0); // 0秒待つ => ずっと入力待ち
cvReleaseImage( & imgA);
cvDestroyWindow("original");
cvReleaseImage( & imgB);
cvDestroyWindow("Dithering");
return 0;
}
void DITHERING(IplImage *imgA,IplImage *dst)
{
cvCopy(imgA,dst);
double e;
for(int j = 0;j<dst->height-1;j++)
{
for(int i = 1;i<dst->widthStep-1;i++)
{
if(unsigned char(dst->imageData[j*dst->widthStep + i]) > unsigned char(127))
{
e = double(dst->imageData[j*dst->widthStep + i] - char (255));
dst->imageData[j*dst->widthStep + i] = char (255);
}else
{
e = double(dst->imageData[j*dst->widthStep + i]);
dst->imageData[j*dst->widthStep + i] = char (0);
}
dst->imageData[j*dst->widthStep + i+1] += char(double(e)*(7/16.0));
dst->imageData[(j+1)*dst->widthStep + i-1] += char(double(e)*(3/16.0));
dst->imageData[(j+1)*dst->widthStep + i] += char(double(e)*(5/16.0));
dst->imageData[(j+1)*dst->widthStep + i+1] += char(double(e)*(1/16.0));
}
}
}
#include <cv.h>
#include <highgui.h>
#include <cstdio>
void DITHERING(IplImage *imgA,IplImage *dst);
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);
}
IplImage *imgB = cvCreateImage(cvGetSize(imgA),IPL_DEPTH_8U,1);
DITHERING(imgA, imgB); //第2引数が出力先
cvNamedWindow("original",CV_WINDOW_AUTOSIZE);
cvShowImage("original",imgA);
cvNamedWindow("Dithering",CV_WINDOW_AUTOSIZE);
cvShowImage("Dithering",imgB);
cvWaitKey(0); // 0秒待つ => ずっと入力待ち
cvReleaseImage( & imgA);
cvDestroyWindow("original");
cvReleaseImage( & imgB);
cvDestroyWindow("Dithering");
return 0;
}
void DITHERING(IplImage *imgA,IplImage *dst)
{
cvCopy(imgA,dst);
double e;
for(int j = 0;j<dst->height-1;j++)
{
for(int i = 1;i<dst->widthStep-1;i++)
{
if(unsigned char(dst->imageData[j*dst->widthStep + i]) > unsigned char(127))
{
e = double(dst->imageData[j*dst->widthStep + i] - char (255));
dst->imageData[j*dst->widthStep + i] = char (255);
}else
{
e = double(dst->imageData[j*dst->widthStep + i]);
dst->imageData[j*dst->widthStep + i] = char (0);
}
dst->imageData[j*dst->widthStep + i+1] += char(double(e)*(7/16.0));
dst->imageData[(j+1)*dst->widthStep + i-1] += char(double(e)*(3/16.0));
dst->imageData[(j+1)*dst->widthStep + i] += char(double(e)*(5/16.0));
dst->imageData[(j+1)*dst->widthStep + i+1] += char(double(e)*(1/16.0));
}
}
}