📄 bitmap.cpp
字号:
/*
nn-utility (Provides neural networking utilities for c++ programmers)
Copyright (C) 2003 Panayiotis Thomakos
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
//To contact the author send an email to panthomakos@users.sourceforge.net
#include "nn-utility.h"
using namespace nn_utility;
/* The purpose of this function is to read the "index" bitmap of a bitmap file.
*
* Bitmaps are stored in a file whose name is stored in BITMAP::filename (a string).
* This function reads into the file and locates the "index" bitmap that is stored
* there. It then reads that bitmap and stores it's value in "vector" The return
* value is the length of the bitmap that was read and stored in "vector".*/
template<class T>int BITMAP<T>::readbitmap( VECTOR &vector, int index ){
fstream infile( filename, ios::in );
char buffer[FILE_NAME_SIZE];
if ( index > 0 ){
//scroll through to get to the point that index references
for ( int i = 0; i < index; i++ ){
infile >> buffer; //read :name:
infile >> buffer; //read next line
while ( buffer[0] != ':' && buffer != NULL ) //keep going until :name2:
infile >> buffer;
}
}
else
infile >> buffer;
if ( buffer[0] != ':' ){ //if : get another line because we are not there yet
infile >> buffer;}
int cnt = 0; //vector counter used to store values in "vector"
if ( buffer[0] == ':' ){
infile >> buffer; //read first line in :name: block
//while it's not a : and it's not the NULL end and while we are still within the SIZE limit,
//keep going
while ( ( buffer[0] != ':' && buffer != NULL ) && cnt < ( NN_UTIL_SIZE-strlen( buffer ) ) ){
for ( int i = 0; i < strlen( buffer ); i++ ){
vector[cnt++] = (T)((buffer[i]-'0')*1.0); }
infile >> buffer; //read next line
}
}
infile.close();
return cnt;
}
/*The purpose of this function is to distort or add static to a bitmap.*/
//type 0 => Add noise (1/0) only next to 1's in the "vector" bitmap
//type 1 => Random noise anywhere in the "vector" bitmap
//amplitude references the rand() % amplitude == 0 chance of noice occuring
template<class T> void BITMAP<T>::noise( VECTOR &vector, int length, int type, int amplitude ){
for ( int i = 1; i < length-1; i++ ){
if ( rand() % amplitude == 0 ){
if ( type == 0 && vector[i] <= 0.0 ){ vector[i] = (T)1.0; }
else if ( type == 0 && vector[i] <= 0.1 ){ vector[i] = (T)0.9; }
else if ( vector[i] == (T)1 ){
( rand() % 2 == 0 ? vector[i-1] = (T)1.0 : vector[i+1] = (T)1.0 );}
else if ( vector[i] == 0.9 ){
( rand() % 2 == 0 ? vector[i-1] = (T)0.9 : vector[i+1] = (T)0.9 );}
}
}
}
template<class T> void BITMAP<T>::noise( VECTOR &vector, int length, int amplitude ){
for ( int i = 0; i < length; i++ ){
if ( rand() % amplitude == 0 )
vector[i] = (T)1;
}
}
/*The purpose of this function is to print a bitmap according to it's intended output format.*/
//line references the # of bits on a line
//length references the entire length of the vector
template<class T> void BITMAP<T>::Print( VECTOR vector, int line, int length ){
for ( int i = 0; i < length; i++ ){
if ( vector[i] < 0.5 ) //notice that this also works for hopefield SGN
cout << "0";
else if ( vector[i] >= 0.5 )
cout << "1";
else
cout << vector[i];
if ( (i+1) % line == 0 )
cout << '\n';
}
}
template BITMAP<float>;
template BITMAP<double>;
template BITMAP<int>;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -