ページ 11

c++

Posted: 2013年7月07日(日) 15:47
by kmrnnm50173
はじめてこのフォーラムを利用させていただきます.
よろしくお願いします.

[1] 質問文
 [1.1] 自分が今行いたい事は何か
PGM画像を読み込み、加工(4近傍鮮鋭化)したい.

4近傍鮮鋭化について(以下の9ページ目参照)
http://p.tl/PlpU

 [1.2] どのように取り組んだか(プログラムコードがある場合記載)
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
...
class GS_img{
public:
int width; // 幅
int height; // 高さ
int bright; // 輝度
string **data; // 画素データ(2次元配列)

GS_img(int w=0, int h=0, int b=0){
width = w;
height = h;
bright = b;
data = NULL;
}
~GS_img(){}

// 画素データ動的メモリ確保
void make_Data(){
...
}
// 画素データ動的メモリ解放
void delete_Data(){
...
}
};

bool load_pgm(GS_img *gs, string file_name){
ifstream file; // ファイルストリーム
string buff; // 読み込み用バッファ
int width, height, bright; // 幅, 高さ, 輝度
stringstream ss; // 文字列ストリーム
string tmp; // 作業用変数
char c; // 作業用変数
...

// 画素データの読み込み
gs->make_Data();
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
ss.clear(); // 状態をクリア.
ss.str(""); // 文字列をクリア.
file.get(c); // 1文字読み込み
tmp=c; // string型に変換
gs->data[j] = tmp;
}
}
...
}

int main(){
string file1 = "gray.pgm";
string file2 = "gray_make.pgm";
GS_img *gs;
gs = new GS_img;

// PGMファイル読み込み
load_pgm(gs, file1);
...

gs->delete_Data();

}

 [1.4] 今何がわからないのか、知りたいのか
読み込んだString型の文字列を数値(?)に変換したい.
例えば
data[j]=data[j]*(-1)
のように計算したい.

[2] 環境  
 [2.1] OS : Windows8 64bit
 [2.2] コンパイラ名 : gcc

[3] その他
 ・c++は初めて5ヶ月ほどです.

よろしくお願いします.

Re: c++

Posted: 2013年7月07日(日) 20:44
by beatle
文字列から整数への変換なら atoi や strtol が使えます.
ちなみにこれらはC言語の標準関数です.

Re: c++

Posted: 2013年7月07日(日) 21:58
by みけCAT
文字列から整数への変換はsscanfを使う方法もあります。
sscanfならstdlib.hが必要なく、stdio.hだけで使えるので便利かもしれないです。

Re: c++

Posted: 2013年7月08日(月) 07:29
by naohiro19
boost C++ Librariesの「Lexical Cast」を用いるのもOKです。

コード:

#include <iostream>
#include <boost/lexical_cast.hpp>

int main()
{
    int ret = boost::lexical_cast<int>("1234");
    std::cout << ret << std::endl;
    return 0;
}

Re: c++

Posted: 2013年7月11日(木) 01:15
by kmrnnm50173
返信ありがとうございます.
返事が遅れて申し訳ございません.

この課題は言語を統一するように言われてて、c++ならc++のみで作らなければいけません.
sscanfやatoi , strtolはc言語の標準関数だと思うので使えません.
あとから付け足すようですいません.

どうにかしてc++のみで数値に、具体的には10進数に変換(?)する方法はないでしょうか?

Re: c++

Posted: 2013年7月11日(木) 01:37
by KORYUOH
c++の標準のみでやるならsstream(stringstream)
もしくはnaohiro19さんの言うようにboost C++ Librariesの「Lexical Cast」を用いるとよいと思います

Re: c++

Posted: 2013年7月11日(木) 02:34
by nullptr
kmrnnm50173 さんが書きました:返信ありがとうございます.
返事が遅れて申し訳ございません.

この課題は言語を統一するように言われてて、c++ならc++のみで作らなければいけません.
sscanfやatoi , strtolはc言語の標準関数だと思うので使えません.
あとから付け足すようですいません.

どうにかしてc++のみで数値に、具体的には10進数に変換(?)する方法はないでしょうか?
C標準ライブラリは、std名前空間に配置されたC++版が標準でほぼ用意されています。sscanfもstd::sscanfとすればC++言語のものです。
std::sscanfは#include <cstdio>をすれば使用できます。cstdioはCのstdio.hのC++版です。

ただまぁ、私はboost::lexical_castを用いるのをおすすめしますが。不正なデータが入力された場合、例外を投げてくれます。
それにキャストと同じ構文なんで見た目が綺麗ですね。

Re: c++

Posted: 2013年7月11日(木) 04:50
by かずま
PGMファイルが読みたいのですか?

コード:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

typedef unsigned short Pixcel;

struct Data {
    Pixcel *pixcel;
    int width;

    Data() : pixcel(0), width(0) { }
    Pixcel *operator[](int h) { return pixcel + width * h; }
};

struct  GS_img {
    int width, height, bright;
    Data data;

    void delete_Data() { delete[] data.pixcel; data.pixcel = 0; }
};

bool load_pgm(GS_img *gs, string file_name)
{
    ifstream file(file_name);
    if (!file) return false;
    string buff;
    getline(file, buff);  // "P2"
    getline(file, buff);  // #<コメント>
    int width, height, bright;
    if (!(file >> width >> height >> bright)) return false;
    gs->width = width, gs->height = height, gs->bright = bright;
    Pixcel *p = new Pixcel[width * height];
    gs->data.pixcel = p;
    gs->data.width = width;
    for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++) file >> *p++;
    return file;
}

int main()
{
    string file1 = "gray.pgm";
    GS_img *gs= new GS_img;

    if (load_pgm(gs, file1)) {
        for (int i = 0; i < gs->height; i++) {
            for (int j = 0; j < gs->width; j++)
                cout << setw(4) << gs->data[i][j];
            cout << endl;
        }
        gs->delete_Data();
    }
}