2値化について

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
hnkto
記事: 10
登録日時: 6年前

2値化について

#1

投稿記事 by hnkto » 5年前

パラメータpを適当に指定し、のp-タイル法を実装せよ. pを変化させて実行し,結果に対して考察せよ.
という課題なのですが、C++を全くやったことがないのですべてわかりません。大雑把でもよろしいので解説していただきたいです。よろしくお願いします。

コード:

#include "common.h"

uchar ptile(const cv::Mat_<uchar> image, double p)
{
	cv::Mat_<int> hist = cv::Mat::zeros(256, 1, CV_32SC1);

	double th_point = image.total()*p/100;	//■ここで全体のp%になる累積画素数のしきい値を求めよう.入力画像の画素数は image.total() です.

	printf("%d,%1f ¥n", image.total(), th_point);

	// まずはヒストグラムを求める
	for (int y = 0; y < image.rows; ++y)
	{
		for (int x = 0; x < image.cols; ++x)
		{
			int tmp = image(y, x);
				hist(tmp, 0)++;
		}
	}

	int sum = 0;
	uchar gray_value=0;
	//低い方の濃度値からその頻度を足していき,th_pointを超えたらその時の濃度値(gray_value)がしきい値
	//となる処理を書こう

	while (sum < th_point) {
		sum += hist(gray_value, 0);
		gray_value++;
	}

	return gray_value;
}

int main(void)
{
	cv::Mat_<uchar> image = cv::imread("flower.JPG", 0);
	cv::Mat_<uchar> image2 = image.clone();
	cv::Mat_<uchar> out;
	
	image2 /= 2;	//濃度値が半分になった画像を同じパラメータで処理したらどうなる?

	cv::imshow("input", image);
	cv::imshow("input2", image2);

	int th = ptile(image, 30);
	out = image > th;
	cv::imshow("output", out);

	th = ptile(image2, 30);
	out = image2 > th;
	cv::imshow("output2", out);

	cv::waitKey();


	return 0;
}

アバター
usao
記事: 1887
登録日時: 11年前

Re: 2値化について

#2

投稿記事 by usao » 5年前

(手元にOpenCVを使える環境が無いので試してませんけども)

> 実装せよ.

って言ってるけど,ぱっと見,実装されてるように見える……
どこに問題があるのでしょうか?

返信

“C言語何でも質問掲示板” へ戻る