という課題なのですが、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;
}