I need help (histogram, segmentation, filter) opencv c++

Hay
I wanted to ask you if anyone could help me find the bug (doesn’t show the pictures)

//includes hinzugefügt
#include <opencv2\opencv.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <string.h>
#include <iostream>
#include <fstream>


using namespace std;
using namespace cv;


int size = 512;
int MAX_KERNEL_LENGTH = 31;
int hist_w = 512;
int hist_h = 400;


int readDicom(string data, Mat* img)
{
    ifstream file(data, fstream::binary);   //Aufrufen der Datei

    if (!file.is_open())
        return 1;

    int img_size;
    file.seekg(0, file.end);
    int length = file.tellg(); //tellg für aktuelle position des get Zeigers

    if (length < 512 * 512 * 2)
        img_size = 256;
    //img->resize(256, 256);    //resize weggemacht
    else
        img_size = 512;

    int head_tail = length - img_size * img_size * 2;

    cout << "File Laenge: " << length << endl << "Ende (head_tail): " << head_tail << endl;

    file.seekg(head_tail, file.beg);    //setzen des Zeigers an Ende der Header

    //Buffer für Bilddaten reservieren
    uchar* read_data = new uchar[length - head_tail]; //tail da nur dicom und nicht header ausgegeben werden soll
    file.read((char*)read_data, length - head_tail);

    Mat grey_img(img_size, img_size, CV_16U, read_data);

    //Min & Max Werte bestimmen
    double minVal, maxVal;
    minMaxLoc(grey_img, &minVal, &maxVal);
    cout << endl << endl << "Min: " << minVal << endl << "Max: " << maxVal << endl;

    //Grauwertstufen Bild erstellen
    double scale = 255 / (maxVal - minVal);
    double shift = (-minVal) * scale;
    convertScaleAbs(grey_img, *img, scale, shift);

    //*img = grey_img;
    double minVal2, maxVal2;				//max und min Grauwert bestimmmen nach convertScaleAbs
    minMaxLoc(*img, &minVal2, &maxVal2);
    cout << "min Grauwert (neu): " << minVal2 << endl << "max Grauwert (neu): " << maxVal2 << endl;
    //png erstellen
    //Bild in png diesmal in der Main gelöst
    //imwrite(/*x + */"0.png", grey_img);

    file.close();
    delete[] read_data;

    return 0;
}



void histogramm(Mat& img, Mat& hist, Mat& histImage, int hist_w, int hist_h)
{
    int histSize = 256;
    // Setzen des Bereichs für das hist
    float range[] = { 0, 256 };
    const float* histRange = { range };
    // Berechnen des hists
    calcHist(&img, 1, 0, Mat(), hist, 1, &histSize, &histRange, true, false);
    //Größe des histbildes festlegen
    int bin_w = (int)((double)hist_w / histSize);

    blur(img, hist, Size(7, 1), Point(-1, -1));
    // Normalisieren des Ergebnisses, dass es in Bild passt [ 0, histImage.rows ]
    normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
    //Zeichnen des hists in Blau
    for (int i = 0; i < histSize; i++)
    {
        line(histImage, Point(bin_w * (i), hist_h - cvRound(hist.at<float>(i))), Point(bin_w * (i), hist_h), Scalar(255, 255, 255));
    }

   //imshow("Histogramm", histImage);
}



void segment(Mat& input)
{
    Mat hist;
    Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));

    // Max. Grauwert herausfinden und Groessen des Histogramms bestimmen
    double minVal, maxVal;
    minMaxLoc(input, &minVal, &maxVal);

    // Histogramm Funktion aufrufen
    histogramm(input, hist, histImage, hist_w, 400);

    // Algorithmus von Tsai
    // Maximale Kruemmung im Histrogramm bestimmen
    Mat tsai(hist_w, 1, CV_32F, Scalar(0, 0, 0));
    Mat curvature(1, hist_w, CV_32F, Scalar(0, 0, 0));
    int R = 25;

    for (int i = minVal + R; i < maxVal - R; i++)
    {
        for (int kr = 1; kr <= R; kr++)
        {
            tsai.at<float>(i) += ((hist.at<float>(i + kr) - hist.at<float>(i - kr)) / (2.0 * i));
        }
        tsai.at<float>(i) = (tsai.at<float>(i) * (1.0 / R));
    }

    // Kruemmung K bestimmen (Algorithmus 2)
    for (int i = minVal + R; i < maxVal - R; i++)
    {
        for (int kr = 1; kr <= R; kr++)
        {
            curvature.at<float>(i) += abs(tsai.at<float>(i + kr) - tsai.at<float>(i - kr));
        }
        curvature.at<float>(i) = curvature.at<float>(i) * (1.0 / R);
    }

    // Zeichnen der Linie ins Histrogramm
    normalize(curvature, curvature, 1, histImage.rows, NORM_MINMAX, -1, Mat());
    for (int i = minVal + R; i < maxVal - R; i++)
    {
        line(histImage, Point(i, 400 - cvRound(curvature.at<float>(i))), Point(i, 400), Scalar(190, 0, 250), 1);
    }

    //imshow("Histogramm/Segment", histImage);
}



int main(int argc, char* argv[])
{
    string new_argv(argv[0]);
    string path = argv[1];

    for (int i = 16; i <= 16; i++)
    {
        Mat img, median, bilateral, gaussian;
        cout << "Bild " << i << " wird eingelesen:" << endl;
        string datei = path + "/" + to_string(i) + ".dcm";
        Mat Bild;
        readDicom(datei, &Bild);
        //imshow("Bild" + to_string(i), Bild);	//gibt die 3 Bilder aus
        string bildPng = path + "/" + to_string(i) + ".png";
        imwrite(bildPng, Bild);

        img = imread(bildPng, 0);

        if (!img.data)
        {
            cout << "Bild konnte nicht geladen werden!\n";
            exit(0);
        }

        //Filteranwendung
        for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2)
        {
            medianBlur(img, median, i);
            GaussianBlur(img, gaussian, Size(i, i), 0, 0);
            bilateralFilter(img, bilateral, i, i * 2, i / 2);
        }

       
        segment(median);

        //Ausgabe des Bildes
        imshow("Original", img);
        imshow("Median", median);
        imshow("Gaussian", gaussian);
        imshow("Bilateral", bilateral);
        waitKey();
    }
    return 0;
}

welcome. there’s a lot going on. please reduce your code to the “minimum reproducing example”.

The best way to find a bug is to use a debugger :grin:

1 Like