最終更新: mikk_ni3_92 2007年10月30日(火) 12:59:18履歴
現在地 >> メニュー >> サンプルコード::OpenCV >> 可視透かし
#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace std;
IplImage *CV_LOAD_WITH_THRESHOLD(char *filename,int threshold_type= CV_THRESH_BINARY, double threshold=128,double max_value=255);
void VISIBLE_WATER_MARK(IplImage *imgA, IplImage *imgB,IplImage *dst,double h = 0.3, double b = 1.0);
int main( int argc, char **argv)
{
IplImage *imgA = cvLoadImage( "file6.jpg", CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);
if(imgA ==NULL)
{
cout<<"Can't Load Image ." << endl;
std::exit(0);
}
IplImage *imgB = CV_LOAD_WITH_THRESHOLD("de.jpg",CV_THRESH_BINARY);
if(imgB ==NULL)
{
cout<<"Can't Load Image ." << endl;
std::exit(0);
}
VISIBLE_WATER_MARK(imgA,imgB,imgA); //第3引数が出力先
cvNamedWindow("window",CV_WINDOW_AUTOSIZE);
cvShowImage("window",imgA);
cvNamedWindow("window2",CV_WINDOW_AUTOSIZE);
cvShowImage("window2",imgB);
cvWaitKey(0);
cvReleaseImage( &imgA );
cvReleaseImage( &imgB );
cvDestroyWindow("window");
cvDestroyWindow("window2");
return 0;
}
/**********[ 読み込んで2値化する ]*********************/
IplImage *CV_LOAD_WITH_THRESHOLD(char *filename,int threshold_type, double threshold,double max_value)
{
IplImage *img= cvLoadImage( filename, CV_LOAD_IMAGE_GRAYSCALE);
if(img ==NULL)
{
return NULL;
}
IplImage *dst;
dst = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
cvThreshold(img, dst,threshold, max_value,threshold_type);
cvReleaseImage( & img );
return (dst);
}
/**********[ 可視透かし作成 ]**********************/
void VISIBLE_WATER_MARK(IplImage *imgA, IplImage *imgB,IplImage *dst,double h, double b)
{
int k=0;
for(int j = 0; j < imgA->height;j++){
for(int i = 0; i < imgA->widthStep;i+=3,k++)
{
if( unsigned char(imgB->imageData[k]) < unsigned char(255))
{
dst->imageData[j*imgA->widthStep + i] =
char(imgA->imageData[j*imgA->widthStep + i] * b +
imgA->imageData[j*imgA->widthStep + i+1] * (-h) +
imgA->imageData[j*imgA->widthStep + i+2] * h);
dst->imageData[j*imgA->widthStep + i+1] =
char(imgA->imageData[j*imgA->widthStep + i] * h +
imgA->imageData[j*imgA->widthStep + i+1] * b +
imgA->imageData[j*imgA->widthStep + i+2] * (-h));
dst->imageData[j*imgA->widthStep + i+2] =
char(imgA->imageData[j*imgA->widthStep + i] * (-h) +
imgA->imageData[j*imgA->widthStep + i+1] * h +
imgA->imageData[j*imgA->widthStep + i+2] * b);
}
}
}
}
#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace std;
IplImage *CV_LOAD_WITH_THRESHOLD(char *filename,int threshold_type= CV_THRESH_BINARY, double threshold=128,double max_value=255);
void VISIBLE_WATER_MARK(IplImage *imgA, IplImage *imgB,IplImage *dst,double h = 0.3, double b = 1.0);
int main( int argc, char **argv)
{
IplImage *imgA = cvLoadImage( "file6.jpg", CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);
if(imgA ==NULL)
{
cout<<"Can't Load Image ." << endl;
std::exit(0);
}
IplImage *imgB = CV_LOAD_WITH_THRESHOLD("de.jpg",CV_THRESH_BINARY);
if(imgB ==NULL)
{
cout<<"Can't Load Image ." << endl;
std::exit(0);
}
VISIBLE_WATER_MARK(imgA,imgB,imgA); //第3引数が出力先
cvNamedWindow("window",CV_WINDOW_AUTOSIZE);
cvShowImage("window",imgA);
cvNamedWindow("window2",CV_WINDOW_AUTOSIZE);
cvShowImage("window2",imgB);
cvWaitKey(0);
cvReleaseImage( &imgA );
cvReleaseImage( &imgB );
cvDestroyWindow("window");
cvDestroyWindow("window2");
return 0;
}
/**********[ 読み込んで2値化する ]*********************/
IplImage *CV_LOAD_WITH_THRESHOLD(char *filename,int threshold_type, double threshold,double max_value)
{
IplImage *img= cvLoadImage( filename, CV_LOAD_IMAGE_GRAYSCALE);
if(img ==NULL)
{
return NULL;
}
IplImage *dst;
dst = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
cvThreshold(img, dst,threshold, max_value,threshold_type);
cvReleaseImage( & img );
return (dst);
}
/**********[ 可視透かし作成 ]**********************/
void VISIBLE_WATER_MARK(IplImage *imgA, IplImage *imgB,IplImage *dst,double h, double b)
{
int k=0;
for(int j = 0; j < imgA->height;j++){
for(int i = 0; i < imgA->widthStep;i+=3,k++)
{
if( unsigned char(imgB->imageData[k]) < unsigned char(255))
{
dst->imageData[j*imgA->widthStep + i] =
char(imgA->imageData[j*imgA->widthStep + i] * b +
imgA->imageData[j*imgA->widthStep + i+1] * (-h) +
imgA->imageData[j*imgA->widthStep + i+2] * h);
dst->imageData[j*imgA->widthStep + i+1] =
char(imgA->imageData[j*imgA->widthStep + i] * h +
imgA->imageData[j*imgA->widthStep + i+1] * b +
imgA->imageData[j*imgA->widthStep + i+2] * (-h));
dst->imageData[j*imgA->widthStep + i+2] =
char(imgA->imageData[j*imgA->widthStep + i] * (-h) +
imgA->imageData[j*imgA->widthStep + i+1] * h +
imgA->imageData[j*imgA->widthStep + i+2] * b);
}
}
}
}