|
@@ -0,0 +1,82 @@
|
|
|
+#include <iostream>
|
|
|
+#include "AutoWWWL.h"
|
|
|
+#include <opencv2/opencv.hpp>
|
|
|
+
|
|
|
+using namespace std;
|
|
|
+using namespace cv;
|
|
|
+namespace ATWL
|
|
|
+{
|
|
|
+ int AutoWWWL(unsigned short* input,unsigned char* mask,int width,int height,int &WW,int &WL)
|
|
|
+ {
|
|
|
+ if (input == nullptr || mask == nullptr || width <= 0 || height <= 0)
|
|
|
+ return 0;
|
|
|
+
|
|
|
+ Mat src(Size(width, height), CV_16U, input);
|
|
|
+ Mat collimator_mask(Size(width, height), CV_8U, mask);
|
|
|
+
|
|
|
+ if (countNonZero(collimator_mask) == 0)
|
|
|
+ return 0;
|
|
|
+
|
|
|
+ int histSize = 65536; // 灰度级范围是 0-65535
|
|
|
+ float range[] = { 0, histSize }; // 直方图的取值范围
|
|
|
+ const float* histRange = { range };
|
|
|
+ Mat hist;
|
|
|
+ calcHist(&src, 1, 0, collimator_mask, hist, 1, &histSize, &histRange);
|
|
|
+
|
|
|
+ float cutRation = 0.001;
|
|
|
+ int leftCut = 0;
|
|
|
+ int rightCut = 0;
|
|
|
+ float sumArea = sum(hist)[0];
|
|
|
+
|
|
|
+ if (sumArea <= 0.0f)
|
|
|
+ return 0;
|
|
|
+
|
|
|
+ float sumCount = 0;
|
|
|
+ for (int i = 0; i < hist.rows; i++)
|
|
|
+ {
|
|
|
+ sumCount += hist.at<float>(i);
|
|
|
+ if (sumCount > cutRation * sumArea)
|
|
|
+ {
|
|
|
+ leftCut = i;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ sumCount = 0;
|
|
|
+ for (int i = hist.rows - 1; i >= 0; i--)
|
|
|
+ {
|
|
|
+ sumCount += hist.at<float>(i);
|
|
|
+ if (sumCount > cutRation * sumArea)
|
|
|
+ {
|
|
|
+ rightCut = i;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (leftCut >= rightCut)
|
|
|
+ return 0;
|
|
|
+
|
|
|
+ double meanv = mean(src,collimator_mask)[0];
|
|
|
+
|
|
|
+ int lw = 0;
|
|
|
+ int rw = 0;
|
|
|
+
|
|
|
+ if ((meanv - leftCut) < (rightCut - meanv))
|
|
|
+ {
|
|
|
+ lw = leftCut;
|
|
|
+ rw = meanv + leftCut;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ rw = rightCut;
|
|
|
+ lw = 2 * meanv - rightCut;
|
|
|
+ }
|
|
|
+
|
|
|
+ WW = rw - lw;
|
|
|
+ if (WW <= 0)
|
|
|
+ return 0;
|
|
|
+ WL = 0.5 * (rw + lw);
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+}
|