Reading PNG file

Nov 13, 2023 at 11:48pm
I am utilizing the OpenCV library to read a PNG file and successfully visualize it. The PNG file represents three distinct datasets: Raw MRI (depicted by a gray dotted line), ensemble average (shown as a black continuous line), and feature point average (illustrated as a black long dashed line). The ultimate objective is to obtain the equation representing the black continuous line. Unfortunately, the code is not displaying the black line as anticipated. The PNG is uploaded here https://file.io/4S3F2HAEphOU .Here is the code. Any assistance would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "ImageProcessor.h"
#include <iostream>
#include <opencv2/highgui.hpp>

ImageProcessor::ImageProcessor(const std::string& imageFile) : imageFile(imageFile) {}

void ImageProcessor::displayImage() {
    cv::Mat originalImage = cv::imread(imageFile);

    if (originalImage.empty()) {
        std::cerr << "Error: Could not open image." << std::endl;
        return;
    }

    cv::imshow("Original Image", originalImage);

    cv::waitKey(0);
}


void ImageProcessor::showContinuousBlackLine() {
    cv::Mat originalImage = cv::imread(imageFile);

    if (originalImage.empty()) {
        std::cerr << "Error: Could not open image." << std::endl;
        return;
    }

    cv::Mat grayImage;
    cv::cvtColor(originalImage, grayImage, cv::COLOR_BGR2GRAY);

    cv::Mat thresholded;
    cv::threshold(grayImage, thresholded, 100, 255, cv::THRESH_BINARY);

    std::vector<std::vector<cv::Point>> contours;
    cv::findContours(thresholded, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);

    cv::Mat reshapedImage = originalImage.reshape(1, originalImage.total());
    reshapedImage.convertTo(reshapedImage, CV_32F);

    int k = 3;
    cv::Mat labels, centers;
    cv::kmeans(reshapedImage, k, labels, cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::MAX_ITER, 100, 0.2), 3, cv::KMEANS_RANDOM_CENTERS, centers);

    labels = labels.reshape(0, originalImage.rows);

    cv::Mat resultImage(originalImage.size(), CV_8UC3, cv::Scalar(255, 255, 255));

    for (size_t i = 0; i < contours.size(); ++i) {
        double area = cv::contourArea(contours[i]);
        double perimeter = cv::arcLength(contours[i], true);
        double circularity = 4 * CV_PI * area / (perimeter * perimeter);
        cv::Scalar meanColor = cv::mean(originalImage, thresholded);
        int clusterLabel = static_cast<int>(labels.at<int>(i, 0));

        if (clusterLabel == 2 && circularity > 0.6) {
            cv::drawContours(resultImage, contours, static_cast<int>(i), cv::Scalar(0, 0, 0), 2);
        }
    }

    cv::imshow("Continuous Black Line", resultImage);

    cv::waitKey(0);

int main(int argc, char* argv[]) {

        std::string imageFile = "inlet.png";
        ImageProcessor ImageProcessor(imageFile);
        ImageProcessor.displayImage();
        ImageProcessor.showContinuousBlackLine();
}
}
Last edited on Nov 13, 2023 at 11:51pm
Nov 14, 2023 at 12:39pm
I don't know CV so I probably can't help much, but the code you posted has incorrect braces placement so it would probably be more helpful if you posted the actual code that at least compiles.
Nov 15, 2023 at 5:40am
The PNG file has also gone.
Topic archived. No new replies allowed.