Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.5 kB
3
Indexable
Never
void praguriMultiple() {
	int hist[256];
	float fdp[256];
	for (int i = 0; i < 256; i++) {
		hist[i] = 0;
	}
	char fname[MAX_PATH];

	std::vector<uchar> maxes, threshold;
	maxes.push_back(0);
	while (openFileDlg(fname))
	{
		Mat src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
		int height = src.rows;
		int width = src.cols;
		for (int i = 0; i < height; i++)
		{
			for (int j = 0; j < width; j++)
			{
				int val = src.at<uchar>(i, j);
				hist[val]++;
			}
		}

		for (int i = 0; i < 256; i++) {
			fdp[i] = ((float)hist[i]) / (height * width);
		}
		int WH = 5;
		float TH = 0.0003;
		for (int i = WH; i <= 255 - WH; i++) {
			float avg = 0.0;
			bool isMax = true;
			for (int j = 0; j < 2 * WH + 1; j++) {
				avg += fdp[i + j - WH];
				if (fdp[i + j - WH] > fdp[i]) {
					isMax = false;
				}
			}
			avg /= 2 * WH + 1;
			if (isMax && fdp[i] > avg + TH) {
				maxes.push_back(i);
			}
		}
		maxes.push_back(255);
		
		for (int i = 0; i < maxes.size() - 1; i++) {
			threshold.push_back((maxes[i] + maxes[i + 1]) / 2);
		}

		Mat dst(src.rows, src.cols, CV_8UC1);
		for (int i = 0; i < src.rows; i++) {
			for (int j = 0; j < src.cols; j++) {
				uchar val = src.at<uchar>(i, j);
				int maxPos = maxes.size() - 1;
				for (int k = 0; k < threshold.size(); k++) {
					if (val < threshold[k]) {
						maxPos = k;
						break;
					}
				}
				dst.at<uchar>(i, j) = maxes[maxPos];
			}
		}
		imshow("Input image", src);
		imshow("Modified image", dst);
		waitKey();
	}
}