【开发日志】2023.05 ZENO----Image Processing----ImageDilateCV、ImageErodeCV

ImageDilateCV

 

 

// 图像膨胀函数
void dilateImage(cv::Mat& src, cv::Mat& dst, int kernelSize, float dilateStrength) {
    // 定义结构元素
    cv::Mat kernel = getStructuringElement(cv::MORPH_RECT, cv::Size(kernelSize, kernelSize));
    // 进行膨胀操作
    cv::dilate(src, dst, kernel, cv::Point(-1, -1), dilateStrength);
}
struct ImageDilateCV: INode {
    void apply() override {
        std::shared_ptr<PrimitiveObject> image = get_input<PrimitiveObject>("image");
        int kernelsize = get_input2<int>("kernelsize");
        int strength = get_input2<int>("strength");
        auto &ud = image->userData();
        int w = ud.get2<int>("w");
        int h = ud.get2<int>("h");
        cv::Mat imagecvin(h, w, CV_32FC3);
        cv::Mat imagecvout(h, w, CV_32FC3);
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                vec3f rgb = image->verts[i * w + j];
                imagecvin.at<cv::Vec3f>(i, j) = {rgb[0], rgb[1], rgb[2]};
            }
        }
        const int kernelSize = 3;
        dilateImage(imagecvin, imagecvout, kernelsize, strength);
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cv::Vec3f rgb = imagecvout.at<cv::Vec3f>(i, j);
                image->verts[i * w + j] = {rgb[0], rgb[1], rgb[2]};
            }
        }
        set_output("image", image);
    }
};
ZENDEFNODE(ImageDilateCV, {
    {
        {"image"},
        {"int", "kernelsize", "3"},
        {"float", "strength", "1"},
    },
    {
        {"image"},
    },
    {},
    {"comp"},
});

ImageErodeCV 

 

 

 

 

 

 

 

struct ImageErodeCV: INode {
    void apply() override {
        std::shared_ptr<PrimitiveObject> image = get_input<PrimitiveObject>("image");
        int strength = get_input2<int>("strength");
        auto &ud = image->userData();
        int w = ud.get2<int>("w");
        int h = ud.get2<int>("h");
        cv::Mat imagecvin(h, w, CV_32FC3);
        cv::Mat imagecvout(h, w, CV_32FC3);
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                vec3f rgb = image->verts[i * w + j];
                imagecvin.at<cv::Vec3f>(i, j) = {rgb[0], rgb[1], rgb[2]};
            }
        }
        cv::Mat element = getStructuringElement(cv::MORPH_RECT, cv::Size(2 * strength + 1, 2 * strength + 1),
                                                cv::Point(strength, strength));
        cv::erode(imagecvin, imagecvout, element);
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cv::Vec3f rgb = imagecvout.at<cv::Vec3f>(i, j);
                image->verts[i * w + j] = {rgb[0], rgb[1], rgb[2]};
            }
        }
        set_output("image", image);
    }
};
ZENDEFNODE(ImageErodeCV, {
    {
        {"image"},
        {"int", "strength", "1"},
    },
    {
        {"image"},
    },
    {},
    {"comp"},
});
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// 膨胀操作函数
void dilate(vector<vector<int>>& image, int kernelSize, int strength) {
    int height = image.size();
    int width = image[0].size();
    int halfKernelSize = kernelSize / 2;

    // 创建膨胀核
    vector<vector<int>> kernel(kernelSize, vector<int>(kernelSize, 1));

    // 对图像进行膨胀操作
    for (int i = halfKernelSize; i < height - halfKernelSize; i++) {
        for (int j = halfKernelSize; j < width - halfKernelSize; j++) {
            int maxValue = 0;
            for (int m = -halfKernelSize; m <= halfKernelSize; m++) {
                for (int n = -halfKernelSize; n <= halfKernelSize; n++) {
                    maxValue = max(maxValue, image[i + m][j + n]);
                }
            }
            image[i][j] = min(image[i][j] + strength * maxValue, 255);
        }
    }
}

int main() {
    // 读入图像数据
    vector<vector<int>> image = { 
        { 100, 120, 130, 140, 120 },
        { 90, 110, 120, 130, 110 },
        { 80, 100, 110, 120, 100 },
        { 70, 90, 100, 110, 90 },
        { 60, 80, 90, 100, 80 }
    };

    // 对图像进行膨胀操作
    int kernelSize = 3;  // 膨胀核大小
    int strength = 1;    // 膨胀强度
    dilate(image, kernelSize, strength);

    // 输出膨胀后的图像
    for (auto row : image) {
        for (auto pixel : row) {
            cout << pixel << " ";
        }
        cout << endl;
    }

    return 0;
}

#include <iostream>
#include <fstream>

using namespace std;

const int kernelSize = 3;
const int kernel[kernelSize][kernelSize] = {
    {1, 1, 1},
    {1, 1, 1},
    {1, 1, 1}
};

int main()
{
    ifstream fin("input.bmp", ios::binary);
    ofstream fout("output.bmp", ios::binary);

    // 读取 BMP 文件头和信息头等信息
    // ...

    // 读取图像数据
    unsigned char *imageData = new unsigned char[width * height * 3];
    // ...

    // 定义输出图像数据
    unsigned char *outputData = new unsigned char[width * height * 3];

    // 定义目标区域的左上角和右下角坐标
    int x1 = 100, y1 = 100;
    int x2 = 200, y2 = 200;

    // 对于目标区域中的每个像素进行膨胀操作
    for (int y = y1 + kernelSize / 2; y < y2 - kernelSize / 2; y++) {
        for (int x = x1 + kernelSize / 2; x < x2 - kernelSize / 2; x++) {
            int maxValue = 0;
            for (int ky = -kernelSize / 2; ky <= kernelSize / 2; ky++) {
                for (int kx = -kernelSize / 2; kx <= kernelSize / 2; kx++) {
                    int px = x + kx;
                    int py = y + ky;
                    int value = imageData[(py * width + px) * 3];
                    if (value > maxValue) {
                        maxValue = value;
                    }
                }
            }
            outputData[(y * width + x) * 3] = maxValue;
            outputData[(y * width + x) * 3 + 1] = maxValue;
            outputData[(y * width + x) * 3 + 2] = maxValue;
        }
    }

    // 将输出图像数据写入文件
    // ...

    // 释放内存
    delete[] imageData;
    delete[] outputData;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Angelloveyatou/article/details/130512287