1. 写yml矩阵
- 代码:
-
#include "cxcore.h"int main( int argc, char** argv ){ CvMat* mat = cvCreateMat( 3, 3, CV_32F ); CvFileStorage* fs = cvOpenFileStorage( "example.yml", 0, CV_STORAGE_WRITE ); cvSetIdentity( mat ); cvWrite( fs, "A", mat, cvAttrList(0,0) ); cvReleaseFileStorage( &fs ); cvReleaseMat( &mat ); return 0;}
2. 读yml矩阵
- 代码:
-
#include <stdio.h>#include "cxcore.h" #include "highgui.h"void main( ){ CvMat* A1 = (CvMat*)cvLoad( "example.yml" ); for(int i = 0; i <3; i++ ) { printf( "\n"); for(int j = 0; j < 3; j++ ) printf( "%f ", (float) cvGetReal2D( A1, i, j )); } printf( "\n\n"); }
3. 写xml矩阵
- 代码:
-
#include <stdio.h>#include "cxcore.h" #include "highgui.h"void main( ){ double a[] = { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 }; CvMat A1; cvInitMatHeader( &A1, 5, 5, CV_64FC1, a, CV_AUTOSTEP ); cvSave( "my_matrix_test.xml", &A1 );}
4. 读xml矩阵
- 代码:
-
#include <stdio.h>#include "cxcore.h" #include "highgui.h"void main( ){ CvMat* A1 = (CvMat*)cvLoad( "my_matrix.xml" ); for(int i = 0; i <5; i++ ) { printf( "\n"); for(int j = 0; j < 5; j++ ) printf( "%f ", (float) cvGetReal2D( A1, i, j )); } printf( "\n\n"); }
5. 写点列
- 代码:
-
#include <stdio.h>#include "highgui.h"#include "cxcore.h"void main( ){ CvMemStorage* memstorage = cvCreateMemStorage(0); CvSeq *up_seq = cvCreateSeq( CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint), memstorage); for(int i=1; i<=100; i++) { // 产生单个点 CvPoint pt1 = cvPoint( i, i ); cvSeqPush(up_seq,&pt1); cvSave("upimg_matched_point.yml", up_seq, NULL, "matched_points_up", cvAttrList(NULL,NULL)); }}
6. 读点列
- 代码:
-
#include <stdio.h>#include "cxcore.h"#include <stdio.h>#include "cxcore.h"int main( int argc, char** argv ){ CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* seq; CvSeqWriter writer; CvSeqReader reader; CvFileNode *node; CvFileStorage* fs = cvOpenFileStorage( "upimg_matched_point.yml", 0, CV_STORAGE_READ ); node = cvGetFileNodeByName (fs, NULL, "upimg_matched_point"); seq = (CvSeq *) cvRead (fs, node); cvStartReadSeq( seq, &reader, 0 ); for(int i = 0; i < 100; i++ ) { CvPoint pt1; #if 1 CV_READ_SEQ_ELEM( pt1, reader ); printf("%d %d is read\n", pt1.x, pt1.y ); #else printf("%d is read\n", *(int*)reader.ptr ); CV_NEXT_SEQ_ELEM( seq->elem_size, reader ); #endif } cvReleaseFileStorage( &fs ); return 0;}
7. 写图像xml数据
- 代码:
-
#include <stdio.h>#include "cv.h" #include "cxcore.h" #include "highgui.h"void main( ){ IplImage *Image1,*Image2; int height,width,step,channels; int i, j, k, i1; uchar *data; CvScalar Scalar1; Image1=cvLoadImage("lena.jpg", 1); height = Image1->height; width = Image1->width; step = Image1->widthStep; channels = Image1->nChannels; data = (uchar *)Image1->imageData; cvNamedWindow("rawimage",1); cvShowImage("rawimage", Image1); cvWaitKey(0); CvMat *M=cvCreateMat(width, height, CV_32FC3); for(i=0; i<width; i++) for(j=0; j<height; j++) { Scalar1=cvGet2D(Image1, i, j); cvSet2D(M, i, j, Scalar1); } CvFileStorage* fs = cvOpenFileStorage("lena.xml", 0, CV_STORAGE_WRITE); cvWriteInt( fs, "frame_count", 1 ); cvStartWriteStruct( fs, "frame_size", CV_NODE_SEQ); cvWriteInt( fs, 0, width ); cvWriteInt( fs, 0, height ); cvEndWriteStruct(fs); cvWrite( fs, "lena", M ); cvReleaseFileStorage( &fs ); cvReleaseImage( &Image1 );}
8. 读图像xml数据
- 代码:
-
#include <stdio.h>#include "cv.h" #include "highgui.h"void main( ){ IplImage *Image1; int i, j; CvFileNode *node; CvScalar Scalar1; CvFileStorage* fs = cvOpenFileStorage("lena.xml", 0, CV_STORAGE_READ); int frame_count = cvReadIntByName( fs, 0, "frame_count", 10 /* default value */ ); CvSeq* s = cvGetFileNodeByName(fs, 0, "frame_size")->data.seq; int width = cvReadInt( (CvFileNode*)cvGetSeqElem(s, 0) ); int height = cvReadInt( (CvFileNode*)cvGetSeqElem(s, 1) ); node = cvGetFileNodeByName(fs, NULL, "lena"); CvMat *M1=cvCreateMatHeader(width, height, CV_32FC3); M1 = (CvMat *)cvRead( fs, cvGetFileNodeByName(fs, NULL, "lena")); cvReleaseFileStorage( &fs ); Image1 = cvCreateImage( cvSize(width, height), 8, 3 ); for(i=0; i<width; i++) for(j=0; j<height; j++) { Scalar1=cvGet2D(M1, i, j); cvSet2D(Image1, i, j, Scalar1); } cvNamedWindow("rawimage",1); cvShowImage("rawimage", Image1); cvWaitKey(0); cvReleaseFileStorage( &fs ); cvReleaseImage( &Image1 );}
9. 由数据产生图像
- 代码:
-
#include "cv.h"#include "highgui.h"#include <math.h>#include <stdio.h>#include <stdlib.h>void main(){ int step,channels; uchar *data; // 创建空白图板 int width=301, height=301; IplImage* image = cvCreateImage( cvSize(width,height), 8, 1 ); // 获取图像格式 step = image->widthStep; channels = image->nChannels; data = (uchar *)image->imageData; // 产生图像数据 for(int i=0; i<height; i++) for(int j=0; j<width; j++) { // for color image channels changes from 0 to 2 img_dat= function[(i-centre_i, j-centre_j)]; // your subroutine here. data[i*step+j*channels]=max(0, min(255, (int)img_dat)); } cvNamedWindow( "My_img", 1 ); cvShowImage( "My_img", image ); cvSaveImage( "My_img.bmp", image ); cvReleaseImage(&image ); cvWaitKey(0);}
1. 高斯噪声和均匀分布噪声
- 代码:
-
// 初始化随机数发生器 CvRNG rng = cvRNG(-1); // 产生高斯噪声数据cvRandArr( &rng, noise, CV_RAND_NORMAL, cvScalarAll(0), cvScalarAll(sigma) ); // 产生均匀分布噪声数据cvRandArr( &rng, noise, CV_RAND_UNI, cvScalarAll(-sigma), cvScalarAll(sigma) );
-
2. 添加泊松噪声
- 代码:
-
// 设定噪声图像模板 noise=cvCreateImage( size, IPL_DEPTH_8U, channels ); srand( time(NULL) ); // 设置随机数种子的 for(i=0; i<height; i++) for(j=0; j<width; j++) { for(k=0; k<channels; k++) { i1=rand(); x1=(float)i1/32767.0; x2= numda*sqrt(-2* log(1-x1)); CV_IMAGE_ELEM(noise, unsigned char, i, j*3+k) = x2; } } // 图像加噪操作 for(i=0; i<height; i++) for(j=0; j<width; j++) { for( k=0; k<channels; k++) { CV_IMAGE_ELEM(image, unsigned char, i, j*3+k) = max(0, min(255, CV_IMAGE_ELEM(image, unsigned char, i, j*3+k) + CV_IMAGE_ELEM(noise, unsigned char, i, j*3+k))); } }
3. 添加椒盐噪声
- 代码:
-
float psalt=0.1; //probability salt float ppepper=0.2; //probability pepper for(i=0; i<height; i++) for(j=0; j<width; j++) { float valsalt=(float)rand()/65535; float valpepper=(float)rand()/65535; s=cvGet2D(image1, i, j); for(k=0; k<channels; k++) { if (valsalt<=psalt && valsalt>valpepper) s.val[k]=255; else if(valpepper<=ppepper && valpepper>=valsalt) s.val[k]=0; } cvSet2D(image1, i, j, s); } cvAdd(image1, noise, image2);
4. 添加柏林噪声 perlin.h
- 代码:
-
// Perlin.h#include <stdlib.h>#include <stdio.h>#include <math.h>float persistence = 0.55f; // 可调参数1,可由editbox输入int Number_Of_Octaves = 3; // 可调参数2,可由editbox输入/* 一个噪声发生器 */float Noise1(int x, int y){ int n = x + y * 57; n = (n<<13) ^ n; return ( 1.0f - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f); }/* 一个光滑噪声发生器 */float SmoothNoise_1(int x, int y){ float corners = ( Noise1(x-1, y-1)+Noise1(x+1, y-1)+Noise1(x-1, y+1)+Noise1(x+1, y+1) ) / 16.0f; float sides = ( Noise1(x-1, y) +Noise1(x+1, y) +Noise1(x, y-1) +Noise1(x, y+1) ) / 8.0f; float center = Noise1(x, y) / 4.0f; return corners + sides + center;}/* 插值函数 */float Cosine_Interpolate(float a, float b, float x){ double ft = x * 3.1415927; double f = (1 - cos(ft)) * 0.5f; return (float)(a*(1-f) + b*f);}/* 插值噪声发生器 */float InterpolatedNoise_1(float x, float y){ int integer_X = (int)floor(x+0.5); float fractional_X = x - integer_X; int integer_Y = (int)floor(y+0.5); float fractional_Y = y - integer_Y; float v1 = SmoothNoise_1(integer_X, integer_Y); float v2 = SmoothNoise_1(integer_X + 1, integer_Y); float v3 = SmoothNoise_1(integer_X, integer_Y + 1); float v4 = SmoothNoise_1(integer_X + 1, integer_Y + 1); float i1 = Cosine_Interpolate(v1 , v2 , fractional_X); float i2 = Cosine_Interpolate(v3 , v4 , fractional_X); return Cosine_Interpolate(i1 , i2 , fractional_Y);}/* 最终的PERLIN NOISE */float PerlinNoise_2D(float x, float y){ float total = 0.0f; float p = persistence; int n = Number_Of_Octaves - 1; int ii; for(ii=0;ii<=n;ii++) { float frequency = (float) pow((float)2,ii); float amplitude = (float) pow(p,ii); total = total + InterpolatedNoise_1(x * frequency, y * frequency) * amplitude; } return total;} float myperlin( int x, int y ){ int ii, jj; float ynoise=0; for(ii=0; ii< x; ii++) for(jj=0; jj< y; jj++) { ynoise=PerlinNoise_2D((float)ii, (float)jj); ynoise= (float)(1+ynoise)*127.0; } return ynoise;}
- 代码:
-
// 产生噪声 CvMat *M=cvCreateMat(width, height, CV_32FC1); // 不可用CV_8C1 IplImage* dst=cvCreateImage(size, img->depth, img->nChannels); for(i=0; i<height; i++) for(j=0; j<width; j++) { x1= myperlin(i, j); cvmSet(M, j, i, x1 ); }
我在vc下可以做得很快,但在opencv下只能处理64X64
5. 1/f噪声 下面给出一维1/f噪声数组产生程序(1/f噪声的谱方法)
- 代码:
-
function sig=FractRnd(beta, n, HuaTu)%sig=FractRnd(beta, n)%To generate 1/f^betta noise%INPUT% beta is the exponent, a number% n is the number of synthetic data points%OUTPUT% sig is an n-points array%if nargin==2HuaTu=0;endlen_beta=length(beta);phi_n=2*pi*rand(1,n);f=[1:n].^(-beta/2).*exp(i*phi_n(1:n));sig=real(ifft(f));sig=(sig-mean(sig))./std(sig);if HuaTu%To verify the scaling behavior of power spectral density:% P(f) ~ 1/f^betafigure(HuaTu)F=fft(sig);P=(abs(F)).^2;P([1,round(end/2):end])=[];% plot(log(1:length(P)),log(P))loglog(1:length(P),P,'k')xlabel('Frequency');ylabel('Power spectrum')% pauseend
在程序中可以通过vc调用matlab实现