⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmyimage.cpp

📁 国外一个大牛人写的MEAN-SHIFT目标跟踪算法
💻 CPP
字号:
/***************************************************************************                          CMyImage.cpp  -  description                             -------------------    begin                : Wed Jul 18 2001    copyright            : (C) 2001 by Alper Yilmaz    email                : yilmaz@cs.ucf.edu ***************************************************************************//*************************************************************************** *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License as published by  * *   the Free Software Foundation; either version 2 of the License, or     * *   (at your option) any later version.                                   * *                                                                         * ***************************************************************************/#include <fstream.h>#include <string.h>#include "stdio.h"#include "stdlib.h"#include "definitions.h"#include "utilities.h"#include "CMyImage.h"/*************************************************************************** *                                                                         * *   Constructor                                                           * *                                                                         * ***************************************************************************/CMyImage::CMyImage(){}/*************************************************************************** *                                                                         * *   Distructor                                                            * *                                                                         * ***************************************************************************/CMyImage::~CMyImage(){}/*************************************************************************** *                                                                         * *   Read image file                                                       * *                                                                         * ***************************************************************************/void CMyImage::read_file(char *filename){	int  rows,		 cols,		 max,i,j;	char buffer [500];	char im_type[5];	unsigned char value;	FILE *fp;	fp=fopen(filename,"rb");	fscanf(fp, "%s\n", im_type);	//read type of the file	if (im_type[1]=='5')		type=PGM;	else		type=PPM;	//read the comments	do	{		fgets(buffer,499,fp);	}	while(buffer[0] == '#');	//read number of rowsand cols	sscanf(buffer,"%d %d",&cols,&rows);	//read max gray level	fscanf(fp, "%d", &max);	fgetc(fp); // read the extra newline character	R.ReSize(rows,cols);	G.ReSize(rows,cols);	B.ReSize(rows,cols);	if (type == PGM)		for(i=1;i<=rows;i++)for(j=1;j<=cols;j++)		{			value=fgetc(fp);				R(i,j)=(Real)value;			G(i,j)=(Real)value;			B(i,j)=(Real)value;		}	else //PPM	{		for(i=1;i<=rows;i++)for(j=1;j<=cols;j++)		{			value=fgetc(fp);	R(i,j)=(Real)value;			value=fgetc(fp);	G(i,j)=(Real)value;			value=fgetc(fp);	B(i,j)=(Real)value;		}	}	}/*************************************************************************** *                                                                         * *   Write image file                                                      * *                                                                         * ***************************************************************************/void CMyImage::write_file(char *filename){	int  i,j;	/*	ofstream outfile;	outfile.open(filename,ios::out);	if (type==PGM)	{		outfile <<"P5"<<endl<<"#my output"<<endl    		<<R.Ncols()<<" "<<R.Nrows()<<endl		    <<255<<endl;		for (i=1;i<=R.Nrows();i++) for (j=1;j<=R.Ncols();j++)    		outfile << (unsigned char)R(i,j);	}	else	{	outfile <<"P6"<<endl<<"#my output"<<endl			<<R.Ncols()<<" "<<R.Nrows()<<endl		    <<255<<endl;		for (i=1;i<=R.Nrows();i++) for (j=1;j<=R.Ncols();j++)    		outfile << (unsigned char)R(i,j)    				<< (unsigned char)G(i,j)    				<< (unsigned char)B(i,j);	}	outfile.close();
	*/
	FILE *outfile;
	outfile = fopen(filename, "wb");
	if (type == PGM)
	{
		fprintf(outfile, "P5\n# my output\n%d %d\n255\n", R.Ncols(), R.Nrows());
		for (i=1;i<=R.Nrows();i++) for (j=1;j<=R.Ncols();j++)
			fprintf(outfile, "%c", (unsigned char)R(i,j));
	}
	else
	{
			fprintf(outfile, "P6\n# my output\n%d %d\n255\n", R.Ncols(), R.Nrows());
		for (i=1;i<=R.Nrows();i++) for (j=1;j<=R.Ncols();j++)
		{

			fprintf(outfile, "%c", (unsigned char)R(i,j));
			fprintf(outfile, "%c", (unsigned char)G(i,j));
			fprintf(outfile, "%c", (unsigned char)B(i,j));
		}
	}
	fclose(outfile);

	}/*************************************************************************** *                                                                         * *   Draw circle in the image                                              * *                                                                         * ***************************************************************************/void CMyImage::draw_circle (int origx,int origy,int radius,float r,float g,float b){    int x,y;	int rows=R.Nrows(),cols=R.Ncols();    for (x=0;x<=radius;x++)    {     	y=(int)ceil(sqrt(radius*radius-x*x));        if (origx+x<=cols && origy-y>0)        {        	R( (origy-y),(origx+x))=r;        	G( (origy-y),(origx+x))=g;        	B( (origy-y),(origx+x))=b;        }        if (origx+x<=cols && origy+y<=rows)        {        	R( (origy+y),(origx+x))=r;        	G( (origy+y),(origx+x))=g;        	B( (origy+y),(origx+x))=b;        }        if (origx-x>0 && origy-y>0)        {        	R( (origy-y),(origx-x))=r;        	G( (origy-y),(origx-x))=g;        	B( (origy-y),(origx-x))=b;        }        if (origx-x>0 && origy+y<=rows)        {        	R( (origy+y),(origx-x))=r;        	G( (origy+y),(origx-x))=g;        	B( (origy+y),(origx-x))=b;        }    }    for (y=0;y<=radius;y++)    {     	x=(int)ceil(sqrt(radius*radius-y*y));        if (origx+x<=cols && origy-y>0)        {        	R( (origy-y),(origx+x))=r;        	G( (origy-y),(origx+x))=g;        	B( (origy-y),(origx+x))=b;        }        if (origx+x<=cols && origy+y<=rows)        {        	R( (origy+y),(origx+x))=r;        	G( (origy+y),(origx+x))=g;        	B( (origy+y),(origx+x))=b;        }        if (origx-x>0 && origy-y>0)        {        	R( (origy-y),(origx-x))=r;        	G( (origy-y),(origx-x))=g;        	B( (origy-y),(origx-x))=b;        }        if (origx-x>0 && origy+y<=rows)        {        	R( (origy+y),(origx-x))=r;        	G( (origy+y),(origx-x))=g;        	B( (origy+y),(origx-x))=b;        }    }}/*************************************************************************** *                                                                         * *   Draw a cros in the image                                              * *                                                                         * ***************************************************************************/void CMyImage::draw_cross (int center_x,int center_y,int radius,float r,float g,float b){ 	int i;    for (i=-radius;i<=radius;i++)    {    	if (center_x+i>=1 && center_x+i<=R.Ncols())    	{    		R(center_y,center_x+i)=r;    		G(center_y,center_x+i)=g;    		B(center_y,center_x+i)=b;    	}    	if (center_y+i>=1 && center_y+i<=R.Nrows())    	{    		R(center_y+i,center_x)=r;    		G(center_y+i,center_x)=g;    		B(center_y+i,center_x)=b;    	}    }}/*************************************************************************** *                                                                         * *   Subsample the image by half                                           * *                                                                         * ***************************************************************************/void CMyImage::sub_sample(Matrix &out_r,Matrix &out_g,Matrix &out_b){	Matrix smooth;	Matrix gauss(5,1);	int		i,j;	gauss << 0.05 << 0.25 << 0.4 << 0.25 << 0.05;	//perform subsampling on r	smooth=conv(R,gauss);	smooth=conv(smooth,gauss.t());	out_r.ReSize(smooth.Nrows()/2,smooth.Ncols()/2);	for (i=1;i<=out_r.Nrows();i++)	for (j=1;j<=out_r.Ncols();j++)		out_r(i,j)=smooth(2*i,2*j);	//perform subsampling on g	smooth=conv(G,gauss);	smooth=conv(smooth,gauss.t());	out_g.ReSize(smooth.Nrows()/2,smooth.Ncols()/2);	for (i=1;i<=out_g.Nrows();i++)	for (j=1;j<=out_g.Ncols();j++)		out_g(i,j)=smooth(2*i,2*j);	//perform subsampling on b	smooth=conv(B,gauss);	smooth=conv(smooth,gauss.t());	out_b.ReSize(smooth.Nrows()/2,smooth.Ncols()/2);	for (i=1;i<=out_b.Nrows();i++)	for (j=1;j<=out_b.Ncols();j++)		out_b(i,j)=smooth(2*i,2*j);}
int CMyImage::convert(IplImage *input)
{

	//char* m_windowName = (char*) (malloc(sizeof(char)*100));
	//strcpy(m_windowName,"Output Window");
	//cvNamedWindow(m_windowName,CV_WINDOW_AUTOSIZE);

	//cvShowImage(m_windowName,input);
	//cvWaitKey(0);
	CvSize size = cvGetSize(input);

	int rows,columns;
	rows = size.height;
	columns =size.width;

	R.ReSize(rows,columns);
	G.ReSize(rows,columns);
	B.ReSize(rows,columns);

	//convert data types
	uchar* pixel;

	for ( int i = 0; i < rows; i++) {
		for ( int j =0; j < columns; j++) {

			pixel = &((uchar*)((input)->imageData + (input)->widthStep*i))[j*3];
			R(i+1,j+1)=(Real)pixel[2];
			G(i+1,j+1)=(Real)pixel[1];
			B(i+1,j+1)=(Real)pixel[0];
		}
	}

	return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -