Browse Source

自动窗宽窗位

sw 3 days ago
commit
622f780785
3 changed files with 123 additions and 0 deletions
  1. 10 0
      include/AutoWWWL.h
  2. 82 0
      src/AutoWWWL.cpp
  3. 31 0
      test/test.cpp

+ 10 - 0
include/AutoWWWL.h

@@ -0,0 +1,10 @@
+#ifndef AUTOWWWL_H
+#define AUTOWWWL_H
+
+namespace ATWL
+{
+
+int AutoWWWL(unsigned short* input,unsigned char* mask,int width,int height,int &WW,int &WL);
+}
+
+#endif // AUTOWWWL.h

+ 82 - 0
src/AutoWWWL.cpp

@@ -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;
+    }
+}

+ 31 - 0
test/test.cpp

@@ -0,0 +1,31 @@
+#include <iostream>
+#include "AutoWWWL.h"
+#include <opencv2/opencv.hpp>
+
+
+using namespace std;
+using namespace cv;
+using namespace ATWL;
+
+vector<String> globroot(String pattern)
+{
+    vector<String> fn;
+    glob(pattern, fn, false);
+    return fn;
+}
+
+int main()
+{
+	Mat img = imread(R"(Image_FinalImage.tif)",-1);
+	Mat mask = imread(R"(mask.tif)", -1);
+	int width = img.cols;
+	int height = img.rows;
+	int ww = 0;
+	int wl = 0;
+	int ret = AutoWWWL(img.ptr<ushort>(), mask.ptr<uchar>(), width, height, ww, wl);
+	cout << "WW:" << ww << endl;
+	cout << "WL:" << wl << endl;
+
+	system("pause");
+	return 0;
+}